From c3caae87c8d2958f831a39af7c095beb5e6dabfd Mon Sep 17 00:00:00 2001 From: tigerbot Date: Thu, 20 Apr 2017 16:42:04 -0600 Subject: [PATCH] added a simple menu Realistically simpler than the default. Note this is also where keyboard shortcuts are defined. --- index.js | 3 + menu.js | 263 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 menu.js diff --git a/index.js b/index.js index db7d685..4d34142 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,9 @@ function createWindow () { var tray = require('./tray'); tray.init(win); + var menu = require('./menu'); + menu.init(win); + // and load the index.html of the app. win.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), diff --git a/menu.js b/menu.js new file mode 100644 index 0000000..f7d787f --- /dev/null +++ b/menu.js @@ -0,0 +1,263 @@ +var electron = require('electron'); +var app = electron.app; + +var win, menu; + +function getMenuItem (label) { + var menuItem; + menu.items.some(function (topLvl) { + menuItem = topLvl.submenu.items.find(function (item) { + return item.label === label; + }); + return menuItem; + }); + return menuItem || {}; +} + +function updateFullScreenCheckbox() { + getMenuItem('Full Screen').checked = win.isFullScreen(); + win.setMenuBarVisibility(!win.isFullScreen()); +} +function toggleFullScreen(flag) { + if (!win || !win.isVisible()) { + return; + } + if (typeof flag !== 'boolean') { + flag = !win.isFullScreen(); + } + + if (flag) { + // Fullscreen and aspect ratio do not play well together. (Mac) + win.setAspectRatio(0); + } + win.setFullScreen(flag); +} + +// Sets whether the window should always show on top of other windows +function toggleAlwaysOnTop(flag) { + if (!win) { + return; + } + if (typeof flag !== 'boolean') { + flag = !win.isAlwaysOnTop(); + } + + win.setAlwaysOnTop(flag); + getMenuItem('Float on Top').checked = flag; +} + +function toggleDevTools() { + if (!win) { + return; + } + if (win.webContents.isDevToolsOpened()) { + win.webContents.closeDevTools(); + } else { + win.webContents.openDevTools(); + } +} + +function init(window) { + if (win) { + console.error("can't initialize the tray mulitple times"); + return; + } + win = window; + + var template = [ + { + label: 'File', + submenu: [ + { + role: 'close' + } + ] + }, + { + label: 'Edit', + submenu: [ + { + role: 'undo' + }, + { + role: 'redo' + }, + { + type: 'separator' + }, + { + role: 'cut' + }, + { + role: 'copy' + }, + { + role: 'paste' + }, + { + role: 'delete' + }, + { + role: 'selectall' + } + ] + }, + { + label: 'View', + submenu: [ + { + label: 'Full Screen', + type: 'checkbox', + accelerator: process.platform === 'darwin' ? 'Ctrl+Command+F' : 'F11', + click: toggleFullScreen, + }, + { + label: 'Float on Top', + type: 'checkbox', + click: toggleAlwaysOnTop, + }, + { + type: 'separator' + }, + { + type: 'separator' + }, + { + label: 'Developer', + submenu: [ + { + label: 'Developer Tools', + accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I', + click: toggleDevTools, + }, + ] + } + ] + }, + ]; + + if (process.platform === 'darwin') { + // WebTorrent menu (Mac) + template.unshift({ + label: 'Electron Demo', + submenu: [ + { + role: 'about' + }, + { + type: 'separator' + }, + { + label: 'Preferences', + accelerator: 'Cmd+,', + click: function () { console.log('TODO: implement preferences'); } + }, + { + type: 'separator' + }, + { + role: 'services', + submenu: [] + }, + { + type: 'separator' + }, + { + role: 'hide' + }, + { + role: 'hideothers' + }, + { + role: 'unhide' + }, + { + type: 'separator' + }, + { + role: 'quit' + } + ] + }); + + // Edit menu (Mac) + template[2].submenu.push( + { + type: 'separator' + }, + { + label: 'Speech', + submenu: [ + { + role: 'startspeaking' + }, + { + role: 'stopspeaking' + } + ] + } + ); + + // Window menu (Mac) + template.push({ + role: 'window', + submenu: [ + { + role: 'minimize' + }, + { + type: 'separator' + }, + { + role: 'front' + } + ] + }); + } + + // On Windows and Linux, open dialogs do not support selecting both files and + // folders and files, so add an extra menu item so there is one for each type. + if (process.platform === 'linux' || process.platform === 'win32') { + // Edit menu (Windows, Linux) + template[1].submenu.push( + { + type: 'separator' + }, + { + label: 'Preferences', + accelerator: 'CmdOrCtrl+,', + click: function () { console.log('TODO: implement preferences'); } + } + ); + + // Add an help.about option + template.push({ + label: 'Help', + submenu: [ + { + label: 'About', + click: function () { console.log('TODO: implement about'); }, + } + ], + }); + } + // Add "File > Quit" menu item so Linux distros where the system tray icon is + // missing will have a way to quit the app. + if (process.platform === 'linux') { + // File menu (Linux) + template[0].submenu.push({ + label: 'Quit', + accelerator: 'CmdOrCtrl+Q', + click: function () { return app.quit(); } + }); + } + + menu = electron.Menu.buildFromTemplate(template); + electron.Menu.setApplicationMenu(menu); + + win.webContents.on('dom-ready', updateFullScreenCheckbox); + win.on('enter-full-screen', updateFullScreenCheckbox); + win.on('leave-full-screen', updateFullScreenCheckbox); +} + +module.exports.init = init;