use gpui::{Action, App, FocusHandle, InteractiveElement as _, IntoElement, Styled, Window, div};
#[derive(Clone)]
pub(crate) struct DialogDispatchAnchor {
handle: FocusHandle,
}
impl DialogDispatchAnchor {
pub(crate) fn new(key: &'static str, window: &mut Window, cx: &mut App) -> Self {
let handle = window
.use_keyed_state(key, cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
Self { handle }
}
pub(crate) fn element(&self) -> impl IntoElement {
div().absolute().size_0().track_focus(&self.handle)
}
pub(crate) fn dispatch(&self, action: &dyn Action, window: &mut Window, cx: &mut App) {
if self.handle.is_focused(window) || self.is_rendered(window) {
self.handle.dispatch_action(action, window, cx);
} else {
window.dispatch_action(action.boxed_clone(), cx);
}
}
fn is_rendered(&self, window: &Window) -> bool {
self.handle.contains(&self.handle, window)
}
}