// Copyright © SixtyFPS GmbH // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial export component SliderBase { in property enabled <=> touch-area.enabled; in property minimum: 0; in property maximum: 100; in property orientation; in property handle-x; in property handle-y; in property handle-width; in property handle-height; out property pressed <=> touch-area.enabled; out property has-hover <=> touch-area.has-hover; out property vertical: root.orientation == Orientation.vertical; out property has-focus <=> focus-scope.has-focus; out property handle-has-hover: touch-area.mouse-x >= root.handle-x && touch-area.mouse-x <= root.handle-x + root.handle-width && touch-area.mouse-y >= root.handle-y && touch-area.mouse-y <= root.handle-y + root.handle-height; out property handle-pressed; in-out property value: 0; callback changed(/* value */ float); private property ref-size: !root.vertical ? root.handle-width : root.handle-height; forward-focus: focus-scope; touch-area := TouchArea { property pressed-value; width: 100%; height: 100%; pointer-event(event) => { if (event.button != PointerEventButton.left) { return; } if (event.kind == PointerEventKind.up) { root.handle-pressed = false; return; } if (!root.handle-has-hover) { root.set-value((!root.vertical ? root.size-to-value(touch-area.mouse-x, root.width) : root.size-to-value(touch-area.mouse-y, root.height)) + root.minimum); } self.pressed-value = value; root.handle-pressed = true; } moved => { if (!self.enabled) { return; } root.set-value(self.pressed-value + (!vertical ? root.size-to-value(touch-area.mouse-x - touch-area.pressed-x, root.width - root.ref-size) : root.size-to-value(touch-area.mouse-y - touch-area.pressed-y, root.height - root.ref-size)) ); } } focus-scope := FocusScope { x: 0; y: 0; width: 0; height: 0; enabled: root.enabled; key-pressed(event) => { if (!self.enabled) { return reject; } if ((!vertical && event.text == Key.RightArrow) || (vertical && event.text == Key.DownArrow)) { root.set-value(root.value + 1); return accept; } else if ((!vertical && event.text == Key.LeftArrow) || (vertical && event.text == Key.UpArrow)) { root.set-value(root.value - 1); return accept; } reject } } pure function size-to-value(size: length, range: length) -> float { size * (root.maximum - root.minimum) / range; } function set-value(value: float) { if (root.value == value) { return; } root.value = max(root.minimum, min(root.maximum, value)); root.changed(root.value); } }