Add zoom menu item roles

This commit is contained in:
Kevin Sawicki 2016-08-08 10:09:45 -07:00
parent 59ffe35781
commit bcc372568f
4 changed files with 47 additions and 31 deletions

View file

@ -181,11 +181,11 @@ Role kRolesMap[] = {
// Set menu item's role. // Set menu item's role.
base::string16 role = model->GetRoleAt(index); base::string16 role = model->GetRoleAt(index);
if (role.empty()) { [item setTarget:self];
[item setTarget:self]; if (!role.empty()) {
} else {
for (const Role& pair : kRolesMap) { for (const Role& pair : kRolesMap) {
if (role == base::ASCIIToUTF16(pair.role)) { if (role == base::ASCIIToUTF16(pair.role)) {
[item setTarget:nil];
[item setAction:pair.selector]; [item setAction:pair.selector];
break; break;
} }

View file

@ -101,35 +101,13 @@ app.once('ready', () => {
type: 'separator' type: 'separator'
}, },
{ {
label: 'Actual Size', role: 'resetzoom'
accelerator: 'CmdOrCtrl+0',
click (item, focusedWindow) {
if (focusedWindow) focusedWindow.webContents.setZoomLevel(0)
}
}, },
{ {
label: 'Zoom In', role: 'zoomin'
accelerator: 'CmdOrCtrl+Plus',
click (item, focusedWindow) {
if (focusedWindow) {
const {webContents} = focusedWindow
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel + 0.5)
})
}
}
}, },
{ {
label: 'Zoom Out', role: 'zoomout'
accelerator: 'CmdOrCtrl+-',
click (item, focusedWindow) {
if (focusedWindow) {
const {webContents} = focusedWindow
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel - 0.5)
})
}
}
}, },
{ {
type: 'separator' type: 'separator'

View file

@ -56,6 +56,9 @@ The `role` property can have following values:
* `close` - Close current window * `close` - Close current window
* `quit`- Quit the application * `quit`- Quit the application
* `togglefullscreen`- Toggle full screen mode on the current window * `togglefullscreen`- Toggle full screen mode on the current window
* `resetzoom` - Reset the focused page's zoom level to the original size
* `zoomin` - Zoom in the focused page by 10%
* `zoomout` - Zoom out the focused page by 10%
On macOS `role` can also have following additional values: On macOS `role` can also have following additional values:

View file

@ -72,6 +72,13 @@ const roles = {
accelerator: 'Shift+CommandOrControl+Z', accelerator: 'Shift+CommandOrControl+Z',
webContentsMethod: 'redo' webContentsMethod: 'redo'
}, },
resetzoom: {
label: 'Actual Size',
accelerator: 'CommandOrControl+0',
webContentsMethod: (webContents) => {
webContents.setZoomLevel(0)
}
},
selectall: { selectall: {
label: 'Select All', label: 'Select All',
accelerator: 'CommandOrControl+A', accelerator: 'CommandOrControl+A',
@ -106,9 +113,34 @@ const roles = {
}, },
zoom: { zoom: {
label: 'Zoom' label: 'Zoom'
},
zoomin: {
label: 'Zoom In',
accelerator: 'CommandOrControl+Plus',
webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel + 0.5)
})
}
},
zoomout: {
label: 'Zoom Out',
accelerator: 'CommandOrControl+-',
webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel - 0.5)
})
}
} }
} }
const canExecuteRole = (role) => {
if (!roles.hasOwnProperty(role)) return false
if (process.platform !== 'darwin') return true
// macOS handles all roles natively except the ones listed below
return ['zoomin', 'zoomout', 'resetzoom'].includes(role)
}
exports.getDefaultLabel = (role) => { exports.getDefaultLabel = (role) => {
if (roles.hasOwnProperty(role)) { if (roles.hasOwnProperty(role)) {
return roles[role].label return roles[role].label
@ -122,8 +154,7 @@ exports.getDefaultAccelerator = (role) => {
} }
exports.execute = (role, focusedWindow, focusedWebContents) => { exports.execute = (role, focusedWindow, focusedWebContents) => {
if (!roles.hasOwnProperty(role)) return false if (!canExecuteRole(role)) return false
if (process.platform === 'darwin') return false
const {appMethod, webContentsMethod, windowMethod} = roles[role] const {appMethod, webContentsMethod, windowMethod} = roles[role]
@ -142,7 +173,11 @@ exports.execute = (role, focusedWindow, focusedWebContents) => {
} }
if (webContentsMethod && focusedWebContents != null) { if (webContentsMethod && focusedWebContents != null) {
focusedWebContents[webContentsMethod]() if (typeof webContentsMethod === 'function') {
webContentsMethod(focusedWebContents)
} else {
focusedWebContents[webContentsMethod]()
}
return true return true
} }