Add diff panel keyboard toggle and shortcut hint
- Bind `mod+d` to `diff.toggle` when terminal is not focused - Wire ChatView to handle `diff.toggle` and show its shortcut in the diff button tooltip - Limit sidebar `mod+b` listener to mobile to avoid desktop shortcut conflicts - Extend web/contracts keybinding tests and command list for `diff.toggle`
This commit is contained in:
1 parent
10c1cae6ab
commit
9b458ef115
7 files changed
+68
-11
No files matched your search
@@ -65,6 +65,7 @@ export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
|
||||
{ key: "mod+d", command: "terminal.split", when: "terminalFocus" },
|
||||
{ key: "mod+n", command: "terminal.new", when: "terminalFocus" },
|
||||
{ key: "mod+w", command: "terminal.close", when: "terminalFocus" },
|
||||
{ key: "mod+d", command: "diff.toggle", when: "!terminalFocus" },
|
||||
{ key: "mod+n", command: "chat.new", when: "!terminalFocus" },
|
||||
{ key: "mod+shift+o", command: "chat.new", when: "!terminalFocus" },
|
||||
{ key: "mod+shift+n", command: "chat.newLocal", when: "!terminalFocus" },
|
||||
|
||||
@@ -615,6 +615,20 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
() => shortcutLabelForCommand(keybindings, "terminal.close"),
|
||||
[keybindings],
|
||||
);
|
||||
const diffPanelShortcutLabel = useMemo(
|
||||
() => shortcutLabelForCommand(keybindings, "diff.toggle"),
|
||||
[keybindings],
|
||||
);
|
||||
const onToggleDiff = useCallback(() => {
|
||||
void navigate({
|
||||
to: "/$threadId",
|
||||
params: { threadId },
|
||||
search: (previous) => {
|
||||
const rest = stripDiffSearchParams(previous);
|
||||
return diffOpen ? rest : { ...rest, diff: "1" };
|
||||
},
|
||||
});
|
||||
}, [diffOpen, navigate, threadId]);
|
||||
|
||||
const envLocked = Boolean(
|
||||
activeThread &&
|
||||
@@ -1147,6 +1161,13 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (command === "diff.toggle") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
onToggleDiff();
|
||||
return;
|
||||
}
|
||||
|
||||
const scriptId = projectScriptIdFromCommand(command);
|
||||
if (!scriptId || !activeProject) return;
|
||||
const script = activeProject.scripts.find((entry) => entry.id === scriptId);
|
||||
@@ -1168,6 +1189,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
runProjectScript,
|
||||
splitTerminal,
|
||||
keybindings,
|
||||
onToggleDiff,
|
||||
toggleTerminalVisibility,
|
||||
]);
|
||||
|
||||
@@ -1612,16 +1634,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
const onExpandTimelineImage = useCallback((image: ExpandedImagePreview) => {
|
||||
setExpandedImage(image);
|
||||
}, []);
|
||||
const onToggleDiff = useCallback(() => {
|
||||
void navigate({
|
||||
to: "/$threadId",
|
||||
params: { threadId },
|
||||
search: (previous) => {
|
||||
const rest = stripDiffSearchParams(previous);
|
||||
return diffOpen ? rest : { ...rest, diff: "1" };
|
||||
},
|
||||
});
|
||||
}, [diffOpen, navigate, threadId]);
|
||||
const onOpenTurnDiff = useCallback(
|
||||
(turnId: TurnId, filePath?: string) => {
|
||||
void navigate({
|
||||
@@ -1692,6 +1704,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
activeProject ? (lastInvokedScriptByProjectId[activeProject.id] ?? null) : null
|
||||
}
|
||||
keybindings={keybindings}
|
||||
diffToggleShortcutLabel={diffPanelShortcutLabel}
|
||||
gitCwd={gitCwd}
|
||||
diffOpen={diffOpen}
|
||||
onRunProjectScript={(script) => {
|
||||
@@ -2043,6 +2056,7 @@ interface ChatHeaderProps {
|
||||
activeProjectScripts: ProjectScript[] | undefined;
|
||||
preferredScriptId: string | null;
|
||||
keybindings: ResolvedKeybindingsConfig;
|
||||
diffToggleShortcutLabel: string | null;
|
||||
gitCwd: string | null;
|
||||
diffOpen: boolean;
|
||||
onRunProjectScript: (script: ProjectScript) => void;
|
||||
@@ -2058,6 +2072,7 @@ const ChatHeader = memo(function ChatHeader({
|
||||
activeProjectScripts,
|
||||
preferredScriptId,
|
||||
keybindings,
|
||||
diffToggleShortcutLabel,
|
||||
gitCwd,
|
||||
diffOpen,
|
||||
onRunProjectScript,
|
||||
@@ -2101,6 +2116,9 @@ const ChatHeader = memo(function ChatHeader({
|
||||
pressed={diffOpen}
|
||||
onPressedChange={onToggleDiff}
|
||||
aria-label="Toggle diff panel"
|
||||
title={
|
||||
diffToggleShortcutLabel ? `Toggle diff panel (${diffToggleShortcutLabel})` : undefined
|
||||
}
|
||||
variant="outline"
|
||||
size="xs"
|
||||
>
|
||||
|
||||
@@ -132,6 +132,7 @@ function SidebarProvider({
|
||||
// Adds a keyboard shortcut to toggle the sidebar.
|
||||
React.useEffect(() => {
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (!isMobile) return;
|
||||
if (event.key === SIDEBAR_KEYBOARD_SHORTCUT && (event.metaKey || event.ctrlKey)) {
|
||||
event.preventDefault();
|
||||
toggleSidebar();
|
||||
@@ -140,7 +141,7 @@ function SidebarProvider({
|
||||
|
||||
window.addEventListener("keydown", handleKeyDown);
|
||||
return () => window.removeEventListener("keydown", handleKeyDown);
|
||||
}, [toggleSidebar]);
|
||||
}, [isMobile, toggleSidebar]);
|
||||
|
||||
// We add a state so that we can do data-state="expanded" or "collapsed".
|
||||
// This makes it easier to style the sidebar with Tailwind classes.
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
formatShortcutLabel,
|
||||
isChatNewShortcut,
|
||||
isChatNewLocalShortcut,
|
||||
isDiffToggleShortcut,
|
||||
isOpenFavoriteEditorShortcut,
|
||||
isTerminalClearShortcut,
|
||||
isTerminalCloseShortcut,
|
||||
@@ -91,6 +92,11 @@ const DEFAULT_BINDINGS = compile([
|
||||
command: "terminal.close",
|
||||
whenAst: whenIdentifier("terminalFocus"),
|
||||
},
|
||||
{
|
||||
shortcut: modShortcut("d"),
|
||||
command: "diff.toggle",
|
||||
whenAst: whenNot(whenIdentifier("terminalFocus")),
|
||||
},
|
||||
{ shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
|
||||
{ shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
|
||||
{ shortcut: modShortcut("o"), command: "editor.openFavorite" },
|
||||
@@ -230,6 +236,7 @@ describe("shortcutLabelForCommand", () => {
|
||||
|
||||
it("returns labels for non-terminal commands", () => {
|
||||
assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
|
||||
assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "diff.toggle", "Linux"), "Ctrl+D");
|
||||
assert.strictEqual(
|
||||
shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
|
||||
"Ctrl+O",
|
||||
@@ -276,6 +283,21 @@ describe("chat/editor shortcuts", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("matches diff.toggle shortcut outside terminal focus", () => {
|
||||
assert.isTrue(
|
||||
isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
|
||||
platform: "MacIntel",
|
||||
context: { terminalFocus: false },
|
||||
}),
|
||||
);
|
||||
assert.isFalse(
|
||||
isDiffToggleShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
|
||||
platform: "MacIntel",
|
||||
context: { terminalFocus: true },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("cross-command precedence", () => {
|
||||
|
||||
@@ -198,6 +198,14 @@ export function isTerminalCloseShortcut(
|
||||
return matchesCommandShortcut(event, keybindings, "terminal.close", options);
|
||||
}
|
||||
|
||||
export function isDiffToggleShortcut(
|
||||
event: ShortcutEventLike,
|
||||
keybindings: ResolvedKeybindingsConfig,
|
||||
options?: ShortcutMatchOptions,
|
||||
): boolean {
|
||||
return matchesCommandShortcut(event, keybindings, "diff.toggle", options);
|
||||
}
|
||||
|
||||
export function isChatNewShortcut(
|
||||
event: ShortcutEventLike,
|
||||
keybindings: ResolvedKeybindingsConfig,
|
||||
|
||||
@@ -35,6 +35,12 @@ it.effect("parses keybinding rules", () =>
|
||||
});
|
||||
assert.strictEqual(parsedClose.command, "terminal.close");
|
||||
|
||||
const parsedDiffToggle = yield* decode(KeybindingRule, {
|
||||
key: "mod+d",
|
||||
command: "diff.toggle",
|
||||
});
|
||||
assert.strictEqual(parsedDiffToggle.command, "diff.toggle");
|
||||
|
||||
const parsedLocal = yield* decode(KeybindingRule, {
|
||||
key: "mod+shift+n",
|
||||
command: "chat.newLocal",
|
||||
|
||||
@@ -14,6 +14,7 @@ export const STATIC_KEYBINDING_COMMANDS = [
|
||||
"terminal.split",
|
||||
"terminal.new",
|
||||
"terminal.close",
|
||||
"diff.toggle",
|
||||
"chat.new",
|
||||
"chat.newLocal",
|
||||
"editor.openFavorite",
|
||||
|
||||
Reference in new issue
Block a user