Skip to content

Commit a1d207d

Browse files
bmdavis419claude
andcommitted
feat(web): the right panel works like a browser from the keyboard
Browser and right-panel shortcuts now apply while the panel or a page in it has focus: new tab, address bar, back/forward, hard reload, pick element, DevTools, device toolbar, tab cycling and jumps. Each surface the panel can open gets its own customizable shortcut. A focused preview page receives keys in its own process, so the desktop shell now hands the app's browser and panel chords back to the renderer, which replays them as keydowns. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 736130c commit a1d207d

24 files changed

Lines changed: 656 additions & 56 deletions

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export const PREVIEW_RECORDING_SAVE_CHANNEL = "desktop:preview-recording-save";
110110
export const PREVIEW_RECORDING_FRAME_CHANNEL = "desktop:preview-recording-frame";
111111
export const PREVIEW_STATE_CHANGE_CHANNEL = "desktop:preview-state-change";
112112
export const PREVIEW_POINTER_EVENT_CHANNEL = "desktop:preview-pointer-event";
113+
export const PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL = "desktop:preview-set-forwarded-shortcuts";
114+
export const PREVIEW_SHORTCUT_CHANNEL = "desktop:preview-shortcut";
113115

114116
export const MAC_PERMISSION_HELPER_CHANNEL = "desktop:mac-permission-helper";
115117

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
DesktopPreviewScreenshotArtifactSchema,
1717
DesktopPreviewSetAudioMutedInputSchema,
1818
DesktopPreviewSetColorSchemeInputSchema,
19+
DesktopPreviewSetForwardedShortcutsInputSchema,
1920
BrowserImportResult,
2021
BrowserImportSource,
2122
DesktopPreviewClearDataInputSchema,
@@ -175,6 +176,15 @@ export const setAudioMuted = DesktopIpc.makeIpcMethod({
175176
yield* manager.setAudioMuted(tabId, audioMuted);
176177
}),
177178
});
179+
export const setForwardedShortcuts = DesktopIpc.makeIpcMethod({
180+
channel: IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL,
181+
payload: DesktopPreviewSetForwardedShortcutsInputSchema,
182+
result: Schema.Void,
183+
handler: Effect.fn("desktop.ipc.preview.setForwardedShortcuts")(function* ({ shortcuts }) {
184+
const manager = yield* PreviewManager.PreviewManager;
185+
yield* manager.setForwardedShortcuts(shortcuts);
186+
}),
187+
});
178188
export const openDevTools = tabMethod(
179189
IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
180190
"desktop.ipc.preview.openDevTools",
@@ -496,6 +506,7 @@ export const methods = [
496506
hardReload,
497507
setColorScheme,
498508
setAudioMuted,
509+
setForwardedShortcuts,
499510
openDevTools,
500511
clearCookies,
501512
clearCache,

‎apps/desktop/src/preload.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
DesktopBridge,
33
DesktopPreviewPointerEvent,
4+
DesktopPreviewShortcutEvent,
45
DesktopPreviewRecordingInputEvent,
56
DesktopPreviewRecordingFrame,
67
DesktopPreviewTabState,
@@ -406,5 +407,16 @@ contextBridge.exposeInMainWorld("desktopBridge", {
406407
return () =>
407408
ipcRenderer.removeListener(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, wrappedListener);
408409
},
410+
setForwardedShortcuts: (shortcuts) =>
411+
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_FORWARDED_SHORTCUTS_CHANNEL, { shortcuts }),
412+
onShortcut: (listener) => {
413+
const wrappedListener = (_event: Electron.IpcRendererEvent, shortcutEvent: unknown) => {
414+
if (typeof shortcutEvent !== "object" || shortcutEvent === null) return;
415+
listener(shortcutEvent as DesktopPreviewShortcutEvent);
416+
};
417+
ipcRenderer.on(IpcChannels.PREVIEW_SHORTCUT_CHANNEL, wrappedListener);
418+
return () =>
419+
ipcRenderer.removeListener(IpcChannels.PREVIEW_SHORTCUT_CHANNEL, wrappedListener);
420+
},
409421
},
410422
} satisfies DesktopBridge);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { assert, describe, it } from "vite-plus/test";
2+
3+
import { forwardedShortcutEvent } from "./ForwardedShortcuts.ts";
4+
5+
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 };
8+
9+
const input = (overrides: Partial<Parameters<typeof forwardedShortcutEvent>[0]>) => ({
10+
type: "keyDown",
11+
key: "l",
12+
code: "KeyL",
13+
meta: false,
14+
control: false,
15+
shift: false,
16+
alt: false,
17+
isAutoRepeat: false,
18+
...overrides,
19+
});
20+
21+
describe("forwardedShortcutEvent", () => {
22+
it("claims forwarded chords with exactly their modifiers", () => {
23+
assert.deepStrictEqual(forwardedShortcutEvent(input({ meta: true }), [focusUrl]), {
24+
key: "l",
25+
code: "KeyL",
26+
metaKey: true,
27+
ctrlKey: false,
28+
shiftKey: false,
29+
altKey: false,
30+
repeat: false,
31+
});
32+
assert.isNotNull(
33+
forwardedShortcutEvent(input({ key: "Tab", code: "Tab", control: true }), [nextTab]),
34+
);
35+
assert.isNull(forwardedShortcutEvent(input({ meta: true, shift: true }), [focusUrl]));
36+
assert.isNull(forwardedShortcutEvent(input({ meta: true, type: "keyUp" }), [focusUrl]));
37+
});
38+
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]));
50+
});
51+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type {
2+
DesktopPreviewForwardedShortcut,
3+
DesktopPreviewShortcutEvent,
4+
} 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+
};
30+
31+
type ShortcutInput = Pick<
32+
Electron.Input,
33+
"type" | "key" | "code" | "meta" | "control" | "shift" | "alt" | "isAutoRepeat"
34+
>;
35+
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+
51+
/**
52+
* The event to hand back to the app when a press in a focused preview page is
53+
* one of the app's forwarded shortcuts, or null when the page keeps the key.
54+
* Presses without Command or Control are typing, so they always stay with the
55+
* page unless they are function keys.
56+
*/
57+
export const forwardedShortcutEvent = (
58+
input: ShortcutInput,
59+
shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>,
60+
): 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);
64+
const claimed = shortcuts.some(
65+
(shortcut) =>
66+
shortcut.metaKey === input.meta &&
67+
shortcut.ctrlKey === input.control &&
68+
shortcut.shiftKey === input.shift &&
69+
shortcut.altKey === input.alt &&
70+
keys.has(shortcut.key),
71+
);
72+
if (!claimed) return null;
73+
return {
74+
key: input.key,
75+
code: input.code,
76+
metaKey: input.meta,
77+
ctrlKey: input.control,
78+
shiftKey: input.shift,
79+
altKey: input.alt,
80+
repeat: input.isAutoRepeat,
81+
};
82+
};

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
DesktopPreviewAutomationStatus,
1616
DesktopPreviewColorScheme,
1717
DesktopPreviewFavicon,
18+
DesktopPreviewForwardedShortcut,
1819
DesktopPreviewPointerEvent,
1920
PreviewAnnotationPayload,
2021
PreviewAnnotationRect,
@@ -66,8 +67,12 @@ import * as Scope from "effect/Scope";
6667
import * as SynchronizedRef from "effect/SynchronizedRef";
6768

