use crate::{
drawer::Drawer,
input::InputState,
modal::Modal,
notification::{Notification, NotificationList},
window_border, ActiveTheme, Placement,
};
use gpui::{
actions, canvas, div, prelude::FluentBuilder as _, AnyView, App, AppContext, Context,
DefiniteLength, Entity, FocusHandle, InteractiveElement, IntoElement, KeyBinding,
ParentElement as _, Render, Styled, Window,
};
use std::{any::TypeId, rc::Rc};
actions!(root, [Tab, TabPrev]);
const CONTENT: &str = "Root";
pub(crate) fn init(cx: &mut App) {
cx.bind_keys([
KeyBinding::new("tab", Tab, Some(CONTENT)),
KeyBinding::new("shift-tab", TabPrev, Some(CONTENT)),
]);
}
pub trait ContextModal: Sized {
fn open_drawer<F>(&mut self, cx: &mut App, build: F)
where
F: Fn(Drawer, &mut Window, &mut App) -> Drawer + 'static;
fn open_drawer_at<F>(&mut self, placement: Placement, cx: &mut App, build: F)
where
F: Fn(Drawer, &mut Window, &mut App) -> Drawer + 'static;
fn has_active_drawer(&mut self, cx: &mut App) -> bool;
fn close_drawer(&mut self, cx: &mut App);
fn open_modal<F>(&mut self, cx: &mut App, build: F)
where
F: Fn(Modal, &mut Window, &mut App) -> Modal + 'static;
fn has_active_modal(&mut self, cx: &mut App) -> bool;
fn close_modal(&mut self, cx: &mut App);
fn close_all_modals(&mut self, cx: &mut App);
fn push_notification(&mut self, note: impl Into<Notification>, cx: &mut App);
fn remove_notification<T: Sized + 'static>(&mut self, cx: &mut App);
fn clear_notifications(&mut self, cx: &mut App);
fn notifications(&mut self, cx: &mut App) -> Rc<Vec<Entity<Notification>>>;
fn focused_input(&mut self, cx: &mut App) -> Option<Entity<InputState>>;
fn has_focused_input(&mut self, cx: &mut App) -> bool;
}
impl ContextModal for Window {
fn open_drawer<F>(&mut self, cx: &mut App, build: F)
where
F: Fn(Drawer, &mut Window, &mut App) -> Drawer + 'static,
{
self.open_drawer_at(Placement::Right, cx, build)
}
fn open_drawer_at<F>(&mut self, placement: Placement, cx: &mut App, build: F)
where
F: Fn(Drawer, &mut Window, &mut App) -> Drawer + 'static,
{
Root::update(self, cx, move |root, window, cx| {
if root.active_drawer.is_none() {
root.previous_focus_handle = window.focused(cx);
}
let focus_handle = cx.focus_handle();
focus_handle.focus(window);
root.active_drawer = Some(ActiveDrawer {
focus_handle,
placement,
builder: Rc::new(build),
});
cx.notify();
})
}
fn has_active_drawer(&mut self, cx: &mut App) -> bool {
Root::read(self, cx).active_drawer.is_some()
}
fn close_drawer(&mut self, cx: &mut App) {
Root::update(self, cx, |root, window, cx| {
root.focused_input = None;
root.active_drawer = None;
root.focus_back(window, cx);
cx.notify();
})
}
fn open_modal<F>(&mut self, cx: &mut App, build: F)
where
F: Fn(Modal, &mut Window, &mut App) -> Modal + 'static,
{
Root::update(self, cx, move |root, window, cx| {
if root.active_modals.len() == 0 {
root.previous_focus_handle = window.focused(cx);
}
let focus_handle = cx.focus_handle();
focus_handle.focus(window);
root.active_modals.push(ActiveModal {
focus_handle,
builder: Rc::new(build),
});
cx.notify();
})
}
fn has_active_modal(&mut self, cx: &mut App) -> bool {
Root::read(self, cx).active_modals.len() > 0
}
fn close_modal(&mut self, cx: &mut App) {
Root::update(self, cx, move |root, window, cx| {
root.focused_input = None;
root.active_modals.pop();
if let Some(top_modal) = root.active_modals.last() {
top_modal.focus_handle.focus(window);
} else {
root.focus_back(window, cx);
}
cx.notify();
})
}
fn close_all_modals(&mut self, cx: &mut App) {
Root::update(self, cx, |root, window, cx| {
root.focused_input = None;
root.active_modals.clear();
root.focus_back(window, cx);
cx.notify();
})
}
fn push_notification(&mut self, note: impl Into<Notification>, cx: &mut App) {
let note = note.into();
Root::update(self, cx, move |root, window, cx| {
root.notification
.update(cx, |view, cx| view.push(note, window, cx));
cx.notify();
})
}
fn remove_notification<T: Sized + 'static>(&mut self, cx: &mut App) {
Root::update(self, cx, move |root, window, cx| {
root.notification.update(cx, |view, cx| {
let id = TypeId::of::<T>();
view.close(id, window, cx);
});
cx.notify();
})
}
fn clear_notifications(&mut self, cx: &mut App) {
Root::update(self, cx, move |root, window, cx| {
root.notification
.update(cx, |view, cx| view.clear(window, cx));
cx.notify();
})
}
fn notifications(&mut self, cx: &mut App) -> Rc<Vec<Entity<Notification>>> {
let entity = Root::read(self, cx).notification.clone();
Rc::new(entity.read(cx).notifications())
}
fn has_focused_input(&mut self, cx: &mut App) -> bool {
Root::read(self, cx).focused_input.is_some()
}
fn focused_input(&mut self, cx: &mut App) -> Option<Entity<InputState>> {
Root::read(self, cx).focused_input.clone()
}
}
pub struct Root {
previous_focus_handle: Option<FocusHandle>,
active_drawer: Option<ActiveDrawer>,
pub(crate) active_modals: Vec<ActiveModal>,
pub(super) focused_input: Option<Entity<InputState>>,
pub notification: Entity<NotificationList>,
drawer_size: Option<DefiniteLength>,
view: AnyView,
}
#[derive(Clone)]
struct ActiveDrawer {
focus_handle: FocusHandle,
placement: Placement,
builder: Rc<dyn Fn(Drawer, &mut Window, &mut App) -> Drawer + 'static>,
}
#[derive(Clone)]
pub(crate) struct ActiveModal {
focus_handle: FocusHandle,
builder: Rc<dyn Fn(Modal, &mut Window, &mut App) -> Modal + 'static>,
}
impl Root {
pub fn new(view: AnyView, window: &mut Window, cx: &mut Context<Self>) -> Self {
Self {
previous_focus_handle: None,
active_drawer: None,
active_modals: Vec::new(),
focused_input: None,
notification: cx.new(|cx| NotificationList::new(window, cx)),
drawer_size: None,
view,
}
}
pub fn update<F, R>(window: &mut Window, cx: &mut App, f: F) -> R
where
F: FnOnce(&mut Self, &mut Window, &mut Context<Self>) -> R,
{
let root = window
.root::<Root>()
.flatten()
.expect("BUG: window first layer should be a gpui_component::Root.");
root.update(cx, |root, cx| f(root, window, cx))
}
pub fn read<'a>(window: &'a Window, cx: &'a App) -> &'a Self {
&window
.root::<Root>()
.expect("The window root view should be of type `ui::Root`.")
.unwrap()
.read(cx)
}
fn focus_back(&mut self, window: &mut Window, _: &mut App) {
if let Some(handle) = self.previous_focus_handle.clone() {
window.focus(&handle);
}
}
pub fn render_notification_layer(
window: &mut Window,
cx: &mut App,
) -> Option<impl IntoElement> {
let root = window.root::<Root>()??;
let active_drawer_placement = root.read(cx).active_drawer.clone().map(|d| d.placement);
let (mt, mr) = match active_drawer_placement {
Some(Placement::Right) => (None, root.read(cx).drawer_size),
Some(Placement::Top) => (root.read(cx).drawer_size, None),
_ => (None, None),
};
Some(
div()
.absolute()
.top_0()
.right_0()
.when_some(mt, |this, offset| this.mt(offset))
.when_some(mr, |this, offset| this.mr(offset))
.child(root.read(cx).notification.clone()),
)
}
pub fn render_drawer_layer(window: &mut Window, cx: &mut App) -> Option<impl IntoElement> {
let root = window.root::<Root>()??;
if let Some(active_drawer) = root.read(cx).active_drawer.clone() {
let mut drawer = Drawer::new(window, cx);
drawer = (active_drawer.builder)(drawer, window, cx);
drawer.focus_handle = active_drawer.focus_handle.clone();
drawer.placement = active_drawer.placement;
let drawer_size = drawer.size;
return Some(
div().relative().child(drawer).child(
canvas(
move |_, _, cx| root.update(cx, |r, _| r.drawer_size = Some(drawer_size)),
|_, _, _, _| {},
)
.absolute()
.size_full(),
),
);
}
None
}
pub fn render_modal_layer(window: &mut Window, cx: &mut App) -> Option<impl IntoElement> {
let root = window.root::<Root>()??;
let active_modals = root.read(cx).active_modals.clone();
if active_modals.is_empty() {
return None;
}
let mut show_overlay_ix = None;
let mut modals = active_modals
.iter()
.enumerate()
.map(|(i, active_modal)| {
let mut modal = Modal::new(window, cx);
modal = (active_modal.builder)(modal, window, cx);
modal.focus_handle = active_modal.focus_handle.clone();
modal.layer_ix = i;
if modal.has_overlay() {
show_overlay_ix = Some(i);
}
modal
})
.collect::<Vec<_>>();
if let Some(ix) = show_overlay_ix {
if let Some(modal) = modals.get_mut(ix) {
modal.overlay_visible = true;
}
}
Some(div().children(modals))
}
pub fn view(&self) -> &AnyView {
&self.view
}
fn on_action_tab(&mut self, _: &Tab, window: &mut Window, _: &mut Context<Self>) {
window.focus_next();
}
fn on_action_tab_prev(&mut self, _: &TabPrev, window: &mut Window, _: &mut Context<Self>) {
window.focus_prev();
}
}
impl Render for Root {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let base_font_size = cx.theme().font_size;
window.set_rem_size(base_font_size);
window_border().child(
div()
.id("root")
.key_context(CONTENT)
.on_action(cx.listener(Self::on_action_tab))
.on_action(cx.listener(Self::on_action_tab_prev))
.relative()
.size_full()
.font_family(".SystemUIFont")
.bg(cx.theme().background)
.text_color(cx.theme().foreground)
.child(self.view.clone()),
)
}
}