use gpui::{actions, div, prelude::*, AnyElement, App, KeyBinding, StyleRefinement, Window};
use crate::sx::SxStyled;
use crate::{styles, styles::ApplyStyleOverrides};
styles! {
APP_ROOT = {
root: {
size: full,
display: flex,
direction: column,
background: background,
color: foreground,
font_family: sans,
text: theme,
text_align: start,
},
}
}
actions!(
rok_ui,
[
FocusNextElement,
FocusPreviousElement,
]
);
#[doc(hidden)]
pub fn bind_focus_navigation_keys(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("tab", FocusNextElement, None),
KeyBinding::new("shift-tab", FocusPreviousElement, None),
]);
}
#[derive(IntoElement)]
pub struct AppRoot {
children: Vec<AnyElement>,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(AppRoot);
impl AppRoot {
#[must_use]
pub fn new() -> Self {
Self {
children: Vec::new(),
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
}
impl Default for AppRoot {
fn default() -> Self {
Self::new()
}
}
impl ParentElement for AppRoot {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for AppRoot {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let root_focus_handle = window
.use_keyed_state("rok-ui-app-root-focus", cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
if !root_focus_handle.contains_focused(window, cx) {
let focus_handle_to_focus = root_focus_handle.clone();
window.defer(cx, move |window, _| window.focus(&focus_handle_to_focus));
}
div()
.id("rok-ui-app-root")
.track_focus(&root_focus_handle)
.sx((&APP_ROOT.root, &self.sx))
.on_action(|_: &FocusNextElement, window, _| window.focus_next())
.on_action(|_: &FocusPreviousElement, window, _| window.focus_prev())
.children(self.children)
.children(toaster())
.child(pointer_modality_listener())
.apply_style_overrides(&self.style_overrides)
}
}
#[cfg(feature = "toast")]
#[allow(clippy::unnecessary_wraps)] fn toaster() -> Option<gpui::AnyElement> {
Some(super::toast::Toaster.into_any_element())
}
#[cfg(not(feature = "toast"))]
fn toaster() -> Option<gpui::AnyElement> {
None
}
fn pointer_modality_listener() -> impl IntoElement {
gpui::canvas(
|_, _, _| {},
|_, (), window, _| {
window.on_mouse_event(|_: &gpui::MouseDownEvent, phase, window, _| {
if phase == gpui::DispatchPhase::Capture && crate::sx::set_keyboard_modality(false)
{
window.refresh();
}
});
},
)
.absolute()
.size_0()
}