Tunal/crates/tunal/ui/dialogs/shortcut-editor.slint
astar76 e370dac40f keymap: one dispatch path for shortcuts, editor page, new window, tab menu
Commands with default chords, [shortcuts] overrides in settings.toml, conflict
detection; Shortcuts settings page with capture, Replace/Cancel and resets.
Ctrl+N opens a new window, tab right-click menu, closed-tab stack (10).
Probe gains key and middle-button modes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 10:51:00 -06:00

236 lines
8.1 KiB
Text

import { ListView } from "std-widgets.slint";
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icons } from "../components/icon.slint";
import { IconButton } from "../components/icon-button.slint";
import { Button, ButtonKind } from "../components/button.slint";
import { Chip, ChipKind } from "../components/chip.slint";
import { StateLayer } from "../components/state-layer.slint";
import { TextField } from "../components/text-field.slint";
// One command with its keys, as the editor lists it.
export struct KeyRow {
// The command, for the callbacks (its place in the full list).
index: int,
label: string,
// `Ctrl+C, Ctrl+Insert`; empty when unbound.
keys: string,
// Differs from the default: the row offers Reset.
edited: bool,
}
// The keys of one command: a pill, or the prompt while a new key is recorded.
component KeysPill inherits Rectangle {
in property <string> keys;
in property <bool> recording;
property <string> shown: root.recording ? "Press a key…" : root.keys != "" ? root.keys : "None";
height: 28px;
width: label.preferred-width + 2 * Theme.space-3;
border-radius: Theme.corner-full(self.height);
background: root.recording ? Theme.primary-container : Theme.surface-container-highest;
animate background { duration: Motion.time(Motion.short4); easing: Motion.standard; }
label := Text {
text: root.shown;
color: root.recording ? Theme.on-primary-container
: root.keys != "" ? Theme.on-surface : Theme.on-surface-variant;
font-size: Theme.label-large.size;
font-weight: Theme.label-large.weight;
horizontal-alignment: center;
vertical-alignment: center;
}
}
// The shortcut editor: a searchable list of commands with their keys. A click records a new
// key for that command; a key another command has shows the conflict in the row, with Replace
// (which takes it from the other command) and Cancel. The owner applies and stores each
// change and hands back the rows.
export component ShortcutEditor inherits FocusScope {
in property <[KeyRow]> rows;
// The command being recorded (a row's `index`), -1 for none.
in property <int> recording: -1;
// Under the recording row: why the key can't be used, or the conflict.
in property <string> message;
// `message` is a conflict that Replace settles.
in property <bool> conflict;
in property <bool> can-reset-all;
callback search(text: string);
callback record(index: int);
// A key while recording: Slint's key text and Ctrl, Shift, Alt, Super.
callback pressed(text: string, ctrl: bool, shift: bool, alt: bool, meta: bool);
callback replace();
callback cancel();
callback reset(index: int);
callback reset-all();
property <length> row-h: 44px;
// Takes the keys while a command is recorded; Escape gives up.
focus-on-click: false;
changed recording => {
if root.recording >= 0 {
root.focus();
}
}
key-pressed(event) => {
if root.recording < 0 {
return reject;
}
if event.text == Key.Escape {
root.cancel();
} else {
root.pressed(event.text, event.modifiers.control, event.modifiers.shift, event.modifiers.alt, event.modifiers.meta);
}
accept
}
VerticalLayout {
spacing: Theme.space-2;
HorizontalLayout {
spacing: Theme.space-3;
vertical-stretch: 0;
TextField {
horizontal-stretch: 1;
placeholder: "Search commands or keys";
edited(text) => {
root.search(text);
}
}
VerticalLayout {
alignment: start;
padding-top: Theme.space-1;
Chip {
kind: ChipKind.assist;
icon: Icons.restore;
text: "Reset all";
enabled: root.can-reset-all;
clicked => {
root.reset-all();
}
}
}
}
ListView {
vertical-stretch: 1;
for row in root.rows: StateLayer {
property <bool> is-recording: row.index == root.recording;
property <bool> open: self.is-recording && root.message != "";
height: root.row-h + (self.open ? root.row-h : 0);
border-radius: Theme.corner-sm;
squeeze: Motion.squeeze-large;
ring-offset: -Theme.focus-ring-width;
focusable: false;
background: self.is-recording ? Theme.surface-container-highest : transparent;
animate height { duration: Motion.time(Motion.short4); easing: Motion.emphasized; }
animate background { duration: Motion.time(Motion.short3); easing: Motion.standard; }
clip: true;
accessible-role: list-item;
accessible-label: row.label;
accessible-description: row.keys;
clicked => {
root.record(row.index);
}
VerticalLayout {
padding-left: Theme.space-3;
padding-right: Theme.space-1;
HorizontalLayout {
height: root.row-h;
spacing: Theme.space-2;
Text {
horizontal-stretch: 1;
text: row.label;
color: Theme.on-surface;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
vertical-alignment: center;
overflow: elide;
}
VerticalLayout {
alignment: center;
KeysPill {
keys: row.keys;
recording: row.index == root.recording;
}
}
VerticalLayout {
alignment: center;
width: 32px;
if row.edited: IconButton {
size: 32px;
icon-size: 20px;
icon: Icons.restore;
tooltip: "Back to the default keys";
clicked => {
root.reset(row.index);
}
}
}
}
if row.index == root.recording && root.message != "": HorizontalLayout {
height: root.row-h;
spacing: Theme.space-2;
Text {
horizontal-stretch: 1;
text: root.message;
color: root.conflict ? Theme.on-surface : Theme.error;
font-size: Theme.body-small.size;
font-weight: Theme.body-small.weight;
vertical-alignment: center;
wrap: word-wrap;
}
VerticalLayout {
alignment: center;
Button {
text: "Cancel";
kind: ButtonKind.text;
clicked => {
root.cancel();
}
}
}
if root.conflict: VerticalLayout {
alignment: center;
Button {
text: "Replace";
kind: ButtonKind.tonal;
clicked => {
root.replace();
}
}
}
}
}
}
}
Text {
text: root.recording >= 0 ? "Press the new key with its modifiers. Escape cancels." : "Click a command to give it a new key.";
color: Theme.on-surface-variant;
font-size: Theme.body-small.size;
font-weight: Theme.body-small.weight;
wrap: word-wrap;
}
}
}