From a16d907684fca735ca1b5a402adabb6cefdd0bdd Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Jun 2016 17:03:48 -0700 Subject: [PATCH 1/8] Add quit role --- lib/browser/api/menu-item.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/browser/api/menu-item.js b/lib/browser/api/menu-item.js index 4d2737d0fa..5ff96766c9 100644 --- a/lib/browser/api/menu-item.js +++ b/lib/browser/api/menu-item.js @@ -15,7 +15,8 @@ rolesMap = { selectall: 'selectAll', minimize: 'minimize', close: 'close', - delete: 'delete' + delete: 'delete', + quit: 'quit' } // Maps methods that should be called directly on the BrowserWindow instance @@ -24,12 +25,16 @@ methodInBrowserWindow = { close: true } +const methodInApp = { + quit: true +} + MenuItem = (function () { MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'] function MenuItem (options) { var click, ref - const Menu = require('electron').Menu + const {app, Menu} = require('electron') click = options.click this.selector = options.selector this.type = options.type @@ -73,7 +78,9 @@ MenuItem = (function () { } if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) { methodName = rolesMap[this.role] - if (methodInBrowserWindow[methodName]) { + if(methodInApp[methodName]) { + return app[methodName]() + } else if (methodInBrowserWindow[methodName]) { return focusedWindow[methodName]() } else { return (ref2 = focusedWindow.webContents) != null ? ref2[methodName]() : void 0 From 67f7b4d4c583798ab0037d735732e1befad49030 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Jun 2016 17:03:59 -0700 Subject: [PATCH 2/8] Map quit role to terminate selector --- atom/browser/ui/cocoa/atom_menu_controller.mm | 1 + 1 file changed, 1 insertion(+) diff --git a/atom/browser/ui/cocoa/atom_menu_controller.mm b/atom/browser/ui/cocoa/atom_menu_controller.mm index 642721e6bd..26cf2a94ed 100644 --- a/atom/browser/ui/cocoa/atom_menu_controller.mm +++ b/atom/browser/ui/cocoa/atom_menu_controller.mm @@ -38,6 +38,7 @@ Role kRolesMap[] = { { @selector(performMiniaturize:), "minimize" }, { @selector(performClose:), "close" }, { @selector(performZoom:), "zoom" }, + { @selector(terminate:), "quit" }, }; } // namespace From 7a6436014e9a3d67faf84d979c4fec7938b7a532 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Jun 2016 17:04:03 -0700 Subject: [PATCH 3/8] Use quit role --- default_app/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/default_app/main.js b/default_app/main.js index 4e5ea96b03..0fdf30bf82 100644 --- a/default_app/main.js +++ b/default_app/main.js @@ -214,7 +214,7 @@ app.once('ready', () => { { label: 'Quit ' + app.getName(), accelerator: 'Command+Q', - click () { app.quit() } + role: 'quit' } ] }) From 11ba7eaa91d8e9d1d55746a485ccb2d1940a6aa0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Jun 2016 17:08:45 -0700 Subject: [PATCH 4/8] Add File > Exit menu on Windows --- default_app/main.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/default_app/main.js b/default_app/main.js index 0fdf30bf82..4c3c658420 100644 --- a/default_app/main.js +++ b/default_app/main.js @@ -243,6 +243,18 @@ app.once('ready', () => { ] } + if (process.platform === 'win32') { + template.unshift({ + label: 'File', + submenu: [ + { + label: 'Exit', + role: 'quit' + } + ] + }) + } + const menu = Menu.buildFromTemplate(template) Menu.setApplicationMenu(menu) }) From 2d8ec60eedecc0aa53d14a41f763719bc6288002 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Jun 2016 17:14:16 -0700 Subject: [PATCH 5/8] Use const/let instead of var --- lib/browser/api/menu-item.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/browser/api/menu-item.js b/lib/browser/api/menu-item.js index 5ff96766c9..15ede39609 100644 --- a/lib/browser/api/menu-item.js +++ b/lib/browser/api/menu-item.js @@ -1,11 +1,9 @@ 'use strict' -var MenuItem, methodInBrowserWindow, nextCommandId, rolesMap - -nextCommandId = 0 +let nextCommandId = 0 // Maps role to methods of webContents -rolesMap = { +const rolesMap = { undo: 'undo', redo: 'redo', cut: 'cut', @@ -20,7 +18,7 @@ rolesMap = { } // Maps methods that should be called directly on the BrowserWindow instance -methodInBrowserWindow = { +const methodInBrowserWindow = { minimize: true, close: true } @@ -29,11 +27,11 @@ const methodInApp = { quit: true } -MenuItem = (function () { +const MenuItem = (function () { MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'] function MenuItem (options) { - var click, ref + let click, ref const {app, Menu} = require('electron') click = options.click this.selector = options.selector @@ -72,7 +70,7 @@ MenuItem = (function () { this.commandId = ++nextCommandId this.click = (focusedWindow) => { // Manually flip the checked flags when clicked. - var methodName, ref1, ref2 + let methodName, ref1, ref2 if ((ref1 = this.type) === 'checkbox' || ref1 === 'radio') { this.checked = !this.checked } From 32ea5a5e27e1d99e6bef46a3b44d9761ff261873 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Jun 2016 17:16:34 -0700 Subject: [PATCH 6/8] Remove CoffeeScript ref variables --- lib/browser/api/menu-item.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/browser/api/menu-item.js b/lib/browser/api/menu-item.js index 15ede39609..7b5a688cb1 100644 --- a/lib/browser/api/menu-item.js +++ b/lib/browser/api/menu-item.js @@ -31,9 +31,9 @@ const MenuItem = (function () { MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'] function MenuItem (options) { - let click, ref const {app, Menu} = require('electron') - click = options.click + + const click = options.click this.selector = options.selector this.type = options.type this.role = options.role @@ -51,7 +51,7 @@ const MenuItem = (function () { if ((this.type == null) && (this.submenu != null)) { this.type = 'submenu' } - if (this.type === 'submenu' && ((ref = this.submenu) != null ? ref.constructor : void 0) !== Menu) { + if (this.type === 'submenu' && (this.submenu != null ? this.submenu.constructor : void 0) !== Menu) { throw new Error('Invalid submenu') } this.overrideReadOnlyProperty('type', 'normal') @@ -70,18 +70,19 @@ const MenuItem = (function () { this.commandId = ++nextCommandId this.click = (focusedWindow) => { // Manually flip the checked flags when clicked. - let methodName, ref1, ref2 - if ((ref1 = this.type) === 'checkbox' || ref1 === 'radio') { + if (this.type === 'checkbox' || this.type === 'radio') { this.checked = !this.checked } + if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) { - methodName = rolesMap[this.role] + const methodName = rolesMap[this.role] if(methodInApp[methodName]) { return app[methodName]() } else if (methodInBrowserWindow[methodName]) { return focusedWindow[methodName]() } else { - return (ref2 = focusedWindow.webContents) != null ? ref2[methodName]() : void 0 + const {webContents} = focusedWindow + return webContents != null ? webContents[methodName]() : void 0 } } else if (typeof click === 'function') { return click(this, focusedWindow) From 395c3727020792a9a8975d4d8b5aa5f788f7fc93 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 20 Jun 2016 17:20:57 -0700 Subject: [PATCH 7/8] Document quit role --- docs/api/menu-item.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/api/menu-item.md b/docs/api/menu-item.md index ec3fef55e5..391b639e11 100644 --- a/docs/api/menu-item.md +++ b/docs/api/menu-item.md @@ -51,6 +51,7 @@ The `role` property can have following values: * `delete` * `minimize` - Minimize current window * `close` - Close current window +* `quit`- Quit the application On macOS `role` can also have following additional values: From fbfac6d49bd49c90106e68ae78e71d9789459ac3 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 21 Jun 2016 09:07:56 -0700 Subject: [PATCH 8/8] Fix lint errors --- lib/browser/api/menu-item.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/browser/api/menu-item.js b/lib/browser/api/menu-item.js index 7b5a688cb1..f38ab970d6 100644 --- a/lib/browser/api/menu-item.js +++ b/lib/browser/api/menu-item.js @@ -76,7 +76,7 @@ const MenuItem = (function () { if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) { const methodName = rolesMap[this.role] - if(methodInApp[methodName]) { + if (methodInApp[methodName]) { return app[methodName]() } else if (methodInBrowserWindow[methodName]) { return focusedWindow[methodName]()