75 lines
2.8 KiB
Text
75 lines
2.8 KiB
Text
import { Theme } from "../theme.slint";
|
|
import { Motion } from "../motion.slint";
|
|
import { StateLayer } from "state-layer.slint";
|
|
|
|
// An on/off switch. The thumb slides and grows when on, grows further while pressed, and a
|
|
// halo behind it carries the hover and press tint.
|
|
export component Switch inherits StateLayer {
|
|
in-out property <bool> checked;
|
|
|
|
property <length> halo-size: 40px;
|
|
property <length> thumb-size: root.pressed ? 28px : root.checked ? 24px : 16px;
|
|
// Centre of the thumb.
|
|
property <length> thumb-x: root.checked ? root.width - root.height / 2 : root.height / 2;
|
|
|
|
width: 52px;
|
|
height: 32px;
|
|
border-radius: Theme.corner-full(self.height);
|
|
background: root.checked
|
|
? (root.enabled ? Theme.primary : Theme.on-surface.with-alpha(Theme.disabled-container))
|
|
: (root.enabled ? Theme.surface-container-highest : Theme.surface-container-highest.with-alpha(Theme.disabled-container));
|
|
border-width: root.checked ? 0 : 2px;
|
|
border-color: root.enabled ? Theme.outline : Theme.on-surface.with-alpha(Theme.disabled-container);
|
|
// The thumb grows instead of the whole switch squeezing.
|
|
squeeze: 1;
|
|
fill-tint: false;
|
|
animate background, border-color {
|
|
duration: Motion.time(Motion.short4);
|
|
easing: Motion.standard;
|
|
}
|
|
animate thumb-x {
|
|
duration: Motion.time(Motion.short4);
|
|
easing: Motion.emphasized;
|
|
}
|
|
animate thumb-size {
|
|
duration: Motion.time(root.pressed ? Motion.short2 : Motion.short3);
|
|
easing: Motion.standard;
|
|
}
|
|
|
|
activated => {
|
|
root.checked = !root.checked;
|
|
}
|
|
|
|
accessible-role: switch;
|
|
accessible-enabled: root.enabled;
|
|
accessible-checkable: true;
|
|
accessible-checked: root.checked;
|
|
accessible-action-default => {
|
|
self.activate();
|
|
}
|
|
|
|
Rectangle {
|
|
x: root.thumb-x - self.width / 2;
|
|
y: (root.height - self.height) / 2;
|
|
width: root.halo-size;
|
|
height: root.halo-size;
|
|
border-radius: Theme.corner-full(self.height);
|
|
background: (root.checked ? Theme.primary : Theme.on-surface).with-alpha(root.tint-opacity);
|
|
}
|
|
|
|
Rectangle {
|
|
x: root.thumb-x - self.width / 2;
|
|
y: (root.height - self.height) / 2;
|
|
width: root.thumb-size;
|
|
height: root.thumb-size;
|
|
border-radius: Theme.corner-full(self.height);
|
|
background: !root.enabled
|
|
? (root.checked ? Theme.surface : Theme.on-surface.with-alpha(Theme.disabled-content))
|
|
: root.checked ? (root.hovered || root.pressed ? Theme.primary-container : Theme.on-primary)
|
|
: (root.hovered || root.pressed ? Theme.on-surface-variant : Theme.outline);
|
|
animate background {
|
|
duration: Motion.time(Motion.short4);
|
|
easing: Motion.standard;
|
|
}
|
|
}
|
|
}
|