Skip to content

The agent that grows with you

fix(webapp): Ctrl shortcuts in a Chrome app window

Tekniumfc1719a98bb9
diff --git a/apps/desktop/src/lib/keybinds/actions.ts b/apps/desktop/src/lib/keybinds/actions.ts
index 67aaaedc519..45407927d7d 100644
--- a/apps/desktop/src/lib/keybinds/actions.ts
+++ b/apps/desktop/src/lib/keybinds/actions.ts
@@ -20,6 +20,21 @@ export type KeybindCategory = 'composer' | 'profiles' | 'session' | 'navigation'
// character there; AltGr arrives as Ctrl+Alt or AltGraph), ⌃⌘ + the same key
// on macOS (Option types a character). Alt+Tab is the OS window switcher, so
// tab cycling takes ] and [. Native Desktop keeps the native chords.
+//
+// A Chrome app window (Install / "Open as window", `--app=`) reserves none of
+// them: `BrowserCommandController::IsReservedCommandOrKey` returns false for
+// TYPE_APP windows, so the page gets Ctrl+N/T/W/Tab first there; such a
+// window matches one of the display modes below. Off macOS it keeps the
+// native Ctrl chords, the same ones Desktop ships. macOS keeps ⌃⌘ either way.
+const APP_WINDOW_DISPLAY_MODES = ['standalone', 'minimal-ui', 'window-controls-overlay'] as const
+
+function inBrowserAppWindow(): boolean {
+ return (
+ typeof window.matchMedia === 'function' &&
+ APP_WINDOW_DISPLAY_MODES.some(mode => window.matchMedia(`(display-mode: ${mode})`).matches)
+ )
+}
+
interface BrowserChords {
/** Omitted: the native default already reaches the page on macOS. */
mac?: string
@@ -37,6 +52,10 @@ function hostDefaults(native: readonly string[], browser: BrowserChords): HostDe
return { defaults: browser.mac ? [browser.mac] : native }
}
+ if (inBrowserAppWindow()) {
+ return { defaults: native }
+ }
+
// Alt+letter has no Cmd/Ctrl, so it needs the editable-target opt-in to
// fire from the composer the way the native ⌘/Ctrl chord does.
return { defaults: [browser.other], editableTargetPolicy: 'modified' }
diff --git a/apps/desktop/src/lib/keybinds/browser-app-window-defaults.test.ts b/apps/desktop/src/lib/keybinds/browser-app-window-defaults.test.ts
new file mode 100644
index 00000000000..b0a05ced6a1
--- /dev/null
+++ b/apps/desktop/src/lib/keybinds/browser-app-window-defaults.test.ts
@@ -0,0 +1,66 @@
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+// The Alt (and ⌃⌘) browser defaults exist because a browser TAB acts on these
+// Ctrl chords before the page sees them. A Chrome app window reserves none of
+// them, so off macOS it ships the same Ctrl chords native Desktop does.
+const shortcuts = [
+ { id: 'session.new', native: ['mod+n'], mac: 'mod+ctrl+n', tab: 'alt+n' },
+ { id: 'session.newTab', native: ['mod+t'], mac: 'mod+ctrl+t', tab: 'alt+t' },
+ { id: 'session.newWindow', native: ['mod+shift+n'], mac: 'mod+ctrl+shift+n', tab: 'alt+shift+n' },
+ { id: 'session.next', native: ['ctrl+tab', 'ctrl+pagedown'], mac: 'mod+ctrl+]', tab: 'alt+]' },
+ { id: 'session.prev', native: ['ctrl+shift+tab', 'ctrl+pageup'], mac: 'mod+ctrl+[', tab: 'alt+[' },
+ { id: 'view.closeTab', native: ['mod+w'], mac: 'mod+ctrl+w', tab: 'alt+w' },
+ { id: 'view.reopenTab', native: ['mod+shift+t'], mac: 'mod+ctrl+shift+t', tab: 'alt+shift+t' },
+ { id: 'view.closeTerminal', native: ['ctrl+shift+w'], mac: 'ctrl+shift+w', tab: 'alt+shift+w' }
+]
+
+const setPlatform = (value: string) => Object.defineProperty(navigator, 'platform', { configurable: true, value })
+
+function stubDisplayMode(mode: string) {
+ vi.stubGlobal('matchMedia', (query: string) => ({ matches: query === `(display-mode: ${mode})`, media: query }))
+}
+
+beforeEach(() => {
+ vi.resetModules()
+ localStorage.clear()
+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'
+})
+
+afterEach(() => {
+ delete window.document.documentElement.dataset.hermesDesktopHost
+ setPlatform('')
+ vi.unstubAllGlobals()
+ localStorage.clear()
+})
+
+it.each(['standalone', 'minimal-ui', 'window-controls-overlay'])(
+ 'a %s app window off macOS ships the Ctrl chords, and a real Ctrl keydown dispatches them',
+ async mode => {
+ setPlatform('Win32')
+ stubDisplayMode(mode)
+ const { comboFromEvent, formatCombo } = await import('./combo')
+ const { $comboIndex, bindingsFor } = await import('@/store/keybinds')
+
+ for (const shortcut of shortcuts) {
+ expect(bindingsFor(shortcut.id)).toEqual(shortcut.native)
+ expect(formatCombo(shortcut.native[0]).startsWith('Ctrl+')).toBe(true)
+ }
+
+ const ctrlN = new KeyboardEvent('keydown', { code: 'KeyN', ctrlKey: true, key: 'n' })
+ expect($comboIndex.get().get(comboFromEvent(ctrlN)!)).toEqual(['session.new'])
+ expect($comboIndex.get().has('alt+n')).toBe(false)
+ }
+)
+
+it.each([
+ ['a macOS app window', 'MacIntel', 'standalone'],
+ ['a Windows tab', 'Win32', 'browser']
+])('%s keeps the browser-tab defaults', async (_, platform, mode) => {
+ setPlatform(platform)
+ stubDisplayMode(mode)
+ const { bindingsFor } = await import('@/store/keybinds')
+
+ for (const shortcut of shortcuts) {
+ expect(bindingsFor(shortcut.id)).toEqual([platform === 'MacIntel' ? shortcut.mac : shortcut.tab])
+ }
+})