fix(desktop): retain browser keys across windows

This commit is contained in:
Mohamed Boudra
2026-07-16 21:47:42 +00:00
parent d5499a9078
commit 674233418a
2 changed files with 38 additions and 5 deletions
@@ -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);
@@ -53,7 +53,7 @@ interface BrowserKeyboardGuest {
}
export class BrowserKeyboard {
private readonly guestsByBrowserId = new Map<string, BrowserKeyboardGuest>();
private readonly guestsByHostAndBrowserId = new Map<string, BrowserKeyboardGuest>();
private readonly guestsByWebContentsId = new Map<number, BrowserKeyboardGuest>();
private readonly policiesByHostWebContentsId = new Map<number, BrowserKeyboardPolicy>();
@@ -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}`;
}