Tunal/crates/tunal/ui/shell/sidebar.slint

319 lines
12 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,
// A second line under the name.
detail: string,
// Names the button at the end of the row (Unmount, Eject), empty for none.
eject: string,
glyph: string,
// An image in place of the glyph.
picture: image,
has-picture: bool,
// 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,
// Takes folders, and its entries can be moved (places, bookmarks).
editable: 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`.
// The current place: its section and row, and the rows of the open sections before it.
in property <bool> has-current;
in property <int> current-section;
in property <int> current-row;
in property <int> rows-before;
callback open(section: int, index: int, new-tab: bool);
callback toggle(section: int);
callback eject(section: int, index: 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;
property <length> header-height: 36px;
// Rows move because a section folds: the selection goes with them instead of springing.
property <bool> folding;
fold-time := Timer {
running: root.folding;
interval: Motion.medium2;
triggered => {
root.folding = false;
}
}
// The drop strip between two rows.
property <length> gap-height: 12px;
// 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 entries; the wheel scrolls.
mouse-drag-pan-enabled: false;
content-height: max(self.height, column.preferred-height);
// The selection: springs to the place that was opened.
Rectangle {
x: Theme.space-3;
y: Theme.space-3 + root.header-height + root.current-section * (root.header-height + Theme.space-1)
+ (root.rows-before + root.current-row) * root.row-height;
width: parent.width - 2 * Theme.space-3;
height: root.row-height;
border-radius: Theme.corner-full(self.height);
background: Theme.secondary-container;
opacity: root.has-current ? 1 : 0;
animate y {
duration: Motion.time(root.folding ? Motion.medium2 : Motion.medium3);
easing: root.folding ? Motion.emphasized : Motion.spring;
}
animate opacity {
duration: Motion.time(Motion.short4);
easing: Motion.standard;
}
}
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.folding = !Motion.reduced;
root.toggle(s);
}
add => {
root.add-bookmark();
}
connect => {
root.add-connection();
}
// Folders dropped on the header join the section (if it takes any).
DropZone {
target: DropTarget.section;
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;
}
// So do those dropped on the empty section's text.
if section.items.length == 0: DropZone {
target: DropTarget.section;
a: s;
radius: Theme.corner-full(root.row-height);
}
// A place takes drops into its folder; an entry dragged onto another takes
// its slot. A thin strip between two takes folders into the section.
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.editable;
allow-move: true;
data: Dnd.place-payload(s, i);
place := ListRow {
height: root.row-height;
icon: item.glyph;
picture: item.picture;
has-picture: item.has-picture;
title: item.name;
supporting: item.detail;
tooltip: item.tip;
selected: item.current;
own-fill: false;
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);
}
}
if item.eject != "": VerticalLayout {
alignment: center;
IconButton {
icon: Icons.eject;
size: 28px;
icon-size: 18px;
tooltip: item.eject;
clicked => {
root.eject(s, i);
}
}
}
}
}
}
if section.editable: DropZone {
y: i == 0 ? 0 : -self.height / 2;
height: root.gap-height;
target: DropTarget.section;
a: s;
b: i;
radius: Theme.corner-full(self.height);
}
}
if section.editable && section.items.length > 0: DropZone {
y: parent.full - self.height;
height: root.gap-height;
target: DropTarget.section;
a: s;
b: section.items.length;
radius: Theme.corner-full(self.height);
}
}
}
}
}
}