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

Add on_release message to Slider #378

Merged
merged 5 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion examples/integration/src/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Controls {
sliders: [slider::State; 3],
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Message {
BackgroundColorChanged(Color),
}
Expand Down
21 changes: 20 additions & 1 deletion native/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct Slider<'a, Message, Renderer: self::Renderer> {
range: RangeInclusive<f32>,
value: f32,
on_change: Box<dyn Fn(f32) -> Message>,
on_release: Option<Message>,
width: Length,
style: Renderer::Style,
}
Expand Down Expand Up @@ -71,11 +72,25 @@ impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> {
value: value.max(*range.start()).min(*range.end()),
range,
on_change: Box::new(on_change),
on_release: None,
width: Length::Fill,
style: Renderer::Style::default(),
}
}

/// Sets the release message of the [`Slider`].
/// This is called when the mouse is released from the slider.
///
/// Typically, the user's interaction with the slider is finished when this message is produced.
/// This is useful if you need to spawn a long-running task from the slider's result, where
/// the default on_change message could create too many events.
///
/// [`Slider`]: struct.Slider.html
pub fn on_release(mut self, on_release: Message) -> Self {
self.on_release = Some(on_release);
self
}

/// Sets the width of the [`Slider`].
///
/// [`Slider`]: struct.Slider.html
Expand Down Expand Up @@ -114,6 +129,7 @@ impl<'a, Message, Renderer> Widget<Message, Renderer>
for Slider<'a, Message, Renderer>
where
Renderer: self::Renderer,
Message: Clone,
{
fn width(&self) -> Length {
self.width
Expand Down Expand Up @@ -171,6 +187,9 @@ where
}
}
mouse::Event::ButtonReleased(mouse::Button::Left) => {
Masterchef365 marked this conversation as resolved.
Show resolved Hide resolved
if let Some(on_release) = self.on_release.clone() {
messages.push(on_release);
}
self.state.is_dragging = false;
}
mouse::Event::CursorMoved { .. } => {
Expand Down Expand Up @@ -252,7 +271,7 @@ impl<'a, Message, Renderer> From<Slider<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: 'a + self::Renderer,
Message: 'a,
Message: 'a + Clone,
{
fn from(
slider: Slider<'a, Message, Renderer>,
Expand Down