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

67 lines
2.4 KiB
Text

import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon } from "icon.slint";
import { StateLayer } from "state-layer.slint";
// Emphasis, from the main action of a screen (filled) down to the least (text).
export enum ButtonKind { filled, tonal, outlined, text }
// A labelled button with an optional leading icon.
export component Button inherits StateLayer {
in property <string> text;
// Glyph from `Icons`; empty for none.
in property <string> icon;
in property <ButtonKind> kind: ButtonKind.filled;
property <bool> contained: root.kind == ButtonKind.filled || root.kind == ButtonKind.tonal;
property <color> content: !root.enabled ? Theme.on-surface.with-alpha(Theme.disabled-content)
: root.kind == ButtonKind.filled ? Theme.on-primary
: root.kind == ButtonKind.tonal ? Theme.on-secondary-container
: Theme.primary;
height: 40px;
border-radius: Theme.corner-full(self.height);
background: !root.contained ? transparent
: !root.enabled ? Theme.on-surface.with-alpha(Theme.disabled-container)
: root.kind == ButtonKind.filled ? Theme.primary
: Theme.secondary-container;
border-width: root.kind == ButtonKind.outlined ? Theme.hairline : 0;
border-color: root.enabled ? Theme.outline : Theme.on-surface.with-alpha(Theme.disabled-container);
tint: root.content;
animate background, border-color {
duration: Motion.time(Motion.short4);
easing: Motion.standard;
}
accessible-role: button;
accessible-label: root.text;
accessible-enabled: root.enabled;
accessible-action-default => {
self.activate();
}
HorizontalLayout {
padding-left: root.kind == ButtonKind.text ? Theme.space-3 : root.icon != "" ? Theme.space-4 : Theme.space-6;
padding-right: root.kind == ButtonKind.text ? (root.icon != "" ? Theme.space-4 : Theme.space-3) : Theme.space-6;
spacing: Theme.space-2;
if root.icon != "": VerticalLayout {
alignment: center;
Icon {
glyph: root.icon;
size: 18px;
color: root.content;
}
}
Text {
text: root.text;
color: root.content;
font-size: Theme.label-large.size;
font-weight: Theme.label-large.weight;
vertical-alignment: center;
overflow: elide;
}
}
}