Add Modal::has_focus and introduce a ModalHandle trait object

This commit is contained in:
Antonio Scandurra 2023-07-05 09:39:56 +02:00
parent 25564ea058
commit a8602b2a0c
3 changed files with 38 additions and 0 deletions

View file

@ -25,6 +25,7 @@ pub struct Picker<D: PickerDelegate> {
theme: Arc<Mutex<Box<dyn Fn(&theme::Theme) -> theme::Picker>>>,
confirmed: bool,
pending_update_matches: Task<Option<()>>,
has_focus: bool,
}
pub trait PickerDelegate: Sized + 'static {
@ -140,13 +141,22 @@ impl<D: PickerDelegate> View for Picker<D> {
}
fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
self.has_focus = true;
if cx.is_self_focused() {
cx.focus(&self.query_editor);
}
}
fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {
self.has_focus = false;
}
}
impl<D: PickerDelegate> Modal for Picker<D> {
fn has_focus(&self) -> bool {
self.has_focus
}
fn dismiss_on_event(event: &Self::Event) -> bool {
matches!(event, PickerEvent::Dismiss)
}
@ -191,6 +201,7 @@ impl<D: PickerDelegate> Picker<D> {
theme,
confirmed: false,
pending_update_matches: Task::ready(None),
has_focus: false,
};
this.update_matches(String::new(), cx);
this