Tunal/crates/tunal/ui/components/state-layer.slint

209 lines
7.9 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, MotionClock } from "../motion.slint";
import { Tips } 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;
// Press ripple spreading from the pointer; off for controls that draw their own press.
in property <bool> ripple: 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);
// With a ripple the press shows in the ripple; the flat tint stays at hover strength.
out property <float> tint-opacity: pressed && !ripple ? Theme.state-press : focused ? Theme.state-focus : hovered || pressed ? 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;
// Raw pointer events and moves while pressed, for drags and other buttons.
callback pointer(PointerEvent);
callback moved();
out property <length> mouse-x: touch.mouse-x;
out property <length> mouse-y: touch.mouse-y;
out property <length> pressed-x: touch.pressed-x;
out property <length> pressed-y: touch.pressed-y;
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;
if event.button == PointerEventButton.left {
root.ripple-x = self.mouse-x;
root.ripple-y = self.mouse-y;
root.ripple-epoch += 1;
}
}
root.pointer(event);
}
moved => {
root.moved();
}
changed has-hover => {
if !self.has-hover {
root.tip-due = false;
root.hide-tip();
}
}
changed pressed => {
if self.pressed {
root.hide-tip();
}
}
}
@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);
}
// The ripple: a circle from the press point that grows to cover the far corner, then fades
// after release. Its clock ticks only while it spreads.
property <length> ripple-x: root.width / 2;
property <length> ripple-y: root.height / 2;
property <int> ripple-epoch;
property <length> ripple-reach: sqrt(pow(max(root.ripple-x, root.width - root.ripple-x) / 1px, 2) + pow(max(root.ripple-y, root.height - root.ripple-y) / 1px, 2)) * 1px;
property <float> ripple-fade: root.pressed ? 1 : 0;
animate ripple-fade {
duration: Motion.time(root.pressed ? Motion.short1 : Motion.medium2);
easing: Motion.standard;
}
ripple-clock := MotionClock {
epoch: root.ripple-epoch;
duration: Motion.time(Motion.medium4);
}
if root.ripple && root.fill-tint && root.ripple-fade > 0: Rectangle {
clip: true;
border-radius: root.border-radius;
// A forced press (gallery) shows the ripple half spread.
property <float> spread: root.force-state == ForceState.pressed ? 0.5 : Motion.settle(ripple-clock.progress);
Rectangle {
width: 2 * root.ripple-reach * parent.spread;
height: self.width;
x: root.ripple-x - self.width / 2;
y: root.ripple-y - self.height / 2;
border-radius: self.width / 2;
background: root.tint.with-alpha(Theme.state-press * root.ripple-fade);
}
}
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.
// The tooltip itself is the window's `TooltipLayer`.
property <bool> tip-due;
property <int> tip-owner: -1;
function hide-tip() {
if Tips.owner == root.tip-owner {
Tips.shown = false;
}
}
Timer {
interval: Motion.tooltip-delay;
running: root.tooltip != "" && touch.has-hover && !touch.pressed && !root.tip-due;
triggered => {
root.tip-due = true;
Tips.owner += 1;
root.tip-owner = Tips.owner;
Tips.text = root.tooltip;
Tips.x = root.absolute-position.x + root.width / 2;
Tips.bottom = root.absolute-position.y + root.height;
Tips.top = root.absolute-position.y;
Tips.shown = true;
}
}
}