Skip to content

Don't rebroadcast redundant slider messages #1114

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

Merged
merged 3 commits into from
Nov 16, 2021
Merged
Changes from 2 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
24 changes: 17 additions & 7 deletions native/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ where
}

/// The local state of a [`Slider`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct State {
is_dragging: bool,
}
Expand Down Expand Up @@ -193,12 +193,14 @@ where
_clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> event::Status {
let is_dragging = self.state.is_dragging;

let mut change = || {
let bounds = layout.bounds();
if cursor_position.x <= bounds.x {
messages.push((self.on_change)(*self.range.start()));
let new_value = if cursor_position.x <= bounds.x {
*self.range.start()
} else if cursor_position.x >= bounds.x + bounds.width {
messages.push((self.on_change)(*self.range.end()));
*self.range.end()
} else {
let step = self.step.into();
let start = (*self.range.start()).into();
Expand All @@ -211,8 +213,16 @@ where
let value = steps * step + start;

if let Some(value) = T::from_f64(value) {
messages.push((self.on_change)(value));
value
} else {
return;
}
};

if (self.value.into() - new_value.into()).abs() > f64::EPSILON {
messages.push((self.on_change)(new_value));

self.value = new_value;
}
};

Expand All @@ -229,7 +239,7 @@ where
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| Event::Touch(touch::Event::FingerLifted { .. })
| Event::Touch(touch::Event::FingerLost { .. }) => {
if self.state.is_dragging {
if is_dragging {
if let Some(on_release) = self.on_release.clone() {
messages.push(on_release);
}
Expand All @@ -240,7 +250,7 @@ where
}
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => {
if self.state.is_dragging {
if is_dragging {
change();

return event::Status::Captured;
Expand Down