From ccaa48a1129e38e48a631247ebc72f51081a35b5 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sun, 22 Nov 2026 21:45:09 +0700 Subject: [PATCH] refactor: replace shell with root view wrapping app content --- crates/signed_app/src/app/mod.rs | 38 ++--- crates/signed_app/src/identity/passphrase.rs | 4 +- crates/signed_app/src/lib.rs | 9 +- .../signed_app/src/pull_requests/helpers.rs | 11 +- crates/signed_app/src/pull_requests/new.rs | 67 ++++---- crates/signed_app/src/repo/commit_diff.rs | 5 +- crates/signed_app/src/repo/dialogs/create.rs | 4 +- crates/signed_app/src/repo/dialogs/init.rs | 4 +- crates/signed_app/src/repo/files.rs | 24 +-- crates/signed_app/src/repo/helpers.rs | 11 +- crates/signed_app/src/repo/mod.rs | 8 +- crates/signed_app/src/shared/panels.rs | 18 +- crates/signed_app/src/titlebar/mod.rs | 38 +---- crates/ui/src/layout/mod.rs | 2 +- crates/ui/src/layout/root.rs | 158 ++++++++++++++++++ crates/ui/src/layout/shell.rs | 132 --------------- crates/ui/src/layout/titlebar.rs | 10 +- 17 files changed, 259 insertions(+), 284 deletions(-) create mode 100644 crates/ui/src/layout/root.rs delete mode 100644 crates/ui/src/layout/shell.rs diff --git a/crates/signed_app/src/app/mod.rs b/crates/signed_app/src/app/mod.rs index cd12ec4..4f4ae4f 100644 --- a/crates/signed_app/src/app/mod.rs +++ b/crates/signed_app/src/app/mod.rs @@ -10,13 +10,12 @@ use gpui::{ use helpers::apply_appearance_mode; use settings::SettingsStore; use signed_state::{Backend, BackendEvent}; -use ui::common::h_flex; +use ui::common::{h_flex, v_flex}; use ui::icons::IconRole; use ui::layout::panel::PanelRow; -use ui::layout::shell::Shell; +use ui::layout::root::Root; use ui::layout::titlebar::Titlebar; -use ui::theme::ActiveTheme as _; -use ui::toast::{Toast, Toasts}; +use ui::toast::Toast; use crate::app::placeholder::PlaceholderView; use crate::app::sign_in::SignedOutView; @@ -32,7 +31,6 @@ use crate::workspace::Workspace; pub struct AppRoot { workspace: Entity, titlebar: Entity, - toasts: Entity, sign_in: Entity, explore: Entity, empty: Entity, @@ -47,7 +45,6 @@ impl AppRoot { let workspace = cx.new(|_| Workspace::new()); let titlebar = cx.new(|cx| TitlebarView::new(workspace.downgrade(), window, cx)); - let toasts = cx.new(|_| Toasts::default()); let sign_in = cx.new(|_| SignedOutView::new()); let explore = cx.new(|cx| ExploreView::new(workspace.downgrade(), cx)); let empty = cx.new(|_| PlaceholderView::new("Select a repository")); @@ -64,9 +61,7 @@ impl AppRoot { this.open_passphrase(cx); } BackendEvent::Error(message) => { - this.toasts.update(cx, |toasts, cx| { - toasts.push(Toast::new(message.clone()).icon(IconRole::Warning), cx); - }); + Root::push_toast(Toast::new(message.clone()).icon(IconRole::Warning), cx); } BackendEvent::SignerChanged => { this.workspace.update(cx, |workspace, cx| { @@ -95,7 +90,6 @@ impl AppRoot { Self { workspace, titlebar, - toasts, sign_in, explore, empty, @@ -105,8 +99,12 @@ impl AppRoot { } fn open_passphrase(&mut self, cx: &mut Context) { - self.passphrase = Some(PassphraseDialog::new(cx)); - cx.notify(); + if self.passphrase.is_some() { + return; + } + let dialog = PassphraseDialog::new(cx); + self.passphrase = Some(dialog.clone()); + Root::open_dialog(dialog, cx); } } @@ -122,8 +120,6 @@ impl Render for AppRoot { div() .relative() .size_full() - .font_family(cx.theme().ui_font()) - .text_color(cx.theme().colors.foreground) .key_context(REPOSITORY_KEY_CONTEXT) .on_action(cx.listener(|this, action: &SwitchRepository, window, cx| { this.titlebar.update(cx, |titlebar, cx| { @@ -136,8 +132,10 @@ impl Render for AppRoot { }); })) .child( - Shell::new("signed-shell") - .titlebar( + v_flex() + .size_full() + .overflow_hidden() + .child( Titlebar::new("signed-titlebar") .prefix( h_flex() @@ -154,7 +152,7 @@ impl Render for AppRoot { titlebar.suffix(menu) }), ) - .panel( + .child( PanelRow::new("signed-panel") .when_some(main.as_ref(), |row, main| { row.main(main.clone()).when(workspace.has_details(), |row| { @@ -168,11 +166,7 @@ impl Render for AppRoot { row.main(self.sign_in.clone()) .split_at(self.explore.clone(), px(300.)) }), - ) - .toasts(self.toasts.clone()), + ), ) - .child(titlebar_view.palette()) - .children(titlebar_view.dialogs()) - .when_some(self.passphrase.clone(), |el, view| el.child(view)) } } diff --git a/crates/signed_app/src/identity/passphrase.rs b/crates/signed_app/src/identity/passphrase.rs index 075ad3e..bfc2f24 100644 --- a/crates/signed_app/src/identity/passphrase.rs +++ b/crates/signed_app/src/identity/passphrase.rs @@ -10,7 +10,9 @@ use ui::field::Field; use ui::icons::IconRole; use ui::input::{Input, InputState, InputStateEvent}; -use crate::shared::progress::{DialogProgress, dialog_submit_button, error_row, push_steps_row, run_dialog_task}; +use crate::shared::progress::{ + DialogProgress, dialog_submit_button, error_row, push_steps_row, run_dialog_task, +}; pub struct PassphraseDialog { handle: DialogHandle, diff --git a/crates/signed_app/src/lib.rs b/crates/signed_app/src/lib.rs index 9e3b172..5ac65d4 100644 --- a/crates/signed_app/src/lib.rs +++ b/crates/signed_app/src/lib.rs @@ -14,7 +14,12 @@ mod workspace; pub use app::AppRoot; pub use app::helpers::apply_appearance_mode; use gpui::{App, AppContext as _, Entity, Window}; +use ui::layout::root::Root; -pub fn root(window: &mut Window, cx: &mut App) -> Entity { - cx.new(|cx| AppRoot::new(window, cx)) +pub fn root(window: &mut Window, cx: &mut App) -> Entity { + let root = cx.new(|cx| Root::new(window, cx)); + let content: Entity = cx.new(|cx| AppRoot::new(window, cx)); + + root.update(cx, |root, cx| root.set_content(content, cx)); + root } diff --git a/crates/signed_app/src/pull_requests/helpers.rs b/crates/signed_app/src/pull_requests/helpers.rs index 63220ad..912e99a 100644 --- a/crates/signed_app/src/pull_requests/helpers.rs +++ b/crates/signed_app/src/pull_requests/helpers.rs @@ -78,8 +78,10 @@ pub fn source_menu_items( .file_name() .map(|name| name.to_string_lossy().into_owned()) .unwrap_or_else(|| path.display().to_string()); + let target = view.clone(); let path_for_action = path.clone(); + items.push( MenuItem::new(truncate_label(&label)) .checked(active_path.as_ref() == Some(&path)) @@ -114,14 +116,12 @@ pub fn source_menu_items( } else { SharedString::from(format!("by {}", shorten_owner(&candidate.owner))) }; - let title = format!( - "{} — {}", - announcement_display_name(&candidate), - subtitle - ); + + let title = format!("{} — {}", announcement_display_name(&candidate), subtitle); let target = view.clone(); let checked = active_fork == Some(candidate.addr()); let announcement = candidate; + items.push( MenuItem::new(truncate_label(&title)) .checked(checked) @@ -136,5 +136,6 @@ pub fn source_menu_items( ); } } + items } diff --git a/crates/signed_app/src/pull_requests/new.rs b/crates/signed_app/src/pull_requests/new.rs index 29e4cea..9cd51d2 100644 --- a/crates/signed_app/src/pull_requests/new.rs +++ b/crates/signed_app/src/pull_requests/new.rs @@ -276,9 +276,7 @@ impl NewPullRequestView { let active_fork = self.fork.as_ref().map(|fork| fork.announcement.addr()); let can_submit = self.submit_enabled(cx); let source_label = match &self.fork { - Some(fork) => truncate_label(&announcement_display_name( - &fork.announcement, - )), + Some(fork) => truncate_label(&announcement_display_name(&fork.announcement)), None => self.repo_path.as_ref().map_or_else( || SharedString::from("No source"), |path| truncate_label(&path.display().to_string()), @@ -305,32 +303,35 @@ impl NewPullRequestView { .child(div().w(px(160.)).child(self.compare_select.clone())), ) .child( - v_flex().gap_1().child(div().text_xs().font_medium().child("Source")).child( - Menu::new("pr-source") - .open(self.source_open) - .on_change(move |open, _, cx| { - log_dropped(weak_menu.update(cx, |this, cx| { - this.source_open = open; - cx.notify(); - })); - }) - .trigger(move |_, _| { - Button::new("compare-source") - .label(source_label.clone()) - .variant(ButtonVariant::Secondary) - .into_any_element() - }) - .items(move |_, _| { - super::helpers::source_menu_items( - source_view.clone(), - associated.clone(), - active_path.clone(), - candidates.clone(), - user, - active_fork.clone(), - ) - }), - ), + v_flex() + .gap_1() + .child(div().text_xs().font_medium().child("Source")) + .child( + Menu::new("pr-source") + .open(self.source_open) + .on_change(move |open, _, cx| { + log_dropped(weak_menu.update(cx, |this, cx| { + this.source_open = open; + cx.notify(); + })); + }) + .trigger(move |_, _| { + Button::new("compare-source") + .label(source_label.clone()) + .variant(ButtonVariant::Secondary) + .into_any_element() + }) + .items(move |_, _| { + super::helpers::source_menu_items( + source_view.clone(), + associated.clone(), + active_path.clone(), + candidates.clone(), + user, + active_fork.clone(), + ) + }), + ), ) .when_some(refresh_fork, |this, fork| { this.child( @@ -349,9 +350,7 @@ impl NewPullRequestView { .loading(self.submitting) .disabled(!can_submit) .on_click(move |_, window, cx| { - log_dropped( - weak.update(cx, |this, cx| this.submit(window, cx)), - ); + log_dropped(weak.update(cx, |this, cx| this.submit(window, cx))); }), ) .into_any_element() @@ -385,9 +384,7 @@ impl NewPullRequestView { return Spinner::centered("new-pr-loading"); } if !self.has_source() { - return EmptyState::centered( - "Choose a local checkout or an announced fork to compare", - ); + return EmptyState::centered("Choose a local checkout or an announced fork to compare"); } if self.commits.is_none() && self.error.is_some() { return EmptyState::centered("Nothing to compare"); diff --git a/crates/signed_app/src/repo/commit_diff.rs b/crates/signed_app/src/repo/commit_diff.rs index b3339d2..7a43530 100644 --- a/crates/signed_app/src/repo/commit_diff.rs +++ b/crates/signed_app/src/repo/commit_diff.rs @@ -287,10 +287,7 @@ impl DiffPane { let view = view; move |index, _, cx| { if let Err(error) = view.update(cx, |this, cx| { - let id = this - .rows - .get(index) - .map(|row| row.key().to_string()); + let id = this.rows.get(index).map(|row| row.key().to_string()); if let Some(id) = id { this.toggle_folder(&id, cx); } diff --git a/crates/signed_app/src/repo/dialogs/create.rs b/crates/signed_app/src/repo/dialogs/create.rs index b1a99a7..9584cca 100644 --- a/crates/signed_app/src/repo/dialogs/create.rs +++ b/crates/signed_app/src/repo/dialogs/create.rs @@ -17,7 +17,9 @@ use ui::tooltip::Tooltip; use super::grasp_servers::{GraspServersState, grasp_servers_field, load_user_grasp_servers}; use crate::repo::actions::open_repo_panel; -use crate::shared::progress::{DialogProgress, dialog_submit_button, error_row, push_steps_row, run_dialog_task_with}; +use crate::shared::progress::{ + DialogProgress, dialog_submit_button, error_row, push_steps_row, run_dialog_task_with, +}; use crate::workspace::Workspace; /// Progress of the create-repository flow, so async results can be rendered. diff --git a/crates/signed_app/src/repo/dialogs/init.rs b/crates/signed_app/src/repo/dialogs/init.rs index b10a6a2..5aa19c3 100644 --- a/crates/signed_app/src/repo/dialogs/init.rs +++ b/crates/signed_app/src/repo/dialogs/init.rs @@ -20,7 +20,9 @@ use ui::theme::ActiveTheme as _; use super::grasp_servers::{GraspServersState, grasp_servers_field, load_user_grasp_servers}; use crate::repo::RepoDetailView; -use crate::shared::progress::{DialogProgress, dialog_submit_button, error_row, push_steps_row, run_dialog_task_with}; +use crate::shared::progress::{ + DialogProgress, dialog_submit_button, error_row, push_steps_row, run_dialog_task_with, +}; pub struct InitRepoDialog { handle: DialogHandle, diff --git a/crates/signed_app/src/repo/files.rs b/crates/signed_app/src/repo/files.rs index 6ab684f..d26ab5a 100644 --- a/crates/signed_app/src/repo/files.rs +++ b/crates/signed_app/src/repo/files.rs @@ -119,14 +119,11 @@ impl RepoFilesView { fn rebuild_rows(&mut self) { self.rows = TreeItemSeed::flatten_all(&self.seeds, &self.expanded); - self.selected_index = self - .selected_file - .as_ref() - .and_then(|path| { - self.rows - .iter() - .position(|row| row.key().to_string() == path.as_ref()) - }); + self.selected_index = self.selected_file.as_ref().and_then(|path| { + self.rows + .iter() + .position(|row| row.key().to_string() == path.as_ref()) + }); } fn toggle_folder(&mut self, id: &str) { @@ -281,10 +278,7 @@ impl RepoFilesView { let view = disclosure_view; move |index, _, cx| { if let Err(error) = view.update(cx, |this, cx| { - let id = this - .rows - .get(index) - .map(|row| row.key().to_string()); + let id = this.rows.get(index).map(|row| row.key().to_string()); if let Some(id) = id { this.toggle_folder(&id); cx.emit(RepoFilesEvent::Activated); @@ -310,9 +304,9 @@ impl RepoFilesView { self.code_element(path.as_ref()) } } - Some(FileContent::Binary) => EmptyState::centered( - "Binary file - preview not supported", - ), + Some(FileContent::Binary) => { + EmptyState::centered("Binary file - preview not supported") + } Some(FileContent::TooLarge) => EmptyState::centered("File is too large to preview"), Some(FileContent::Failed(message)) => EmptyState::centered(message), None => Spinner::centered("repo-preview-spinner"), diff --git a/crates/signed_app/src/repo/helpers.rs b/crates/signed_app/src/repo/helpers.rs index 141eb67..18a1157 100644 --- a/crates/signed_app/src/repo/helpers.rs +++ b/crates/signed_app/src/repo/helpers.rs @@ -174,7 +174,10 @@ pub(super) fn nostr_clone_url(announcement: &Announcement, nip05: Option<&str>) } /// The display name of the announcement addressing `addr`, falling back to its identifier. -pub(crate) fn upstream_display_name(addr: &RepoAddr, announcements: &[Announcement]) -> SharedString { +pub(crate) fn upstream_display_name( + addr: &RepoAddr, + announcements: &[Announcement], +) -> SharedString { announcements .iter() .find(|a| a.addr() == *addr) @@ -195,10 +198,8 @@ pub(super) fn fork_row( let (label, clickable) = match &upstream.addr { Some(addr) => { - let name = upstream_display_name( - addr, - &RepoListStore::global(cx).read(cx).announcements, - ); + let name = + upstream_display_name(addr, &RepoListStore::global(cx).read(cx).announcements); (SharedString::from(format!("Forked from {name}")), true) } None => (SharedString::from(upstream.display().as_str()), false), diff --git a/crates/signed_app/src/repo/mod.rs b/crates/signed_app/src/repo/mod.rs index 055d9a3..6bc1ff1 100644 --- a/crates/signed_app/src/repo/mod.rs +++ b/crates/signed_app/src/repo/mod.rs @@ -43,8 +43,8 @@ pub mod refs; use files::RepoFilesView; use helpers::{ - RepoData, announcement_display_name, bound_repo_label, clone_group, fork_row, - load_repo_data, nostr_clone_url, repo_display_name, snapshot_worktree, + RepoData, announcement_display_name, bound_repo_label, clone_group, fork_row, load_repo_data, + nostr_clone_url, repo_display_name, snapshot_worktree, }; use history::RepoHistoryView; use refs::RefSwitcher; @@ -52,11 +52,10 @@ use utils::relative_time_secs; use crate::issues::IssuesView; use crate::pull_requests::PullRequestsView; +use crate::repo::helpers::{short_id, sorted_worktree_paths}; use crate::shared::log_dropped; use crate::workspace::Workspace; -use crate::repo::helpers::{short_id, sorted_worktree_paths}; - #[derive(Clone, Copy, PartialEq, Eq)] pub(super) enum RepoSection { Files, @@ -479,6 +478,7 @@ impl RepoDetailView { if let Ok(Some((moved, snapshot))) = refresh { let branches: Vec = snapshot.branches.iter().map(Into::into).collect(); + let tags: Vec = snapshot.tags.iter().map(Into::into).collect(); let current_branch = snapshot.current_branch.clone(); let head_commit = snapshot.head_commit.clone(); diff --git a/crates/signed_app/src/shared/panels.rs b/crates/signed_app/src/shared/panels.rs index 140dc46..70758a6 100644 --- a/crates/signed_app/src/shared/panels.rs +++ b/crates/signed_app/src/shared/panels.rs @@ -96,14 +96,7 @@ pub fn open_primary( P: Panel + 'static, F: Fn(&mut Window, &mut App) -> Entity

+ 'static, { - open_slot( - PanelSlot::Main, - workspace, - id, - build, - window, - cx, - ); + open_slot(PanelSlot::Main, workspace, id, build, window, cx); } /// Opens a detail panel beside the main panel, reusing it when it is already cached. @@ -117,12 +110,5 @@ pub fn open_detail( P: Panel + 'static, F: Fn(&mut Window, &mut App) -> Entity

+ 'static, { - open_slot( - PanelSlot::Detail, - workspace, - id, - build, - window, - cx, - ); + open_slot(PanelSlot::Detail, workspace, id, build, window, cx); } diff --git a/crates/signed_app/src/titlebar/mod.rs b/crates/signed_app/src/titlebar/mod.rs index 19eb66b..8b8a223 100644 --- a/crates/signed_app/src/titlebar/mod.rs +++ b/crates/signed_app/src/titlebar/mod.rs @@ -2,7 +2,7 @@ use std::rc::Rc; use std::sync::Arc; use gpui::{ - AnyElement, AnyView, App, AppContext as _, Context, Entity, IntoElement, KeyBinding, + AnyElement, App, AppContext as _, Context, Entity, IntoElement, KeyBinding, KeyBindingContextPredicate, ParentElement as _, SharedString, Subscription, WeakEntity, Window, }; use metadata::{Profile, ProfileDirectory}; @@ -16,6 +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::menu::{Menu, MenuItem}; use ui::tabs::{Tab, Tabs, TabsVariant}; use ui::tooltip::Tooltip; @@ -65,12 +66,7 @@ pub struct TitlebarView { local_repos: Arc>, /// Profiles rendered by the repository switcher. directory: Entity, - /// The command palette opened from the titlebar. - palette: Entity, repository_switcher: Entity, - settings: Option>, - exit: Option>, - create: Option>, user_menu_open: bool, _subscriptions: Vec, } @@ -84,10 +80,10 @@ impl TitlebarView { let backend = Backend::global(cx); let repos = RepoListStore::global(cx); let local = LocalReposStore::global(cx); + let directory = ProfileDirectory::new(cx); let weak = cx.entity().downgrade(); - let palette = Self::new_palette(weak.clone(), window, cx); - let directory = ProfileDirectory::new(cx); + Root::open_dialog(Self::new_palette(weak.clone(), window, cx), cx); let entity = cx.entity(); let mut subscriptions = Vec::new(); @@ -156,11 +152,7 @@ impl TitlebarView { announcements: Arc::new(Vec::new()), local_repos: Arc::new(Vec::new()), directory, - palette, repository_switcher, - settings: None, - exit: None, - create: None, user_menu_open: false, _subscriptions: subscriptions, } @@ -215,10 +207,6 @@ impl TitlebarView { ] } - pub fn palette(&self) -> Entity { - self.palette.clone() - } - fn refresh(&mut self, cx: &mut Context) -> bool { let backend = Backend::global(cx); let user = backend.read(cx).current_user(); @@ -377,33 +365,21 @@ impl TitlebarView { } fn open_settings(&mut self, cx: &mut Context) { - self.settings = Some(SettingsDialog::new(cx)); + Root::open_dialog(SettingsDialog::new(cx), cx); cx.notify(); } fn open_exit(&mut self, cx: &mut Context) { - self.exit = Some(ExitDialog::new(cx)); + Root::open_dialog(ExitDialog::new(cx), cx); cx.notify(); } fn open_create(&mut self, window: &mut Window, cx: &mut Context) { let view = dialogs::create::CreateRepoDialog::new(self.workspace.clone(), window, cx); - self.create = Some(view); + Root::open_dialog(view, cx); cx.notify(); } - /// The dialogs this titlebar opens, rendered at the window root. - pub fn dialogs(&self) -> Vec { - [ - self.settings.clone().map(Into::into), - self.exit.clone().map(Into::into), - self.create.clone().map(Into::into), - ] - .into_iter() - .flatten() - .collect() - } - fn activate(&mut self, index: usize, window: &mut Window, cx: &mut Context) { if let Some(announcement) = self.announcements.get(index) { let addr = announcement.addr(); diff --git a/crates/ui/src/layout/mod.rs b/crates/ui/src/layout/mod.rs index d66a3c5..b342b6f 100644 --- a/crates/ui/src/layout/mod.rs +++ b/crates/ui/src/layout/mod.rs @@ -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; diff --git a/crates/ui/src/layout/root.rs b/crates/ui/src/layout/root.rs new file mode 100644 index 0000000..63aa41e --- /dev/null +++ b/crates/ui/src/layout/root.rs @@ -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, +} + +impl Global for RootGlobal {} + +/// The window's content and floating-surface host. +pub struct Root { + content: Option, + dialogs: Vec, + toasts: Entity, + alerts: Entity, + 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 { + 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> { + cx.try_global::() + .and_then(|global| global.root.upgrade()) + } + + /// Sets the application content view. + pub fn set_content(&mut self, content: impl Into, cx: &mut Context) { + self.content = Some(content.into()); + cx.notify(); + } + + /// The root's toast store. + pub fn toasts(&self) -> Entity { + self.toasts.clone() + } + + /// The root's alert store. + pub fn alerts(&self) -> Entity { + 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 a dialog view above the content. + fn mount_dialog(&mut self, view: impl Into, cx: &mut Context) { + 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) -> 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())) + } +} diff --git a/crates/ui/src/layout/shell.rs b/crates/ui/src/layout/shell.rs deleted file mode 100644 index eaf1430..0000000 --- a/crates/ui/src/layout/shell.rs +++ /dev/null @@ -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, - panel: Option, - transparent: bool, - grain: bool, - toasts: Option>, - alerts: Option>, - style: StyleRefinement, -} - -impl Shell { - /// Creates the shell. - pub fn new(id: I) -> Self - where - I: Into, - { - 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(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) -> Self { - self.toasts = Some(toasts); - self - } - - /// Hosts the alert overlay stacked on `alerts`. - pub fn alerts(mut self, alerts: Entity) -> 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)) - }) - } -} diff --git a/crates/ui/src/layout/titlebar.rs b/crates/ui/src/layout/titlebar.rs index 85c8d1d..716387b 100644 --- a/crates/ui/src/layout/titlebar.rs +++ b/crates/ui/src/layout/titlebar.rs @@ -22,7 +22,6 @@ pub struct Titlebar { prefix: Vec, tab_bar: Option, suffix: Vec, - 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))