Skip to content

The agent that grows with you

fix(webapp): a window-kind change never moves a user's own keybind

Tekniumbd8436dfcc69
diff --git a/apps/desktop/src/lib/keybinds/app-window.ts b/apps/desktop/src/lib/keybinds/app-window.ts
index ada7a26b40d..048c186642d 100644
--- a/apps/desktop/src/lib/keybinds/app-window.ts
+++ b/apps/desktop/src/lib/keybinds/app-window.ts
@@ -59,12 +59,19 @@ export function onBrowserWindowKindChange(listener: () => void): () => void {
type Bindings = Record<string, string[]>
/** Moves every binding still on its old shipped default onto the new one; a
- * user's own rebind (anything else, including a cleared binding) stays. */
-export function rebaseShippedDefaults(bindings: Bindings, previous: Bindings, next: Bindings): Bindings {
+ * user's own rebind (anything else, including a cleared binding) stays, and
+ * so does any id `keep` claims. Value equality alone cannot tell a rebind
+ * from a default: a rebind to Alt+W in an app window IS the tab default. */
+export function rebaseShippedDefaults(
+ bindings: Bindings,
+ previous: Bindings,
+ next: Bindings,
+ keep: (id: string, combos: string[]) => boolean = () => false
+): Bindings {
const out = { ...bindings }
for (const [id, combos] of Object.entries(next)) {
- if (!arraysEqual(out[id] ?? [], previous[id] ?? []) || arraysEqual(out[id] ?? [], combos)) {
+ if (!arraysEqual(out[id] ?? [], previous[id] ?? []) || arraysEqual(out[id] ?? [], combos) || keep(id, combos)) {
continue
}
@@ -77,14 +84,15 @@ export function rebaseShippedDefaults(bindings: Bindings, previous: Bindings, ne
/** Keeps `store`'s defaults on the current window kind. */
export function followBrowserWindowKind(
store: { get(): Bindings; set(value: Bindings): void },
- defaults: () => Bindings
+ defaults: () => Bindings,
+ keep?: (id: string, combos: string[]) => boolean
): () => void {
let shipped = defaults()
return onBrowserWindowKindChange(() => {
const next = defaults()
const current = store.get()
- const rebased = rebaseShippedDefaults(current, shipped, next)
+ const rebased = rebaseShippedDefaults(current, shipped, next, keep)
shipped = next
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
index d6250fb8eaf..bb6bc0d20c1 100644
--- a/apps/desktop/src/lib/keybinds/browser-app-window-defaults.test.ts
+++ b/apps/desktop/src/lib/keybinds/browser-app-window-defaults.test.ts
@@ -109,6 +109,42 @@ it('follows a live display-mode change and leaves a user rebind alone', async ()
expect($bindings.get()['view.closeTab']).toEqual(['mod+shift+q'])
})
+it('keeps a rebind that equals the other window kind\'s default across flips and a reload', async () => {
+ setPlatform('Win32')
+ const moveTo = stubDisplayMode('standalone')
+ const { bindingsFor, setBinding } = await import('@/store/keybinds')
+
+ setBinding('view.closeTab', ['alt+w'])
+ moveTo('browser')
+ moveTo('standalone')
+
+ expect(bindingsFor('view.closeTab')).toEqual(['alt+w'])
+
+ vi.resetModules()
+ const reloaded = await import('@/store/keybinds')
+
+ expect(reloaded.bindingsFor('view.closeTab')).toEqual(['alt+w'])
+})
+
+it('skips a default move onto a chord the user gave another action', async () => {
+ setPlatform('Win32')
+ const moveTo = stubDisplayMode('standalone')
+ const { $comboIndex, bindingsFor, conflictsFor, setBinding } = await import('@/store/keybinds')
+
+ setBinding('session.focusSearch', ['alt+n'])
+ moveTo('browser')
+
+ expect($comboIndex.get().get('alt+n')).toEqual(['session.focusSearch'])
+ expect(bindingsFor('session.new')).toEqual(['mod+n'])
+ expect(conflictsFor('session.focusSearch', 'alt+n')).toEqual([])
+ expect(bindingsFor('session.next')).toEqual(['alt+]'])
+
+ vi.resetModules()
+ const reloaded = await import('@/store/keybinds')
+
+ expect(reloaded.$comboIndex.get().get('alt+n')).toEqual(['session.focusSearch'])
+})
+
it('a non-Chromium web-app window keeps the browser-tab defaults', async () => {
setPlatform('Win32')
setUserAgent(FIREFOX_UA)
diff --git a/apps/desktop/src/store/keybinds.ts b/apps/desktop/src/store/keybinds.ts
index b6112d29060..57562670897 100644
--- a/apps/desktop/src/store/keybinds.ts
+++ b/apps/desktop/src/store/keybinds.ts
@@ -38,6 +38,12 @@ function readStoredOverrides(): Record<string, string[]> {
const storedOverrides = readStoredOverrides()
+// Actions the user set themselves. A display-mode change moves shipped
+// defaults (Ctrl+W in an app window, Alt+W in a tab) and must never move
+// these, even when the user's combo equals the other mode's default. Every
+// stored entry is one: older builds stored exactly the rebinds.
+const explicitIds = new Set(Object.keys(storedOverrides))
+
// Defaults overlaid with the user's stored overrides. Unknown / stale action
// ids are dropped; actions added in a later release pick up their shipped
// default; late-registered contributed actions resolve via `bindingsFor`.
@@ -55,8 +61,10 @@ function loadBindings(): KeybindBindings {
return base
}
-// Persist only the actions whose combos differ from their shipped default, so
-// changing a default never gets shadowed by a stored snapshot.
+// Persist the user's own bindings and nothing that still follows a shipped
+// default, so changing a default never gets shadowed by a stored snapshot. A
+// user binding is kept even while it equals the current default: it is the
+// other window kind's default too, and storing a diff would drop it.
function persistBindings(bindings: KeybindBindings, options: WriteOptions): void {
const defaults = defaultBindings()
const diff: KeybindBindings = {}
@@ -64,7 +72,7 @@ function persistBindings(bindings: KeybindBindings, options: WriteOptions): void
for (const action of allKeybindActions()) {
const current = bindings[action.id] ?? []
- if (!arraysEqual(current, defaults[action.id] ?? [])) {
+ if (explicitIds.has(action.id) || !arraysEqual(current, defaults[action.id] ?? [])) {
diff[action.id] = current
}
}
@@ -88,8 +96,26 @@ export const $bindings = atom<KeybindBindings>(loadBindings())
persistStore($bindings, persistBindings, defaultBindings())
+// A default move that would land on a chord the user gave another action is
+// skipped, so the user's binding is not silently shadowed; the action keeps
+// its combo as a saved one, or a reload would bring the collision back.
+function keepOffUserChords(id: string, combos: string[]): boolean {
+ if (explicitIds.has(id)) {
+ return true
+ }
+
+ const bindings = $bindings.get()
+ const taken = combos.some(combo => conflictsFor(id, combo, bindings).some(other => explicitIds.has(other)))
+
+ if (taken) {
+ explicitIds.add(id)
+ }
+
+ return taken
+}
+
// "Open in Chrome" turns an app window into a tab without a reload.
-followBrowserWindowKind($bindings, defaultBindings)
+followBrowserWindowKind($bindings, defaultBindings, keepOffUserChords)
/** Live combos for an action: explicit binding → stored override → default. */
export function bindingsFor(id: string, bindings: KeybindBindings = $bindings.get()): string[] {
@@ -121,10 +147,19 @@ export const $comboIndex = computed([$bindings, $registryVersion], bindings => {
})
export function setBinding(actionId: string, combos: string[]): void {
- if (!keybindAction(actionId)) {
+ const action = keybindAction(actionId)
+
+ if (!action) {
return
}
+ // Picking the current default again is a reset, not a pin.
+ if (arraysEqual(combos, [...action.defaults])) {
+ explicitIds.delete(actionId)
+ } else {
+ explicitIds.add(actionId)
+ }
+
$bindings.set({ ...$bindings.get(), [actionId]: [...combos] })
}
@@ -140,18 +175,19 @@ export function resetBinding(actionId: string): void {
return
}
+ explicitIds.delete(actionId)
$bindings.set({ ...$bindings.get(), [actionId]: [...action.defaults] })
}
export function resetAllBindings(): void {
+ explicitIds.clear()
$bindings.set(defaultBindings())
}
// Other actions that already use `combo` (excluding `actionId` itself). A
// `passthrough` action layered over a later one shares the chord by design,
// so that pair is not reported from either side.
-export function conflictsFor(actionId: string, combo: string): string[] {
- const bindings = $bindings.get()
+export function conflictsFor(actionId: string, combo: string, bindings = $bindings.get()): string[] {
const actions = allKeybindActions()
const self = actions.findIndex(action => action.id === actionId)