-
Notifications
You must be signed in to change notification settings - Fork 567
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
Make slider scrollable by mouse wheel #2104
Open
gordonshieh
wants to merge
9
commits into
linebender:master
Choose a base branch
from
gordonshieh:scrollable-slider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+53
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e31ad9f
Make slider scrollable by mouse wheel
gordonshieh 978f995
Use 10% steps based off the min and max range of slider
gordonshieh 0d715ed
Update CHANGELOG.md
gordonshieh 0521741
Fix missing mapping field
gordonshieh 5e99231
Don't move slider when mousedown and knob is active
gordonshieh 1dc943c
Move calculate_scroll_value to SliderValueMapping
gordonshieh 4366da1
Handle mouse wheel on RangeSlider
gordonshieh 60a7481
Properly bound mouse wheel event ranges
gordonshieh e61ab09
Don't infinite loop when the slider ranges are equal
gordonshieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,15 @@ impl Widget<f64> for Slider { | |
ctx.set_active(true); | ||
} | ||
} | ||
if let Event::Wheel(me) = event { | ||
if !self.knob.active { | ||
*data = (*data + self.mapping.calculate_scroll_value(me.wheel_delta.y)) | ||
.max(self.mapping.min) | ||
.min(self.mapping.max); | ||
ctx.request_paint(); | ||
ctx.set_handled(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -359,6 +368,26 @@ impl Widget<(f64, f64)> for RangeSlider { | |
ctx.request_paint(); | ||
} | ||
} | ||
if let Event::Wheel(me) = event { | ||
if !self.left_knob.is_active() && !self.right_knob.is_active() { | ||
let knob_size = env.get(theme::BASIC_WIDGET_HEIGHT); | ||
let press_value = | ||
self.mapping | ||
.calculate_value(me.pos, knob_size, ctx.size(), 0.0); | ||
|
||
if press_value - data.0 < data.1 - press_value { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to remember which knob was scrolled last |
||
data.0 = (data.0 + self.mapping.calculate_scroll_value(me.wheel_delta.y)) | ||
.min(data.1) | ||
.max(self.mapping.min); | ||
} else { | ||
data.1 = (data.1 + self.mapping.calculate_scroll_value(me.wheel_delta.y)) | ||
.min(self.mapping.max) | ||
.max(data.0); | ||
} | ||
ctx.request_paint(); | ||
ctx.set_handled(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -476,6 +505,9 @@ impl<T, W: Widget<T>> Annotated<T, W> { | |
self.labels.push(layout); | ||
|
||
walk += self.labeled_steps; | ||
if self.labeled_steps == 0.0 { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -567,6 +599,9 @@ impl<T: Data, W: Widget<T>> Widget<T> for Annotated<T, W> { | |
|
||
ctx.stroke(line, &text_color, 1.0); | ||
walk += self.unlabeled_steps; | ||
if self.labeled_steps == 0.0 { | ||
return; | ||
} | ||
} | ||
|
||
let mut walk = self.mapping.min; | ||
|
@@ -597,6 +632,9 @@ impl<T: Data, W: Widget<T>> Widget<T> for Annotated<T, W> { | |
); | ||
|
||
walk += self.labeled_steps; | ||
if self.labeled_steps == 0.0 { | ||
return; | ||
} | ||
} | ||
|
||
self.inner.paint(ctx, data, env); | ||
|
@@ -657,6 +695,20 @@ impl SliderValueMapping { | |
value | ||
} | ||
|
||
fn calculate_scroll_value(&self, mouse_delta: f64) -> f64 { | ||
let increment = if let Some(step) = self.step { | ||
step | ||
} else { | ||
(self.max - self.min) / 10.0 | ||
}; | ||
|
||
if mouse_delta < 0.0 { | ||
increment | ||
} else { | ||
-increment | ||
} | ||
} | ||
|
||
fn get_point(&self, value: f64, knob_size: f64, widget_size: Size) -> Point { | ||
let knob_major = | ||
(self.axis.major(widget_size) - knob_size) * self.normalize(value) + knob_size / 2.; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the links aren't automatically generated. Please manually define both
[#2104]
and[@gordonshieh]
at the bottom of the file.