Tunal/crates/tunal/ui/components/state-layer.slint
astar76 c94b868b2e S005: overlay layer, menu with submenu, modal dialog, toast stack
Menus, dialogs and toasts draw on an own full-window layer so they can
play exit motion; gallery pages show them open for screenshots.

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

142 lines
4.8 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;
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;
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;
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();
}
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;
}
}
}