Tunal/crates/tunal/ui/components/chip.slint

87 lines
3.3 KiB
Text

import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon, Icons } from "icon.slint";
import { StateLayer } from "state-layer.slint";
// Filter chips toggle `checked`; assist chips run an action and may carry an icon.
export enum ChipKind { filter, assist }
// The chip's width is fixed by its label: the leading slot is always reserved. When the
// check (or icon) is hidden the label sits centred, so checking it slides the label over
// instead of resizing the chip and pushing its neighbours.
export component Chip inherits StateLayer {
in property <string> text;
// Assist chips: leading glyph from `Icons`; empty for none.
in property <string> icon;
in property <ChipKind> kind: ChipKind.filter;
in-out property <bool> checked;
property <bool> on: root.kind == ChipKind.filter && root.checked;
property <bool> lead-shown: root.kind == ChipKind.filter ? root.checked : root.icon != "";
property <length> lead-size: 18px;
property <length> label-x: Theme.space-2 + root.lead-size + Theme.space-2;
property <color> content: !root.enabled ? Theme.on-surface.with-alpha(Theme.disabled-content)
: root.on ? Theme.on-secondary-container
: root.kind == ChipKind.assist ? Theme.on-surface
: Theme.on-surface-variant;
height: 32px;
min-width: root.label-x + label.preferred-width + Theme.space-4;
preferred-width: self.min-width;
horizontal-stretch: 0;
border-radius: Theme.corner-sm;
background: !root.on ? transparent : root.enabled ? Theme.secondary-container : Theme.on-surface.with-alpha(Theme.disabled-container);
border-width: root.on ? 0 : Theme.hairline;
border-color: root.enabled ? Theme.outline-variant : Theme.on-surface.with-alpha(Theme.disabled-container);
tint: root.content;
animate background, border-color {
duration: Motion.time(Motion.short4);
easing: Motion.standard;
}
activated => {
if root.kind == ChipKind.filter {
root.checked = !root.checked;
}
}
// Role must be constant; checkable tells filter chips apart.
accessible-role: button;
accessible-label: root.text;
accessible-enabled: root.enabled;
accessible-checkable: root.kind == ChipKind.filter;
accessible-checked: root.on;
accessible-action-default => {
self.activate();
}
Icon {
x: Theme.space-2;
y: (root.height - self.height) / 2;
glyph: root.kind == ChipKind.filter ? Icons.check : root.icon;
size: root.lead-size;
color: !root.enabled ? root.content : root.kind == ChipKind.assist ? Theme.primary : root.content;
opacity: root.lead-shown ? 1 : 0;
transform-scale: root.lead-shown ? 1 : 0.6;
animate opacity, transform-scale {
duration: Motion.time(Motion.short4);
easing: Motion.emphasized-decelerate;
}
}
label := Text {
x: root.lead-shown ? root.label-x : (root.width - self.width) / 2;
y: 0;
height: root.height;
width: self.preferred-width;
text: root.text;
color: root.content;
font-size: Theme.label-large.size;
font-weight: Theme.label-large.weight;
vertical-alignment: center;
animate x {
duration: Motion.time(Motion.short4);
easing: Motion.emphasized;
}
}
}