import { Nine } from "base.slint";
import { Pieces } from "pieces.slint";
export component HorizontalScrollbar {
out property <bool> dragging: touch.pressed;
in property <length> thumb-position;
in property <length> thumb-width: 100px;
in property <string> state: "auto";
callback moved(length);
callback scroll(length, length);
preferred-height: 12px;
Nine { height: root.height; width: root.width; source: Pieces.horizontal-scrollbar-default-background-base; }
property <string> visual: root.state != "auto" ? root.state : touch.pressed ? "clicked" : touch.has-hover ? "hovered" : "default";
Image { y: 1px; x: root.thumb-position; height: root.height - 2px; width: 4px; source: root.visual == "hovered" ? Pieces.horizontal-scrollbar-thumb-hovered-base-left : Pieces.horizontal-scrollbar-thumb-default-base-left; image-fit: fill; image-rendering: pixelated; }
Image { y: 1px; x: root.thumb-position + 4px; height: root.height - 2px; width: root.thumb-width - 8px; source: root.visual == "hovered" ? Pieces.horizontal-scrollbar-thumb-hovered-base-tile : Pieces.horizontal-scrollbar-thumb-default-base-tile; image-fit: fill; horizontal-tiling: repeat; horizontal-alignment: left; image-rendering: pixelated; }
Image { y: 1px; x: root.thumb-position + root.thumb-width - 4px; height: root.height - 2px; width: 4px; source: root.visual == "hovered" ? Pieces.horizontal-scrollbar-thumb-hovered-base-right : Pieces.horizontal-scrollbar-thumb-default-base-right; image-fit: fill; image-rendering: pixelated; }
if root.visual == "hovered": Nine { y: -3px; x: root.thumb-position - 4px; height: root.height + 6px; width: root.thumb-width + 8px; source: Pieces.horizontal-scrollbar-thumb-hovered-glow; }
// Where inside the thumb the pointer took hold; a press on the bare track grabs the thumb by its centre (it jumps there).
property <length> grab;
property <length> last-pointer;
property <length> last-width;
property <length> last-thumb-width;
// The call sites place the thumb centre at `x`, so hand them the centre of the clamped wanted position.
function drag(pointer: length) {
if (root.width != root.last-width || root.thumb-width != root.last-thumb-width) {
root.grab = root.last-pointer - root.thumb-position;
}
root.last-width = root.width;
root.last-thumb-width = root.thumb-width;
root.last-pointer = pointer;
let top = clamp(pointer - root.grab, 0px, max(0px, root.width - root.thumb-width));
// Discard overshoot at either end, so reversing direction moves the thumb immediately.
root.grab = pointer - top;
if (root.width > root.thumb-width) { root.moved(top + root.thumb-width / 2); }
}
touch := TouchArea {
scroll-event(event) => { root.scroll(event.delta-x, event.delta-y); return accept; }
pointer-event(e) => {
if (e.kind == PointerEventKind.down && e.button == PointerEventButton.left) {
root.grab = self.mouse-x >= root.thumb-position && self.mouse-x <= root.thumb-position + root.thumb-width ? self.mouse-x - root.thumb-position : root.thumb-width / 2;
root.last-pointer = self.mouse-x;
root.last-width = root.width;
root.last-thumb-width = root.thumb-width;
root.drag(self.mouse-x);
}
}
moved => { if (self.pressed) { root.drag(self.mouse-x); } }
}
}