From efb5761c5611a84e460e88dc7772f828c8fcb391 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Mon, 23 Nov 2026 02:55:29 +0700 Subject: [PATCH] refactor: expose root presentation apis via root context trait --- crates/signed_app/src/app/mod.rs | 6 +-- crates/signed_app/src/titlebar/mod.rs | 18 +++++--- crates/ui/src/layout/root.rs | 62 +++++++++++++++++++++------ 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/crates/signed_app/src/app/mod.rs b/crates/signed_app/src/app/mod.rs index 4f4ae4f..6b21a35 100644 --- a/crates/signed_app/src/app/mod.rs +++ b/crates/signed_app/src/app/mod.rs @@ -13,7 +13,7 @@ use signed_state::{Backend, BackendEvent}; use ui::common::{h_flex, v_flex}; use ui::icons::IconRole; use ui::layout::panel::PanelRow; -use ui::layout::root::Root; +use ui::layout::root::RootContext as _; use ui::layout::titlebar::Titlebar; use ui::toast::Toast; @@ -61,7 +61,7 @@ impl AppRoot { this.open_passphrase(cx); } BackendEvent::Error(message) => { - Root::push_toast(Toast::new(message.clone()).icon(IconRole::Warning), cx); + cx.toast(Toast::new(message.clone()).icon(IconRole::Warning)); } BackendEvent::SignerChanged => { this.workspace.update(cx, |workspace, cx| { @@ -104,7 +104,7 @@ impl AppRoot { } let dialog = PassphraseDialog::new(cx); self.passphrase = Some(dialog.clone()); - Root::open_dialog(dialog, cx); + cx.open_dialog(dialog); } } diff --git a/crates/signed_app/src/titlebar/mod.rs b/crates/signed_app/src/titlebar/mod.rs index 8b8a223..9e90da9 100644 --- a/crates/signed_app/src/titlebar/mod.rs +++ b/crates/signed_app/src/titlebar/mod.rs @@ -16,7 +16,7 @@ use ui::button::{Button, ButtonVariant}; use ui::command_palette::{Command, CommandPalette, CommandPaletteState}; use ui::icons::{Icon, IconRole}; use ui::layout::panel::PanelView; -use ui::layout::root::Root; +use ui::layout::root::RootContext as _; use ui::menu::{Menu, MenuItem}; use ui::tabs::{Tab, Tabs, TabsVariant}; use ui::tooltip::Tooltip; @@ -82,10 +82,12 @@ impl TitlebarView { let local = LocalReposStore::global(cx); let directory = ProfileDirectory::new(cx); - let weak = cx.entity().downgrade(); - Root::open_dialog(Self::new_palette(weak.clone(), window, cx), cx); - let entity = cx.entity(); + let weak = entity.downgrade(); + + let palette = Self::new_palette(weak.clone(), window, cx); + cx.open_palette(palette); + let mut subscriptions = Vec::new(); let repository_switcher = cx.new(|cx| { @@ -365,18 +367,20 @@ impl TitlebarView { } fn open_settings(&mut self, cx: &mut Context) { - Root::open_dialog(SettingsDialog::new(cx), cx); + let dialog = SettingsDialog::new(cx); + cx.open_dialog(dialog); cx.notify(); } fn open_exit(&mut self, cx: &mut Context) { - Root::open_dialog(ExitDialog::new(cx), cx); + let dialog = ExitDialog::new(cx); + cx.open_dialog(dialog); cx.notify(); } fn open_create(&mut self, window: &mut Window, cx: &mut Context) { let view = dialogs::create::CreateRepoDialog::new(self.workspace.clone(), window, cx); - Root::open_dialog(view, cx); + cx.open_dialog(view); cx.notify(); } diff --git a/crates/ui/src/layout/root.rs b/crates/ui/src/layout/root.rs index 63aa41e..d5d2c5e 100644 --- a/crates/ui/src/layout/root.rs +++ b/crates/ui/src/layout/root.rs @@ -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); + + /// Presents a dialog view above the mounted root's content. + fn open_dialog(&mut self, view: impl Into); +} + +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) { + 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) { + 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, + palette: Option, dialogs: Vec, toasts: Entity, alerts: Entity, @@ -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, 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, cx: &mut Context) { + 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()))