Skip to content

Commit 335ecdf

Browse files
fix(web): clear automatic shortcut condition on command change
1 parent 1342d56 commit 335ecdf

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎apps/web/src/components/settings/KeybindingsSettings.browser.test.tsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,39 @@ it("re-enables Save when a When editor closes on an expression it could not pars
125125
await expect.element(expression).toHaveValue("terminalFocus");
126126
await expect.element(screen.getByRole("button", { name: "Save" })).toBeEnabled();
127127
});
128+
129+
it("clears only the automatic terminal guard when changing commands", async () => {
130+
await using screen = await render(
131+
<RegistryProvider>
132+
<KeybindingsSettingsPanel />
133+
</RegistryProvider>,
134+
);
135+
await screen.getByRole("button", { name: "Add keybinding" }).click();
136+
const select = screen.getByRole("combobox");
137+
const newWhenText = () =>
138+
select
139+
.element()
140+
.closest('[data-slot="settings-row"]')
141+
?.querySelector('[aria-label^="Edit when clause"]')?.textContent;
142+
await select.click();
143+
await page.getByRole("option", { name: "Thread: Archive" }).click();
144+
await expect
145+
.element(screen.getByRole("button", { name: "Edit when clause for Thread: Archive" }))
146+
.toHaveTextContent("!terminalFocus");
147+
148+
await select.click();
149+
await page.getByRole("option", { name: "Terminal: Toggle" }).click();
150+
await vi.waitFor(() => expect(newWhenText()).toContain("Always"));
151+
152+
await select.click();
153+
await page.getByRole("option", { name: "Thread: Delete" }).click();
154+
const when = screen.getByRole("button", { name: "Edit when clause for Thread: Delete" });
155+
await when.click();
156+
const expression = screen.getByRole("textbox", { name: "When expression" });
157+
await userEvent.clear(expression);
158+
await userEvent.type(expression, "editorFocus");
159+
await userEvent.keyboard("{Escape}");
160+
await select.click();
161+
await page.getByRole("option", { name: "Terminal: Toggle" }).click();
162+
await vi.waitFor(() => expect(newWhenText()).toContain("editorFocus"));
163+
});

‎apps/web/src/components/settings/KeybindingsSettings.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ function useNewKeybindingDraft({
10451045
onSave: (input: ServerUpsertKeybindingInput) => void;
10461046
}) {
10471047
const [commandDraft, setCommandDraft] = useState<KeybindingCommand | "">("");
1048+
const [hasAutomaticWhen, setHasAutomaticWhen] = useState(false);
10481049
const [draft, setDraft] = useReducer(keybindingRowDraftReducer, {
10491050
keyDraft: "",
10501051
whenDraft: undefined,
@@ -1091,10 +1092,15 @@ function useNewKeybindingDraft({
10911092
commandDraft,
10921093
setCommandDraft: (command: KeybindingCommand) => {
10931094
setCommandDraft(command);
1094-
if ((command === "thread.archive" || command === "thread.delete") && !whenDraft) {
1095+
const needsTerminalGuard = command === "thread.archive" || command === "thread.delete";
1096+
if (needsTerminalGuard && !whenDraft) {
10951097
setDraft({
10961098
whenDraft: { type: "not", node: { type: "identifier", name: "terminalFocus" } },
10971099
});
1100+
setHasAutomaticWhen(true);
1101+
} else if (!needsTerminalGuard && hasAutomaticWhen) {
1102+
setDraft({ whenDraft: undefined });
1103+
setHasAutomaticWhen(false);
10981104
}
10991105
},
11001106
keyDraft,
@@ -1105,6 +1111,10 @@ function useNewKeybindingDraft({
11051111
commandLabelText,
11061112
canSave,
11071113
setDraft,
1114+
setWhenDraft: (whenDraft: KeybindingWhenNode | undefined) => {
1115+
setHasAutomaticWhen(false);
1116+
setDraft({ whenDraft });
1117+
},
11081118
save,
11091119
captureKeybinding,
11101120
};
@@ -1189,7 +1199,7 @@ function NewKeybindingWhen({
11891199
expression={draft.whenDraftExpression}
11901200
value={draft.whenDraft}
11911201
variables={variables}
1192-
onChange={(whenDraft) => draft.setDraft({ whenDraft })}
1202+
onChange={draft.setWhenDraft}
11931203
onValidityChange={(isWhenDraftValid) => draft.setDraft({ isWhenDraftValid })}
11941204
/>
11951205
);

‎docs/user/keybindings.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ shortcut; assign one in **Settings → Keybindings**.
156156
`thread.archive` and `thread.delete` act on the current saved thread. Assign a
157157
shortcut in **Settings → Keybindings**; neither has one by default. They respect
158158
the thread confirmation settings, and deletion permanently clears the conversation.
159-
New bindings default to `!terminalFocus`, which you can change in Settings.
159+
Bindings created in Settings default to `!terminalFocus`, which you can change.
160+
When editing `keybindings.json` directly, add `"when": "!terminalFocus"` to
161+
exclude terminal focus; a binding without `when` also matches in the terminal.
160162

161163
`thread.undo` (`mod+z` by default) reverses the actions shown in the notice at the
162164
bottom of the sidebar, such as unpin, settle, snooze, archive, or discarding a

0 commit comments

Comments
 (0)