refactor: replace shell with root view wrapping app content
This commit is contained in:
@@ -8,7 +8,7 @@ pub use crate::components::common::{StyledExt, focus_handle, focus_ring, h_flex,
|
||||
pub mod panel;
|
||||
pub mod resizable;
|
||||
pub mod resize_handle;
|
||||
pub mod shell;
|
||||
pub mod root;
|
||||
pub mod titlebar;
|
||||
pub mod window_controls;
|
||||
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
//! The window root: application content in the client-side frame, with the
|
||||
//! toast, alert, and dialog overlays the root itself owns mounted above it.
|
||||
//!
|
||||
//! Applications mount [`Root`] as the window's root view and set their content
|
||||
//! entity; the root owns the frame, the background, the floating surfaces, and
|
||||
//! their layering. Code anywhere presents dialogs or pushes toasts through the
|
||||
//! static helpers, which reach the mounted root via its global.
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyView, App, AppContext as _, Context, Entity, Global, IntoElement, ParentElement as _,
|
||||
Render, StyleRefinement, Styled, WeakEntity, Window, div,
|
||||
};
|
||||
|
||||
use crate::components::alert::{Alerts, AlertsView};
|
||||
use crate::components::grain::grain as grain_overlay;
|
||||
use crate::components::toast::{Toasts, ToastsView};
|
||||
use crate::layout::window_frame::frame;
|
||||
use crate::layout::{StyledExt as _, v_flex};
|
||||
use crate::theme::ActiveTheme as _;
|
||||
|
||||
/// The mounted root's handle, for access from anywhere.
|
||||
struct RootGlobal {
|
||||
root: WeakEntity<Root>,
|
||||
}
|
||||
|
||||
impl Global for RootGlobal {}
|
||||
|
||||
/// The window's content and floating-surface host.
|
||||
pub struct Root {
|
||||
content: Option<AnyView>,
|
||||
dialogs: Vec<AnyView>,
|
||||
toasts: Entity<Toasts>,
|
||||
alerts: Entity<Alerts>,
|
||||
transparent: bool,
|
||||
grain: bool,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
impl Root {
|
||||
/// Creates the root and its toast and alert stores.
|
||||
pub fn new(_window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let toasts = cx.new(|_| Toasts::default());
|
||||
let alerts = cx.new(|_| Alerts::default());
|
||||
let weak = cx.weak_entity();
|
||||
cx.set_global(RootGlobal { root: weak });
|
||||
|
||||
Self {
|
||||
content: None,
|
||||
dialogs: Vec::new(),
|
||||
toasts,
|
||||
alerts,
|
||||
transparent: false,
|
||||
grain: false,
|
||||
style: StyleRefinement::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The mounted root, if the window has opened one.
|
||||
pub fn global(cx: &App) -> Option<Entity<Self>> {
|
||||
cx.try_global::<RootGlobal>()
|
||||
.and_then(|global| global.root.upgrade())
|
||||
}
|
||||
|
||||
/// Sets the application content view.
|
||||
pub fn set_content(&mut self, content: impl Into<AnyView>, cx: &mut Context<Self>) {
|
||||
self.content = Some(content.into());
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// The root's toast store.
|
||||
pub fn toasts(&self) -> Entity<Toasts> {
|
||||
self.toasts.clone()
|
||||
}
|
||||
|
||||
/// The root's alert store.
|
||||
pub fn alerts(&self) -> Entity<Alerts> {
|
||||
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 a dialog view above the content.
|
||||
fn mount_dialog(&mut self, view: impl Into<AnyView>, cx: &mut Context<Self>) {
|
||||
self.dialogs.push(view.into());
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Paints the app background with the translucent material fill.
|
||||
pub fn transparent(mut self, transparent: bool) -> Self {
|
||||
self.transparent = transparent;
|
||||
self
|
||||
}
|
||||
|
||||
/// Textures the app background with a film-grain overlay.
|
||||
pub fn grain(mut self, grain: bool) -> Self {
|
||||
self.grain = grain;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Styled for Root {
|
||||
fn style(&mut self) -> &mut StyleRefinement {
|
||||
&mut self.style
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Root {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let background = if self.transparent {
|
||||
theme.material.background
|
||||
} else {
|
||||
theme.colors.muted
|
||||
};
|
||||
|
||||
div()
|
||||
.relative()
|
||||
.size_full()
|
||||
.font_family(theme.ui_font())
|
||||
.text_color(theme.colors.foreground)
|
||||
.child(frame(
|
||||
v_flex()
|
||||
.w_full()
|
||||
.h_full()
|
||||
.overflow_hidden()
|
||||
.bg(background)
|
||||
.refine_style(&self.style)
|
||||
.when(self.transparent && self.grain, |layout| {
|
||||
layout.child(grain_overlay())
|
||||
})
|
||||
.child(
|
||||
self.content
|
||||
.clone()
|
||||
.map(AnyView::into_any_element)
|
||||
.unwrap_or_else(|| div().into_any_element()),
|
||||
),
|
||||
window,
|
||||
cx,
|
||||
))
|
||||
.children(self.dialogs.clone())
|
||||
.child(ToastsView::new("root-toasts", self.toasts.clone()))
|
||||
.child(AlertsView::new("root-alerts", self.alerts.clone()))
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
//! The application shell: the titlebar above the panel row, wrapped in the
|
||||
//! client-side window frame, with the toast and alert overlays mounted at
|
||||
//! the window root.
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyElement, App, ElementId, Entity, IntoElement, ParentElement as _, RenderOnce,
|
||||
StyleRefinement, Styled as _, Window, div,
|
||||
};
|
||||
|
||||
use crate::components::alert::{Alerts, AlertsView};
|
||||
use crate::components::grain::grain as grain_overlay;
|
||||
use crate::components::toast::{Toasts, ToastsView};
|
||||
use crate::layout::titlebar::Titlebar;
|
||||
use crate::layout::window_frame::frame;
|
||||
use crate::layout::{StyledExt as _, v_flex};
|
||||
use crate::theme::ActiveTheme as _;
|
||||
|
||||
/// The application shell.
|
||||
#[derive(IntoElement)]
|
||||
#[must_use]
|
||||
pub struct Shell {
|
||||
id: ElementId,
|
||||
titlebar: Option<Titlebar>,
|
||||
panel: Option<AnyElement>,
|
||||
transparent: bool,
|
||||
grain: bool,
|
||||
toasts: Option<Entity<Toasts>>,
|
||||
alerts: Option<Entity<Alerts>>,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
impl Shell {
|
||||
/// Creates the shell.
|
||||
pub fn new<I>(id: I) -> Self
|
||||
where
|
||||
I: Into<ElementId>,
|
||||
{
|
||||
Self {
|
||||
id: id.into(),
|
||||
titlebar: None,
|
||||
panel: None,
|
||||
transparent: false,
|
||||
grain: false,
|
||||
toasts: None,
|
||||
alerts: None,
|
||||
style: StyleRefinement::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the titlebar drawn above the panel row.
|
||||
pub fn titlebar(mut self, titlebar: Titlebar) -> Self {
|
||||
self.titlebar = Some(titlebar);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the panel row filling the rest of the window.
|
||||
pub fn panel<E>(mut self, panel: E) -> Self
|
||||
where
|
||||
E: IntoElement,
|
||||
{
|
||||
self.panel = Some(panel.into_any_element());
|
||||
self
|
||||
}
|
||||
|
||||
/// Paints the app background with the translucent material fill.
|
||||
pub fn transparent(mut self, transparent: bool) -> Self {
|
||||
self.transparent = transparent;
|
||||
self
|
||||
}
|
||||
|
||||
/// Textures the app background with a film-grain overlay.
|
||||
pub fn grain(mut self, grain: bool) -> Self {
|
||||
self.grain = grain;
|
||||
self
|
||||
}
|
||||
|
||||
/// Hosts the toast overlay raised on `toasts`.
|
||||
pub fn toasts(mut self, toasts: Entity<Toasts>) -> Self {
|
||||
self.toasts = Some(toasts);
|
||||
self
|
||||
}
|
||||
|
||||
/// Hosts the alert overlay stacked on `alerts`.
|
||||
pub fn alerts(mut self, alerts: Entity<Alerts>) -> Self {
|
||||
self.alerts = Some(alerts);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl gpui::Styled for Shell {
|
||||
fn style(&mut self) -> &mut StyleRefinement {
|
||||
&mut self.style
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for Shell {
|
||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let id = self.id.clone();
|
||||
let theme = cx.theme();
|
||||
|
||||
let background = if self.transparent {
|
||||
theme.material.background
|
||||
} else {
|
||||
theme.colors.muted
|
||||
};
|
||||
|
||||
frame(
|
||||
v_flex()
|
||||
.w_full()
|
||||
.h_full()
|
||||
.overflow_hidden()
|
||||
.bg(background)
|
||||
.refine_style(&self.style)
|
||||
.when(self.transparent && self.grain, |layout| {
|
||||
layout.child(grain_overlay())
|
||||
})
|
||||
.when_some(self.titlebar, |layout, titlebar| {
|
||||
layout.child(titlebar.transparent(self.transparent))
|
||||
})
|
||||
.child(self.panel.unwrap_or_else(|| div().into_any_element())),
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
.when_some(self.toasts, |frame, toasts| {
|
||||
frame.child(ToastsView::new((id.clone(), "toasts"), toasts))
|
||||
})
|
||||
.when_some(self.alerts, |frame, alerts| {
|
||||
frame.child(AlertsView::new((id, "alerts"), alerts))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ pub struct Titlebar {
|
||||
prefix: Vec<AnyElement>,
|
||||
tab_bar: Option<AnyElement>,
|
||||
suffix: Vec<AnyElement>,
|
||||
transparent: bool,
|
||||
style: StyleRefinement,
|
||||
}
|
||||
|
||||
@@ -37,7 +36,6 @@ impl Titlebar {
|
||||
prefix: Vec::new(),
|
||||
tab_bar: None,
|
||||
suffix: Vec::new(),
|
||||
transparent: false,
|
||||
style: StyleRefinement::default(),
|
||||
}
|
||||
}
|
||||
@@ -68,12 +66,6 @@ impl Titlebar {
|
||||
self.suffix = vec![suffix.into_any_element()];
|
||||
self
|
||||
}
|
||||
|
||||
/// Leaves the strip unpainted so the shell's material backdrop shows through.
|
||||
pub(crate) fn transparent(mut self, transparent: bool) -> Self {
|
||||
self.transparent = transparent;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl gpui::Styled for Titlebar {
|
||||
@@ -99,7 +91,7 @@ impl RenderOnce for Titlebar {
|
||||
.w_full()
|
||||
.h(height)
|
||||
.gap_2()
|
||||
.when(!self.transparent, |bar| bar.bg(theme.colors.muted))
|
||||
.bg(theme.colors.muted)
|
||||
.px_3()
|
||||
.refine_style(&self.style)
|
||||
.when(cfg!(target_os = "macos"), |bar| bar.pl(TRAFFIC_LIGHT_PAD))
|
||||
|
||||
Reference in New Issue
Block a user