import { UiSounds, UiCue } from "audio.slint";
import { Nine, Label, Theme } from "base.slint";
import { Pieces } from "pieces.slint";
export struct TabItem { text: string, badge: bool, enabled: bool }
// style `tab` (l.7637): bold 14, min width 84, 36 px high; the selected tab is 4 px taller so it covers the pane's top
// edge and merges into it (`increase_height_when_selected`). Badge = 11x12 pill with a "!" (`default_badge_graphical_set`).
// state: "auto" follows the pointer; "default" | "hovered" | "clicked" | "disabled" pin it.
export component Tab {
in property <string> text;
in property <bool> badge;
in property <bool> selected;
in property <bool> enabled: true;
// Unselected tabs next to another tab drop the glow on that side: the game draws only the selected tab's glow over its neighbours.
in property <bool> has-left;
in property <bool> has-right;
in property <string> state: "auto";
callback clicked();
forward-focus: key;
public function activate() {
if (root.enabled) { UiSounds.begin(); root.clicked(); UiSounds.finish(UiCue.tab); }
}
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;
}
}
min-width: 84px;
min-height: root.selected ? 40px : 36px;
max-height: root.selected ? 40px : 36px;
preferred-height: root.selected ? 40px : 36px;
preferred-width: max(84px, label.preferred-width + 16px + (root.badge ? 15px : 0px));
property <string> visual: root.state != "auto" ? root.state
: !root.enabled ? "disabled" : touch.pressed ? "clicked" : touch.has-hover ? "hovered" : "default";
property <image> glow-image: root.visual == "hovered" && !root.selected ? Pieces.tab-hovered-glow : root.shadow(root.selected, root.visual);
property <length> content-width: label.preferred-width + (root.badge ? 15px : 0px);
pure function shadow(sel: bool, s: string) -> image {
if (sel) { return s == "hovered" ? Pieces.tab-selected-hovered-shadow : s == "clicked" ? Pieces.tab-selected-clicked-shadow : Pieces.tab-selected-shadow; }
return s == "clicked" ? Pieces.tab-clicked-shadow : s == "disabled" ? Pieces.tab-disabled-shadow : Pieces.tab-default-shadow;
}
pure function face(sel: bool, s: string) -> image {
if (sel) { return s == "hovered" ? Pieces.tab-selected-hovered-base : s == "clicked" ? Pieces.tab-selected-clicked-base : Pieces.tab-selected-base; }
return s == "hovered" ? Pieces.tab-hovered-base : s == "clicked" ? Pieces.tab-clicked-base : s == "disabled" ? Pieces.tab-disabled-base : Pieces.tab-default-base;
}
pure function badge-face(sel: bool, s: string) -> image {
if (sel) { return s == "hovered" ? Pieces.tab-badge-selected-hovered-base : s == "clicked" ? Pieces.tab-badge-selected-clicked-base : Pieces.tab-badge-selected-base; }
return s == "hovered" ? Pieces.tab-badge-hovered-base : s == "clicked" ? Pieces.tab-badge-clicked-base : s == "disabled" ? Pieces.tab-badge-disabled-base : Pieces.tab-badge-default-base;
}
// Unequal tab_glow corners and straight bands share their outer edge.
Rectangle {
x: -4px; y: -4px; width: root.width + 8px; height: 4px; clip: true;
Nine { x: 0px; y: 0px; width: root.width + 8px; height: root.height + 4px; source: root.glow-image; }
}
Rectangle {
x: !root.selected && root.visual != "hovered" && root.has-left ? 0px : -4px;
y: 0px; height: 36px;
width: root.width + (!root.selected && root.visual != "hovered" && root.has-right ? 0px : 4px) - self.x;
clip: true;
Nine { x: -4px - parent.x; y: -4px; width: root.width + 8px; height: root.height + 4px; source: root.glow-image; }
}
Nine { width: root.width; height: root.height; source: root.face(root.selected, root.visual); }
label := Label {
x: (root.width - root.content-width) / 2; y: -1px; height: 36px;
vertical-alignment: center;
text: root.text;
font-weight: 700;
color: root.selected ? Theme.caption : root.visual == "disabled" ? #80808080 : Theme.black;
}
if root.badge: Rectangle {
x: label.x + label.preferred-width + 4px; y: 11px; width: 11px; height: 12px;
Nine { width: 11px; height: 12px; source: root.badge-face(root.selected, root.visual); }
Label { width: 11px; height: 12px; horizontal-alignment: center; vertical-alignment: center; text: "!"; font-size: 10px; font-weight: 700; color: root.selected ? #404040 : #8e8e8e; }
}
touch := TouchArea { enabled: root.enabled; clicked => { key.focus(); root.activate(); } }
}
// The tab strip: tabs side by side with no gap, selected one taller. TS owns `current`.
// The game paints the tabbed pane (and its shadow) over the unselected tabs and the selected tab over the pane: draw the strip
// twice, `part: "others"` below the pane, `part: "selected"` above it.
export component Tabs {
in property <bool> compact;
in property <[TabItem]> items;
in property <int> current;
// "all" | "others" (unselected only) | "selected" (only the selected tab); see above.
in property <string> part: "all";
callback selected(int);
HorizontalLayout {
alignment: start;
spacing: 0px;
for item[i] in root.items: Tab {
min-width: root.compact ? 76px : 84px;
text: item.text;
badge: item.badge;
enabled: item.enabled;
selected: i == root.current;
z: i == root.current ? 1 : 0;
visible: root.part == "all" || (root.part == "selected") == (i == root.current);
has-left: i > 0;
has-right: i < root.items.length - 1;
clicked => { root.selected(i); }
}
}
}