diff --git a/src/electron/menu.js b/src/electron/menu.js index 6fbdaf5a2d9..0f7a5d03926 100644 --- a/src/electron/menu.js +++ b/src/electron/menu.js @@ -1,152 +1,166 @@ var app = require('app'); var Menu = require('menu'); -var debug = require('debug')('scout-electron:menu'); -function getTemplate(_window) { - if (process.platform === 'darwin') { - return [ - { - label: 'MongoDB Compass', - submenu: [ - { - label: 'About Compass', - selector: 'orderFrontStandardAboutPanel:' - }, - { - type: 'separator' - }, - { - label: 'Hide', - accelerator: 'Command+H', - selector: 'hide:' - }, - { - label: 'Hide Others', - accelerator: 'Command+Shift+H', - selector: 'hideOtherApplications:' - }, - { - label: 'Show All', - selector: 'unhideAllApplications:' - }, - { - type: 'separator' - }, - { - label: 'Quit', - accelerator: 'Command+Q', - click: function() { - app.quit(); - } - } - ] - }, - { - label: 'Connect', - submenu: [ - { - label: 'Connect to...', - accelerator: 'Command+N', - click: function() { - app.emit('show connect dialog'); - } +var menu = (function() { + return { + init: function(window) { + /* eslint-disable no-extra-parens */ + var menu = (process.platform == 'darwin') + ? darwinMenu(window) : nonDarwinMenu(window); + /* eslint-enable no-extra-parens */ + menu = Menu.buildFromTemplate(menu); + Menu.setApplicationMenu(menu); + } + }; +}()); + +// menus +function darwinMenu(window) { + return [ + { + label: 'MongoDB Compass', + submenu: [ + { + label: 'About Compass', + selector: 'orderFrontStandardAboutPanel:' + }, + { + type: 'separator' + }, + { + label: 'Hide', + accelerator: 'Command+H', + selector: 'hide:' + }, + { + label: 'Hide Others', + accelerator: 'Command+Shift+H', + selector: 'hideOtherApplications:' + }, + { + label: 'Show All', + selector: 'unhideAllApplications:' + }, + { + type: 'separator' + }, + { + label: 'Quit', + accelerator: 'Command+Q', + click: function() { + app.quit(); } - ] - }, - { - label: 'Edit', - submenu: [ - { - label: 'Undo', - accelerator: 'Command+Z', - role: 'undo' - }, - { - label: 'Redo', - accelerator: 'Shift+Command+Z', - role: 'redo' - }, - { - type: 'separator' - }, - { - label: 'Cut', - accelerator: 'Command+X', - role: 'cut' - }, - { - label: 'Copy', - accelerator: 'Command+C', - role: 'copy' - }, - { - label: 'Paste', - accelerator: 'Command+V', - role: 'paste' - }, - { - label: 'Select All', - accelerator: 'Command+A', - role: 'selectall' + } + ] + }, + { + label: 'Connect', + submenu: [ + { + label: 'Connect to...', + accelerator: 'Command+N', + click: function() { + app.emit('show connect dialog'); } - ] - }, - { - label: 'View', - submenu: [ - { - label: 'Reload', - accelerator: 'Command+R', - click: function() { - _window.restart(); - } - }, - { - label: 'Toggle DevTools', - accelerator: 'Alt+Command+I', - click: function() { - _window.toggleDevTools(); - } + } + ] + }, + { + label: 'Edit', + submenu: [ + { + label: 'Undo', + accelerator: 'Command+Z', + role: 'undo' + }, + { + label: 'Redo', + accelerator: 'Shift+Command+Z', + role: 'redo' + }, + { + type: 'separator' + }, + { + label: 'Cut', + accelerator: 'Command+X', + role: 'cut' + }, + { + label: 'Copy', + accelerator: 'Command+C', + role: 'copy' + }, + { + label: 'Paste', + accelerator: 'Command+V', + role: 'paste' + }, + { + label: 'Select All', + accelerator: 'Command+A', + role: 'selectall' + } + ] + }, + { + label: 'View', + submenu: [ + { + label: 'Reload', + accelerator: 'Command+R', + click: function() { + window.restart(); } - ] - }, - { - label: 'Share', - submenu: [ - { - label: 'Share Schema as JSON', - accelerator: 'Alt+Command+S', - click: function() { - _window.webContents.send('message', 'menu-share-schema-json'); - } + }, + { + label: 'Toggle DevTools', + accelerator: 'Alt+Command+I', + click: function() { + window.toggleDevTools(); } - ] - }, - { - label: 'Window', - submenu: [ - { - label: 'Minimize', - accelerator: 'Command+M', - role: 'minimize' - }, - { - label: 'Close', - accelerator: 'Command+W', - role: 'close' - }, - { - type: 'separator' - }, - { - label: 'Bring All to Front', - selector: 'arrangeInFront:' + } + ] + }, + + { + label: 'Share', + submenu: [ + { + label: 'Share Schema as JSON', + accelerator: 'Alt+Command+S', + click: function() { + window.webContents.send('message', 'menu-share-schema-json'); } - ] - } - ]; - } + } + ] + }, + { + label: 'Window', + submenu: [ + { + label: 'Minimize', + accelerator: 'Command+M', + role: 'minimize' + }, + { + label: 'Close', + accelerator: 'Command+W', + role: 'close' + }, + { + type: 'separator' + }, + { + label: 'Bring All to Front', + selector: 'arrangeInFront:' + } + ] + } + ]; +}; +function nonDarwinMenu(window) { return [ { label: 'File', @@ -177,28 +191,19 @@ function getTemplate(_window) { label: 'Reload', accelerator: 'Ctrl+R', click: function() { - _window.restart(); + window.restart(); } }, { label: 'Toggle DevTools', accelerator: 'Alt+Ctrl+I', click: function() { - _window.toggleDevTools(); + window.toggleDevTools(); } } ] } ]; -} - -/** - * @param {BrowserWindow} _window - The window to attach to. - * @see https://github.com/atom/electron/blob/master/docs/api/menu.md - */ -module.exports = function(_window) { - debug('attaching window menu'); - var template = getTemplate(_window); - var menu = Menu.buildFromTemplate(template); - Menu.setApplicationMenu(menu); }; + +module.exports = menu; \ No newline at end of file diff --git a/src/electron/window-manager.js b/src/electron/window-manager.js index df4556c6f38..3648696a164 100644 --- a/src/electron/window-manager.js +++ b/src/electron/window-manager.js @@ -6,10 +6,10 @@ var path = require('path'); var _ = require('lodash'); var app = require('app'); -var attachMenu = require('./menu'); var BrowserWindow = require('browser-window'); var config = require('./config'); var debug = require('debug')('scout-electron:window-manager'); +var menu = require('./menu'); /** * When running in electron, we're in `RESOURCES/src/electron`. @@ -62,6 +62,7 @@ module.exports.create = function(opts) { 'direct-write': true } }); + menu.init(_window); // makes the application a single instance application // see "app.makeSingleInstance" in https://github.com/atom/electron/blob/master/docs/api/app.md @@ -79,7 +80,6 @@ module.exports.create = function(opts) { return; } - attachMenu(_window); _window.loadUrl(opts.url); _window.webContents.on('new-window', function(event, url) {