Skip to content

Commit 206a8e6

Browse files
authored
fix(desktop): close focused DevTools with Cmd+W (pingdotgg#33518)
Add a native Cmd/Ctrl+W shortcut that closes focused DevTools while retaining renderer tab and terminal shortcuts and repeated-key protection. Validation: 31 focused tests, check:changed, and CI passed. Electron runtime checks verified the close handler; automated native keypress verification remains unconfirmed. Model: GPT 6.1 Sol. Harness: Codex.
1 parent d0f7a50 commit 206a8e6

4 files changed

Lines changed: 61 additions & 7 deletions

File tree

‎apps/desktop/src/window/DesktopApplicationMenu.test.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import * as Deferred from "effect/Deferred";
44
import * as Effect from "effect/Effect";
55
import * as Layer from "effect/Layer";
66

7-
import type * as Electron from "electron";
7+
import * as Electron from "electron";
8+
import { vi } from "vite-plus/test";
9+
10+
vi.mock("electron", () => ({
11+
webContents: { getAllWebContents: vi.fn(() => []) },
12+
}));
813

914
import * as ElectronApp from "../electron/ElectronApp.ts";
1015
import * as ElectronDialog from "../electron/ElectronDialog.ts";
@@ -227,10 +232,32 @@ describe("DesktopApplicationMenu", () => {
227232
items.find((item) => item.role === "windowMenu" && item.submenu === undefined),
228233
platform,
229234
);
230-
assert.isUndefined(
231-
items.find((item) => item.accelerator?.toLowerCase().endsWith("+w")),
232-
platform,
233-
);
235+
const closeTools = items.find((item) => item.accelerator === "CmdOrCtrl+W");
236+
assert.equal(closeTools?.label, "Close Developer Tools", platform);
237+
assert.isFunction(closeTools?.click);
238+
const appContents = {
239+
isDestroyed: () => false,
240+
isDevToolsFocused: () => false,
241+
closeDevTools: vi.fn(),
242+
};
243+
const inspectedContents = {
244+
...appContents,
245+
isDevToolsFocused: () => true,
246+
closeDevTools: vi.fn(),
247+
};
248+
using allContents = vi
249+
.spyOn(Electron.webContents, "getAllWebContents")
250+
.mockReturnValue([
251+
appContents as unknown as Electron.WebContents,
252+
inspectedContents as unknown as Electron.WebContents,
253+
]);
254+
closeTools?.click?.({} as Electron.MenuItem, undefined, {} as KeyboardEvent);
255+
assert.equal(inspectedContents.closeDevTools.mock.calls.length, 1);
256+
assert.equal(appContents.closeDevTools.mock.calls.length, 0);
257+
258+
allContents.mockReturnValue([appContents as unknown as Electron.WebContents]);
259+
closeTools?.click?.({} as Electron.MenuItem, undefined, {} as KeyboardEvent);
260+
assert.equal(appContents.closeDevTools.mock.calls.length, 0);
234261
}
235262
}),
236263
);

‎apps/desktop/src/window/DesktopApplicationMenu.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Layer from "effect/Layer";
44
import * as Option from "effect/Option";
55
import * as Schema from "effect/Schema";
66

7-
import type * as Electron from "electron";
7+
import * as Electron from "electron";
88

99
import { makeComponentLogger } from "../app/DesktopObservability.ts";
1010
import * as ElectronDialog from "../electron/ElectronDialog.ts";
@@ -200,6 +200,17 @@ export const make = Effect.gen(function* () {
200200
},
201201
{ type: "separator" as const },
202202
]),
203+
{
204+
label: "Close Developer Tools",
205+
accelerator: "CmdOrCtrl+W",
206+
visible: false,
207+
click: () => {
208+
Electron.webContents
209+
.getAllWebContents()
210+
.find((contents) => !contents.isDestroyed() && contents.isDevToolsFocused())
211+
?.closeDevTools();
212+
},
213+
},
203214
// Cmd/Ctrl+W belongs to the renderer's tab close; it must never close the window.
204215
environment.platform === "darwin"
205216
? { label: "Close Window", click: (_item, window) => window?.close() }

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function makeFakeBrowserWindow() {
9494
replaceMisspelling: vi.fn(),
9595
send: vi.fn(),
9696
setBackgroundThrottling: vi.fn(),
97+
setIgnoreMenuShortcuts: vi.fn(),
9798
setWindowOpenHandler: vi.fn(),
9899
toggleDevTools: vi.fn(),
99100
};
@@ -143,6 +144,7 @@ function makeFakeBrowserWindow() {
143144
reloadIgnoringCache: webContents.reloadIgnoringCache,
144145
toggleDevTools: webContents.toggleDevTools,
145146
navigationHistory: webContents.navigationHistory,
147+
setIgnoreMenuShortcuts: webContents.setIgnoreMenuShortcuts,
146148
send: webContents.send,
147149
setZoomLevel: webContents.setZoomLevel,
148150
setWindowButtonPosition: window.setWindowButtonPosition,
@@ -646,7 +648,7 @@ describe("DesktopWindow", () => {
646648
}),
647649
);
648650

649-
it.effect("blocks only repeated Cmd+W input before it reaches the native window menu", () =>
651+
it.effect("leaves Cmd+W to the renderer and blocks repeated presses", () =>
650652
Effect.gen(function* () {
651653
const fakeWindow = makeFakeBrowserWindow();
652654
const createCount = yield* Ref.make(0);
@@ -687,6 +689,13 @@ describe("DesktopWindow", () => {
687689
prevented = false;
688690
beforeInput(event, { ...input, meta: false });
689691
assert.isFalse(prevented);
692+
693+
assert.deepEqual(
694+
fakeWindow.setIgnoreMenuShortcuts.mock.calls.map(([ignore]) => ignore),
695+
[true, true, false],
696+
);
697+
beforeInput(event, { ...input, key: "r", isAutoRepeat: false });
698+
assert.isFalse(fakeWindow.setIgnoreMenuShortcuts.mock.lastCall?.[0]);
690699
}).pipe(Effect.provide(layer));
691700
}),
692701
);

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,13 @@ export const make = Effect.gen(function* () {
677677
});
678678
window.webContents.on("before-input-event", (event, input) => {
679679
quitShortcutHandler(event, input);
680+
// Cmd/Ctrl+W reaches the renderer; DevTools uses the native menu shortcut.
681+
window.webContents.setIgnoreMenuShortcuts(
682+
(environment.platform === "darwin" ? input.meta : input.control) &&
683+
!input.alt &&
684+
!input.shift &&
685+
input.key.toLowerCase() === "w",
686+
);
680687
// A held Cmd/Ctrl+W closes one tab or terminal, not each one behind it.
681688
if (input.type !== "keyDown" || !input.isAutoRepeat) return;
682689
const modifier = environment.platform === "darwin" ? input.meta : input.control;

0 commit comments

Comments
 (0)