Tunal/crates/tunal/ui/shell/sidebar.slint
2026-11-19 00:35:39 -06:00

235 lines
8.8 KiB
Text

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 { ListRow } from "../components/list-row.slint";
import { StateLayer } from "../components/state-layer.slint";
import { ShapeKind } from "../components/shape.slint";
import { Dnd, DropTarget, DropZone } from "../components/drop-menu.slint";
export struct PlaceItem {
name: string,
// The real name of a folder shown by another one.
tip: string,
glyph: string,
// Shows the selection fill: the active pane is at this place.
current: bool,
}
// One section of the sidebar (places, bookmarks, devices, network), set from Rust.
export struct SidebarSection {
title: string,
items: [PlaceItem],
collapsed: bool,
// Shown when there are no items.
empty: string,
// Offers "Add the current folder" in its header (bookmarks).
can-add: bool,
// Offers "Add a connection" in its header (network).
can-connect: bool,
}
component SectionHeader inherits StateLayer {
in property <string> title;
in property <bool> collapsed;
in property <bool> can-add;
in property <bool> can-connect;
callback add();
callback connect();
height: 36px;
border-radius: Theme.corner-full(self.height);
squeeze: Motion.squeeze-large;
tint: Theme.on-surface-variant;
accessible-role: button;
accessible-label: root.title;
accessible-expandable: true;
accessible-expanded: !root.collapsed;
accessible-action-default => {
self.activate();
}
HorizontalLayout {
padding-left: Theme.space-4;
padding-right: Theme.space-1;
spacing: Theme.space-2;
Text {
horizontal-stretch: 1;
text: root.title;
color: Theme.on-surface-variant;
font-size: Theme.label-medium.size;
font-weight: Theme.label-medium.weight;
vertical-alignment: center;
overflow: elide;
}
if root.can-add: VerticalLayout {
alignment: center;
IconButton {
icon: Icons.bookmark-add;
size: 28px;
icon-size: 18px;
tooltip: "Bookmark the current folder (Ctrl+D)";
clicked => {
root.add();
}
}
}
if root.can-connect: VerticalLayout {
alignment: center;
IconButton {
icon: Icons.add;
size: 28px;
icon-size: 18px;
tooltip: "Add a connection";
clicked => {
root.connect();
}
}
}
VerticalLayout {
alignment: center;
Rectangle {
width: 28px;
height: 28px;
Icon {
glyph: Icons.expand-more;
size: 18px;
color: Theme.on-surface-variant;
transform-rotation: root.collapsed ? -90deg : 0deg;
animate transform-rotation { duration: Motion.time(Motion.short4); easing: Motion.emphasized; }
}
}
}
}
}
// Places, bookmarks, devices and network locations in collapsible sections.
export component Sidebar inherits Rectangle {
in property <[SidebarSection]> sections;
// Left click (or middle click, `new-tab`) on item `index` of section `section`.
callback open(section: int, index: int, new-tab: bool);
callback toggle(section: int);
callback add-bookmark();
callback add-connection();
// Right click on an item, at window coordinates.
callback item-menu(section: int, index: int, x: length, y: length);
property <length> row-height: 44px;
// Resting shapes of the place glyphs, in turn; the current place morphs to a cookie9.
property <[ShapeKind]> shapes: [ShapeKind.cookie6, ShapeKind.clover, ShapeKind.cookie12, ShapeKind.scallop, ShapeKind.cookie4];
// On the window's surface-container: the sidebar has no fill of its own.
Flickable {
// Mouse drags start drags of bookmarks; the wheel scrolls.
mouse-drag-pan-enabled: false;
content-height: max(self.height, column.preferred-height);
column := VerticalLayout {
padding: Theme.space-3;
spacing: Theme.space-1;
alignment: start;
for section[s] in root.sections: VerticalLayout {
SectionHeader {
title: section.title;
collapsed: section.collapsed;
can-add: section.can-add;
can-connect: section.can-connect;
clicked => {
root.toggle(s);
}
add => {
root.add-bookmark();
}
connect => {
root.add-connection();
}
// Folders dropped on the header become bookmarks (Rust accepts only there).
DropZone {
target: DropTarget.bookmarks;
a: s;
radius: parent.border-radius;
}
}
// Folds to nothing: clipped while its height animates.
Rectangle {
property <length> full: section.items.length == 0 ? root.row-height : section.items.length * root.row-height;
height: section.collapsed ? 0 : self.full;
clip: true;
animate height { duration: Motion.time(Motion.medium2); easing: Motion.emphasized; }
if section.items.length == 0: Text {
x: Theme.space-4;
y: 0;
width: parent.width - 2 * Theme.space-4;
height: root.row-height;
text: section.empty;
color: Theme.on-surface-variant;
font-size: Theme.body-small.size;
font-weight: Theme.body-small.weight;
vertical-alignment: center;
overflow: elide;
}
// Files dropped on the empty bookmarks text become bookmarks too.
if section.items.length == 0: DropZone {
target: DropTarget.bookmarks;
a: s;
radius: Theme.corner-full(root.row-height);
}
// A place takes drops into its folder; a bookmark dragged onto another
// takes its slot.
for item[i] in section.items: Rectangle {
x: 0;
y: i * root.row-height;
width: parent.width;
height: root.row-height;
DropZone {
target: DropTarget.place;
a: s;
b: i;
radius: place.border-radius;
DragArea {
enabled: section.can-add;
allow-move: true;
data: Dnd.bookmark-payload(i);
place := ListRow {
height: root.row-height;
icon: item.glyph;
title: item.name;
tooltip: item.tip;
selected: item.current;
shaped: true;
shape: root.shapes[mod(i, root.shapes.length)];
focusable: false;
clicked => {
root.open(s, i, false);
}
pointer(event) => {
if event.kind == PointerEventKind.up && event.button == PointerEventButton.middle {
root.open(s, i, true);
}
if event.kind == PointerEventKind.down && event.button == PointerEventButton.right {
root.item-menu(s, i, self.absolute-position.x + self.mouse-x, self.absolute-position.y + root.row-height);
}
}
}
}
}
}
}
}
}
}
}