diff options
author | 2023-07-26 22:45:56 +0200 | |
---|---|---|
committer | 2023-07-26 22:45:56 +0200 | |
commit | a0a3cf7eb71ca9a934a60144b92c555cf7efe69a (patch) | |
tree | 3657e6311e60e3fdbf530f1117be7e42d2cf6b16 /widget/src/overlay/menu.rs | |
parent | 4cf1b4fd1c8f45fe50ff254decd91f0d589f70ff (diff) | |
parent | 559ebdbb3a0b90ff28f361f5466f17178b6d8137 (diff) | |
download | iced-a0a3cf7eb71ca9a934a60144b92c555cf7efe69a.tar.gz iced-a0a3cf7eb71ca9a934a60144b92c555cf7efe69a.tar.bz2 iced-a0a3cf7eb71ca9a934a60144b92c555cf7efe69a.zip |
Merge pull request #1954 from jhff/widget/combobox
Add `ComboBox` widget
Diffstat (limited to 'widget/src/overlay/menu.rs')
-rw-r--r-- | widget/src/overlay/menu.rs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index 72662422..f7bdeef6 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -28,6 +28,7 @@ where options: &'a [T], hovered_option: &'a mut Option<usize>, on_selected: Box<dyn FnMut(T) -> Message + 'a>, + on_option_hovered: Option<&'a dyn Fn(T) -> Message>, width: f32, padding: Padding, text_size: Option<f32>, @@ -52,12 +53,14 @@ where options: &'a [T], hovered_option: &'a mut Option<usize>, on_selected: impl FnMut(T) -> Message + 'a, + on_option_hovered: Option<&'a dyn Fn(T) -> Message>, ) -> Self { Menu { state, options, hovered_option, on_selected: Box::new(on_selected), + on_option_hovered, width: 0.0, padding: Padding::ZERO, text_size: None, @@ -187,6 +190,7 @@ where options, hovered_option, on_selected, + on_option_hovered, width, padding, font, @@ -200,6 +204,7 @@ where options, hovered_option, on_selected, + on_option_hovered, font, text_size, text_line_height, @@ -321,6 +326,7 @@ where options: &'a [T], hovered_option: &'a mut Option<usize>, on_selected: Box<dyn FnMut(T) -> Message + 'a>, + on_option_hovered: Option<&'a dyn Fn(T) -> Message>, padding: Padding, text_size: Option<f32>, text_line_height: text::LineHeight, @@ -405,8 +411,21 @@ where self.text_line_height.to_absolute(Pixels(text_size)), ) + self.padding.vertical(); - *self.hovered_option = - Some((cursor_position.y / option_height) as usize); + let new_hovered_option = + (cursor_position.y / option_height) as usize; + + if let Some(on_option_hovered) = self.on_option_hovered { + if *self.hovered_option != Some(new_hovered_option) { + if let Some(option) = + self.options.get(new_hovered_option) + { + shell + .publish(on_option_hovered(option.clone())); + } + } + } + + *self.hovered_option = Some(new_hovered_option); } } Event::Touch(touch::Event::FingerPressed { .. }) => { |