slint/internal/compiler/widgets/material-base/widget-slider.slint
2023-02-10 16:49:25 +01:00

119 lines
4 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
import { md } from "md.slint";
// Allows to select a value from a range of values.
export component Slider {
in property<float> maximum: 100;
in property<float> minimum: 0;
in-out property<float> value;
out property<bool> has-focus: fs.has-focus;
in property<bool> enabled <=> touch.enabled;
callback changed(float);
height: 20px;
accessible-role: slider;
accessible-value: root.value;
accessible-value-minimum: root.minimum;
accessible-value-maximum: root.maximum;
accessible-value-step: (root.maximum - root.minimum) / 100;
container := Rectangle {
background: md.sys.color.surface-variant;
opacity: 0.38;
y: (parent.height - self.height) / 2;
width: 100%;
height: 4px;
border-radius: 2px;
}
track := Rectangle {
background: md.sys.color.primary;
x: container.x;
y: (parent.height - self.height) / 2;
width: handle.x + (handle.width / 2);
height: container.height;
border-radius: container.border-radius;
}
state-layer := Rectangle {
opacity: 0;
background: md.sys.color.primary;
x: handle.x - (self.width - handle.width) / 2;
y: (parent.height - self.height) / 2;
width: 40px;
height: 40px;
border-radius: max(self.width, self.height) / 2;
animate opacity { duration: 250ms; easing: ease; }
}
handle := Rectangle {
background: md.sys.color.primary;
x: (parent.width - handle.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: (parent.height - self.height) / 2;
width: root.height;
height: root.height;
border-radius: max(self.width, self.height) / 2;
drop-shadow-color: md.sys.color.shadow;
drop-shadow-blur: md.sys.elevation.level1;
drop-shadow-offset-y: 1px;
animate drop-shadow-blur { duration: 250ms; easing: ease; }
}
touch := TouchArea {
property <float> pressed-value;
property <bool> handle-hover: self.has-hover && self.mouse-x >= handle.x && self.mouse-x <= handle.x + handle.width
&& self.mouse-y >= handle.y && self.mouse-y <= handle.y + handle.height;
pointer-event(event) => {
if (event.button == PointerEventButton.left && event.kind == PointerEventKind.down) {
self.pressed-value = root.value;
}
}
moved => {
if (self.enabled && self.pressed) {
root.value = max(root.minimum, min(root.maximum,
self.pressed-value + (touch.mouse-x - touch.pressed-x) * (root.maximum - root.minimum) / (root.width - handle.width)));
root.changed(root.value);
}
}
}
fs := FocusScope {
x:0;
width: 0px;
key-pressed(event) => {
if (self.enabled && event.text == Key.RightArrow) {
root.value = Math.min(root.value + 1, root.maximum);
accept
} else if (self.enabled && event.text == Key.LeftArrow) {
root.value = Math.max(root.value - 1, root.minimum);
accept
} else {
reject
}
}
}
states [
disabled when !root.enabled : {
handle.background: md.sys.color.on-surface;
handle.drop-shadow-blur: md.sys.elevation.level0;
track.background: md.sys.color.on-surface;
container.background: md.sys.color.on-surface;
root.opacity: 0.38;
}
pressed when (touch.pressed && touch.handle-hover) || fs.has-focus : {
state-layer.opacity: 0.12;
handle.drop-shadow-blur: md.sys.elevation.level0;
}
hover when touch.handle-hover : {
state-layer.background: md.sys.color.on-surface;
state-layer.opacity: 0.08;
}
]
}