Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inverted slider 2025 #7389

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Slider {
### maximum
<SlintProperty propName="maximum" typeName="float" defaultValue="100">
The maximum value.
If the maximum value is lower than the minimum, the slider is inverted.
```slint "maximum: 10;"
Slider {
maximum: 10;
Expand Down
55 changes: 47 additions & 8 deletions examples/gallery/ui/pages/controls_page.slint
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,54 @@ export component ControlsPage inherits Page {
}

GroupBox {
title: @tr("Slider");
title: @tr("Sliders");
vertical-stretch: 0;

slider := Slider {
min-width: 160px;
minimum: -100;
maximum: 100;
value: 42;
enabled: GallerySettings.widgets-enabled;
HorizontalLayout{
VerticalLayout {
Text { text: "Slider"; }
slider := Slider {
min-width: 160px;
minimum: -100;
maximum: 100;
value: 42;
enabled: GallerySettings.widgets-enabled;
}
Text { text: "Value: \{slider.value}"; }

Text { text: "Inverted Slider"; }
slider-inv := Slider {
min-width: 160px;
minimum: 100;
maximum: -100;
value: 42;
enabled: GallerySettings.widgets-enabled;
}
Text { text: "Value: \{slider-inv.value}"; }
}
VerticalLayout {
Text { text: "Vertical slider"; }
v-slider := Slider {
min-height: 160px;
minimum: -100;
maximum: 100;
value: 42;
orientation: Orientation.vertical;
enabled: GallerySettings.widgets-enabled;
}
Text { text: "Value: \{v-slider.value}"; }
}
VerticalLayout {
Text { text: "Inverted vertical slider"; }
v-slider-inv := Slider {
min-height: 160px;
minimum: 100;
maximum: -100;
value: 42;
orientation: Orientation.vertical;
enabled: GallerySettings.widgets-enabled;
}
Text { text: "Value: \{v-slider-inv.value}"; }
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/backends/qt/qt_widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ impl Item for NativeSlider {

impl NativeSlider {
fn set_value(self: Pin<&Self>, new_val: f32) {
let new_val = new_val.max(self.minimum()).min(self.maximum());
let new_val =
new_val.max(self.minimum().min(self.maximum())).min(self.maximum().max(self.minimum()));
self.value.set(new_val);
Self::FIELD_OFFSETS.changed.apply_pin(self).call(&(new_val,));
}
Expand Down
14 changes: 7 additions & 7 deletions internal/compiler/widgets/common/slider-base.slint
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export component SliderBase {


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);
root.set-value( (!vertical ? root.size-to-value(touch-area.mouse-x + ((touch-area.mouse-x/root.width - 0.5)*root.ref-size ), root.width) :
root.size-to-value(touch-area.mouse-y + ((touch-area.mouse-y/root.height - 0.5)*root.ref-size ), root.height))
);
}

self.pressed-value = value;
Expand All @@ -61,9 +62,8 @@ export component SliderBase {
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))
root.set-value( (!vertical ? root.size-to-value(touch-area.mouse-x + ((touch-area.mouse-x/root.width - 0.5)*root.ref-size ), root.width) :
root.size-to-value(touch-area.mouse-y + ((touch-area.mouse-y/root.height - 0.5)*root.ref-size ), root.height))
);
}
}
Expand Down Expand Up @@ -104,15 +104,15 @@ export component SliderBase {
}

pure function size-to-value(size: length, range: length) -> float {
size * (root.maximum - root.minimum) / range;
size / range * (root.maximum - root.minimum) + root.minimum
}

function set-value(value: float) {
if (root.value == value) {
return;
}

root.value = max(root.minimum, min(root.maximum, value));
root.value = root.maximum>root.minimum ? max(root.minimum, min(root.maximum, value)) : max(root.maximum, min(root.minimum, value));
root.changed(root.value);
}
}
10 changes: 6 additions & 4 deletions internal/compiler/widgets/cosmic/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ export component Slider {
}

track := Rectangle {
x: base.vertical ? (parent.width - self.width) / 2 : 0;
y: base.vertical ? 0 : (parent.height - self.height) / 2;
width: base.vertical ? rail.width : thumb.x + (thumb.width / 2);
height: base.vertical ? thumb.y + (thumb.height / 2) : rail.height;
x: base.vertical ? (parent.width - self.width) / 2 :
root.maximum>root.minimum ? 0 : thumb.x + (thumb.width / 2);
y: base.vertical ? root.maximum>root.minimum ? 0 : thumb.y + (thumb.height / 2) : (parent.height - self.height) / 2;
width: base.vertical ? rail.width :
root.maximum>root.minimum ? thumb.x + (thumb.width / 2) : parent.width - thumb.x - (thumb.width / 2);
height: base.vertical ? root.maximum>root.minimum ? thumb.y + (thumb.height / 2) : parent.height - thumb.y - (thumb.height / 2) : rail.height;
background: CosmicPalette.secondary-accent-background;
border-radius: rail.border-radius;
}
Expand Down
10 changes: 6 additions & 4 deletions internal/compiler/widgets/cupertino/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ export component Slider {
}

track := Rectangle {
x: base.vertical ? (parent.width - self.width) / 2 : 0;
y: base.vertical ? 0 : (parent.height - self.height) / 2;
width: base.vertical ? rail.width : thumb.x + (thumb.width / 2);
height: base.vertical ? thumb.y + (thumb.height / 2) : rail.height;
x: base.vertical ? (parent.width - self.width) / 2 :
root.maximum>root.minimum ? 0 : thumb.x + (thumb.width / 2);
y: base.vertical ? root.maximum>root.minimum ? 0 : thumb.y + (thumb.height / 2) : (parent.height - self.height) / 2;
width: base.vertical ? rail.width :
root.maximum>root.minimum ? thumb.x + (thumb.width / 2) : parent.width - thumb.x - (thumb.width / 2);
height: base.vertical ? root.maximum>root.minimum ? thumb.y + (thumb.height / 2) : parent.height - thumb.y - (thumb.height / 2) : rail.height;
background: CupertinoPalette.accent-background;
border-radius: rail.border-radius;
}
Expand Down
10 changes: 6 additions & 4 deletions internal/compiler/widgets/fluent/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export component Slider {
}

track := Rectangle {
x: base.vertical ? (parent.width - self.width) / 2 : 0;
y: base.vertical ? 0 : (parent.height - self.height) / 2;
width: base.vertical ? rail.width : thumb.x + (thumb.width / 2);
height: base.vertical ? thumb.y + (thumb.height / 2) : rail.height;
x: base.vertical ? (parent.width - self.width) / 2 :
root.maximum>root.minimum ? 0 : thumb.x + (thumb.width / 2);
y: base.vertical ? root.maximum>root.minimum ? 0 : thumb.y + (thumb.height / 2) : (parent.height - self.height) / 2;
width: base.vertical ? rail.width :
root.maximum>root.minimum ? thumb.x + (thumb.width / 2) : parent.width - thumb.x - (thumb.width / 2);
height: base.vertical ? root.maximum>root.minimum ? thumb.y + (thumb.height / 2) : parent.height - thumb.y - (thumb.height / 2) : rail.height;
background: FluentPalette.accent-background;
border-radius: rail.border-radius;
}
Expand Down
11 changes: 7 additions & 4 deletions internal/compiler/widgets/material/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ export component Slider {

track := Rectangle {
background: MaterialPalette.accent-background;
x: base.vertical ? (parent.width - self.width) / 2 : background.x;
y: base.vertical ? background.y : (parent.height - self.height) / 2;
width: base.vertical? background.width : handle.x + (handle.width / 2);
height: base.vertical? handle.y + (handle.height / 2) : background.height;

x: base.vertical ? (parent.width - self.width) / 2 :
root.maximum>root.minimum ? background.x : handle.x + (handle.width / 2);
y: base.vertical ? root.maximum>root.minimum ? background.y : handle.y + (handle.height / 2) : (parent.height - self.height) / 2;
width: base.vertical ? background.width :
root.maximum>root.minimum ? handle.x + (handle.width / 2) : parent.width - handle.x - (handle.width / 2);
height: base.vertical ? root.maximum>root.minimum ? handle.y + (handle.height / 2) : parent.height - handle.y - (handle.height / 2) : background.height;
border-radius: background.border-radius;
}

Expand Down
Loading