refactor: replace shell with root view wrapping app content
This commit is contained in:
@@ -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<Workspace>,
|
||||
titlebar: Entity<TitlebarView>,
|
||||
toasts: Entity<Toasts>,
|
||||
sign_in: Entity<SignedOutView>,
|
||||
explore: Entity<ExploreView>,
|
||||
empty: Entity<PlaceholderView>,
|
||||
@@ -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>) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<AppRoot> {
|
||||
cx.new(|cx| AppRoot::new(window, cx))
|
||||
pub fn root(window: &mut Window, cx: &mut App) -> Entity<Root> {
|
||||
let root = cx.new(|cx| Root::new(window, cx));
|
||||
let content: Entity<AppRoot> = cx.new(|cx| AppRoot::new(window, cx));
|
||||
|
||||
root.update(cx, |root, cx| root.set_content(content, cx));
|
||||
root
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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<SharedString> =
|
||||
snapshot.branches.iter().map(Into::into).collect();
|
||||
|
||||
let tags: Vec<SharedString> = snapshot.tags.iter().map(Into::into).collect();
|
||||
let current_branch = snapshot.current_branch.clone();
|
||||
let head_commit = snapshot.head_commit.clone();
|
||||
|
||||
@@ -96,14 +96,7 @@ pub fn open_primary<P, F>(
|
||||
P: Panel + 'static,
|
||||
F: Fn(&mut Window, &mut App) -> Entity<P> + '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, F>(
|
||||
P: Panel + 'static,
|
||||
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
|
||||
{
|
||||
open_slot(
|
||||
PanelSlot::Detail,
|
||||
workspace,
|
||||
id,
|
||||
build,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
open_slot(PanelSlot::Detail, workspace, id, build, window, cx);
|
||||
}
|
||||
|
||||
@@ -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<Vec<ResolvedLocalRepo>>,
|
||||
/// Profiles rendered by the repository switcher.
|
||||
directory: Entity<ProfileDirectory>,
|
||||
/// The command palette opened from the titlebar.
|
||||
palette: Entity<CommandPaletteState>,
|
||||
repository_switcher: Entity<RepositorySwitcher>,
|
||||
settings: Option<Entity<SettingsDialog>>,
|
||||
exit: Option<Entity<ExitDialog>>,
|
||||
create: Option<Entity<dialogs::create::CreateRepoDialog>>,
|
||||
user_menu_open: bool,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
@@ -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<CommandPaletteState> {
|
||||
self.palette.clone()
|
||||
}
|
||||
|
||||
fn refresh(&mut self, cx: &mut Context<Self>) -> 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>) {
|
||||
self.settings = Some(SettingsDialog::new(cx));
|
||||
Root::open_dialog(SettingsDialog::new(cx), cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn open_exit(&mut self, cx: &mut Context<Self>) {
|
||||
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<Self>) {
|
||||
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<AnyView> {
|
||||
[
|
||||
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<Self>) {
|
||||
if let Some(announcement) = self.announcements.get(index) {
|
||||
let addr = announcement.addr();
|
||||
|
||||
@@ -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