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

63 lines
2.1 KiB
Text

import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon } from "icon.slint";
import { StateLayer } from "state-layer.slint";
// An icon-only button. Always give it a `tooltip` naming its action. With `toggle` it flips
// `checked` on click and shows a tonal container while checked.
export component IconButton inherits StateLayer {
// Glyph from `Icons`.
in property <string> icon;
in property <bool> toggle;
in-out property <bool> checked;
in property <length> size: 40px;
in property <length> icon-size: 24px;
property <bool> on: root.toggle && root.checked;
property <color> content: !root.enabled ? Theme.on-surface.with-alpha(Theme.disabled-content)
: root.on ? Theme.on-secondary-container
: Theme.on-surface-variant;
width: root.size;
height: root.size;
// Pressed, the circle squashes towards a squircle on top of the squeeze.
border-radius: root.pressed ? (root.size >= 40px ? Theme.corner-md : Theme.corner-sm) : Theme.corner-full(root.size);
background: !root.on ? transparent : root.enabled ? Theme.secondary-container : Theme.on-surface.with-alpha(Theme.disabled-container);
tint: root.content;
squeeze: Motion.squeeze-small;
animate border-radius {
duration: Motion.time(root.pressed ? Motion.short2 : Motion.short3);
easing: Motion.standard;
}
animate background {
duration: Motion.time(Motion.short4);
easing: Motion.standard;
}
activated => {
if root.toggle {
root.checked = !root.checked;
}
}
accessible-role: button;
accessible-label: root.tooltip;
accessible-enabled: root.enabled;
accessible-checkable: root.toggle;
accessible-checked: root.checked;
accessible-action-default => {
self.activate();
}
Icon {
x: (root.width - self.width) / 2;
y: (root.height - self.height) / 2;
glyph: root.icon;
size: root.icon-size;
color: root.content;
animate color {
duration: Motion.time(Motion.short4);
easing: Motion.standard;
}
}
}