import { UiSounds, UiCue } from "audio.slint";
import { Nine, Label, Theme } from "base.slint";
import { Pieces } from "pieces.slint";
// style.lua `button` / `green_button` / `red_button` (l.1015, 1078, 1270): a 28-px high nine-slice with a 3-4 px
// dirt shadow outside, a glow on hover and the label one px lower while pressed.
// kind: "grey" | "green" | "red". state: "auto" follows the pointer; "default" | "hovered" | "clicked" | "disabled" pin it.
export component Button {
in property <string> text;
in property <string> kind: "grey";
in property <UiCue> sound: root.kind == "green" ? UiCue.confirm : root.kind == "red" ? UiCue.red : UiCue.click;
in property <bool> focused: false;
in property <bool> enabled: true;
// Row surfaces change colour; an opaque subheader precomposition leaves a dark frame.
in property <bool> transparent-background;
in property <string> state: "auto";
callback clicked();
forward-focus: key;
public function activate() {
if (root.enabled) { UiSounds.begin(); root.clicked(); UiSounds.finish(root.sound); }
}
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;
min-width: 108px;
min-height: 28px;
preferred-width: max(self.min-width, label.preferred-width + 16px);
preferred-height: 28px;
property <string> visual: root.state != "auto" ? root.state
: !root.enabled ? "disabled" : touch.pressed ? "clicked" : (root.focused || touch.has-hover) ? "hovered" : "default";
pure function shadow(k: string, s: string) -> image {
if (k == "green") { return s == "hovered" ? Pieces.green-button-hovered-shadow : s == "clicked" ? Pieces.green-button-clicked-shadow : s == "disabled" ? Pieces.green-button-disabled-shadow : Pieces.green-button-default-shadow; }
if (k == "red") { return s == "hovered" ? Pieces.red-button-hovered-shadow : s == "clicked" ? Pieces.red-button-clicked-shadow : s == "disabled" ? Pieces.red-button-disabled-shadow : Pieces.red-button-default-shadow; }
return s == "hovered" ? Pieces.button-hovered-shadow : s == "clicked" ? Pieces.button-clicked-shadow : s == "disabled" ? Pieces.button-disabled-shadow : Pieces.button-default-shadow;
}
pure function face(k: string, s: string) -> image {
if (k == "green") { return s == "hovered" ? Pieces.green-button-hovered-base : s == "clicked" ? Pieces.green-button-clicked-base : s == "disabled" ? Pieces.green-button-disabled-base : Pieces.green-button-default-base; }
if (k == "red") { return s == "hovered" ? Pieces.red-button-hovered-base : s == "clicked" ? Pieces.red-button-clicked-base : s == "disabled" ? Pieces.red-button-disabled-base : Pieces.red-button-default-base; }
return s == "hovered" ? Pieces.button-hovered-base : s == "clicked" ? Pieces.button-clicked-base : s == "disabled" ? Pieces.button-disabled-base : Pieces.button-default-base;
}
pure function glow(k: string) -> image {
return k == "green" ? Pieces.green-button-hovered-glow : k == "red" ? Pieces.red-button-hovered-glow : Pieces.button-hovered-glow;
}
if root.transparent-background || !(root.visual == "hovered" && root.kind != "grey"): Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.shadow(root.kind, root.visual); }
if root.transparent-background || !(root.visual == "hovered" && root.kind != "grey"): Nine { width: root.width; height: root.height; source: root.face(root.kind, root.visual); }
if root.visual == "hovered" && (root.kind == "grey" || root.transparent-background): Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.glow(root.kind); }
if root.visual == "hovered" && root.kind != "grey" && !root.transparent-background: Nine { x: -4px; y: -4px; width: root.width + 8px; height: root.height + 8px; source: root.kind == "green" ? Pieces.green-button-hovered-precomposed : Pieces.red-button-hovered-precomposed; }
label := Label {
x: 0px; y: root.visual == "clicked" ? 1px : 0px;
width: root.width; height: root.height;
horizontal-alignment: center; vertical-alignment: center;
text: root.text;
font-weight: 600;
color: root.visual == "disabled" ? Theme.disabled-text : Theme.black;
}
touch := TouchArea {
enabled: root.enabled;
clicked => { key.focus(); root.activate(); }
}
}