feat: add registerAccelerator flag to allow menu items to optionally skip accelerator registration (#15723)

* feat: add registerAccelerator flag to allow menu items to skip registration

* docs: add docs for registerAccelerator

* docs: re-add accidentally removed line
This commit is contained in:
Heilig Benedek 2018-11-26 19:43:55 +01:00 committed by Jeremy Apthorp
parent 3748ee49ea
commit 0242818f39
9 changed files with 43 additions and 7 deletions

View file

@ -16,12 +16,14 @@ const roles = {
copy: {
label: 'Copy',
accelerator: 'CommandOrControl+C',
webContentsMethod: 'copy'
webContentsMethod: 'copy',
registerAccelerator: false
},
cut: {
label: 'Cut',
accelerator: 'CommandOrControl+X',
webContentsMethod: 'cut'
webContentsMethod: 'cut',
registerAccelerator: false
},
delete: {
label: 'Delete',
@ -59,12 +61,14 @@ const roles = {
paste: {
label: 'Paste',
accelerator: 'CommandOrControl+V',
webContentsMethod: 'paste'
webContentsMethod: 'paste',
registerAccelerator: false
},
pasteandmatchstyle: {
label: 'Paste and Match Style',
accelerator: 'Shift+CommandOrControl+V',
webContentsMethod: 'pasteAndMatchStyle'
webContentsMethod: 'pasteAndMatchStyle',
registerAccelerator: false
},
quit: {
get label () {
@ -243,6 +247,10 @@ exports.getDefaultAccelerator = (role) => {
if (roles.hasOwnProperty(role)) return roles[role].accelerator
}
exports.shouldRegisterAccelerator = (role) => {
return roles.hasOwnProperty(role) ? roles[role].registerAccelerator : true
}
exports.getDefaultSubmenu = (role) => {
if (!roles.hasOwnProperty(role)) return

View file

@ -36,6 +36,7 @@ const MenuItem = function (options) {
this.overrideProperty('enabled', true)
this.overrideProperty('visible', true)
this.overrideProperty('checked', false)
this.overrideProperty('registerAccelerator', roles.shouldRegisterAccelerator(this.role))
if (!MenuItem.types.includes(this.type)) {
throw new Error(`Unknown menu item type: ${this.type}`)

View file

@ -24,6 +24,7 @@ const delegate = {
if (command.accelerator != null) return command.accelerator
if (useDefaultAccelerator) return command.getDefaultRoleAccelerator()
},
shouldRegisterAcceleratorForCommandId: (menu, id) => menu.commandsMap[id] ? menu.commandsMap[id].registerAccelerator : undefined,
executeCommand: (menu, event, id) => {
const command = menu.commandsMap[id]
if (!command) return