Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

3.7 KB
Raw Blame
// The app menu (macOS: the menu bar; Windows: hidden, its keys still work) and the tray icon (Windows: the app keeps
// running there when its window is closed, for notifications). The words come from the web app (bridge.ts Labels);
// until then the system's own (menu roles) and the app's name. No key here takes one the web app uses (⌘K, ⌘S, ⌘A,
// ⌘Enter, single letters): only the system's own and ⌘, ⌘[ ⌘] ⌘R.
import { app, Menu, type MenuItemConstructorOptions, nativeImage, shell, Tray } from 'electron'
import type { Labels } from './bridge.ts'

export type MenuActions = {
  open: () => void
  settings: () => void
  back: () => void
  forward: () => void
  quit: () => void
}

const DOCS_URL = 'https://linedesk.ai/docs'
const isMac = process.platform === 'darwin'

export function appMenu(labels: Labels | null, actions: MenuActions): Menu {
  const name = app.getName()
  const template: MenuItemConstructorOptions[] = [
    ...(isMac
      ? [
          {
            label: name,
            submenu: [
              { role: 'about' },
              { type: 'separator' },
              ...(labels ? [{ label: `${labels.settings}…`, accelerator: 'Cmd+,', click: actions.settings }] : []),
              { type: 'separator' },
              { role: 'services' },
              { type: 'separator' },
              { role: 'hide' },
              { role: 'hideOthers' },
              { role: 'unhide' },
              { type: 'separator' },
              { role: 'quit', ...(labels ? { label: labels.quit } : {}), click: actions.quit },
            ],
          } satisfies MenuItemConstructorOptions,
        ]
      : [
          {
            label: labels?.file ?? name,
            submenu: [
              ...(labels ? [{ label: labels.settings, accelerator: 'Ctrl+,', click: actions.settings }] : []),
              { type: 'separator' },
              labels ? { label: labels.quit, accelerator: 'Ctrl+Q', click: actions.quit } : { role: 'quit' },
            ],
          } satisfies MenuItemConstructorOptions,
        ]),
    { role: 'editMenu', ...(labels ? { label: labels.edit } : {}) },
    {
      label: labels?.view,
      role: labels ? undefined : 'viewMenu',
      submenu: labels
        ? [
            { label: labels.back, accelerator: 'CmdOrCtrl+[', click: actions.back },
            { label: labels.forward, accelerator: 'CmdOrCtrl+]', click: actions.forward },
            { label: labels.reload, accelerator: 'CmdOrCtrl+R', role: 'reload' },
            { type: 'separator' },
            { role: 'resetZoom' },
            { role: 'zoomIn' },
            { role: 'zoomOut' },
            { type: 'separator' },
            { role: 'togglefullscreen' },
            ...(app.isPackaged ? [] : [{ role: 'toggleDevTools' } as const]),
          ]
        : undefined,
    },
    { role: 'windowMenu', ...(labels ? { label: labels.window } : {}) },
    ...(labels
      ? [
          {
            role: 'help',
            label: labels.help,
            submenu: [{ label: labels.docs, click: () => void shell.openExternal(DOCS_URL) }],
          } satisfies MenuItemConstructorOptions,
        ]
      : []),
  ]
  return Menu.buildFromTemplate(template)
}

/** The tray (Windows; macOS has the Dock). Click: the window. */
export function makeTray(icon: string, actions: MenuActions): Tray {
  const tray = new Tray(nativeImage.createFromPath(icon))
  tray.setToolTip(app.getName())
  tray.on('click', actions.open)
  return tray
}

export function trayMenu(labels: Labels | null, actions: MenuActions): Menu {
  const name = app.getName()
  return Menu.buildFromTemplate([
    { label: labels?.open ?? name, click: actions.open },
    { type: 'separator' },
    labels ? { label: labels.quit, click: actions.quit } : { role: 'quit' },
  ])
}