// mollusc.json menus are Electron menu templates with three conveniences: // `items` is accepted for `submenu`; `send` names an event the page receives // through `mollusc.on(name, fn)` when the item is chosen, with the item's // `data` (if any) in the payload, so several items can share an event; and // `action` names something the shell itself does (`showWindow`), for what the // page cannot do from a hidden window. export const ACTIONS = ['showWindow']; export function translate (template, send, actions = {}) { return template.map((item) => { const { items, submenu, send: event, data, action, ...out } = item; const sub = submenu ?? items; if (Array.isArray(sub)) out.submenu = translate(sub, send, actions); if (event) out.click = (menuItem) => send(event, { label: menuItem.label, checked: menuItem.checked, data }); else if (action && actions[action]) out.click = () => actions[action](); return out; }); } // The application menu. macOS wants the app menu first; it is added when the // template does not start with one. export function applicationTemplate (template, { send, actions, isMac = process.platform === 'darwin' }) { const items = translate(template, send, actions); if (isMac && items[0]?.role !== 'appMenu') items.unshift({ role: 'appMenu' }); return items; }