import { UiSounds, UiCue } from "audio.slint";
import { Nine, Label, Theme } from "base.slint";
import { Pieces } from "pieces.slint";
import { Button } from "button.slint";
import { Icons } from "icons.slint";
export component Dropdown {
in property <[string]> items;
in property <int> current;
in property <bool> open;
in property <string> state: "auto";
// Narrow hosts: shorten the text with an ellipsis instead of running into the arrow.
in property <bool> shorten;
// Capture hooks: draw the list open, and one item hovered / pressed, without a pointer.
in property <bool> force-open;
in property <int> force-hover: -1;
in property <int> force-pressed: -1;
callback toggled(); callback selected(int);
preferred-width: 150px; preferred-height: 28px;
Button { sound: UiCue.none; width: root.width; height: root.height; state: root.open || root.force-open ? "clicked" : root.state; clicked => {
UiSounds.finish(UiCue.click);
root.toggled();
if (root.open) { UiSounds.finish(UiCue.dropdown); }
} }
if !root.shorten: Label { x: 10px; y: -1px; height: root.height; text: root.items[root.current]; vertical-alignment: center; color: Theme.black; font-weight: 600; }
if root.shorten: Label { x: 10px; y: -1px; width: max(0px, root.width - 30px); height: root.height; text: root.items[root.current]; vertical-alignment: center; color: Theme.black; font-weight: 600; overflow: elide; }
Image { x: root.width - 24px; y: (root.height - 16px)/2 - 1px; width: 16px; height: 16px; source: Icons.dropdown; image-rendering: pixelated; }
if root.open || root.force-open: Rectangle {
x: 0px; y: root.height; width: root.width; height: root.items.length * 28px;
for item[i] in root.items: cell := Rectangle {
y: i * 28px; width: parent.width; height: 28px;
property <bool> current: i == root.current;
property <bool> dark: self.current || self.visual != "default";
property <string> visual: root.force-pressed == i || touch.pressed ? "clicked" : root.force-hover == i || touch.has-hover ? "hovered" : "default";
// list_box_item (style.lua): hovered orange, clicked / selected light; selected keeps its look while hovered or held.
Nine { width: parent.width; height: 28px; source: cell.visual == "clicked" ? (cell.current ? Pieces.list-box-item-selected-clicked-base : Pieces.list-box-item-clicked-base) : cell.visual == "hovered" ? (cell.current ? Pieces.list-box-item-selected-hovered-base : Pieces.list-box-item-hovered-base) : cell.current ? Pieces.list-box-item-selected-base : Pieces.list-box-item-default-base; }
if !root.shorten: Label { x: 12px; y: -1px; height: 28px; vertical-alignment: center; text: item; color: cell.dark ? Theme.black : Theme.text; }
if root.shorten: Label { x: 12px; y: -1px; width: max(0px, parent.width - 16px); height: 28px; vertical-alignment: center; text: item; color: cell.dark ? Theme.black : Theme.text; overflow: elide; }
touch := TouchArea { clicked => { UiSounds.begin(); root.selected(i); UiSounds.finish(UiCue.click); } }
}
}
}