Skip to content

Commit 4738bb7

Browse files
bmdavis419claude
andcommitted
fix(web): keep panel shortcuts in the panel and user bindings first
- mod+t opens a terminal tab from a terminal and a browser tab elsewhere in a thread; mod+l only acts while the panel has focus again. - Surface openers move to mod+alt+shift+<letter>, clear of macOS Minimize All. - Startup backfill skips a new default whose chord a user rule already owns. - Focused pages give up only browser-window chords, and only while shown as a panel tab; the floating player keeps every key. Forwarded chords win over the native reload, AltGr text on Windows stays with the page, and the first chord after agent automation is no longer swallowed. - Shortcut bridge calls wait for the tab's page and ignore its rejections. - Desktop and web share one key matcher; the action bus carries command IDs. - Drop address-bar search and the floating-preview change from this PR; keep the guard that hides an empty floating tab. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 55fcd7a commit 4738bb7

27 files changed

Lines changed: 409 additions & 475 deletions

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,12 @@ export const setForwardedShortcuts = DesktopIpc.makeIpcMethod({
180180
channel: IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL,
181181
payload: DesktopPreviewSetForwardedShortcutsInputSchema,
182182
result: Schema.Void,
183-
handler: Effect.fn("desktop.ipc.preview.setForwardedShortcuts")(function* ({ shortcuts }) {
183+
handler: Effect.fn("desktop.ipc.preview.setForwardedShortcuts")(function* ({
184+
shortcuts,
185+
tabIds,
186+
}) {
184187
const manager = yield* PreviewManager.PreviewManager;
185-
yield* manager.setForwardedShortcuts(shortcuts);
188+
yield* manager.setForwardedShortcuts(shortcuts, tabIds);
186189
}),
187190
});
188191
export const openDevTools = tabMethod(

‎apps/desktop/src/preload.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,11 @@ contextBridge.exposeInMainWorld("desktopBridge", {
407407
return () =>
408408
ipcRenderer.removeListener(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, wrappedListener);
409409
},
410-
setForwardedShortcuts: (shortcuts) =>
411-
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL, { shortcuts }),
410+
setForwardedShortcuts: (shortcuts, tabIds) =>
411+
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL, {
412+
shortcuts,
413+
tabIds,
414+
}),
412415
onShortcut: (listener) => {
413416
const wrappedListener = (_event: Electron.IpcRendererEvent, shortcutEvent: unknown) => {
414417
if (typeof shortcutEvent !== "object" || shortcutEvent === null) return;

‎apps/desktop/src/preview/ForwardedShortcuts.test.ts‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { assert, describe, it } from "vite-plus/test";
33
import { forwardedShortcutEvent } from "./ForwardedShortcuts.ts";
44

55
const focusUrl = { key: "l", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false };
6-
const nextTab = { key: "tab", metaKey: false, ctrlKey: true, shiftKey: false, altKey: false };
7-
const openDevTools = { key: "i", metaKey: true, ctrlKey: false, shiftKey: false, altKey: true };
6+
const devTools = { key: "i", metaKey: false, ctrlKey: true, shiftKey: false, altKey: true };
87

98
const input = (overrides: Partial<Parameters<typeof forwardedShortcutEvent>[0]>) => ({
109
type: "keyDown",
@@ -20,7 +19,7 @@ const input = (overrides: Partial<Parameters<typeof forwardedShortcutEvent>[0]>)
2019

2120
describe("forwardedShortcutEvent", () => {
2221
it("claims forwarded chords with exactly their modifiers", () => {
23-
assert.deepStrictEqual(forwardedShortcutEvent(input({ meta: true }), [focusUrl]), {
22+
assert.deepStrictEqual(forwardedShortcutEvent(input({ meta: true }), [focusUrl], "darwin"), {
2423
key: "l",
2524
code: "KeyL",
2625
metaKey: true,
@@ -29,23 +28,15 @@ describe("forwardedShortcutEvent", () => {
2928
altKey: false,
3029
repeat: false,
3130
});
32-
assert.isNotNull(
33-
forwardedShortcutEvent(input({ key: "Tab", code: "Tab", control: true }), [nextTab]),
31+
assert.isNull(forwardedShortcutEvent(input({ meta: true, shift: true }), [focusUrl], "darwin"));
32+
assert.isNull(
33+
forwardedShortcutEvent(input({ meta: true, type: "keyUp" }), [focusUrl], "darwin"),
3434
);
35-
assert.isNull(forwardedShortcutEvent(input({ meta: true, shift: true }), [focusUrl]));
36-
assert.isNull(forwardedShortcutEvent(input({ meta: true, type: "keyUp" }), [focusUrl]));
3735
});
3836

39-
it("matches Option symbols by physical key", () => {
40-
assert.isNotNull(
41-
forwardedShortcutEvent(input({ key: "ˆ", code: "KeyI", meta: true, alt: true }), [
42-
openDevTools,
43-
]),
44-
);
45-
});
46-
47-
it("leaves typing with the page", () => {
48-
const plainLetter = { ...focusUrl, metaKey: false };
49-
assert.isNull(forwardedShortcutEvent(input({}), [plainLetter]));
37+
it("leaves AltGr symbols on Windows with the page", () => {
38+
const altGr = { key: "¡", code: "KeyI", control: true, alt: true };
39+
assert.isNull(forwardedShortcutEvent(input(altGr), [devTools], "win32"));
40+
assert.isNotNull(forwardedShortcutEvent(input({ ...altGr, key: "i" }), [devTools], "win32"));
5041
});
5142
});

‎apps/desktop/src/preview/ForwardedShortcuts.ts‎

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,31 @@ import type {
22
DesktopPreviewForwardedShortcut,
33
DesktopPreviewShortcutEvent,
44
} from "@t3tools/contracts";
5-
6-
/** Physical punctuation and digit keys, matching the app's shortcut matcher. */
7-
const CODE_KEYS: Readonly<Record<string, string>> = {
8-
Backquote: "`",
9-
Backslash: "\\",
10-
BracketLeft: "[",
11-
BracketRight: "]",
12-
Comma: ",",
13-
Digit0: "0",
14-
Digit1: "1",
15-
Digit2: "2",
16-
Digit3: "3",
17-
Digit4: "4",
18-
Digit5: "5",
19-
Digit6: "6",
20-
Digit7: "7",
21-
Digit8: "8",
22-
Digit9: "9",
23-
Equal: "=",
24-
Minus: "-",
25-
Period: ".",
26-
Quote: "'",
27-
Semicolon: ";",
28-
Slash: "/",
29-
};
5+
import { shortcutKeysFromEvent } from "@t3tools/shared/keybindings";
306

317
type ShortcutInput = Pick<
328
Electron.Input,
339
"type" | "key" | "code" | "meta" | "control" | "shift" | "alt" | "isAutoRepeat"
3410
>;
3511

36-
/**
37-
* Keys a press can match: the layout key, plus the physical key when the
38-
* layout does not produce a Latin letter (non-Latin layouts, Option symbols).
39-
*/
40-
const inputKeys = (input: ShortcutInput): ReadonlySet<string> => {
41-
const layoutKey = input.key.toLowerCase();
42-
if (/^[a-z]$/.test(layoutKey)) return new Set([layoutKey]);
43-
const keys = new Set([layoutKey === "esc" ? "escape" : layoutKey]);
44-
const letter = /^Key([A-Z])$/.exec(input.code)?.[1];
45-
if (letter) keys.add(letter.toLowerCase());
46-
const physicalKey = CODE_KEYS[input.code];
47-
if (physicalKey) keys.add(physicalKey);
48-
return keys;
49-
};
50-
5112
/**
5213
* The event to hand back to the app when a press in a focused preview page is
5314
* one of the app's forwarded shortcuts, or null when the page keeps the key.
5415
* Presses without Command or Control are typing, so they always stay with the
55-
* page unless they are function keys.
16+
* page.
5617
*/
5718
export const forwardedShortcutEvent = (
5819
input: ShortcutInput,
5920
shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>,
21+
platform: NodeJS.Platform,
6022
): DesktopPreviewShortcutEvent | null => {
61-
if (input.type !== "keyDown") return null;
62-
if (!input.meta && !input.control && !/^F\d{1,2}$/.test(input.key)) return null;
63-
const keys = inputKeys(input);
23+
if (input.type !== "keyDown" || (!input.meta && !input.control)) return null;
24+
// Windows reports AltGr as Control+Alt. A symbol typed with it is text, not
25+
// a chord, matching the renderer's AltGraph rule.
26+
if (platform !== "darwin" && input.control && input.alt && !/^[a-z0-9]$/i.test(input.key)) {
27+
return null;
28+
}
29+
const keys = shortcutKeysFromEvent(input);
6430
const claimed = shortcuts.some(
6531
(shortcut) =>
6632
shortcut.metaKey === input.meta &&

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,10 @@ describe("PreviewManager", () => {
640640
} as never);
641641
yield* manager.createTab("tab_forwarded_keys");
642642
yield* manager.registerWebview("tab_forwarded_keys", 42);
643-
yield* manager.setForwardedShortcuts([
644-
{ key: "l", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false },
645-
]);
643+
yield* manager.setForwardedShortcuts(
644+
[{ key: "l", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false }],
645+
["tab_forwarded_keys"],
646+
);
646647
const beforeInput = preview.listeners.get("before-input-event")!;
647648
const preventDefault = vi.fn();
648649
const input = {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
700700
let frameCaptureWindowOpen = true;
701701
let currentMainWindow: BrowserWindow | undefined;
702702
// Read synchronously from before-input-event, so it lives outside a Ref.
703-
let forwardedShortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut> = [];
703+
let forwarding: {
704+
readonly shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>;
705+
readonly tabIds: ReadonlySet<string>;
706+
} = { shortcuts: [], tabIds: new Set() };
704707
let mainWindowCleanupFiber: Fiber.Fiber<void, never> | undefined;
705708
const tabLifecycleLocks = new Map<
706709
string,
@@ -2043,35 +2046,35 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20432046
};
20442047
const beforeInput = (event: Electron.Event, input: Electron.Input): void => {
20452048
syncMenuShortcuts(wc, input);
2049+
// The app's browser-window shortcuts belong to the app while a person
2050+
// has a panel tab's page focused; the floating player and native editing
2051+
// keep their keys. Agent input never has real focus. getFocusedWebContents()
2052+
// can return a hidden guest after switching tabs, so ask this guest.
2053+
const shortcut =
2054+
forwarding.tabIds.has(tabId) &&
2055+
wc.isFocused() &&
2056+
!isPreviewEditingShortcut(input, hostPlatform)
2057+
? forwardedShortcutEvent(input, forwarding.shortcuts, hostPlatform)
2058+
: null;
2059+
if (shortcut) {
2060+
event.preventDefault();
2061+
runFork(
2062+
attempt({ operation: "shortcut.forward", tabId, webContentsId: wc.id }, () => {
2063+
const host = currentMainWindow?.webContents;
2064+
if (host && !host.isDestroyed()) host.send(PREVIEW_SHORTCUT_CHANNEL, shortcut);
2065+
}).pipe(Effect.ignore),
2066+
);
2067+
return;
2068+
}
2069+
// Reload stays native so it also works in the floating player.
20462070
if (isPreviewRefreshShortcut(input)) {
20472071
event.preventDefault();
20482072
runFork(
20492073
attempt({ operation: "shortcut.refresh", tabId, webContentsId: wc.id }, () =>
20502074
wc.reload(),
20512075
).pipe(Effect.ignore),
20522076
);
2053-
return;
20542077
}
2055-
// Browser and panel shortcuts belong to the app while a person has the
2056-
// page focused. Agent keystrokes and native editing stay with the page.
2057-
// getFocusedWebContents() can return a hidden guest after switching tabs;
2058-
// check the guest receiving this event instead.
2059-
if (
2060-
agentDrivenWebContents.has(wc) ||
2061-
!wc.isFocused() ||
2062-
isPreviewEditingShortcut(input, hostPlatform)
2063-
) {
2064-
return;
2065-
}
2066-
const shortcut = forwardedShortcutEvent(input, forwardedShortcuts);
2067-
if (!shortcut) return;
2068-
event.preventDefault();
2069-
runFork(
2070-
attempt({ operation: "shortcut.forward", tabId, webContentsId: wc.id }, () => {
2071-
const host = currentMainWindow?.webContents;
2072-
if (host && !host.isDestroyed()) host.send(PREVIEW_SHORTCUT_CHANNEL, shortcut);
2073-
}).pipe(Effect.ignore),
2074-
);
20752078
};
20762079
yield* Scope.addFinalizer(
20772080
scope,
@@ -4749,9 +4752,12 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
47494752
setAnnotationTheme,
47504753
setAudioMuted,
47514754
setColorScheme,
4752-
setForwardedShortcuts: (shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>) =>
4755+
setForwardedShortcuts: (
4756+
shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>,
4757+
tabIds: ReadonlyArray<string>,
4758+
) =>
47534759
Effect.sync(() => {
4754-
forwardedShortcuts = shortcuts;
4760+
forwarding = { shortcuts, tabIds: new Set(tabIds) };
47554761
}),
47564762
setMainWindow,
47574763
startRecording,
@@ -5166,8 +5172,10 @@ export class PreviewManager extends Context.Service<
51665172
tabId: string,
51675173
input: PreviewAutomationWaitForInput,
51685174
) => Effect.Effect<void, PreviewManagerError>;
5175+
/** Chords to take from focused pages, and the tabs whose pages give them up. */
51695176
readonly setForwardedShortcuts: (
51705177
shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>,
5178+
tabIds: ReadonlyArray<string>,
51715179
) => Effect.Effect<void>;
51725180
readonly subscribeStateChanges: (listener: Listener) => Effect.Effect<void, never, Scope.Scope>;
51735181
readonly subscribePointerEvents: (

‎apps/server/src/keybindings.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
276276
persisted.some((entry) => entry.command === "terminal.toggle" && entry.key === "mod+j"),
277277
);
278278

279+
// The user's own mod+shift+r keeps the chord from the new default.
280+
assert.isFalse(byCommand.has("preview.hardRefresh"));
279281
for (const defaultRule of Keybindings.DEFAULT_KEYBINDINGS) {
282+
if (defaultRule.command === "preview.hardRefresh") continue;
280283
assert.isTrue(byCommand.has(defaultRule.command), `expected ${defaultRule.command}`);
281284
}
282285
assert.isTrue(byCommand.has("script.run-tests.run"));
@@ -319,6 +322,31 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
319322
);
320323
});
321324

325+
it.effect("keeps a user's own chord when a new default would shadow it", () =>
326+
Effect.gen(function* () {
327+
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
328+
yield* writeKeybindingsConfig(keybindingsConfigPath, [
329+
// Custom, in a different context than the new default: still theirs.
330+
{ key: "cmd+t", command: "script.custom-action.run", when: "!terminalFocus" },
331+
// Stock default sharing a chord with a panel-scoped default.
332+
{ key: "mod+shift+[", command: "thread.previous" },
333+
]);
334+
335+
yield* Effect.gen(function* () {
336+
const keybindings = yield* Keybindings.Keybindings;
337+
yield* keybindings.syncDefaultKeybindingsOnStartup;
338+
});
339+
340+
const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
341+
assert.isFalse(persisted.some((entry) => entry.command === "rightPanel.newTab"));
342+
assert.isTrue(
343+
persisted.some(
344+
(entry) => entry.command === "rightPanel.previousTab" && entry.key === "mod+shift+[",
345+
),
346+
);
347+
}).pipe(Effect.provide(makeKeybindingsLayer())),
348+
);
349+
322350
it.effect("upserts custom keybindings to configured path", () =>
323351
Effect.gen(function* () {
324352
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;

‎apps/server/src/keybindings.ts‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,31 @@ function hasSameShortcutContext(left: KeybindingRule, right: KeybindingRule): bo
119119
return leftContext === rightContext;
120120
}
121121

122+
/**
123+
* Whether two rules can fire on the same key press on some platform, whatever
124+
* their `when`. `mod` is Command on macOS and Control elsewhere.
125+
*/
126+
function canShareChord(left: KeybindingRule, right: KeybindingRule): boolean {
127+
const leftShortcut = parseKeybindingShortcut(left.key);
128+
const rightShortcut = parseKeybindingShortcut(right.key);
129+
if (!leftShortcut || !rightShortcut) return false;
130+
if (
131+
leftShortcut.key !== rightShortcut.key ||
132+
leftShortcut.shiftKey !== rightShortcut.shiftKey ||
133+
leftShortcut.altKey !== rightShortcut.altKey
134+
) {
135+
return false;
136+
}
137+
return [true, false].some((isMac) => {
138+
const meta = (shortcut: KeybindingShortcut) => shortcut.metaKey || (shortcut.modKey && isMac);
139+
const ctrl = (shortcut: KeybindingShortcut) => shortcut.ctrlKey || (shortcut.modKey && !isMac);
140+
return meta(leftShortcut) === meta(rightShortcut) && ctrl(leftShortcut) === ctrl(rightShortcut);
141+
});
142+
}
143+
144+
const isDefaultKeybindingRule = (rule: KeybindingRule) =>
145+
DEFAULT_KEYBINDINGS.some((defaultRule) => isSameKeybindingRule(rule, defaultRule));
146+
122147
function keybindingRuleFromUpsertInput(input: ServerUpsertKeybindingInput): KeybindingRule {
123148
return input.when === undefined
124149
? { key: input.key, command: input.command }
@@ -482,8 +507,12 @@ const make = Effect.gen(function* () {
482507
if (existingCommands.has(defaultRule.command)) {
483508
continue;
484509
}
485-
const conflictingEntry = customConfig.find((entry) =>
486-
hasSameShortcutContext(entry, defaultRule),
510+
// A chord the user bound themselves stays theirs: appending the new
511+
// default after it would make the default win wherever both apply.
512+
const conflictingEntry = customConfig.find(
513+
(entry) =>
514+
hasSameShortcutContext(entry, defaultRule) ||
515+
(!isDefaultKeybindingRule(entry) && canShareChord(entry, defaultRule)),
487516
);
488517
if (conflictingEntry) {
489518
shortcutConflictWarnings.push({
@@ -503,7 +532,7 @@ const make = Effect.gen(function* () {
503532
conflictingCommand: conflict.conflictingCommand,
504533
key: conflict.key,
505534
when: conflict.when,
506-
reason: "shortcut context already used by existing rule",
535+
reason: "shortcut already used by existing rule",
507536
});
508537
}
509538
if (missingDefaults.length === 0) {

0 commit comments

Comments
 (0)