Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default menu items for 'Edit' and 'Window' #2814 #8880

Merged
merged 6 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/api/menu-item.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a space after Undo,

* `menuWindow` - Whole default "Window" menu (Minimize, Close, etc.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about calling these windowMenu and editMenu instead? That reads a little clearer to me.


On macOS `role` can also have following additional values:

* `about` - Map to the `orderFrontStandardAboutPanel` action
Expand Down
78 changes: 78 additions & 0 deletions lib/browser/api/menu-item-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,69 @@ 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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this label needed here? Shouldn't it use the one defined by the front role if unspecified?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the label is unnecessary - fixed.

role: 'front'
} : {}

]
}
}

Expand All @@ -176,6 +239,21 @@ exports.getDefaultAccelerator = (role) => {
if (roles.hasOwnProperty(role)) return roles[role].accelerator
}

exports.getDefaultSubmenu = (role) => {
if (roles.hasOwnProperty(role)) {
let 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about if instead of empty objects you just had null in the data structure, then this check becomes much simpler as return n == null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

})
}

return submenu
}
}

exports.execute = (role, focusedWindow, focusedWebContents) => {
if (!canExecuteRole(role)) return false

Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a spec for these new cases in the https://github.com/electron/electron/blob/master/spec/api-menu-spec.js#L424 block?

Copy link
Contributor Author

@mst128256 mst128256 Mar 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added - please check next commits.

if (this.submenu != null && this.submenu.constructor !== Menu) {
this.submenu = Menu.buildFromTemplate(this.submenu)
}
Expand Down