Skip to content

Commit 2880f4b

Browse files
committed
fix(web): react to terminal focus changes
1 parent 5456e3f commit 2880f4b

5 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎apps/web/src/components/LegacySidebar.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal";
7474
import { useDesktopLocalBootstraps } from "../connection/useDesktopLocalBootstraps";
7575
import { isElectron } from "../env";
76+
import { useTerminalFocus } from "../hooks/useTerminalFocus";
7677
import { useOpenPrLink } from "../lib/openPullRequestLink";
7778
import { isTerminalFocused } from "../lib/terminalFocus";
7879
import { isMacPlatform } from "../lib/utils";
@@ -3075,6 +3076,7 @@ export default function LegacySidebar() {
30753076
const setSelectionAnchor = useThreadSelectionStore((s) => s.setAnchor);
30763077
const platform = navigator.platform;
30773078
const shortcutModifiers = useShortcutModifierState();
3079+
const terminalFocused = useTerminalFocus();
30783080
const { environments } = useEnvironments();
30793081
const primaryEnvironmentId = usePrimaryEnvironmentId();
30803082
const environmentLabelById = useMemo(
@@ -3397,7 +3399,7 @@ export default function LegacySidebar() {
33973399
[threadJumpCommandByKey],
33983400
);
33993401
const sidebarShortcutContext = {
3400-
terminalFocus: isTerminalFocused(),
3402+
terminalFocus: terminalFocused,
34013403
terminalOpen: routeTerminalOpen,
34023404
modelPickerOpen: isModelPickerOpen(),
34033405
};

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import {
8383
threadTraversalDirectionFromCommand,
8484
} from "../keybindings";
8585
import { useShortcutModifierState } from "../shortcutModifierState";
86+
import { useTerminalFocus } from "../hooks/useTerminalFocus";
8687
import { isTerminalFocused } from "../lib/terminalFocus";
8788
import { isModelPickerOpen } from "../modelPickerVisibility";
8889
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
@@ -3305,13 +3306,14 @@ export default function Sidebar() {
33053306
// match a thread-jump binding. Adding Shift (screenshots) or Alt no
33063307
// longer matches ⌘1..9, so the overlay hides for chords like ⌘⇧4.
33073308
const shortcutModifiers = useShortcutModifierState();
3309+
const terminalFocused = useTerminalFocus();
33083310
const shouldShowJumpHintsNow = shouldShowThreadJumpHintsForModifiers(
33093311
shortcutModifiers,
33103312
keybindings,
33113313
{
33123314
platform: navigator.platform,
33133315
context: {
3314-
terminalFocus: isTerminalFocused(),
3316+
terminalFocus: terminalFocused,
33153317
terminalOpen: routeTerminalOpen,
33163318
modelPickerOpen: isModelPickerOpen(),
33173319
},
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { afterEach, describe, expect, it } from "vite-plus/test";
2+
3+
import { subscribeToTerminalFocusChanges } from "./useTerminalFocus";
4+
5+
const originalWindow = globalThis.window;
6+
7+
afterEach(() => {
8+
if (originalWindow === undefined) {
9+
delete (globalThis as { window?: Window }).window;
10+
} else {
11+
globalThis.window = originalWindow;
12+
}
13+
});
14+
15+
describe("subscribeToTerminalFocusChanges", () => {
16+
it("notifies on focus transitions until unsubscribed", () => {
17+
const listeners = new Map<string, Set<() => void>>();
18+
globalThis.window = {
19+
addEventListener: (type: string, listener: EventListenerOrEventListenerObject) => {
20+
const callbacks = listeners.get(type) ?? new Set<() => void>();
21+
callbacks.add(listener as () => void);
22+
listeners.set(type, callbacks);
23+
},
24+
removeEventListener: (type: string, listener: EventListenerOrEventListenerObject) => {
25+
listeners.get(type)?.delete(listener as () => void);
26+
},
27+
} as unknown as Window & typeof globalThis;
28+
let notifications = 0;
29+
30+
const unsubscribe = subscribeToTerminalFocusChanges(() => {
31+
notifications += 1;
32+
});
33+
34+
for (const listener of listeners.get("focusin") ?? []) listener();
35+
for (const listener of listeners.get("focusout") ?? []) listener();
36+
expect(notifications).toBe(2);
37+
38+
unsubscribe();
39+
for (const listener of listeners.get("focusin") ?? []) listener();
40+
expect(notifications).toBe(2);
41+
});
42+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useSyncExternalStore } from "react";
2+
3+
import { isTerminalFocused } from "../lib/terminalFocus";
4+
5+
export function subscribeToTerminalFocusChanges(listener: () => void): () => void {
6+
window.addEventListener("focusin", listener, true);
7+
window.addEventListener("focusout", listener, true);
8+
return () => {
9+
window.removeEventListener("focusin", listener, true);
10+
window.removeEventListener("focusout", listener, true);
11+
};
12+
}
13+
14+
export function useTerminalFocus(): boolean {
15+
return useSyncExternalStore(subscribeToTerminalFocusChanges, isTerminalFocused, () => false);
16+
}

‎apps/web/src/keybindings.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ export function shouldShowThreadJumpHintsForModifiers(
301301
keybindings: ResolvedKeybindingsConfig,
302302
options?: ShortcutMatchOptions,
303303
): boolean {
304-
// The embedded terminal owns keystrokes while it has focus: xterm.js
305-
// consumes the keydown and can write the pressed key into the shell
304+
// The embedded terminal owns keystrokes while it has focus: the Ghostty
305+
// surface encodes the keydown and can write the pressed key into the shell
306306
// before our window-level shortcut handling ever runs, regardless of any
307307
// configured `when` clause on the jump command. Advertising jump hints
308308
// here would promise a shortcut that instead types into the terminal, so

0 commit comments

Comments
 (0)