diff options
author | 2020-07-08 06:55:22 +0200 | |
---|---|---|
committer | 2020-07-08 11:29:21 +0200 | |
commit | 1c12bad866d06b320f16609576d5937413418a0c (patch) | |
tree | c31255d7907477146b379d883cbce1b5d0dd735e /native/src/widget/combo_box.rs | |
parent | 7a105ade27c7f33397e3050280e9faef928bdc1b (diff) | |
download | iced-1c12bad866d06b320f16609576d5937413418a0c.tar.gz iced-1c12bad866d06b320f16609576d5937413418a0c.tar.bz2 iced-1c12bad866d06b320f16609576d5937413418a0c.zip |
Split `Menu::new` into multiple builder methods
Diffstat (limited to 'native/src/widget/combo_box.rs')
-rw-r--r-- | native/src/widget/combo_box.rs | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/native/src/widget/combo_box.rs b/native/src/widget/combo_box.rs index f2dc86e8..f6da076a 100644 --- a/native/src/widget/combo_box.rs +++ b/native/src/widget/combo_box.rs @@ -10,7 +10,8 @@ pub struct ComboBox<'a, T, Message, Renderer: self::Renderer> where [T]: ToOwned<Owned = Vec<T>>, { - internal: Internal<'a, T, Message>, + menu: &'a mut menu::State, + on_selected: Box<dyn Fn(T) -> Message>, options: Cow<'a, [T]>, selected: Option<T>, width: Length, @@ -24,11 +25,6 @@ pub struct State { menu: menu::State, } -pub struct Internal<'a, T, Message> { - menu: &'a mut menu::State, - on_selected: Box<dyn Fn(T) -> Message>, -} - impl<'a, T: 'a, Message, Renderer: self::Renderer> ComboBox<'a, T, Message, Renderer> where @@ -42,10 +38,8 @@ where on_selected: impl Fn(T) -> Message + 'static, ) -> Self { Self { - internal: Internal { - menu: &mut state.menu, - on_selected: Box::new(on_selected), - }, + menu: &mut state.menu, + on_selected: Box::new(on_selected), options: options.into(), selected, width: Length::Shrink, @@ -183,7 +177,7 @@ where if layout.bounds().contains(cursor_position) { let selected = self.selected.as_ref(); - self.internal.menu.open( + self.menu.open( self.options .iter() .position(|option| Some(option) == selected), @@ -216,22 +210,20 @@ where &mut self, layout: Layout<'_>, ) -> Option<Overlay<'_, Message, Renderer>> { - if self.internal.menu.is_open() { + if self.menu.is_open() { let bounds = layout.bounds(); - Some(Overlay::new( - layout.position(), - Box::new(Menu::new( - self.internal.menu, - &self.options, - &self.internal.on_selected, - bounds.width.round() as u16, - bounds.height, - self.text_size, - self.padding, - Renderer::menu_style(&self.style), - )), - )) + let mut menu = + Menu::new(&mut self.menu, &self.options, &self.on_selected) + .width(bounds.width.round() as u16) + .padding(self.padding) + .style(Renderer::menu_style(&self.style)); + + if let Some(text_size) = self.text_size { + menu = menu.text_size(text_size); + } + + Some(menu.overlay(layout.position(), bounds.height)) } else { None } |