diff options
author | 2021-07-22 21:15:35 +0700 | |
---|---|---|
committer | 2021-07-22 21:15:35 +0700 | |
commit | 6069e90c9b1b44c405706e08b608ae39dae0b1f4 (patch) | |
tree | 649f554abaa0ff95474beb659bb1b9a1e32b0b89 /native | |
parent | fbfb28b8d46a56de206cfd84c978f1902effc4f3 (diff) | |
download | iced-6069e90c9b1b44c405706e08b608ae39dae0b1f4.tar.gz iced-6069e90c9b1b44c405706e08b608ae39dae0b1f4.tar.bz2 iced-6069e90c9b1b44c405706e08b608ae39dae0b1f4.zip |
Abstract and improve scroll logic in `PickList`
Diffstat (limited to 'native')
-rw-r--r-- | native/src/widget/pick_list.rs | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 71c167a6..667c9f18 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -256,37 +256,33 @@ where | mouse::ScrollDelta::Pixels { y, .. } => y, }; - if y.is_sign_negative() { - let mut options_iter = self.options.iter(); + fn find_next<'a, T: PartialEq>( + selected: &'a T, + mut options: impl Iterator<Item = &'a T>, + ) -> Option<&'a T> { + let _ = options.find(|&option| option == selected); + + options.next() + } + + let next_option = if y < 0.0 { 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())); - } - } + find_next(selected, self.options.iter()) } else { - messages - .push((self.on_selected)(self.options[0].clone())); + self.options.first() } - } else { - let mut options_iter = self.options.iter().rev(); + } else if y > 0.0 { 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())); - } - } + find_next(selected, self.options.iter().rev()) } else { - messages.push((self.on_selected)( - self.options[self.options.len() - 1].clone(), - )); + self.options.last() } + } else { + None + }; + + if let Some(next_option) = next_option { + messages.push((self.on_selected)(next_option.clone())); } return event::Status::Captured; |