fix: default accelerator for role-based menu items (#49669)

fix: apply default accelerator for role-based menu items

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot]
2026-02-09 10:09:18 -05:00
committed by GitHub
parent 091c5db763
commit bf4746c7c9
4 changed files with 64 additions and 4 deletions

View File

@@ -107,7 +107,7 @@ A `string` (optional) indicating the item's role, if set. Can be `undo`, `redo`,
#### `menuItem.accelerator`
An `Accelerator` (optional) indicating the item's accelerator, if set.
An `Accelerator | null` indicating the item's accelerator, if set.
#### `menuItem.userAccelerator` _Readonly_ _macOS_

View File

@@ -353,6 +353,7 @@ export function shouldOverrideCheckStatus (role: RoleId) {
export function getDefaultAccelerator (role: RoleId) {
if (hasRole(role)) return roleList[role].accelerator;
return undefined;
}
export function shouldRegisterAccelerator (role: RoleId) {

View File

@@ -25,7 +25,7 @@ const MenuItem = function (this: any, options: any) {
this.overrideReadOnlyProperty('type', roles.getDefaultType(this.role));
this.overrideReadOnlyProperty('role');
this.overrideReadOnlyProperty('accelerator');
this.overrideReadOnlyProperty('accelerator', roles.getDefaultAccelerator(this.role));
this.overrideReadOnlyProperty('icon');
this.overrideReadOnlyProperty('submenu');

View File

@@ -43,6 +43,65 @@ describe('MenuItems', () => {
expect(item).to.have.property('role').that.is.a('string');
expect(item).to.have.property('icon');
});
it('should have a default accelerator for certain roles', () => {
const items: Record<string, Electron.MenuItem['accelerator']> = {
undo: 'CommandOrControl+Z',
redo: process.platform === 'win32' ? 'Control+Y' : 'Shift+CommandOrControl+Z',
cut: 'CommandOrControl+X',
copy: 'CommandOrControl+C',
paste: 'CommandOrControl+V',
pasteAndMatchStyle: process.platform === 'darwin' ? 'Cmd+Option+Shift+V' : 'Shift+CommandOrControl+V',
delete: null,
selectAll: 'CommandOrControl+A',
reload: 'CmdOrCtrl+R',
forceReload: 'Shift+CmdOrCtrl+R',
toggleDevTools: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
resetZoom: 'CommandOrControl+0',
zoomIn: 'CommandOrControl+Plus',
zoomOut: 'CommandOrControl+-',
toggleSpellChecker: null,
togglefullscreen: process.platform === 'darwin' ? 'Control+Command+F' : 'F11',
window: null,
minimize: 'CommandOrControl+M',
close: 'CommandOrControl+W',
help: null,
about: null,
services: null,
hide: 'Command+H',
hideOthers: 'Command+Alt+H',
unhide: null,
quit: process.platform === 'win32' ? null : 'CommandOrControl+Q',
showSubstitutions: null,
toggleSmartQuotes: null,
toggleSmartDashes: null,
toggleTextReplacement: null,
startSpeaking: null,
stopSpeaking: null,
zoom: null,
front: null,
appMenu: null,
fileMenu: null,
editMenu: null,
viewMenu: null,
shareMenu: null,
recentDocuments: null,
toggleTabBar: null,
selectNextTab: null,
selectPreviousTab: null,
showAllTabs: null,
mergeAllWindows: null,
clearRecentDocuments: null,
moveTabToNewWindow: null,
windowMenu: null
};
for (const role in items) {
if (!Object.hasOwn(items, role)) continue;
const item = new MenuItem({ role: role as any });
expect(item.accelerator).to.equal(items[role]);
}
});
});
describe('MenuItem.click', () => {
@@ -480,7 +539,7 @@ describe('MenuItems', () => {
it('should display modifiers correctly for simple keys', () => {
const menu = Menu.buildFromTemplate([
{ label: 'text', accelerator: 'CmdOrCtrl+A' },
{ label: 'text', accelerator: 'CommandOrControl+A' },
{ label: 'text', accelerator: 'Shift+A' },
{ label: 'text', accelerator: 'Alt+A' }
]);
@@ -492,7 +551,7 @@ describe('MenuItems', () => {
it('should display modifiers correctly for special keys', () => {
const menu = Menu.buildFromTemplate([
{ label: 'text', accelerator: 'CmdOrCtrl+Tab' },
{ label: 'text', accelerator: 'CommandOrControl+Tab' },
{ label: 'text', accelerator: 'Shift+Tab' },
{ label: 'text', accelerator: 'Alt+Tab' }
]);