Tunal/crates/tunal/ui/dialogs/menu-editor.slint
astar76 f7af0f56b4 S013: settings dialog, editable context menus (menus.toml), menu editor
Shared files actions.rs, app.slint, main.rs, shell/mod.rs carry S012 hunks and are committed with S012.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-18 18:46:11 -06:00

401 lines
14 KiB
Text

import { ScrollView } from "std-widgets.slint";
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon, Icons } from "../components/icon.slint";
import { IconButton } from "../components/icon-button.slint";
import { Chip, ChipKind } from "../components/chip.slint";
import { StateLayer } from "../components/state-layer.slint";
// One action (or separator) of a menu being edited.
export struct EditRow {
text: string,
// Glyph from `Icons`; empty for none.
icon: string,
shortcut: string,
separator: bool,
}
// Heading over a column of rows.
component ColumnTitle inherits Text {
color: Theme.on-surface-variant;
font-size: Theme.label-medium.size;
font-weight: Theme.label-medium.weight;
height: 24px;
vertical-alignment: center;
}
// Edits the menu of one context: the rows of the menu (drag by the handle or use the arrow
// buttons to reorder, the cross removes) and the actions still available (click to add).
// The owner applies each change and hands back the new rows.
export component MenuEditor inherits VerticalLayout {
in property <[string]> contexts;
in property <int> context;
in property <[EditRow]> rows;
in property <[EditRow]> available;
in property <bool> can-reset;
callback context-selected(int);
callback moved(from: int, to: int);
callback removed(int);
callback added(int);
callback separator-added();
callback reset();
property <length> row-h: 36px;
property <length> handle-w: 32px;
// The row being moved and the slot it is heading for. `dragging`: the pointer holds it;
// otherwise it settles into `target` and the change is applied when it has arrived.
property <int> drag: -1;
property <int> target;
property <bool> dragging;
property <bool> settling;
// Applying a change swaps what the rows show; their positions must not animate then.
property <bool> snap;
property <length> grab;
property <length> pointer-y;
spacing: Theme.space-3;
// Where the row at `i` sits while another one is moving.
pure function slot(i: int, drag: int, target: int) -> int {
if drag < 0 || i == drag {
return i;
}
if drag < target && i > drag && i <= target {
return i - 1;
}
if drag > target && i < drag && i >= target {
return i + 1;
}
i
}
function settle() {
if root.drag < 0 {
return;
}
if root.target == root.drag {
root.drag = -1;
} else if Motion.time(Motion.short4) == Motion.none {
root.finish();
} else {
root.settling = true;
}
}
function finish() {
root.snap = true;
root.moved(root.drag, root.target);
root.drag = -1;
root.settling = false;
}
function nudge(i: int, by: int) {
if root.drag >= 0 {
return;
}
root.drag = i;
root.target = i + by;
root.dragging = false;
root.settle();
}
HorizontalLayout {
spacing: Theme.space-2;
alignment: start;
for name[i] in root.contexts: Chip {
property <bool> sel: i == root.context;
text: name;
checked: self.sel;
changed sel => {
self.checked = self.sel;
}
clicked => {
root.context-selected(i);
self.checked = true;
}
}
}
HorizontalLayout {
spacing: Theme.space-4;
VerticalLayout {
horizontal-stretch: 3;
spacing: Theme.space-1;
ColumnTitle {
text: "In the menu";
}
ScrollView {
height: 280px;
content-height: max(self.height, root.rows.length * root.row-h);
for row[i] in root.rows: Rectangle {
property <bool> held: i == root.drag;
x: 0;
y: self.held && root.dragging
? clamp(root.pointer-y - root.grab, 0, (root.rows.length - 1) * root.row-h)
: root.slot(i, root.drag, root.target) * root.row-h;
width: parent.width;
height: root.row-h;
border-radius: Theme.corner-sm;
background: self.held ? Theme.surface-container-highest : transparent;
drop-shadow-blur: self.held ? Theme.float-shadow-blur / 3 : 0;
drop-shadow-color: Theme.shadow.with-alpha(Theme.float-shadow-opacity);
animate y {
duration: root.snap || (self.held && root.dragging) ? Motion.none : Motion.time(Motion.short4);
easing: Motion.emphasized;
}
animate background, drop-shadow-blur {
duration: Motion.time(Motion.short3);
easing: Motion.standard;
}
HorizontalLayout {
padding-left: root.handle-w;
padding-right: Theme.space-1;
spacing: Theme.space-2;
if row.separator: VerticalLayout {
alignment: center;
horizontal-stretch: 1;
padding-left: Theme.space-2;
spacing: 0;
Rectangle {
height: Theme.hairline;
background: Theme.outline;
}
}
if !row.separator: VerticalLayout {
alignment: center;
Icon {
glyph: row.icon;
size: 20px;
}
}
if !row.separator: Text {
horizontal-stretch: 1;
text: row.text;
color: Theme.on-surface;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
vertical-alignment: center;
overflow: elide;
}
VerticalLayout {
alignment: center;
IconButton {
size: 32px;
icon-size: 20px;
icon: Icons.arrow-upward;
tooltip: "Move up";
enabled: i > 0;
clicked => {
root.nudge(i, -1);
}
}
}
VerticalLayout {
alignment: center;
IconButton {
size: 32px;
icon-size: 20px;
icon: Icons.arrow-downward;
tooltip: "Move down";
enabled: i < root.rows.length - 1;
clicked => {
root.nudge(i, 1);
}
}
}
VerticalLayout {
alignment: center;
IconButton {
size: 32px;
icon-size: 20px;
icon: Icons.close;
tooltip: "Remove from the menu";
clicked => {
if root.drag < 0 {
root.snap = true;
root.removed(i);
}
}
}
}
}
Icon {
x: (root.handle-w - self.width) / 2 + Theme.space-1;
y: (root.row-h - self.height) / 2;
glyph: Icons.drag-handle;
size: 20px;
opacity: parent.held ? 1 : 0.6;
}
}
// The grip column, outside the moving rows so the pointer stays in one frame.
handle := TouchArea {
x: 0;
y: 0;
width: root.handle-w;
height: root.rows.length * root.row-h;
mouse-cursor: root.dragging ? MouseCursor.grabbing : MouseCursor.grab;
pointer-event(event) => {
if event.button == PointerEventButton.left && event.kind == PointerEventKind.down && root.drag < 0 {
root.drag = clamp(floor(self.mouse-y / root.row-h), 0, root.rows.length - 1);
root.target = root.drag;
root.grab = self.mouse-y - root.drag * root.row-h;
root.pointer-y = self.mouse-y;
root.dragging = true;
} else if event.kind == PointerEventKind.up || event.kind == PointerEventKind.cancel {
if root.dragging {
root.dragging = false;
root.settle();
}
}
}
moved => {
if root.dragging {
root.pointer-y = self.mouse-y;
root.target = clamp(round((self.mouse-y - root.grab) / root.row-h), 0, root.rows.length - 1);
}
}
}
// Plays the settle, then applies the change; a pause after it keeps the rows
// from animating into their new content.
Timer {
running: root.settling;
interval: Motion.time(Motion.short4);
triggered => {
root.finish();
}
}
Timer {
running: root.snap;
interval: Motion.snap-pause;
triggered => {
root.snap = false;
}
}
}
}
VerticalLayout {
horizontal-stretch: 2;
spacing: Theme.space-1;
ColumnTitle {
text: "Add to the menu";
}
ScrollView {
height: 280px;
content-height: max(self.height, root.available.length * root.row-h);
for row[i] in root.available: StateLayer {
y: i * root.row-h;
width: parent.width;
height: root.row-h;
border-radius: Theme.corner-sm;
squeeze: Motion.squeeze-large;
ring-offset: -Theme.focus-ring-width;
tooltip: "Add to the menu";
clicked => {
root.snap = true;
root.added(i);
}
HorizontalLayout {
padding-left: Theme.space-2;
padding-right: Theme.space-2;
spacing: Theme.space-2;
VerticalLayout {
alignment: center;
Icon {
glyph: row.icon;
size: 20px;
}
}
Text {
horizontal-stretch: 1;
text: row.text;
color: Theme.on-surface;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
vertical-alignment: center;
overflow: elide;
}
VerticalLayout {
alignment: center;
Icon {
glyph: Icons.add;
size: 20px;
}
}
}
}
if root.available.length == 0: Text {
text: "Every action is in the menu.";
color: Theme.on-surface-variant;
font-size: Theme.body-small.size;
font-weight: Theme.body-small.weight;
y: Theme.space-2;
width: parent.width;
wrap: word-wrap;
}
}
}
}
HorizontalLayout {
spacing: Theme.space-2;
alignment: start;
Chip {
kind: ChipKind.assist;
icon: Icons.add;
text: "Separator";
tooltip: "Add a separator at the end";
clicked => {
root.snap = true;
root.separator-added();
}
}
Chip {
kind: ChipKind.assist;
icon: Icons.restore;
text: "Reset to defaults";
enabled: root.can-reset;
clicked => {
root.snap = true;
root.reset();
}
}
}
}