fix: do not use globalShortcut for tab switching (#7827)

fix #7826
This commit is contained in:
pengx17
2024-08-11 07:56:46 +00:00
parent d82f4b5461
commit a6169ab26a
2 changed files with 26 additions and 22 deletions

View File

@@ -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,
};
}),
},
],
},
{

View File

@@ -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);
}
};