Skip to content

Commit

Permalink
Add on_release message to Slider (#378)
Browse files Browse the repository at this point in the history
* Add on_finish callback to Slider

* Fix formatting

* Rename Slider's on_finish to on_release, make the message simply an event without data

* Satisfy Clone impl requirement on Message in integration test

* Only call on_release after dragging a slider
  • Loading branch information
Masterchef365 authored Jun 8, 2020
1 parent 40750d9 commit 4960a88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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
25 changes: 23 additions & 2 deletions 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,7 +187,12 @@ where
}
}
mouse::Event::ButtonReleased(mouse::Button::Left) => {
self.state.is_dragging = false;
if self.state.is_dragging {
if let Some(on_release) = self.on_release.clone() {
messages.push(on_release);
}
self.state.is_dragging = false;
}
}
mouse::Event::CursorMoved { .. } => {
if self.state.is_dragging {
Expand Down Expand Up @@ -252,7 +273,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

0 comments on commit 4960a88

Please sign in to comment.