refactor: expose root presentation apis via root context trait
This commit is contained in:
@@ -18,6 +18,7 @@ use crate::components::toast::{Toasts, ToastsView};
|
||||
use crate::layout::window_frame::frame;
|
||||
use crate::layout::{StyledExt as _, v_flex};
|
||||
use crate::theme::ActiveTheme as _;
|
||||
use crate::toast::Toast;
|
||||
|
||||
/// The mounted root's handle, for access from anywhere.
|
||||
struct RootGlobal {
|
||||
@@ -26,9 +27,49 @@ struct RootGlobal {
|
||||
|
||||
impl Global for RootGlobal {}
|
||||
|
||||
/// Root presentation helpers on gpui contexts.
|
||||
pub trait RootContext {
|
||||
/// Pushes a toast on the mounted root's store.
|
||||
fn toast(&mut self, toast: Toast);
|
||||
|
||||
/// Mounts the command palette view on the root.
|
||||
///
|
||||
/// Mounted once at startup; the palette renders only while open.
|
||||
fn open_palette(&mut self, view: impl Into<AnyView>);
|
||||
|
||||
/// Presents a dialog view above the mounted root's content.
|
||||
fn open_dialog(&mut self, view: impl Into<AnyView>);
|
||||
}
|
||||
|
||||
impl RootContext for App {
|
||||
fn toast(&mut self, toast: Toast) {
|
||||
if let Some(root) = Root::global(self) {
|
||||
let toasts = root.read(self).toasts();
|
||||
toasts.update(self, |toasts, cx| {
|
||||
toasts.push(toast, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn open_palette(&mut self, view: impl Into<AnyView>) {
|
||||
if let Some(root) = Root::global(self) {
|
||||
root.update(self, |root, cx| root.mount_palette(view, cx));
|
||||
}
|
||||
}
|
||||
|
||||
fn open_dialog(&mut self, view: impl Into<AnyView>) {
|
||||
if let Some(root) = Root::global(self) {
|
||||
root.update(self, |root, cx| {
|
||||
root.mount_dialog(view, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The window's content and floating-surface host.
|
||||
pub struct Root {
|
||||
content: Option<AnyView>,
|
||||
palette: Option<AnyView>,
|
||||
dialogs: Vec<AnyView>,
|
||||
toasts: Entity<Toasts>,
|
||||
alerts: Entity<Alerts>,
|
||||
@@ -47,6 +88,7 @@ impl Root {
|
||||
|
||||
Self {
|
||||
content: None,
|
||||
palette: None,
|
||||
dialogs: Vec::new(),
|
||||
toasts,
|
||||
alerts,
|
||||
@@ -78,19 +120,10 @@ impl Root {
|
||||
self.alerts.clone()
|
||||
}
|
||||
|
||||
/// Presents a dialog view at the window root, from anywhere.
|
||||
pub fn open_dialog(view: impl Into<AnyView>, cx: &mut App) {
|
||||
if let Some(root) = Self::global(cx) {
|
||||
root.update(cx, |root, cx| root.mount_dialog(view, cx));
|
||||
}
|
||||
}
|
||||
|
||||
/// Pushes a toast on the root's store, from anywhere.
|
||||
pub fn push_toast(toast: crate::components::toast::Toast, cx: &mut App) {
|
||||
if let Some(root) = Self::global(cx) {
|
||||
let toasts = root.read(cx).toasts();
|
||||
toasts.update(cx, |toasts, cx| toasts.push(toast, cx));
|
||||
}
|
||||
/// Mounts the command palette view above the content.
|
||||
fn mount_palette(&mut self, view: impl Into<AnyView>, cx: &mut Context<Self>) {
|
||||
self.palette = Some(view.into());
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Mounts a dialog view above the content.
|
||||
@@ -151,6 +184,9 @@ impl Render for Root {
|
||||
window,
|
||||
cx,
|
||||
))
|
||||
.when_some(self.palette.clone(), |layout, palette| {
|
||||
layout.child(palette)
|
||||
})
|
||||
.children(self.dialogs.clone())
|
||||
.child(ToastsView::new("root-toasts", self.toasts.clone()))
|
||||
.child(AlertsView::new("root-alerts", self.alerts.clone()))
|
||||
|
||||
Reference in New Issue
Block a user