Tunal/crates/tunal/ui/components/state-layer.slint
astar76 985f44f50f S008: folder model, playlist sort and details view
FolderModel loads and watches folders off the UI thread, sorts (natural
name, size, type, modified, playlist), groups, filters and keeps the
selection; the details view shows it in a virtualized list with a
click-to-sort header, filter chip and empty/loading/error states.
tunal-core::playlist reads m3u/m3u8/pls/xspf/wpl (D-030). tunal <dir>
opens a folder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-18 13:43:43 -06:00

155 lines
5.3 KiB
Text

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Tooltip } from "tooltip.slint";
// A state shown regardless of the pointer and focus, for gallery screenshots.
export enum ForceState { none, hover, pressed, focused }
// Shared by every control: hover and press tint, press squeeze, keyboard focus ring and
// tooltip. A control inherits it, sets its shape (`border-radius`) and container colours, and
// puts its content in as children. Space and Enter activate the control when it has focus.
export component StateLayer inherits Rectangle {
in property <bool> enabled: true;
in property <ForceState> force-state: ForceState.none;
// Colour role the tint is mixed from: the control's content colour.
in property <color> tint: Theme.on-surface;
// Off when the control draws the tint itself from `tint-opacity` (the switch halo).
in property <bool> fill-tint: true;
// Scale while pressed; 1 for no squeeze.
in property <float> squeeze: Motion.squeeze;
in property <string> tooltip;
// Gap between shape and focus ring; `-Theme.focus-ring-width` draws the ring inside the
// shape, for controls stacked edge to edge (rows, tabs).
in property <length> ring-offset: Theme.focus-ring-offset;
// Off for rows of a list that owns the keyboard itself: no Tab stop per row.
in property <bool> focusable: true;
out property <bool> hovered: enabled && (touch.has-hover || force-state == ForceState.hover);
out property <bool> pressed: enabled && (touch.pressed || force-state == ForceState.pressed);
out property <bool> focused: enabled && (keyboard-focus || force-state == ForceState.focused);
out property <float> tint-opacity: pressed ? Theme.state-press : focused ? Theme.state-focus : hovered ? Theme.state-hover : 0;
// For users of the control.
callback clicked;
// For the control itself (toggle its own state), called before `clicked`.
callback activated;
callback double-clicked;
// Keys held at the last pointer press, for Ctrl/Shift clicks.
out property <KeyboardModifiers> modifiers;
property <bool> keyboard-focus;
property <length> ring-inset: Theme.focus-ring-width + root.ring-offset;
animate tint-opacity {
duration: Motion.time(root.hovered || root.pressed ? Motion.short3 : Motion.short2);
easing: Motion.standard;
}
// Scales from the centre; the layout size stays fixed so neighbours don't move.
transform-scale: pressed ? squeeze : 1;
animate transform-scale {
duration: Motion.time(root.pressed ? Motion.short2 : Motion.short3);
easing: Motion.standard;
}
public function activate() {
root.activated();
root.clicked();
}
// Moves keyboard focus here from code (a dialog focusing its default button); `ring`
// shows the focus ring as Tab would.
out property <bool> has-focus: keys.has-focus;
public function grab-focus(ring: bool) {
keys.focus();
root.keyboard-focus = ring;
}
keys := FocusScope {
x: 0;
width: 0;
enabled: root.enabled && root.focusable;
focus-on-click: false;
// Only keyboard focus shows the ring.
focus-gained(reason) => {
root.keyboard-focus = reason == FocusReason.tab-navigation;
}
focus-lost => {
root.keyboard-focus = false;
}
key-pressed(event) => {
if event.text == Key.Space || event.text == Key.Return {
root.activate();
return accept;
}
reject
}
}
touch := TouchArea {
enabled: root.enabled;
clicked => {
root.activate();
}
double-clicked => {
root.double-clicked();
}
pointer-event(event) => {
if event.kind == PointerEventKind.down {
root.modifiers = event.modifiers;
}
}
changed has-hover => {
if !self.has-hover {
root.tip-due = false;
tip.close();
}
}
changed pressed => {
if self.pressed {
tip.close();
}
}
}
@children
// Above the content so it also shows over a selection fill; 8–10 % barely touches text.
Rectangle {
visible: root.fill-tint;
border-radius: root.border-radius;
background: root.tint.with-alpha(root.tint-opacity);
}
if root.focused: Rectangle {
x: -root.ring-inset;
y: -root.ring-inset;
width: root.width + 2 * root.ring-inset;
height: root.height + 2 * root.ring-inset;
border-width: Theme.focus-ring-width;
border-color: Theme.secondary;
border-radius: root.border-radius + root.ring-inset;
}
// Runs only while hovered and the tooltip is not yet shown: nothing ticks when idle.
property <bool> tip-due;
Timer {
interval: Motion.tooltip-delay;
running: root.tooltip != "" && touch.has-hover && !touch.pressed && !root.tip-due;
triggered => {
root.tip-due = true;
tip.show();
}
}
tip := PopupWindow {
close-policy: no-auto-close;
x: (root.width - self.width) / 2;
y: root.height + Theme.space-1;
Tooltip {
text: root.tooltip;
}
}
}