├── LICENSE ├── README.md ├── index.d.ts ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License (ISC) 2 | Copyright 2015-2017, Carter Thaxton 3 | 4 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 5 | 6 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # electron-default-menu 2 | 3 | A simple module that returns a default Electron menu template, similar to the one you'll get if you don't use `Menu.setApplicationMenu()` at all. You can modify the returned template before creating the application menu. 4 | 5 | Based on the sample code supplied in the [Electron menu documentation](https://github.com/atom/electron/blob/master/docs/api/menu.md) 6 | 7 | Like the sample code, it checks the environment, and returns appropriate additional menus for Mac OS X, and sets the `role` for each menu accordingly. 8 | 9 | Must be used from the Electron environment. 10 | 11 | ## Install 12 | 13 | **Install using npm** 14 | 15 | ```shell 16 | npm install --save electron-default-menu 17 | ``` 18 | 19 | **Install using yarn** 20 | 21 | ```shell 22 | yarn add electron-default-menu 23 | ``` 24 | 25 | ## Example usage: 26 | 27 | ```javascript 28 | import { Menu, app, dialog, shell } from 'electron'; 29 | import defaultMenu from 'electron-default-menu'; 30 | 31 | app.on('ready', () => { 32 | // Get default menu template 33 | const menu = defaultMenu(app, shell); 34 | 35 | // Add custom menu 36 | menu.splice(4, 0, { 37 | label: 'Custom', 38 | submenu: [ 39 | { 40 | label: 'Do something', 41 | click: (item, focusedWindow) => { 42 | dialog.showMessageBox({message: 'Do something', buttons: ['OK'] }); 43 | } 44 | } 45 | ] 46 | }); 47 | 48 | // Set application menu 49 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)); 50 | }); 51 | ``` 52 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare function defaultMenu(app: Electron.App, shell: Electron.Shell): Electron.MenuItemConstructorOptions[]; 2 | export = defaultMenu 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Creates a default menu for electron apps 3 | * 4 | * @param {Object} app electron.app 5 | * @param {Object} shell electron.shell 6 | * @returns {Object} a menu object to be passed to electron.Menu 7 | */ 8 | 9 | module.exports = function(app, shell) { 10 | 11 | const template = [ 12 | { 13 | label: 'Edit', 14 | submenu: [ 15 | { 16 | label: 'Undo', 17 | accelerator: 'CmdOrCtrl+Z', 18 | role: 'undo' 19 | }, 20 | { 21 | label: 'Redo', 22 | accelerator: 'Shift+CmdOrCtrl+Z', 23 | role: 'redo' 24 | }, 25 | { 26 | type: 'separator' 27 | }, 28 | { 29 | label: 'Cut', 30 | accelerator: 'CmdOrCtrl+X', 31 | role: 'cut' 32 | }, 33 | { 34 | label: 'Copy', 35 | accelerator: 'CmdOrCtrl+C', 36 | role: 'copy' 37 | }, 38 | { 39 | label: 'Paste', 40 | accelerator: 'CmdOrCtrl+V', 41 | role: 'paste' 42 | }, 43 | { 44 | label: 'Select All', 45 | accelerator: 'CmdOrCtrl+A', 46 | role: 'selectall' 47 | }, 48 | ] 49 | }, 50 | { 51 | label: 'View', 52 | submenu: [ 53 | { 54 | label: 'Reload', 55 | accelerator: 'CmdOrCtrl+R', 56 | click: function(item, focusedWindow) { 57 | if (focusedWindow) 58 | focusedWindow.reload(); 59 | } 60 | }, 61 | { 62 | label: 'Toggle Full Screen', 63 | accelerator: (function() { 64 | if (process.platform === 'darwin') 65 | return 'Ctrl+Command+F'; 66 | else 67 | return 'F11'; 68 | })(), 69 | click: function(item, focusedWindow) { 70 | if (focusedWindow) 71 | focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); 72 | } 73 | }, 74 | { 75 | label: 'Toggle Developer Tools', 76 | accelerator: (function() { 77 | if (process.platform === 'darwin') 78 | return 'Alt+Command+I'; 79 | else 80 | return 'Ctrl+Shift+I'; 81 | })(), 82 | click: function(item, focusedWindow) { 83 | if (focusedWindow) 84 | focusedWindow.toggleDevTools(); 85 | } 86 | }, 87 | ] 88 | }, 89 | { 90 | label: 'Window', 91 | role: 'window', 92 | submenu: [ 93 | { 94 | label: 'Minimize', 95 | accelerator: 'CmdOrCtrl+M', 96 | role: 'minimize' 97 | }, 98 | { 99 | label: 'Close', 100 | accelerator: 'CmdOrCtrl+W', 101 | role: 'close' 102 | }, 103 | ] 104 | }, 105 | { 106 | label: 'Help', 107 | role: 'help', 108 | submenu: [ 109 | { 110 | label: 'Learn More', 111 | click: function() { shell.openExternal('http://electron.atom.io') } 112 | }, 113 | ] 114 | }, 115 | ]; 116 | 117 | if (process.platform === 'darwin') { 118 | const { name } = app; 119 | template.unshift({ 120 | label: name, 121 | submenu: [ 122 | { 123 | label: 'About ' + name, 124 | role: 'about' 125 | }, 126 | { 127 | type: 'separator' 128 | }, 129 | { 130 | label: 'Services', 131 | role: 'services', 132 | submenu: [] 133 | }, 134 | { 135 | type: 'separator' 136 | }, 137 | { 138 | label: 'Hide ' + name, 139 | accelerator: 'Command+H', 140 | role: 'hide' 141 | }, 142 | { 143 | label: 'Hide Others', 144 | accelerator: 'Command+Shift+H', 145 | role: 'hideothers' 146 | }, 147 | { 148 | label: 'Show All', 149 | role: 'unhide' 150 | }, 151 | { 152 | type: 'separator' 153 | }, 154 | { 155 | label: 'Quit', 156 | accelerator: 'Command+Q', 157 | click: function() { app.quit(); } 158 | }, 159 | ] 160 | }); 161 | const windowMenu = template.find(function(m) { return m.role === 'window' }) 162 | if (windowMenu) { 163 | windowMenu.submenu.push( 164 | { 165 | type: 'separator' 166 | }, 167 | { 168 | label: 'Bring All to Front', 169 | role: 'front' 170 | } 171 | ); 172 | } 173 | } 174 | 175 | return template; 176 | } 177 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-default-menu", 3 | "version": "1.0.2", 4 | "description": "Default application menu for Electron apps", 5 | "main": "index.js", 6 | "keywords": [ 7 | "electron", 8 | "menu" 9 | ], 10 | "types": "index.d.ts", 11 | "author": "Carter Thaxton", 12 | "license": "ISC", 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/carter-thaxton/electron-default-menu.git" 16 | }, 17 | "homepage": "http://github.com/carter-thaxton/electron-default-menu" 18 | } 19 | --------------------------------------------------------------------------------