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 #4705

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/src/language/widgets/slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- **`minimum`** (_in_ _float_): The minimum value (default: 0)
- **`maximum`** (_in_ _float_): The maximum value (default: 100)
- **`orientation`** (_in_ _enum [`Orientation`](../builtins/enums.md#orientation)_): If set to true the Slider is displayed vertical (default: horizontal).
- **`inverted`**: (_in_ _bool_): Defaults to false. If set to true, the slider changes it's direction.

### Callbacks

Expand Down
14 changes: 8 additions & 6 deletions internal/compiler/widgets/common/slider-base.slint
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export component SliderBase {
in property <float> minimum: 0;
in property <float> maximum: 100;
in property <Orientation> orientation;
in property <bool> inverted;
in property <length> handle-x;
in property <length> handle-y;
in property <length> handle-width;
in property <length> handle-height;
out property <bool> pressed <=> touch-area.enabled;
out property <bool> has-hover <=> touch-area.has-hover;
out property <bool> vertical: root.orientation == Orientation.vertical;

out property <bool> has-focus <=> focus-scope.has-focus;
out property <bool> 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;
Expand Down Expand Up @@ -43,8 +45,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 @@ -55,9 +58,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 @@ -87,7 +89,7 @@ export component SliderBase {
}

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

function set-value(value: float) {
Expand Down
10 changes: 8 additions & 2 deletions internal/compiler/widgets/cosmic-base/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SliderBase } from "../common/slider-base.slint";

export component Slider {
in property <Orientation> orientation <=> base.orientation;
in property <bool> inverted <=> base.inverted;
in property <float> maximum <=> base.maximum;
in property <float> minimum <=> base.minimum;
in property <bool> enabled <=> base.enabled;
Expand Down Expand Up @@ -49,8 +50,13 @@ export component Slider {
}

thumb := Rectangle {
x: base.vertical ? (parent.width - self.width) / 2 : (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: base.vertical ? (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum) : (parent.height - self.height) / 2;
x: base.vertical ? (parent.width - self.width) / 2
: inverted ? (parent.width - self.width) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: base.vertical ? (inverted ?
(parent.height - self.height) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum))
: (parent.height - self.height) / 2;
width: 20px;
height: self.width;
border-radius: 10px;
Expand Down
10 changes: 8 additions & 2 deletions internal/compiler/widgets/cupertino-base/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SliderBase } from "../common/slider-base.slint";

export component Slider {
in property <Orientation> orientation <=> i-base.orientation;
in property <bool> inverted <=> i-base.inverted;
in property <float> maximum <=> i-base.maximum;
in property <float> minimum <=> i-base.minimum;
in property <bool> enabled <=> i-base.enabled;
Expand Down Expand Up @@ -57,8 +58,13 @@ export component Slider {
}

i-thumb := Rectangle {
x: i-base.vertical ? (parent.width - self.width) / 2 : (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: i-base.vertical ? (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum) : (parent.height - self.height) / 2;
x: i-base.vertical ? (parent.width - self.width) / 2
: inverted ? (parent.width - self.width) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: i-base.vertical ? (inverted ?
(parent.height - self.height) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum))
: (parent.height - self.height) / 2;
width: 20px;
height: self.width;
border-radius: 10px;
Expand Down
12 changes: 10 additions & 2 deletions internal/compiler/widgets/fluent-base/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SliderBase } from "../common/slider-base.slint";

export component Slider {
in property <Orientation> orientation <=> i-base.orientation;
in property <bool> inverted <=> i-base.inverted;
in property <float> maximum <=> i-base.maximum;
in property <float> minimum <=> i-base.minimum;
in property <bool> enabled <=> i-base.enabled;
Expand Down Expand Up @@ -59,8 +60,15 @@ export component Slider {
}

i-thumb := Rectangle {
x: i-base.vertical ? (parent.width - self.width) / 2 : (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: i-base.vertical ? (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum) : (parent.height - self.height) / 2;
//x: i-base.vertical ? (parent.width - self.width) / 2 : (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
x: i-base.vertical ? (parent.width - self.width) / 2
: inverted ? (parent.width - self.width) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
//y: i-base.vertical ? (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum) : (parent.height - self.height) / 2;
y: i-base.vertical ? (inverted ?
(parent.height - self.height) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum))
: (parent.height - self.height) / 2;
width: 20px;
height: self.width;
border-radius: 10px;
Expand Down
10 changes: 8 additions & 2 deletions internal/compiler/widgets/material-base/slider.slint
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SliderBase } from "../common/slider-base.slint";
// Allows to select a value from a range of values.
export component Slider {
in property <Orientation> orientation <=> i-base.orientation;
in property <bool> inverted <=> i-base.inverted;
in property <float> maximum <=> i-base.maximum;
in property <bool> enabled <=> i-base.enabled;
in property <float> minimum <=> i-base.minimum;
Expand Down Expand Up @@ -76,8 +77,13 @@ export component Slider {

i-handle := Rectangle {
background: MaterialPalette.accent-background;
x: i-base.vertical ? (parent.width - self.width) / 2 : (parent.width - i-handle.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: i-base.vertical ? (parent.height - i-handle.height) * (root.value - root.minimum) / (root.maximum - root.minimum) : (parent.height - self.height) / 2;
x: i-base.vertical ? (parent.width - self.width) / 2
: inverted ? (parent.width - self.width) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.width - self.width) * (root.value - root.minimum) / (root.maximum - root.minimum);
y: i-base.vertical ? (inverted ?
(parent.height - self.height) * (root.maximum - root.value) / (root.maximum - root.minimum)
: (parent.height - self.height) * (root.value - root.minimum) / (root.maximum - root.minimum))
: (parent.height - self.height) / 2;
width: i-base.vertical ? root.width : root.height;
height: i-base.vertical ? root.width : root.height;
border-radius: max(self.width, self.height) / 2;
Expand Down
Loading