Skip to content

The agent that grows with you

fix(desktop): gate Close Tab, Reload, and Zoom on keyDown

brooklyn!850931b4ca9b
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 2820a547ca6..78f4966791c 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -495,6 +495,7 @@ import {
import { isHermesOwnedVenvDaemon } from './venv-holder-select'
import { fetchMarketplaceThemes, searchMarketplaceThemes } from './vscode-marketplace'
import { createWakeIndicatorWindowController } from './wake-indicator-window'
+import { windowAcceleratorAction } from './window-accelerator'
import { enumerateWindowsFrontToBack, enumerationFailed, readWindowBelow } from './window-below'
import { bindWindowChromeEvents } from './window-chrome-events'
import {
@@ -7471,14 +7472,14 @@ function installDevToolsShortcut(window) {
function installPreviewShortcut(window) {
window.webContents.on('before-input-event', (event, input) => {
- const key = String(input.key || '').toLowerCase()
- const accel = (IS_MAC ? input.meta : input.control) && !input.alt
- const isCloseTabShortcut = key === 'w' && accel && !input.shift
+ const action = windowAcceleratorAction(input, IS_MAC)
// Always claim ⌘W here (the File>Close item deliberately has no
// accelerator, so nothing else does). The renderer decides tab-vs-window
// — no `previewShortcutActive` gate, so it works for every closeable tab.
- if (isCloseTabShortcut) {
+ // keyUp is not a claim: a chord that started in another app can deliver
+ // its leftover keyup when this window inherits focus (#105498).
+ if (action === 'close-tab') {
event.preventDefault()
// ⌘W in the HUD is "leave HUD mode", not "close a tab in the app
@@ -7501,7 +7502,7 @@ function installPreviewShortcut(window) {
// see #77845), so a menu accelerator would leave Windows and Linux with no
// way to reload a page at all. ⇧⌘R is left alone — that is `forceReload`,
// the unconditional whole-window escape hatch.
- if (key === 'r' && accel && !input.shift) {
+ if (action === 'reload') {
event.preventDefault()
sendPreviewNavCommand('reload')
}
@@ -7603,33 +7604,19 @@ function installZoomShortcuts(window) {
// Chromium's default handler would use the full 0.2 step, so we intercept
// here for consistency. Ctrl/Cmd+0 resets to DEFAULT_ZOOM_LEVEL, not Chromium 0.
window.webContents.on('before-input-event', (event, input) => {
- const mod = IS_MAC ? input.meta : input.control
-
- if (!mod || input.alt) {
- return
- }
-
- const key = input.key
-
- if (key === '0') {
- if (input.shift) {
- return // Ctrl/Cmd+Shift+0 is not a zoom chord — leave it alone
- }
+ const action = windowAcceleratorAction(input, IS_MAC)
+ if (action === 'zoom-reset') {
event.preventDefault()
setAndPersistZoomLevel(window, DEFAULT_ZOOM_LEVEL)
- } else if (key === '=' || key === '+') {
+ } else if (action === 'zoom-in') {
// Zoom-in must accept the shift modifier: on US layouts Plus is
// physically Shift+=, so Cmd+Plus arrives as Cmd+Shift+'+' (or '='
// depending on platform). The old blanket shift guard silently
// dropped keyboard zoom-in on macOS (#43517).
event.preventDefault()
setAndPersistZoomLevel(window, window.webContents.getZoomLevel() + ZOOM_STEP)
- } else if (key === '-') {
- if (input.shift) {
- return // Shift+'-' is '_' territory on most layouts, not zoom-out
- }
-
+ } else if (action === 'zoom-out') {
event.preventDefault()
setAndPersistZoomLevel(window, window.webContents.getZoomLevel() - ZOOM_STEP)
}
diff --git a/apps/desktop/electron/window-accelerator.test.ts b/apps/desktop/electron/window-accelerator.test.ts
new file mode 100644
index 00000000000..e6ab4c2daab
--- /dev/null
+++ b/apps/desktop/electron/window-accelerator.test.ts
@@ -0,0 +1,117 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { windowAcceleratorAction, type WindowAcceleratorInput } from './window-accelerator'
+
+function chord(overrides: Partial<WindowAcceleratorInput> = {}): WindowAcceleratorInput {
+ return {
+ alt: false,
+ control: false,
+ key: '',
+ meta: false,
+ shift: false,
+ type: 'keyDown',
+ ...overrides
+ }
+}
+
+test('claims Close Tab on a Windows Ctrl+W keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'w', control: true }), false), 'close-tab')
+})
+
+test('ignores a Windows Ctrl+W keyUp left over from another app', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'keyUp', key: 'w', control: true }), false), 'ignore')
+})
+
+test('claims Close Tab on a macOS Cmd+W keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'w', meta: true }), true), 'close-tab')
+})
+
+test('ignores a macOS Cmd+W keyUp', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'keyUp', key: 'w', meta: true }), true), 'ignore')
+})
+
+test('claims Reload on a Windows Ctrl+R keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'r', control: true }), false), 'reload')
+})
+
+test('ignores a Windows Ctrl+R keyUp', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'keyUp', key: 'r', control: true }), false), 'ignore')
+})
+
+test('claims Reload on a macOS Cmd+R keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'r', meta: true }), true), 'reload')
+})
+
+test('ignores a macOS Cmd+R keyUp', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'keyUp', key: 'r', meta: true }), true), 'ignore')
+})
+
+test('claims zoom reset on Ctrl+0 keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: '0', control: true }), false), 'zoom-reset')
+})
+
+test('ignores a Ctrl+0 keyUp', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'keyUp', key: '0', control: true }), false), 'ignore')
+})
+
+test('claims zoom in on Ctrl+= keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: '=', control: true }), false), 'zoom-in')
+})
+
+test('claims zoom in on Ctrl++ keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: '+', control: true }), false), 'zoom-in')
+})
+
+test('claims zoom in when Plus arrives as Shift+=', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: '=', control: true, shift: true }), false), 'zoom-in')
+ assert.equal(windowAcceleratorAction(chord({ key: '+', control: true, shift: true }), false), 'zoom-in')
+})
+
+test('ignores a Ctrl++ keyUp', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'keyUp', key: '+', control: true }), false), 'ignore')
+})
+
+test('claims zoom out on Ctrl+- keyDown', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: '-', control: true }), false), 'zoom-out')
+})
+
+test('ignores a Ctrl+- keyUp', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'keyUp', key: '-', control: true }), false), 'ignore')
+})
+
+test('ignores a char event for the same Ctrl+W chord', () => {
+ assert.equal(windowAcceleratorAction(chord({ type: 'char', key: 'w', control: true }), false), 'ignore')
+})
+
+test('does not treat Ctrl+Shift+W as Close Tab', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'w', control: true, shift: true }), false), 'ignore')
+})
+
+test('does not treat Ctrl+Shift+R as Reload', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'r', control: true, shift: true }), false), 'ignore')
+})
+
+test('does not treat Ctrl+Shift+0 as zoom reset', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: '0', control: true, shift: true }), false), 'ignore')
+})
+
+test('does not treat Ctrl+Shift+- as zoom out', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: '-', control: true, shift: true }), false), 'ignore')
+})
+
+test('does not claim a chord that uses the other platform modifier', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'w', meta: true }), false), 'ignore')
+ assert.equal(windowAcceleratorAction(chord({ key: 'w', control: true }), true), 'ignore')
+})
+
+test('does not claim an Alt-modified chord', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'w', control: true, alt: true }), false), 'ignore')
+ assert.equal(windowAcceleratorAction(chord({ key: '0', control: true, alt: true }), false), 'ignore')
+})
+
+test('folds letter case for Close Tab and Reload only', () => {
+ assert.equal(windowAcceleratorAction(chord({ key: 'W', control: true }), false), 'close-tab')
+ assert.equal(windowAcceleratorAction(chord({ key: 'R', meta: true }), true), 'reload')
+})
diff --git a/apps/desktop/electron/window-accelerator.ts b/apps/desktop/electron/window-accelerator.ts
new file mode 100644
index 00000000000..4fa734d3219
--- /dev/null
+++ b/apps/desktop/electron/window-accelerator.ts
@@ -0,0 +1,57 @@
+export type WindowAcceleratorAction = 'close-tab' | 'reload' | 'zoom-in' | 'zoom-out' | 'zoom-reset' | 'ignore'
+
+export interface WindowAcceleratorInput {
+ alt?: boolean
+ control?: boolean
+ key?: string
+ meta?: boolean
+ shift?: boolean
+ type?: string
+}
+
+/**
+ * Classify a main-process before-input-event for Close Tab, Reload, and Zoom.
+ *
+ * `before-input-event` fires for keydown and keyup. A keyup that arrives after
+ * Windows transfers focus (Ctrl+W started in another app) is not a chord this
+ * window owns, so only `keyDown` is an accelerator.
+ */
+export function windowAcceleratorAction(input: WindowAcceleratorInput, isMac: boolean): WindowAcceleratorAction {
+ if (input.type !== 'keyDown') {
+ return 'ignore'
+ }
+
+ const key = String(input.key || '')
+ const folded = key.toLowerCase()
+ const accel = Boolean((isMac ? input.meta : input.control) && !input.alt)
+
+ if (!accel) {
+ return 'ignore'
+ }
+
+ if (folded === 'w' && !input.shift) {
+ return 'close-tab'
+ }
+
+ if (folded === 'r' && !input.shift) {
+ return 'reload'
+ }
+
+ if (key === '0') {
+ // Ctrl/Cmd+Shift+0 is not a zoom chord.
+ return input.shift ? 'ignore' : 'zoom-reset'
+ }
+
+ if (key === '=' || key === '+') {
+ // Zoom-in accepts Shift: on US layouts Plus is physically Shift+=, so
+ // Cmd+Plus arrives as Cmd+Shift+'+' or '=' (#43517).
+ return 'zoom-in'
+ }
+
+ if (key === '-' && !input.shift) {
+ // Shift+'-' is '_' on most layouts, not zoom-out.
+ return 'zoom-out'
+ }
+
+ return 'ignore'
+}