added default menu items for 'Edit' and 'Window' #2814

This commit is contained in:
mst128256 2017-03-09 16:01:33 +01:00
parent aaa8e81cd4
commit 8aba640250
3 changed files with 85 additions and 1 deletions

View file

@ -64,6 +64,9 @@ The `role` property can have following values:
* `zoomin` - Zoom in the focused page by 10%
* `zoomout` - Zoom out the focused page by 10%
* `menuEdit` - Whole default "Edit" menu (Undo,Copy, etc.)
* `menuWindow` - Whole default "Window" menu (Minimize, Close, etc.)
On macOS `role` can also have following additional values:
* `about` - Map to the `orderFrontStandardAboutPanel` action

View file

@ -154,6 +154,73 @@ const roles = {
webContents.setZoomLevel(zoomLevel - 0.5)
})
}
},
// submenu Edit (should fit both Mac & Windows)
menuEdit: {
label: 'Edit',
submenu: [
{
role: 'undo'
},
{
role: 'redo'
},
{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
process.platform === 'darwin' ?
{
role: 'pasteandmatchstyle'
} : {},
{
role: 'delete'
},
process.platform === 'win32' ?
{
type: 'separator'
} : {},
{
role: 'selectall'
}
]
},
// submenu Window should be used for Mac only
menuWindow: {
label: 'Window',
submenu: [
{
role: 'minimize'
},
{
role: 'close'
},
process.platform === 'darwin' ?
{
type: 'separator'
} : {},
process.platform === 'darwin' ?
{
label: 'Bring All to Front',
role: 'front'
} : {}
]
}
}
@ -176,6 +243,20 @@ exports.getDefaultAccelerator = (role) => {
if (roles.hasOwnProperty(role)) return roles[role].accelerator
}
exports.getDefaultSubmenu = (role) => {
if (roles.hasOwnProperty(role)) {
submenu = roles[role].submenu
// remove empty objects from within the submenu
if (Array.isArray(submenu))
submenu = submenu.filter(function(n){
return n.constructor !== Object || Object.keys(n).length > 0
})
return submenu
}
}
exports.execute = (role, focusedWindow, focusedWebContents) => {
if (!canExecuteRole(role)) return false

View file

@ -11,7 +11,7 @@ const MenuItem = function (options) {
for (let key in options) {
if (!(key in this)) this[key] = options[key]
}
this.submenu = this.submenu || roles.getDefaultSubmenu(this.role)
if (this.submenu != null && this.submenu.constructor !== Menu) {
this.submenu = Menu.buildFromTemplate(this.submenu)
}