diff --git a/packages/frontend/electron/src/main/application-menu/create.ts b/packages/frontend/electron/src/main/application-menu/create.ts index ab78209160..0120646f8a 100644 --- a/packages/frontend/electron/src/main/application-menu/create.ts +++ b/packages/frontend/electron/src/main/application-menu/create.ts @@ -10,6 +10,7 @@ import { reloadView, showDevTools, showMainWindow, + switchTab, undoCloseTab, } from '../windows-manager'; import { applicationMenuSubjects } from './subject'; @@ -138,6 +139,22 @@ export function createApplicationMenu() { undoCloseTab().catch(console.error); }, }, + { + label: 'Switch to tab', + acceleratorWorksWhenHidden: true, + visible: false, + submenu: [1, 2, 3, 4, 5, 6, 7, 8, 9].map(n => { + const shortcut = `CommandOrControl+${n}`; + const listener = () => { + switchTab(n); + }; + return { + label: `Switch to tab ${n}`, + accelerator: shortcut, + click: listener, + }; + }), + }, ], }, { diff --git a/packages/frontend/electron/src/main/windows-manager/tab-views.ts b/packages/frontend/electron/src/main/windows-manager/tab-views.ts index 45ab653409..49a2921cb7 100644 --- a/packages/frontend/electron/src/main/windows-manager/tab-views.ts +++ b/packages/frontend/electron/src/main/windows-manager/tab-views.ts @@ -3,7 +3,6 @@ import { join } from 'node:path'; import { app, type CookiesSetDetails, - globalShortcut, type View, type WebContents, WebContentsView, @@ -700,27 +699,6 @@ export class WebContentViewsManager { }) ); - app.on('ready', () => { - // bind CMD/CTRL+1~8 to switch tabs - // bind CMD/CTRL+9 to switch to the last tab - [1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(n => { - const shortcut = `CommandOrControl+${n}`; - const listener = () => { - if (!this.mainWindow?.isFocused()) { - return; - } - const item = this.tabViewsMeta.workbenches.at(n === 9 ? -1 : n - 1); - if (item) { - this.showTab(item.id).catch(logger.error); - } - }; - globalShortcut.register(shortcut, listener); - disposables.push({ - unsubscribe: () => globalShortcut.unregister(shortcut), - }); - }); - }); - app.on('before-quit', () => { disposables.forEach(d => d.unsubscribe()); }); @@ -1030,3 +1008,12 @@ export const pingAppLayoutReady = (wc: WebContents) => { WebContentViewsManager.instance.setTabUIReady(viewId); } }; + +export const switchTab = (n: number) => { + const item = WebContentViewsManager.instance.tabViewsMeta.workbenches.at( + n === 9 ? -1 : n - 1 + ); + if (item) { + WebContentViewsManager.instance.showTab(item.id).catch(logger.error); + } +};