fix(web): hide thread jump hints while the terminal is focused (#7277)
This commit is contained in:
1 parent
f3fcfe1f63
commit
ce8ca5bb3d
6 files changed
+96
-2
No files matched your search
@@ -73,6 +73,7 @@ import {
|
||||
import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal";
|
||||
import { useDesktopLocalBootstraps } from "../connection/useDesktopLocalBootstraps";
|
||||
import { isElectron } from "../env";
|
||||
import { useTerminalFocus } from "../hooks/useTerminalFocus";
|
||||
import { useOpenPrLink } from "../lib/openPullRequestLink";
|
||||
import { isTerminalFocused } from "../lib/terminalFocus";
|
||||
import { isMacPlatform } from "../lib/utils";
|
||||
@@ -3075,6 +3076,7 @@ export default function LegacySidebar() {
|
||||
const setSelectionAnchor = useThreadSelectionStore((s) => s.setAnchor);
|
||||
const platform = navigator.platform;
|
||||
const shortcutModifiers = useShortcutModifierState();
|
||||
const terminalFocused = useTerminalFocus();
|
||||
const { environments } = useEnvironments();
|
||||
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
||||
const environmentLabelById = useMemo(
|
||||
@@ -3397,7 +3399,7 @@ export default function LegacySidebar() {
|
||||
[threadJumpCommandByKey],
|
||||
);
|
||||
const sidebarShortcutContext = {
|
||||
terminalFocus: false,
|
||||
terminalFocus: terminalFocused,
|
||||
terminalOpen: routeTerminalOpen,
|
||||
modelPickerOpen: isModelPickerOpen(),
|
||||
};
|
||||
|
||||
@@ -83,6 +83,7 @@ import {
|
||||
threadTraversalDirectionFromCommand,
|
||||
} from "../keybindings";
|
||||
import { useShortcutModifierState } from "../shortcutModifierState";
|
||||
import { useTerminalFocus } from "../hooks/useTerminalFocus";
|
||||
import { isTerminalFocused } from "../lib/terminalFocus";
|
||||
import { isModelPickerOpen } from "../modelPickerVisibility";
|
||||
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
|
||||
@@ -3312,10 +3313,18 @@ export default function Sidebar() {
|
||||
// match a thread-jump binding. Adding Shift (screenshots) or Alt no
|
||||
// longer matches ⌘1..9, so the overlay hides for chords like ⌘⇧4.
|
||||
const shortcutModifiers = useShortcutModifierState();
|
||||
const terminalFocused = useTerminalFocus();
|
||||
const shouldShowJumpHintsNow = shouldShowThreadJumpHintsForModifiers(
|
||||
shortcutModifiers,
|
||||
keybindings,
|
||||
{ platform: navigator.platform },
|
||||
{
|
||||
platform: navigator.platform,
|
||||
context: {
|
||||
terminalFocus: terminalFocused,
|
||||
terminalOpen: routeTerminalOpen,
|
||||
modelPickerOpen: isModelPickerOpen(),
|
||||
},
|
||||
},
|
||||
);
|
||||
useEffect(() => {
|
||||
setShowJumpHints(shouldShowJumpHintsNow);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { afterEach, describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { subscribeToTerminalFocusChanges } from "./useTerminalFocus";
|
||||
|
||||
const originalWindow = globalThis.window;
|
||||
|
||||
afterEach(() => {
|
||||
if (originalWindow === undefined) {
|
||||
delete (globalThis as { window?: Window }).window;
|
||||
} else {
|
||||
globalThis.window = originalWindow;
|
||||
}
|
||||
});
|
||||
|
||||
describe("subscribeToTerminalFocusChanges", () => {
|
||||
it("notifies on focus transitions until unsubscribed", () => {
|
||||
const listeners = new Map<string, Set<() => void>>();
|
||||
globalThis.window = {
|
||||
addEventListener: (type: string, listener: EventListenerOrEventListenerObject) => {
|
||||
const callbacks = listeners.get(type) ?? new Set<() => void>();
|
||||
callbacks.add(listener as () => void);
|
||||
listeners.set(type, callbacks);
|
||||
},
|
||||
removeEventListener: (type: string, listener: EventListenerOrEventListenerObject) => {
|
||||
listeners.get(type)?.delete(listener as () => void);
|
||||
},
|
||||
} as unknown as Window & typeof globalThis;
|
||||
let notifications = 0;
|
||||
|
||||
const unsubscribe = subscribeToTerminalFocusChanges(() => {
|
||||
notifications += 1;
|
||||
});
|
||||
|
||||
for (const listener of listeners.get("focusin") ?? []) listener();
|
||||
for (const listener of listeners.get("focusout") ?? []) listener();
|
||||
expect(notifications).toBe(2);
|
||||
|
||||
unsubscribe();
|
||||
for (const listener of listeners.get("focusin") ?? []) listener();
|
||||
expect(notifications).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
|
||||
import { isTerminalFocused } from "../lib/terminalFocus";
|
||||
|
||||
export function subscribeToTerminalFocusChanges(listener: () => void): () => void {
|
||||
window.addEventListener("focusin", listener, true);
|
||||
window.addEventListener("focusout", listener, true);
|
||||
return () => {
|
||||
window.removeEventListener("focusin", listener, true);
|
||||
window.removeEventListener("focusout", listener, true);
|
||||
};
|
||||
}
|
||||
|
||||
export function useTerminalFocus(): boolean {
|
||||
return useSyncExternalStore(subscribeToTerminalFocusChanges, isTerminalFocused, () => false);
|
||||
}
|
||||
@@ -451,6 +451,21 @@ describe("thread navigation helpers", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("never shows jump hints while the terminal is focused, even with an unrestricted binding", () => {
|
||||
assert.isFalse(
|
||||
shouldShowThreadJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
|
||||
platform: "MacIntel",
|
||||
context: { terminalFocus: true },
|
||||
}),
|
||||
);
|
||||
assert.isTrue(
|
||||
shouldShowThreadJumpHints(event({ metaKey: true }), DEFAULT_BINDINGS, {
|
||||
platform: "MacIntel",
|
||||
context: { terminalFocus: false },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("model picker navigation helpers", () => {
|
||||
|
||||
@@ -301,6 +301,16 @@ export function shouldShowThreadJumpHintsForModifiers(
|
||||
keybindings: ResolvedKeybindingsConfig,
|
||||
options?: ShortcutMatchOptions,
|
||||
): boolean {
|
||||
// The embedded terminal owns keystrokes while it has focus: the Ghostty
|
||||
// surface encodes the keydown and can write the pressed key into the shell
|
||||
// before our window-level shortcut handling ever runs, regardless of any
|
||||
// configured `when` clause on the jump command. Advertising jump hints
|
||||
// here would promise a shortcut that instead types into the terminal, so
|
||||
// never show them while the terminal is focused.
|
||||
if (resolveContext(options).terminalFocus) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const platform = resolvePlatform(options);
|
||||
|
||||
for (const command of THREAD_JUMP_KEYBINDING_COMMANDS) {
|
||||
|
||||
Reference in new issue
Block a user