Tunal/crates/tunal/ui/shell/breadcrumb.slint
2026-11-25 15:01:09 -06:00

304 lines
9.5 KiB
Text

import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon, Icons } from "../components/icon.slint";
import { StateLayer } from "../components/state-layer.slint";
import { ScrollInput } from "../components/scroll-input.slint";
import { DropTarget, DropZone } from "../components/drop-menu.slint";
// One segment of the path: the root (glyph and name) or a folder.
export struct Crumb {
name: string,
// The real name of a folder shown by another one.
tip: string,
glyph: string,
}
// A folder that completes the typed path: its name and the whole path it completes to.
export struct Completion {
name: string,
text: string,
}
component CrumbChip inherits StateLayer {
in property <string> name;
in property <string> glyph;
in property <string> tip;
in property <bool> last;
// Only this chip gives up width (eliding its name) when the bar is short.
in property <bool> shrink;
property <color> content: root.last ? Theme.on-secondary-container : Theme.on-surface-variant;
height: 32px;
border-radius: Theme.corner-full(self.height);
// The current folder is a filled pill; the crumbs before it show only on hover.
background: root.last ? Theme.secondary-container : transparent;
squeeze: Motion.squeeze;
tint: root.content;
focusable: false;
tooltip: root.tip;
accessible-role: button;
accessible-label: root.name;
accessible-action-default => {
self.activate();
}
HorizontalLayout {
padding-left: Theme.space-2;
padding-right: Theme.space-2;
spacing: Theme.space-1;
if root.glyph != "": VerticalLayout {
alignment: center;
Icon {
glyph: root.glyph;
size: 18px;
color: root.content;
}
}
Text {
min-width: root.shrink ? 0 : self.preferred-width;
text: root.name;
color: root.content;
font-size: Theme.label-large.size;
font-weight: root.last ? Theme.label-large.weight : Theme.body-medium.weight;
vertical-alignment: center;
overflow: elide;
}
}
}
// The path of the active pane as segment chips. Ctrl+L or a click on the empty part turns
// it into a text field: Enter goes, Esc puts the chips back, Tab and the arrows complete
// folder names (the list is drawn by `CompletionList`, above the panes).
export component Breadcrumb inherits Rectangle {
in property <[Crumb]> crumbs;
// The location as text, what editing starts from.
in property <string> path;
in property <[Completion]> completions;
out property <bool> editing;
in-out property <int> highlight: -1;
out property <string> text: input.text;
callback go(int);
callback edited(string);
callback accepted(string);
// Editing ended without going anywhere.
callback reverted();
public function edit() {
root.editing = true;
input.text = root.path;
input.focus();
input.select-all();
}
public function stop() {
root.editing = false;
root.highlight = -1;
}
// A completion picked with the pointer: go there.
public function pick(to: string) {
root.stop();
root.accepted(to);
}
function complete(to: string) {
input.text = to;
input.set-selection-offsets(to.character-count * 4, to.character-count * 4);
root.highlight = -1;
root.edited(to);
}
height: 40px;
border-radius: Theme.corner-full(self.height);
background: Theme.surface-container-high;
border-width: root.editing ? 2px : 0;
border-color: Theme.primary;
clip: true;
// Behind the chips: the empty part of the bar starts editing.
TouchArea {
mouse-cursor: MouseCursor.text;
clicked => {
root.edit();
}
}
// Crumbs keep their natural width; when they don't fit, the first one elides, and past
// that the row slides left so the current folder stays in view, behind an ellipsis that
// marks the hidden part (a click on it edits the path).
if !root.editing: Rectangle {
row := HorizontalLayout {
x: min(0, root.width - self.width);
width: max(root.width, self.min-width);
padding-left: Theme.space-1;
padding-right: Theme.space-1;
alignment: start;
for crumb[i] in root.crumbs: HorizontalLayout {
if i > 0: VerticalLayout {
alignment: center;
Icon {
glyph: Icons.chevron-right;
size: 18px;
color: Theme.on-surface-variant;
}
}
// Centred in the bar's height: a layout puts a fixed-height child at the top.
VerticalLayout {
alignment: center;
CrumbChip {
name: crumb.name;
tip: crumb.tip;
glyph: crumb.glyph;
last: i == root.crumbs.length - 1;
shrink: i == 0 && root.crumbs.length > 1;
clicked => {
root.go(i);
}
DropZone {
target: DropTarget.crumb;
a: i;
radius: parent.border-radius;
}
}
}
}
}
if row.x < 0: Rectangle {
x: 0;
width: Theme.space-8;
background: Theme.surface-container-high;
Icon {
glyph: Icons.more-horiz;
size: 18px;
color: Theme.on-surface-variant;
}
TouchArea {
mouse-cursor: MouseCursor.text;
clicked => {
root.edit();
}
}
}
}
keys := FocusScope {
visible: root.editing;
enabled: root.editing;
x: Theme.space-4;
width: parent.width - 2 * Theme.space-4;
capture-key-pressed(event) => {
if event.text == Key.Escape {
root.stop();
root.reverted();
return accept;
}
if event.text == Key.DownArrow && root.completions.length > 0 {
root.highlight = mod(root.highlight + 1, root.completions.length);
return accept;
}
if event.text == Key.UpArrow && root.completions.length > 0 {
root.highlight = root.highlight <= 0 ? root.completions.length - 1 : root.highlight - 1;
return accept;
}
// Tab completes; with nothing to complete it still stays in the field.
if event.text == Key.Tab {
if root.completions.length > 0 {
root.complete(root.completions[max(0, root.highlight)].text);
}
return accept;
}
if event.text == Key.Return {
let target = root.highlight >= 0 && root.highlight < root.completions.length
? root.completions[root.highlight].text : input.text;
root.stop();
root.accepted(target);
return accept;
}
reject
}
input := ScrollInput {
width: 100%;
height: 100%;
label: "Location";
edited => {
root.highlight = -1;
root.edited(self.text);
}
changed has-focus => {
if !self.has-focus && root.editing {
root.stop();
root.reverted();
}
}
}
}
}
// The completions of an editing breadcrumb, placed under it by the window.
export component CompletionList inherits Rectangle {
in property <[Completion]> completions;
in property <int> highlight;
callback picked(string);
property <length> row-height: 36px;
property <int> shown: min(8, root.completions.length);
height: root.shown * root.row-height + 2 * Theme.space-1;
visible: root.shown > 0;
border-radius: Theme.corner-menu;
background: Theme.surface-container;
drop-shadow-blur: Theme.float-shadow-blur;
drop-shadow-offset-y: Theme.float-shadow-offset;
drop-shadow-color: Theme.shadow.with-alpha(Theme.float-shadow-opacity);
for c[i] in root.completions: StateLayer {
visible: i < root.shown;
x: Theme.space-1;
y: Theme.space-1 + i * root.row-height;
width: parent.width - 2 * Theme.space-1;
height: root.row-height;
border-radius: Theme.corner-menu-item;
focusable: false;
background: i == root.highlight ? Theme.secondary-container : transparent;
clicked => {
root.picked(c.text);
}
HorizontalLayout {
padding-left: Theme.space-3;
padding-right: Theme.space-3;
spacing: Theme.space-3;
VerticalLayout {
alignment: center;
Icon {
glyph: Icons.folder;
size: 18px;
color: i == root.highlight ? Theme.on-secondary-container : Theme.on-surface-variant;
}
}
Text {
horizontal-stretch: 1;
text: c.name;
color: i == root.highlight ? Theme.on-secondary-container : Theme.on-surface;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
vertical-alignment: center;
overflow: elide;
}
}
}
}