From 9d9ac0ff35f23c15bd2f4dd252855c8638e0f746 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 13 Sep 2024 16:38:38 +0200 Subject: Add `on_open` handler to `combo_box` widget Co-authored-by: Wail Abou --- widget/src/combo_box.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 62785b2c..a51701ca 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -41,6 +41,7 @@ pub struct ComboBox< selection: text_input::Value, on_selected: Box Message>, on_option_hovered: Option Message>>, + on_open: Option, on_close: Option, on_input: Option Message>>, menu_class: ::Class<'a>, @@ -77,6 +78,7 @@ where on_selected: Box::new(on_selected), on_option_hovered: None, on_input: None, + on_open: None, on_close: None, menu_class: ::default_menu(), padding: text_input::DEFAULT_PADDING, @@ -104,6 +106,13 @@ where self } + /// Sets the message that will be produced when the [`ComboBox`] is + /// opened. + pub fn on_open(mut self, message: Message) -> Self { + self.on_open = Some(message); + self + } + /// Sets the message that will be produced when the outside area /// of the [`ComboBox`] is pressed. pub fn on_close(mut self, message: Message) -> Self { @@ -632,15 +641,19 @@ where text_input_state.is_focused() }; - if started_focused && !is_focused && !published_message_to_shell { - if let Some(message) = self.on_close.take() { - shell.publish(message); - } - } - - // Focus changed, invalidate widget tree to force a fresh `view` if started_focused != is_focused { + // Focus changed, invalidate widget tree to force a fresh `view` shell.invalidate_widgets(); + + if !published_message_to_shell { + if is_focused { + if let Some(on_open) = self.on_open.take() { + shell.publish(on_open); + } + } else if let Some(on_close) = self.on_close.take() { + shell.publish(on_close); + } + } } event_status -- cgit