Skip to content
TypeScript

fix(desktop): Cmd/Ctrl+W closes sidebar tabs, never the window (#25860)

Theo Brownea86c05f977ef
diff --git a/apps/desktop/src/window/DesktopApplicationMenu.test.ts b/apps/desktop/src/window/DesktopApplicationMenu.test.ts
index df7e1fd2e85..9d8efd05666 100644
--- a/apps/desktop/src/window/DesktopApplicationMenu.test.ts
+++ b/apps/desktop/src/window/DesktopApplicationMenu.test.ts
@@ -199,6 +199,41 @@ describe("DesktopApplicationMenu", () => {
}),
);
+ it.effect("never binds Cmd/Ctrl+W to closing the window", () =>
+ Effect.gen(function* () {
+ for (const platform of ["darwin", "linux", "win32"] as const) {
+ const applicationMenuTemplate =
+ yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
+ yield* configureMenu(yield* Deferred.make<string>(), applicationMenuTemplate, {
+ platform,
+ });
+
+ const items: Electron.MenuItemConstructorOptions[] = [];
+ const collect = (template: readonly Electron.MenuItemConstructorOptions[]) => {
+ for (const item of template) {
+ items.push(item);
+ if (Array.isArray(item.submenu)) collect(item.submenu);
+ }
+ };
+ collect(yield* Deferred.await(applicationMenuTemplate));
+
+ assert.isUndefined(
+ items.find((item) => item.role === "close"),
+ platform,
+ );
+ // Electron's default windowMenu submenu includes a close role.
+ assert.isUndefined(
+ items.find((item) => item.role === "windowMenu" && item.submenu === undefined),
+ platform,
+ );
+ assert.isUndefined(
+ items.find((item) => item.accelerator?.toLowerCase().endsWith("+w")),
+ platform,
+ );
+ }
+ }),
+ );
+
it.effect("reloads the main window even while a browser page has focus", () =>
Effect.gen(function* () {
const selectedAction = yield* Deferred.make<string>();
diff --git a/apps/desktop/src/window/DesktopApplicationMenu.ts b/apps/desktop/src/window/DesktopApplicationMenu.ts
index 7c70656c1ac..07fd8654af6 100644
--- a/apps/desktop/src/window/DesktopApplicationMenu.ts
+++ b/apps/desktop/src/window/DesktopApplicationMenu.ts
@@ -200,7 +200,10 @@ export const make = Effect.gen(function* () {
},
{ type: "separator" as const },
]),
- { role: environment.platform === "darwin" ? "close" : "quit" },
+ // Cmd/Ctrl+W belongs to the renderer's tab close; it must never close the window.
+ environment.platform === "darwin"
+ ? { label: "Close Window", click: (_item, window) => window?.close() }
+ : { role: "quit" },
],
},
{
@@ -289,7 +292,17 @@ export const make = Effect.gen(function* () {
{ role: "togglefullscreen" },
],
},
- { role: "windowMenu" },
+ {
+ // Electron's default Window submenu adds a Ctrl+W close role off macOS.
+ role: "windowMenu",
+ submenu: [
+ { role: "minimize" },
+ { role: "zoom" },
+ ...(environment.platform === "darwin"
+ ? [{ type: "separator" as const }, { role: "front" as const }]
+ : []),
+ ],
+ },
{
role: "help",
submenu: [
diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts
index 20b43f91e63..ab5fc8be26a 100644
--- a/apps/desktop/src/window/DesktopWindow.ts
+++ b/apps/desktop/src/window/DesktopWindow.ts
@@ -619,7 +619,6 @@ export const make = Effect.gen(function* () {
}
});
- // Terminal-close repeats can outlive the terminal; reject them before the native close-window accelerator.
const quitShortcutHandler = makeQuitShortcutHandler({
platform: environment.platform,
getMode: () =>
@@ -645,6 +644,7 @@ export const make = Effect.gen(function* () {
});
window.webContents.on("before-input-event", (event, input) => {
quitShortcutHandler(event, input);
+ // A held Cmd/Ctrl+W closes one tab or terminal, not each one behind it.
if (input.type !== "keyDown" || !input.isAutoRepeat) return;
const modifier = environment.platform === "darwin" ? input.meta : input.control;
if (modifier && !input.alt && !input.shift && input.key.toLowerCase() === "w") {
diff --git a/docs/user/keybindings.md b/docs/user/keybindings.md
index f30e832b39e..7478d90aa9b 100644
--- a/docs/user/keybindings.md
+++ b/docs/user/keybindings.md
@@ -165,7 +165,7 @@ through the pages you have visited, like a browser's back and forward buttons.
## Reserved shortcuts
In the desktop app, `mod+w` closes the focused terminal or the active right-panel
-tab. When nothing remains to close, it closes the window. In a browser, `mod+w`
+tab. When nothing remains to close, it does nothing; it never closes the window. In a browser, `mod+w`
closes the browser tab; rebind `rightPanel.close` and `terminal.close` to an available
shortcut such as `alt+w`.