6869
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
69-
import { PREVIEW_PICTURE_IN_PICTURE_FRAME_CHANNEL } from "../ipc/channels.ts";
70+
import {
71+
PREVIEW_PICTURE_IN_PICTURE_FRAME_CHANNEL,
72+
PREVIEW_SHORTCUT_CHANNEL,
73+
} from "../ipc/channels.ts";
7074
import * as BrowserSession from "./BrowserSession.ts";
75+
import { forwardedShortcutEvent } from "./ForwardedShortcuts.ts";
7176
import {
7277
ANNOTATION_CAPTURED_CHANNEL,
7378
ANNOTATION_THEME_CHANNEL,
@@ -694,6 +699,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
694699
const agentDrivenWebContents = new WeakSet<Electron.WebContents>();
695700
let frameCaptureWindowOpen = true;
696701
let currentMainWindow: BrowserWindow | undefined;
702+
// Read synchronously from before-input-event, so it lives outside a Ref.
703+
let forwardedShortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut> = [];
697704
let mainWindowCleanupFiber: Fiber.Fiber<void, never> | undefined;
698705
const tabLifecycleLocks = new Map<
699706
string,
@@ -2045,6 +2052,24 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20452052
);
20462053
return;
20472054
}
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+
if (
2058+
agentDrivenWebContents.has(wc) ||
2059+
webContents.getFocusedWebContents() !== wc ||
2060+
isPreviewEditingShortcut(input, hostPlatform)
2061+
) {
2062+
return;
2063+
}
2064+
const shortcut = forwardedShortcutEvent(input, forwardedShortcuts);
2065+
if (!shortcut) return;
2066+
event.preventDefault();
2067+
runFork(
2068+
attempt({ operation: "shortcut.forward", tabId, webContentsId: wc.id }, () => {
2069+
const host = currentMainWindow?.webContents;
2070+
if (host && !host.isDestroyed()) host.send(PREVIEW_SHORTCUT_CHANNEL, shortcut);
2071+
}).pipe(Effect.ignore),
2072+
);
20482073
};
20492074
yield* Scope.addFinalizer(
20502075
scope,
@@ -4722,6 +4747,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
47224747
setAnnotationTheme,
47234748
setAudioMuted,
47244749
setColorScheme,
4750+
setForwardedShortcuts: (shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>) =>
4751+
Effect.sync(() => {
4752+
forwardedShortcuts = shortcuts;
4753+
}),
47254754
setMainWindow,
47264755
startRecording,
47274756
closePictureInPicture,
@@ -5135,6 +5164,9 @@ export class PreviewManager extends Context.Service<
51355164
tabId: string,
51365165
input: PreviewAutomationWaitForInput,
51375166
) => Effect.Effect<void, PreviewManagerError>;
5167+
readonly setForwardedShortcuts: (
5168+
shortcuts: ReadonlyArray<DesktopPreviewForwardedShortcut>,
5169+
) => Effect.Effect<void>;
51385170
readonly subscribeStateChanges: (listener: Listener) => Effect.Effect<void, never, Scope.Scope>;
51395171
readonly subscribePointerEvents: (
51405172
listener: PointerEventListener,
@@ -5235,6 +5267,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
52355267
automationScroll: operations.automationScroll,
52365268
automationEvaluate: operations.automationEvaluate,
52375269
automationWaitFor: operations.automationWaitFor,
5270+
setForwardedShortcuts: operations.setForwardedShortcuts,
52385271
subscribeStateChanges: operations.subscribeStateChanges,
52395272
subscribePointerEvents: operations.subscribePointerEvents,
52405273
subscribeRecordingFrames: operations.subscribeRecordingFrames,

‎apps/web/src/browser/ElectronBrowserHost.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"use client";
22

3+
import { useAtomValue } from "@effect/atom-react";
34
import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
45
import { FILL_PREVIEW_VIEWPORT } from "@t3tools/contracts";
56
import { useEffect, useMemo } from "react";
67

78
import { isElectron } from "~/env";
89
import { useTheme } from "~/hooks/useTheme";
10+
import { previewForwardedShortcuts } from "~/keybindings";
911
import { useActivePreviewSessions } from "~/previewStateStore";
12+
import { primaryServerKeybindingsAtom } from "~/state/server";
1013

1114
import { readPreviewAnnotationTheme } from "./annotationTheme";
1215
import { useBrowserPointerStore } from "./browserPointerStore";
@@ -79,6 +82,28 @@ export function ElectronBrowserHost() {
7982
});
8083
}, []);
8184

85+
// A focused page receives keys in its own process, out of reach of the
86+
// app's window listeners. Main takes the browser and panel chords from it;
87+
// replaying them on the focused webview runs the normal shortcut handlers
88+
// with preview focus.
89+
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
90+
useEffect(() => {
91+
const preview = window.desktopBridge?.preview;
92+
if (!preview) return;
93+
void preview
94+
.setForwardedShortcuts(previewForwardedShortcuts(keybindings))
95+
.catch(() => undefined);
96+
}, [keybindings]);
97+
useEffect(() => {
98+
const preview = window.desktopBridge?.preview;
99+
if (!preview) return;
100+
return preview.onShortcut((shortcut) => {
101+
(document.activeElement ?? document.body).dispatchEvent(
102+
new KeyboardEvent("keydown", { ...shortcut, bubbles: true, cancelable: true }),
103+
);
104+
});
105+
}, []);
106+
82107
if (!isElectron) return null;
83108
return (
84109
<div className="contents" data-electron-browser-host>

0 commit comments

Comments
 (0)