From 674233418a63544e0f168925d27c554f0aad0fc8 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Fri, 10 Jul 2026 15:15:29 +0200 Subject: [PATCH] fix(desktop): retain browser keys across windows --- .../features/browser-keyboard/index.test.ts | 26 +++++++++++++++++++ .../src/features/browser-keyboard/index.ts | 17 ++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/desktop/src/features/browser-keyboard/index.test.ts b/packages/desktop/src/features/browser-keyboard/index.test.ts index 8f3908fe4e..b80fb19b31 100644 --- a/packages/desktop/src/features/browser-keyboard/index.test.ts +++ b/packages/desktop/src/features/browser-keyboard/index.test.ts @@ -193,6 +193,32 @@ describe("BrowserKeyboard", () => { ]); }); + test("keeps same-browser guests active in separate host windows", () => { + const keyboard = new BrowserKeyboard(); + const firstGuest = new FakeBrowserContents(57); + const secondGuest = new FakeBrowserContents(58); + const firstHost = new FakeBrowserContents(59); + const secondHost = new FakeBrowserContents(60); + keyboard.attach({ browserId: "browser-a", contents: firstGuest, hostContents: firstHost }); + keyboard.attach({ browserId: "browser-a", contents: secondGuest, hostContents: secondHost }); + + keyboard.forwardShortcutInput(firstGuest, shortcutInput("browser-a")); + keyboard.forwardShortcutInput(secondGuest, shortcutInput("browser-a")); + + expect(firstHost.sent).toEqual([ + { + channel: "paseo:event:browser-shortcut-input", + payload: shortcutInput("browser-a"), + }, + ]); + expect(secondHost.sent).toEqual([ + { + channel: "paseo:event:browser-shortcut-input", + payload: shortcutInput("browser-a"), + }, + ]); + }); + test("resends the latest shortcut policy after every main-frame load", () => { const keyboard = new BrowserKeyboard(); const guest = new FakeBrowserContents(61); diff --git a/packages/desktop/src/features/browser-keyboard/index.ts b/packages/desktop/src/features/browser-keyboard/index.ts index c12ce3cb48..3627a43ef7 100644 --- a/packages/desktop/src/features/browser-keyboard/index.ts +++ b/packages/desktop/src/features/browser-keyboard/index.ts @@ -53,7 +53,7 @@ interface BrowserKeyboardGuest { } export class BrowserKeyboard { - private readonly guestsByBrowserId = new Map(); + private readonly guestsByHostAndBrowserId = new Map(); private readonly guestsByWebContentsId = new Map(); private readonly policiesByHostWebContentsId = new Map(); @@ -80,11 +80,13 @@ export class BrowserKeyboard { if (guestAtWebContentsId) { this.detachGuest(guestAtWebContentsId); } - const guestForBrowser = this.guestsByBrowserId.get(guest.browserId); + const guestForBrowser = this.guestsByHostAndBrowserId.get( + guestKey(guest.hostWebContentsId, guest.browserId), + ); if (guestForBrowser) { this.detachGuest(guestForBrowser); } - this.guestsByBrowserId.set(guest.browserId, guest); + this.guestsByHostAndBrowserId.set(guestKey(guest.hostWebContentsId, guest.browserId), guest); this.guestsByWebContentsId.set(guest.webContentsId, guest); input.contents.once("destroyed", () => { @@ -149,8 +151,9 @@ export class BrowserKeyboard { if (this.guestsByWebContentsId.get(guest.webContentsId) === guest) { this.guestsByWebContentsId.delete(guest.webContentsId); } - if (this.guestsByBrowserId.get(guest.browserId) === guest) { - this.guestsByBrowserId.delete(guest.browserId); + const key = guestKey(guest.hostWebContentsId, guest.browserId); + if (this.guestsByHostAndBrowserId.get(key) === guest) { + this.guestsByHostAndBrowserId.delete(key); } } @@ -212,3 +215,7 @@ export class BrowserKeyboard { } } } + +function guestKey(hostWebContentsId: number, browserId: string): string { + return `${hostWebContentsId}:${browserId}`; +}