Skip to content

add scrolling to pick_lists #872

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 6 commits into from
Jul 22, 2021
Merged
Changes from 3 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
59 changes: 59 additions & 0 deletions native/src/widget/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ where
on_selected: Box<dyn Fn(T) -> Message>,
options: Cow<'a, [T]>,
selected: Option<T>,
on_change: Option<Message>,
width: Length,
padding: u16,
text_size: Option<u16>,
Expand Down Expand Up @@ -82,6 +83,7 @@ where
on_selected: Box::new(on_selected),
options: options.into(),
selected,
on_change: None,
width: Length::Shrink,
text_size: None,
padding: Renderer::DEFAULT_PADDING,
Expand Down Expand Up @@ -114,6 +116,12 @@ where
self
}

/// Sets the message sent when [`PickList`] selection changes
pub fn on_change(mut self, msg: Message) -> Self {
self.on_change = Some(msg);
self
}

/// Sets the style of the [`PickList`].
pub fn style(
mut self,
Expand Down Expand Up @@ -247,6 +255,57 @@ where
event_status
}
}
Event::Mouse(mouse::Event::WheelScrolled { delta })
if layout.bounds().contains(cursor_position)
&& !*self.is_open =>
{
let y = match delta {
mouse::ScrollDelta::Lines { y, .. }
| mouse::ScrollDelta::Pixels { y, .. } => y,
};

if y.is_sign_negative() {
let mut options_iter = self.options.iter();
if let Some(selected) = self.selected.as_ref() {
if let Some(_) =
options_iter.position(|o| o == selected)
{
if let Some(prev_val) = options_iter.next() {
messages
.push((self.on_selected)(prev_val.clone()));
}
}
} else {
messages
.push((self.on_selected)(self.options[0].clone()));
if let Some(msg) = self.on_change.take() {
messages.push(msg)
}
}
} else {
let mut options_iter = self.options.iter().rev();
if let Some(selected) = self.selected.as_ref() {
if let Some(_) =
options_iter.position(|o| o == selected)
{
if let Some(next_val) = options_iter.next() {
messages
.push((self.on_selected)(next_val.clone()));
}
}
} else {
messages.push((self.on_selected)(
self.options[self.options.len() - 1].clone(),
));
if let Some(msg) = self.on_change.take() {
messages.push(msg)
}
}
}

return event::Status::Captured;
}

_ => event::Status::Ignored,
}
}
Expand Down