import { Nine } from "base.slint";
import { Pieces } from "pieces.slint";
export component Scrollbar {
    in property <length> thumb-position;
    in property <length> thumb-height: 100px;
    in property <string> state: "auto";
    callback moved(length);
    callback scroll(length);
    preferred-width: 12px;
    Nine { width: root.width; height: root.height; source: Pieces.vertical-scrollbar-default-background-base; }
    property <string> visual: root.state != "auto" ? root.state : touch.pressed ? "clicked" : touch.has-hover ? "hovered" : "default";
    Image { x: 1px; y: root.thumb-position; width: root.width - 2px; height: 4px; source: root.visual == "hovered" ? Pieces.vertical-scrollbar-thumb-hovered-base-top : Pieces.vertical-scrollbar-thumb-default-base-top; image-fit: fill; image-rendering: pixelated; }
    Image { x: 1px; y: root.thumb-position + 4px; width: root.width - 2px; height: root.thumb-height - 8px; source: root.visual == "hovered" ? Pieces.vertical-scrollbar-thumb-hovered-base-tile : Pieces.vertical-scrollbar-thumb-default-base-tile; image-fit: fill; vertical-tiling: repeat; vertical-alignment: top; image-rendering: pixelated; }
    Image { x: 1px; y: root.thumb-position + root.thumb-height - 4px; width: root.width - 2px; height: 4px; source: root.visual == "hovered" ? Pieces.vertical-scrollbar-thumb-hovered-base-bottom : Pieces.vertical-scrollbar-thumb-default-base-bottom; image-fit: fill; image-rendering: pixelated; }
    if root.visual == "hovered": Nine { x: -3px; y: root.thumb-position - 4px; width: root.width + 6px; height: root.thumb-height + 8px; source: Pieces.vertical-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-height;
    property <length> last-thumb-height;
    // The call sites place the thumb centre at `y`, so hand them the centre of the clamped wanted position.
    function drag(pointer: length) {
        if (root.height != root.last-height || root.thumb-height != root.last-thumb-height) {
            root.grab = root.last-pointer - root.thumb-position;
        }
        root.last-height = root.height;
        root.last-thumb-height = root.thumb-height;
        root.last-pointer = pointer;
        let top = clamp(pointer - root.grab, 0px, max(0px, root.height - root.thumb-height));
        // Discard overshoot at either end, so reversing direction moves the thumb immediately.
        root.grab = pointer - top;
        if (root.height > root.thumb-height) { root.moved(top + root.thumb-height / 2); }
    }
    touch := TouchArea {
        // This is a sibling of the viewport, so wheel events cannot bubble to it.
        scroll-event(event) => { root.scroll(event.delta-y); return accept; }
        pointer-event(e) => {
            if (e.kind == PointerEventKind.down && e.button == PointerEventButton.left) {
                root.grab = self.mouse-y >= root.thumb-position && self.mouse-y <= root.thumb-position + root.thumb-height ? self.mouse-y - root.thumb-position : root.thumb-height / 2;
                root.last-pointer = self.mouse-y;
                root.last-height = root.height;
                root.last-thumb-height = root.thumb-height;
                root.drag(self.mouse-y);
            }
        }
        moved => { if (self.pressed) { root.drag(self.mouse-y); } }
    }
}
