From fdf5ac1a61016d5da45cde69bb33decfd98040fd Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 11 Mar 2025 15:32:36 -0400 Subject: [PATCH] refactor(dropdown): create dropdown with NewAppMessage for popup --- src/widget/dropdown/mod.rs | 18 +++++++------- src/widget/dropdown/widget.rs | 44 ++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/widget/dropdown/mod.rs b/src/widget/dropdown/mod.rs index 9857f2ae..846871af 100644 --- a/src/widget/dropdown/mod.rs +++ b/src/widget/dropdown/mod.rs @@ -27,7 +27,6 @@ pub fn dropdown< Dropdown::new(selections, selected, on_selected) } -#[cfg(all(feature = "winit", feature = "wayland"))] /// Displays a list of options in a popover menu on select. /// AppMessage must be the App's toplevel message. pub fn popup_dropdown< @@ -39,13 +38,14 @@ pub fn popup_dropdown< selections: &'a [S], selected: Option, on_selected: impl Fn(usize) -> Message + Send + Sync + 'static, - parent_id: window::Id, - on_surface_action: impl Fn(surface::Action) -> Message + Send + Sync + 'static, - map_action: impl Fn(Message) -> AppMessage + Send + Sync + 'static, + _parent_id: window::Id, + _on_surface_action: impl Fn(surface::Action) -> Message + Send + Sync + 'static, + _map_action: impl Fn(Message) -> AppMessage + Send + Sync + 'static, ) -> Dropdown<'a, S, Message, AppMessage> { - Dropdown::new(selections, selected, on_selected).with_popup( - parent_id, - on_surface_action, - map_action, - ) + let dropdown = Dropdown::new(selections, selected, on_selected); + + #[cfg(all(feature = "winit", feature = "wayland"))] + let dropdown = dropdown.with_popup(_parent_id, _on_surface_action, _map_action); + + dropdown } diff --git a/src/widget/dropdown/widget.rs b/src/widget/dropdown/widget.rs index 8b71004c..e4c36087 100644 --- a/src/widget/dropdown/widget.rs +++ b/src/widget/dropdown/widget.rs @@ -99,16 +99,43 @@ where #[cfg(all(feature = "winit", feature = "wayland"))] /// Handle dropdown requests for popup creation. /// Intended to be used with [`crate::app::message::get_popup`] - pub fn with_popup( + pub fn with_popup( mut self, parent_id: window::Id, on_surface_action: impl Fn(surface::Action) -> Message + Send + Sync + 'static, - action_map: impl Fn(Message) -> AppMessage + Send + Sync + 'static, - ) -> Self { - self.window_id = Some(parent_id); - self.on_surface_action = Some(Arc::new(on_surface_action)); - self.action_map = Some(Arc::new(action_map)); - self + action_map: impl Fn(Message) -> NewAppMessage + Send + Sync + 'static, + ) -> Dropdown<'a, S, Message, NewAppMessage> { + let Self { + on_selected, + selections, + icons, + selected, + width, + gap, + padding, + text_size, + text_line_height, + font, + positioner, + .. + } = self; + + Dropdown::<'a, S, Message, NewAppMessage> { + on_selected, + selections, + icons, + selected, + width, + gap, + padding, + text_size, + text_line_height, + font, + on_surface_action: Some(Arc::new(on_surface_action)), + action_map: Some(Arc::new(action_map)), + window_id: Some(parent_id), + positioner, + } } #[cfg(all(feature = "winit", feature = "wayland"))] @@ -525,7 +552,8 @@ pub fn update< positioner: SctkPositioner { size: Some((selections_width as u32 + gap as u32 + pad_width as u32 + icon_width as u32, 10)), anchor_rect, - anchor: cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Anchor::Top, + // TODO: left or right alignment based on direction? + anchor: cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Anchor::Bottom, gravity: cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity::Bottom, reactive: true, ..Default::default()