From 378c9eb73ad82b4493f7416459c0e59ec605ab10 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Mon, 4 Nov 2024 16:34:11 +0800 Subject: [PATCH] fix(electron): active view sometimes does not get focused (#8683) --- .../src/main/application-menu/create.ts | 16 ----------- .../src/main/windows-manager/tab-views.ts | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/packages/frontend/apps/electron/src/main/application-menu/create.ts b/packages/frontend/apps/electron/src/main/application-menu/create.ts index 80dde56c8f..f428b0b707 100644 --- a/packages/frontend/apps/electron/src/main/application-menu/create.ts +++ b/packages/frontend/apps/electron/src/main/application-menu/create.ts @@ -171,22 +171,6 @@ export function createApplicationMenu() { switchToPreviousTab(); }, }, - { - label: 'Switch to next tab (mac)', - accelerator: 'Command+]', - visible: false, - click: () => { - switchToNextTab(); - }, - }, - { - label: 'Switch to previous tab (mac)', - accelerator: 'Command+[', - visible: false, - click: () => { - switchToPreviousTab(); - }, - }, { label: 'Switch to next tab (mac 2)', accelerator: 'Alt+Command+]', diff --git a/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts b/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts index a5de578545..59aaa49f3d 100644 --- a/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts +++ b/packages/frontend/apps/electron/src/main/windows-manager/tab-views.ts @@ -734,6 +734,33 @@ export class WebContentViewsManager { app.on('before-quit', () => { disposables.forEach(d => d.unsubscribe()); }); + + const focusActiveView = () => { + if ( + !this.activeWorkbenchView || + this.activeWorkbenchView.webContents.isFocused() + ) { + return; + } + this.activeWorkbenchView?.webContents.focus(); + setTimeout(() => { + focusActiveView(); + }, 100); + }; + + app.on('browser-window-focus', () => { + focusActiveView(); + }); + + combineLatest([ + this.activeWorkbenchId$, + this.mainWindowManager.mainWindow$, + ]).subscribe(([_, window]) => { + // makes sure the active view is always focused + if (window?.isFocused()) { + focusActiveView(); + } + }); }; getViewById = (id: string) => {