From a6f6a43f35626c2ab50781c2573f9452d1fa9da8 Mon Sep 17 00:00:00 2001 From: Brad Beam Date: Sat, 26 Oct 2024 23:11:31 -0500 Subject: [PATCH] feat: Add additional menu items This adds in the edit/view/window/help menus as well as adds in additional menu items for the main application. This allows shortcuts like cmd+q to quit the application on mac. ref: https://github.com/electron/electron/blob/main/docs/api/menu.md#examples Signed-off-by: Brad Beam --- app/main.js | 103 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 84 insertions(+), 19 deletions(-) diff --git a/app/main.js b/app/main.js index 2927c21f..9ff53df8 100644 --- a/app/main.js +++ b/app/main.js @@ -405,27 +405,92 @@ app.on('ready', async () => { app.setAppUserModelId(process.execPath); createWindow(); - if (process.platform === 'darwin') { - // Create our menu entries so that we can use MAC shortcuts like copy & paste - Menu.setApplicationMenu( - Menu.buildFromTemplate([ + const isMac = process.platform === 'darwin'; + + const template = [ + // { role: 'appMenu' } + ...(isMac + ? [ + { + label: app.name, + submenu: [ + { role: 'about' }, + { type: 'separator' }, + { role: 'services' }, + { type: 'separator' }, + { role: 'hide' }, + { role: 'hideOthers' }, + { role: 'unhide' }, + { type: 'separator' }, + { role: 'quit' }, + ], + }, + ] + : []), + // { role: 'editMenu' } + { + label: 'Edit', + submenu: [ + { role: 'undo' }, + { role: 'redo' }, + { type: 'separator' }, + { role: 'cut' }, + { role: 'copy' }, + { role: 'paste' }, + ...(isMac + ? [ + { role: 'pasteAndMatchStyle' }, + { role: 'delete' }, + { role: 'selectAll' }, + { type: 'separator' }, + { + label: 'Speech', + submenu: [{ role: 'startSpeaking' }, { role: 'stopSpeaking' }], + }, + ] + : [{ role: 'delete' }, { type: 'separator' }, { role: 'selectAll' }]), + ], + }, + // { role: 'viewMenu' } + { + label: 'View', + submenu: [ + { role: 'reload' }, + { role: 'forceReload' }, + { role: 'toggleDevTools' }, + { type: 'separator' }, + { role: 'resetZoom' }, + { role: 'zoomIn' }, + { role: 'zoomOut' }, + { type: 'separator' }, + { role: 'togglefullscreen' }, + ], + }, + // { role: 'windowMenu' } + { + label: 'Window', + submenu: [ + { role: 'minimize' }, + { role: 'zoom' }, + ...(isMac ? [{ type: 'separator' }, { role: 'front' }, { type: 'separator' }, { role: 'window' }] : [{ role: 'close' }]), + ], + }, + { + role: 'help', + submenu: [ { - label: 'Edit', - submenu: [ - { role: 'undo' }, - { role: 'redo' }, - { type: 'separator' }, - { role: 'cut' }, - { role: 'copy' }, - { role: 'paste' }, - { role: 'pasteandmatchstyle' }, - { role: 'delete' }, - { role: 'selectall' }, - ], + label: 'Learn More', + click: async () => { + const { shell } = require('electron'); + await shell.openExternal('https://github.com/Xzandro/sw-exporter'); + }, }, - ]) - ); - } + ], + }, + ]; + + const menu = Menu.buildFromTemplate(template); + Menu.setApplicationMenu(menu); storage.getAll(async (error, data) => { if (error) throw error;