Merge pull request #6154 from electron/quit-role

Add quit menu item role
This commit is contained in:
Kevin Sawicki 2016-06-21 09:20:22 -07:00 committed by GitHub
commit 59ff3dc2f3
4 changed files with 37 additions and 17 deletions

View file

@ -38,6 +38,7 @@ Role kRolesMap[] = {
{ @selector(performMiniaturize:), "minimize" }, { @selector(performMiniaturize:), "minimize" },
{ @selector(performClose:), "close" }, { @selector(performClose:), "close" },
{ @selector(performZoom:), "zoom" }, { @selector(performZoom:), "zoom" },
{ @selector(terminate:), "quit" },
}; };
} // namespace } // namespace

View file

@ -214,7 +214,7 @@ app.once('ready', () => {
{ {
label: 'Quit ' + app.getName(), label: 'Quit ' + app.getName(),
accelerator: 'Command+Q', accelerator: 'Command+Q',
click () { app.quit() } role: 'quit'
} }
] ]
}) })
@ -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) const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu) Menu.setApplicationMenu(menu)
}) })

View file

@ -51,6 +51,7 @@ The `role` property can have following values:
* `delete` * `delete`
* `minimize` - Minimize current window * `minimize` - Minimize current window
* `close` - Close current window * `close` - Close current window
* `quit`- Quit the application
On macOS `role` can also have following additional values: On macOS `role` can also have following additional values:

View file

@ -1,11 +1,9 @@
'use strict' 'use strict'
var MenuItem, methodInBrowserWindow, nextCommandId, rolesMap let nextCommandId = 0
nextCommandId = 0
// Maps role to methods of webContents // Maps role to methods of webContents
rolesMap = { const rolesMap = {
undo: 'undo', undo: 'undo',
redo: 'redo', redo: 'redo',
cut: 'cut', cut: 'cut',
@ -15,22 +13,27 @@ rolesMap = {
selectall: 'selectAll', selectall: 'selectAll',
minimize: 'minimize', minimize: 'minimize',
close: 'close', close: 'close',
delete: 'delete' delete: 'delete',
quit: 'quit'
} }
// Maps methods that should be called directly on the BrowserWindow instance // Maps methods that should be called directly on the BrowserWindow instance
methodInBrowserWindow = { const methodInBrowserWindow = {
minimize: true, minimize: true,
close: true close: true
} }
MenuItem = (function () { const methodInApp = {
quit: true
}
const MenuItem = (function () {
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio'] MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
function MenuItem (options) { function MenuItem (options) {
var click, ref const {app, Menu} = require('electron')
const Menu = require('electron').Menu
click = options.click const click = options.click
this.selector = options.selector this.selector = options.selector
this.type = options.type this.type = options.type
this.role = options.role this.role = options.role
@ -48,7 +51,7 @@ MenuItem = (function () {
if ((this.type == null) && (this.submenu != null)) { if ((this.type == null) && (this.submenu != null)) {
this.type = 'submenu' 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') throw new Error('Invalid submenu')
} }
this.overrideReadOnlyProperty('type', 'normal') this.overrideReadOnlyProperty('type', 'normal')
@ -67,16 +70,19 @@ MenuItem = (function () {
this.commandId = ++nextCommandId this.commandId = ++nextCommandId
this.click = (focusedWindow) => { this.click = (focusedWindow) => {
// Manually flip the checked flags when clicked. // Manually flip the checked flags when clicked.
var methodName, ref1, ref2 if (this.type === 'checkbox' || this.type === 'radio') {
if ((ref1 = this.type) === 'checkbox' || ref1 === 'radio') {
this.checked = !this.checked this.checked = !this.checked
} }
if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) { if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) {
methodName = rolesMap[this.role] const methodName = rolesMap[this.role]
if (methodInBrowserWindow[methodName]) { if (methodInApp[methodName]) {
return app[methodName]()
} else if (methodInBrowserWindow[methodName]) {
return focusedWindow[methodName]() return focusedWindow[methodName]()
} else { } 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') { } else if (typeof click === 'function') {
return click(this, focusedWindow) return click(this, focusedWindow)