use std::rc::Rc;
use gpui_kit::{App, Context, Entity, FocusHandle, Focusable as _, Window};
use super::FindInPage;
pub(super) type ToolbarSearch = Rc<dyn Fn(&App) -> Option<FocusHandle>>;
impl FindInPage {
pub(crate) fn observe_owner_in<T: crate::Dashboard>(
&mut self,
owner: &Entity<T>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.observe_owner(owner, cx);
let board = owner.downgrade();
self.toolbar_search = Some(Rc::new(move |cx| {
board
.read_with(cx, |board, cx| board.toolbar_search_focus(cx))
.ok()
.flatten()
}));
let sub = cx.observe_in(owner, window, |this, _, window, cx| {
this.return_to_toolbar_search(window, cx);
});
self.subscriptions.push(sub);
}
pub(super) fn search_focus(&self, cx: &App) -> Option<FocusHandle> {
self.toolbar_search.as_ref().and_then(|search| search(cx))
}
fn return_to_toolbar_search(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.is_open(cx)
&& let Some(focus) = self.search_focus(cx)
{
let find_focused = self.input.focus_handle(cx).is_focused(window);
_ = self.dismiss(cx);
if find_focused {
focus.focus(window, cx);
}
}
}
}