Skip to content

Commit

Permalink
- Changed focus to open
Browse files Browse the repository at this point in the history
  • Loading branch information
WailAbou committed Jul 30, 2024
1 parent f25c750 commit 2cc9369
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions widget/src/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct ComboBox<
on_option_hovered: Option<Box<dyn Fn(T) -> Message>>,
on_close: Option<Message>,
on_input: Option<Box<dyn Fn(String) -> Message>>,
on_focus: Option<Message>,
on_open: Option<Message>,
menu_class: <Theme as menu::Catalog>::Class<'a>,
padding: Padding,
size: Option<f32>,
Expand Down Expand Up @@ -79,17 +79,16 @@ where
on_option_hovered: None,
on_input: None,
on_close: None,
on_focus: None,
on_open: None,
menu_class: <Theme as Catalog>::default_menu(),
padding: text_input::DEFAULT_PADDING,
size: None,
}
}

/// Sets the message that will be produced when the inside area
/// of the [`ComboBox`] is pressed.
pub fn on_focus(mut self, message: Message) -> Self {
self.on_focus = Some(message);
/// 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
}

Expand Down Expand Up @@ -225,7 +224,7 @@ struct Inner<T> {
options: Vec<T>,
option_matchers: Vec<String>,
filtered_options: Filtered<T>,
is_first_focus: bool,
is_open: bool,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -262,7 +261,7 @@ where
options,
option_matchers,
filtered_options,
is_first_focus: false,
is_open: false,
}))
}

Expand Down Expand Up @@ -415,11 +414,11 @@ where
};

self.state.with_inner_mut(|state| {
if started_focused && !state.is_first_focus {
if let Some(message) = self.on_focus.take() {
if started_focused && !state.is_open {
if let Some(message) = self.on_open.take() {
shell.publish(message);
}
state.is_first_focus = true;
state.is_open = true;
}
});

Expand Down Expand Up @@ -611,7 +610,7 @@ where
// Clear the value and reset the options and menu
state.value = String::new();
state.filtered_options.update(state.options.clone());
state.is_first_focus = false;
state.is_open = false;
menu.menu = menu::State::default();

// Notify the selection
Expand Down Expand Up @@ -647,7 +646,7 @@ where
if let Some(message) = self.on_close.take() {
shell.publish(message);
}
state.is_first_focus = false;
state.is_open = false;
}
});

Expand Down Expand Up @@ -752,9 +751,8 @@ where
)
.unfocus();

self.state.with_inner_mut(|state| {
state.is_first_focus = false
});
self.state
.with_inner_mut(|state| state.is_open = false);
(self.on_selected)(x)
},
self.on_option_hovered.as_deref(),
Expand Down

0 comments on commit 2cc9369

Please sign in to comment.