import { UiSounds, UiCue } from "audio.slint";
import { Nine, Theme } from "base.slint";
import { Pieces } from "pieces.slint";
// style `checkbox` (l.7431): a 14x14 sprite (28 px atlas at 0.5) with the dirt shadow 4 px around it, a glow while
// hovered / pressed and a separate mark sprite (tick, or a bar for "mixed") on top.
// state: "auto" follows the pointer; "default" | "hovered" | "clicked" | "disabled" pin it.
// dirt: false inside table cells (the Manage table draws none).
export component Checkbox {
in property <bool> checked;
in property <bool> mixed;
in property <bool> enabled: true;
in property <bool> dirt: true;
in property <string> state: "auto";
callback toggled();
forward-focus: key;
public function activate() {
if (root.enabled) { UiSounds.begin(); root.toggled(); UiSounds.finish(UiCue.checkbox); }
}
key := FocusScope {
enabled: root.enabled; focus-on-click: false;
width: root.width; height: root.height;
key-pressed(event) => {
if ((event.text == " " || event.text == Key.Return) && root.enabled) {
root.activate(); return accept;
}
return reject;
}
}
out property <bool> hovered: touch.has-hover;
width: 14px;
height: 14px;
property <string> visual: root.state != "auto" ? root.state
: !root.enabled ? "disabled" : touch.pressed ? "clicked" : touch.has-hover ? "hovered" : "default";
pure function face(s: string, c: bool, m: bool) -> image {
if (m) { return s == "disabled" ? Pieces.checkbox-disabled-intermediate-base : s == "hovered" ? Pieces.checkbox-hovered-base : s == "clicked" ? Pieces.checkbox-clicked-base : Pieces.checkbox-intermediate-base; }
if (s == "disabled") { return c ? Pieces.checkbox-disabled-checked-base : Pieces.checkbox-disabled-base; }
if (s == "hovered") { return c ? Pieces.checkbox-hovered-checked-base : Pieces.checkbox-hovered-base; }
if (s == "clicked") { return c ? Pieces.checkbox-clicked-checked-base : Pieces.checkbox-clicked-base; }
return c ? Pieces.checkbox-checked-base : Pieces.checkbox-unchecked-base;
}
pure function shadow(s: string, c: bool, m: bool) -> image {
if (m) { return s == "disabled" ? Pieces.checkbox-disabled-intermediate-shadow : Pieces.checkbox-intermediate-shadow; }
if (s == "disabled") { return c ? Pieces.checkbox-disabled-checked-shadow : Pieces.checkbox-disabled-shadow; }
return c ? Pieces.checkbox-checked-shadow : Pieces.checkbox-unchecked-shadow;
}
pure function glow(s: string, c: bool) -> image {
if (s == "clicked") { return c ? Pieces.checkbox-clicked-checked-glow : Pieces.checkbox-clicked-glow; }
return c ? Pieces.checkbox-hovered-checked-glow : Pieces.checkbox-hovered-glow;
}
pure function mark(s: string, m: bool) -> image {
if (m) { return s == "disabled" ? Pieces.checkbox-disabled-intermediate-intermediate-mark : Pieces.checkbox-intermediate-intermediate-mark; }
if (s == "disabled") { return Pieces.checkbox-disabled-checked-checkmark; }
if (s == "hovered") { return Pieces.checkbox-hovered-checked-checkmark; }
if (s == "clicked") { return Pieces.checkbox-clicked-checked-checkmark; }
return Pieces.checkbox-checked-checkmark;
}
if root.dirt && (root.visual == "default" || root.visual == "disabled"): Nine { x: -4px; y: -4px; width: 22px; height: 22px; source: root.shadow(root.visual, root.checked, root.mixed); }
Image { width: 14px; height: 14px; source: root.face(root.visual, root.checked, root.mixed); image-rendering: pixelated; }
if root.visual == "hovered" || root.visual == "clicked": Nine { x: -4px; y: -4px; width: 22px; height: 22px; source: root.glow(root.visual, root.checked); }
if root.checked || root.mixed: Image { width: 14px; height: 14px; source: root.mark(root.visual, root.mixed); image-rendering: pixelated; }
touch := TouchArea { enabled: root.enabled; clicked => { key.focus(); root.activate(); } }
}