From bef96c614b0035512ae7c4f19735ac7da92805fa Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Mon, 23 Nov 2026 03:26:11 +0700 Subject: [PATCH] refactor: merge workspace state into app root --- crates/signed_app/src/app/mod.rs | 343 ++++++++++++++++-- crates/signed_app/src/explore/helpers.rs | 4 +- crates/signed_app/src/explore/mod.rs | 8 +- crates/signed_app/src/inbox/mod.rs | 6 +- crates/signed_app/src/issues/mod.rs | 6 +- crates/signed_app/src/lib.rs | 1 - crates/signed_app/src/pull_requests/detail.rs | 6 +- crates/signed_app/src/pull_requests/mod.rs | 6 +- crates/signed_app/src/pull_requests/new.rs | 6 +- .../src/pull_requests/send_patch.rs | 6 +- crates/signed_app/src/repo/actions.rs | 6 +- crates/signed_app/src/repo/dialogs/create.rs | 10 +- crates/signed_app/src/repo/history.rs | 6 +- crates/signed_app/src/repo/mod.rs | 12 +- crates/signed_app/src/shared/panels.rs | 14 +- crates/signed_app/src/titlebar/mod.rs | 75 +--- crates/signed_app/src/workspace.rs | 244 ------------- 17 files changed, 374 insertions(+), 385 deletions(-) delete mode 100644 crates/signed_app/src/workspace.rs diff --git a/crates/signed_app/src/app/mod.rs b/crates/signed_app/src/app/mod.rs index 6b21a35..906c248 100644 --- a/crates/signed_app/src/app/mod.rs +++ b/crates/signed_app/src/app/mod.rs @@ -2,19 +2,24 @@ pub mod helpers; mod placeholder; mod sign_in; +use std::collections::HashMap; +use std::sync::Arc; + use gpui::prelude::FluentBuilder as _; use gpui::{ - AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, - Render, Styled as _, Subscription, Window, div, px, + App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, + ParentElement as _, Render, SharedString, Styled as _, Subscription, WeakEntity, Window, div, + px, }; use helpers::apply_appearance_mode; use settings::SettingsStore; use signed_state::{Backend, BackendEvent}; use ui::common::{h_flex, v_flex}; use ui::icons::IconRole; -use ui::layout::panel::PanelRow; +use ui::layout::panel::{Panel, PanelRow, PanelView}; use ui::layout::root::RootContext as _; use ui::layout::titlebar::Titlebar; +use ui::tabs::{Tab, Tabs, TabsVariant}; use ui::toast::Toast; use crate::app::placeholder::PlaceholderView; @@ -26,10 +31,16 @@ use crate::titlebar::{ CreateRepository, REPOSITORY_KEY_CONTEXT, SwitchRepository, TitlebarView, bind_repository_shortcuts, }; -use crate::workspace::Workspace; pub struct AppRoot { - workspace: Entity, + /// The repository shown in the main panel and the detail panels beside it. + main: Option, + main_order: Vec, + main_views: HashMap>, + details: Vec, + detail_views: HashMap>, + active_detail: Option, + detail_pane: Entity, titlebar: Entity, sign_in: Entity, explore: Entity, @@ -42,11 +53,11 @@ impl AppRoot { pub fn new(window: &mut Window, cx: &mut Context) -> Self { bind_repository_shortcuts(cx); - let workspace = cx.new(|_| Workspace::new()); - let titlebar = cx.new(|cx| TitlebarView::new(workspace.downgrade(), window, cx)); - + let app = cx.entity().downgrade(); + let detail_pane = cx.new(|_| DetailPane { app: app.clone() }); + let titlebar = cx.new(|cx| TitlebarView::new(app.clone(), window, cx)); let sign_in = cx.new(|_| SignedOutView::new()); - let explore = cx.new(|cx| ExploreView::new(workspace.downgrade(), cx)); + let explore = cx.new(|cx| ExploreView::new(app.clone(), cx)); let empty = cx.new(|_| PlaceholderView::new("Select a repository")); let backend = Backend::global(cx); @@ -64,12 +75,17 @@ impl AppRoot { cx.toast(Toast::new(message.clone()).icon(IconRole::Warning)); } BackendEvent::SignerChanged => { - this.workspace.update(cx, |workspace, cx| { - workspace.clear(cx); + let app = cx.entity().downgrade(); + let backend = backend.clone(); + // Fires while AppRoot may still be under construction; + // opening panels reads the root, so run outside its lease. + window.defer(cx, move |window, cx| { + let signed_in = backend.read(cx).current_user().is_some(); + log_dropped(app.update(cx, |this, cx| this.clear(cx))); + if signed_in { + ExploreView::open(&app, window, cx); + } }); - if backend.read(cx).current_user().is_some() { - ExploreView::open(&this.workspace.downgrade(), window, cx); - } } _ => {} }, @@ -80,7 +96,6 @@ impl AppRoot { apply_appearance_mode(settings.appearance, cx); })); - let app = cx.entity().downgrade(); window.defer(cx, move |_window, cx| { if backend.read(cx).passphrase_required() { log_dropped(app.update(cx, |this, cx| this.open_passphrase(cx))); @@ -88,7 +103,13 @@ impl AppRoot { }); Self { - workspace, + main: None, + main_order: Vec::new(), + main_views: HashMap::new(), + details: Vec::new(), + detail_views: HashMap::new(), + active_detail: None, + detail_pane, titlebar, sign_in, explore, @@ -106,6 +127,210 @@ impl AppRoot { self.passphrase = Some(dialog.clone()); cx.open_dialog(dialog); } + + pub fn active_main(&self) -> Option> { + self.main + .as_ref() + .and_then(|id| self.main_views.get(id)) + .cloned() + } + + pub fn active_main_id(&self) -> Option<&SharedString> { + self.main.as_ref() + } + + pub fn open_mains(&self) -> &[SharedString] { + &self.main_order + } + + /// The open main panel with this id. + pub fn main_view(&self, id: &SharedString) -> Option> { + self.main_views.get(id).cloned() + } + + pub fn active_detail(&self) -> Option> { + self.active_detail + .as_ref() + .and_then(|id| self.detail_views.get(id)) + .cloned() + } + + pub fn has_main(&self, id: &SharedString) -> bool { + self.main_views.contains_key(id) + } + + pub fn has_details(&self) -> bool { + !self.details.is_empty() + } + + pub fn has_detail(&self, id: &SharedString) -> bool { + self.detail_views.contains_key(id) + } + + pub fn show_main( + &mut self, + id: SharedString, + view: Arc, + cx: &mut Context, + ) { + if self.main.as_ref() != Some(&id) { + self.clear_details(); + } + if !self.main_views.contains_key(&id) { + self.main_order.push(id.clone()); + } + self.main_views.insert(id.clone(), view); + self.main = Some(id); + cx.notify(); + } + + pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context) { + if self.main.as_ref() != Some(id) { + self.clear_details(); + } + self.main = Some(id.clone()); + cx.notify(); + } + + pub fn activate_detail(&mut self, id: &SharedString, cx: &mut Context) { + if self.detail_views.contains_key(id) { + self.active_detail = Some(id.clone()); + cx.notify(); + } + } + + pub fn show_detail( + &mut self, + id: SharedString, + view: Arc, + cx: &mut Context, + ) { + if !self.details.contains(&id) { + self.details.push(id.clone()); + } + self.detail_views.insert(id.clone(), view); + self.active_detail = Some(id); + cx.notify(); + } + + pub fn close_detail(&mut self, id: &SharedString, cx: &mut Context) { + let Some(index) = self.details.iter().position(|open| open == id) else { + return; + }; + self.close_detail_at(index, cx); + } + + pub fn close_main_at(&mut self, index: usize, cx: &mut Context) { + if index >= self.main_order.len() { + return; + } + + let id = self.main_order.remove(index); + self.main_views.remove(&id); + + if self.main.as_ref() == Some(&id) { + self.main = self + .main_order + .get(index) + .or_else(|| { + index + .checked_sub(1) + .and_then(|prev| self.main_order.get(prev)) + }) + .cloned(); + self.clear_details(); + } + + cx.notify(); + } + + pub fn clear(&mut self, cx: &mut Context) { + self.main = None; + self.main_order.clear(); + self.main_views.clear(); + self.clear_details(); + cx.notify(); + } + + fn clear_details(&mut self) { + self.details.clear(); + self.detail_views.clear(); + self.active_detail = None; + } + + fn close_detail_at(&mut self, index: usize, cx: &mut Context) { + let id = self.details.remove(index); + self.detail_views.remove(&id); + + if self.active_detail.as_ref() == Some(&id) { + self.active_detail = self + .details + .get(index) + .or_else(|| index.checked_sub(1).and_then(|prev| self.details.get(prev))) + .cloned(); + } + + cx.notify(); + } + + fn activate_detail_at(&mut self, index: usize, cx: &mut Context) { + let Some(id) = self.details.get(index) else { + return; + }; + self.active_detail = Some(id.clone()); + cx.notify(); + } + + /// Tabs for the repositories opened in the main panel. + fn render_repo_tabs(&self, cx: &mut Context) -> Option { + let open = self.open_mains(); + + if open.is_empty() { + return None; + } + + let tabs: Vec<_> = open + .iter() + .map(|id| (id.clone(), self.main_view(id))) + .collect(); + + let selected = self + .active_main_id() + .and_then(|id| open.iter().position(|open_id| open_id == id)) + .unwrap_or(usize::MAX); + + let app = cx.entity().downgrade(); + let close_app = app.clone(); + let open_for_change = open.to_vec(); + + Some( + Tabs::new("signed-repo-tabs") + .variant(TabsVariant::Titlebar) + .overflow_scroll(true) + .selected(selected) + .tabs(move |_, cx| { + tabs.iter() + .map(|(id, view)| match view { + Some(view) => view.tab(id.clone().into(), cx).closeable(true), + None => Tab::new(id.clone(), id.clone()).closeable(true), + }) + .collect() + }) + .on_change(move |index, _, cx| { + let Some(id) = open_for_change.get(index).cloned() else { + return; + }; + log_dropped(app.update(cx, |app, cx| { + app.activate_main(&id, cx); + })); + }) + .on_close(move |index, _, cx| { + log_dropped(close_app.update(cx, |app, cx| { + app.close_main_at(index, cx); + })); + }), + ) + } } impl Render for AppRoot { @@ -113,9 +338,9 @@ impl Render for AppRoot { let backend = Backend::global(cx); let signed_in = backend.read(cx).current_user().is_some(); + let repo_tabs = self.render_repo_tabs(cx); let titlebar_view = self.titlebar.read(cx); - let workspace = self.workspace.read(cx); - let main = workspace.active_main(); + let main = self.active_main(); div() .relative() @@ -144,10 +369,11 @@ impl Render for AppRoot { .child(titlebar_view.render_explore()) .child(titlebar_view.repository_switcher()), ) - .tab_bar(h_flex().gap_1().when_some( - titlebar_view.render_repo_tabs(workspace), - |bar, tabs| bar.child(tabs), - )) + .tab_bar( + h_flex() + .gap_1() + .when_some(repo_tabs, |bar, tabs| bar.child(tabs)), + ) .when_some(titlebar_view.render_user_menu(cx), |titlebar, menu| { titlebar.suffix(menu) }), @@ -155,8 +381,8 @@ impl Render for AppRoot { .child( PanelRow::new("signed-panel") .when_some(main.as_ref(), |row, main| { - row.main(main.clone()).when(workspace.has_details(), |row| { - row.split(self.workspace.clone()) + row.main(main.clone()).when(self.has_details(), |row| { + row.split(self.detail_pane.clone()) }) }) .when(!main.is_some() && signed_in, |row| { @@ -170,3 +396,72 @@ impl Render for AppRoot { ) } } + +/// The split pane beside the main panel: detail tabs over the active detail. +struct DetailPane { + app: WeakEntity, +} + +impl Panel for DetailPane { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Workspace") + } +} + +impl Render for DetailPane { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + let Some(app) = self.app.upgrade() else { + return div().into_any_element(); + }; + let app = app.read(cx); + + let selected = app + .active_detail + .as_ref() + .and_then(|active| app.details.iter().position(|id| id == active)) + .unwrap_or(usize::MAX); + + let details: Vec<_> = app + .details + .iter() + .map(|id| (id.clone(), app.detail_views.get(id).cloned())) + .collect(); + + let weak = self.app.clone(); + let body = app.active_detail().map(|view| view.view()); + + v_flex() + .size_full() + .gap_2() + .when(app.details.len() > 1, |this| { + let close_weak = weak.clone(); + this.child( + Tabs::new("signed-detail-tabs") + .variant(TabsVariant::Pill) + .overflow_scroll(true) + .selected(selected) + .tabs(move |_, cx| { + details + .iter() + .map(|(id, view)| match view { + Some(view) => view.tab(id.clone().into(), cx).closeable(true), + None => Tab::new(id.clone(), id.clone()).closeable(true), + }) + .collect() + }) + .on_change(move |index, _, cx| { + log_dropped(weak.update(cx, |app, cx| { + app.activate_detail_at(index, cx); + })); + }) + .on_close(move |index, _, cx| { + log_dropped(close_weak.update(cx, |app, cx| { + app.close_detail_at(index, cx); + })); + }), + ) + }) + .child(div().flex_1().min_h_0().w_full().children(body)) + .into_any_element() + } +} diff --git a/crates/signed_app/src/explore/helpers.rs b/crates/signed_app/src/explore/helpers.rs index 2c55db1..ea27f95 100644 --- a/crates/signed_app/src/explore/helpers.rs +++ b/crates/signed_app/src/explore/helpers.rs @@ -12,9 +12,9 @@ use ui::icons::{Icon, IconRole}; use ui::theme::ActiveTheme as _; use utils::relative_time; +use crate::app::AppRoot; use crate::repo::actions::open_repo_panel; use crate::repo::helpers::{announcement_display_name, upstream_display_name}; -use crate::workspace::Workspace; const RECENT_COUNT: usize = 10; @@ -55,7 +55,7 @@ pub(super) fn render_card( last_activity: Option, announcements: &[Announcement], directory: &Entity, - workspace: &WeakEntity, + workspace: &WeakEntity, cx: &mut gpui::App, ) -> AnyElement { let owner = directory.read(cx).profile(&announcement.owner, cx); diff --git a/crates/signed_app/src/explore/mod.rs b/crates/signed_app/src/explore/mod.rs index 5555651..9ee1d4b 100644 --- a/crates/signed_app/src/explore/mod.rs +++ b/crates/signed_app/src/explore/mod.rs @@ -18,8 +18,8 @@ use ui::scrollbar::Scrollbar; use ui::segmented::{Segmented, SegmentedOption}; use ui::theme::ActiveTheme as _; +use crate::app::AppRoot; use crate::shared::panels; -use crate::workspace::Workspace; const COLUMNS: usize = 2; // Name row, avatar strip, meta row, and their borders and gaps, in px. @@ -27,7 +27,7 @@ const CARD_HEIGHT: f32 = 40. + 64. + 48. + 2. + 6.; pub struct ExploreView { store: Entity, - workspace: WeakEntity, + workspace: WeakEntity, scroll_handle: ListState, filter: RepoFilter, /// Indices matching [`Self::filter`] into the store's `announcements`. @@ -44,7 +44,7 @@ impl ExploreView { pub(crate) const PANEL_ID: &'static str = "Explore"; /// Opens the explore panel in the main panel, reusing it when it is already cached. - pub(crate) fn open(workspace: &WeakEntity, window: &mut Window, cx: &mut App) { + pub(crate) fn open(workspace: &WeakEntity, window: &mut Window, cx: &mut App) { panels::open_primary( workspace, Self::PANEL_ID, @@ -57,7 +57,7 @@ impl ExploreView { ); } - pub fn new(workspace: WeakEntity, cx: &mut Context) -> Self { + pub fn new(workspace: WeakEntity, cx: &mut Context) -> Self { let store = RepoListStore::global(cx); let directory = ProfileDirectory::new(cx); diff --git a/crates/signed_app/src/inbox/mod.rs b/crates/signed_app/src/inbox/mod.rs index bedc24f..35fd648 100644 --- a/crates/signed_app/src/inbox/mod.rs +++ b/crates/signed_app/src/inbox/mod.rs @@ -24,9 +24,9 @@ mod helpers; use helpers::{empty_section_row, repo_header, thread}; +use crate::app::AppRoot; use crate::repo::actions::{RepoItem, open_repo_item}; use crate::repo::helpers::announcement_display_name; -use crate::workspace::Workspace; const MAX_SUB_ACTIVITIES: usize = 5; @@ -45,7 +45,7 @@ struct InboxSection { } pub struct InboxView { - workspace: WeakEntity, + workspace: WeakEntity, threads: Arc>, sections: Arc>, rows: Arc>, @@ -60,7 +60,7 @@ pub struct InboxView { } impl InboxView { - pub fn new(workspace: WeakEntity, cx: &mut Context) -> Self { + pub fn new(workspace: WeakEntity, cx: &mut Context) -> Self { let backend = Backend::global(cx); let inbox = backend.read(cx).inbox(); diff --git a/crates/signed_app/src/issues/mod.rs b/crates/signed_app/src/issues/mod.rs index 0196b9d..fbc1922 100644 --- a/crates/signed_app/src/issues/mod.rs +++ b/crates/signed_app/src/issues/mod.rs @@ -19,18 +19,18 @@ use utils::relative_time; use std::rc::Rc; use self::detail::IssueDetailView; +use crate::app::AppRoot; use crate::repo::helpers::short_id; use crate::shared::event_list::{EventHandler, EventList, EventRow, filter_toolbar}; use crate::shared::log_dropped; use crate::shared::panels; -use crate::workspace::Workspace; pub mod detail; pub(crate) mod helpers; pub mod new_issue; pub struct IssuesView { - workspace: WeakEntity, + workspace: WeakEntity, store: Entity, new_issue: Option>, filter: IssueFilter, @@ -46,7 +46,7 @@ pub struct IssuesView { impl IssuesView { pub fn new( - workspace: WeakEntity, + workspace: WeakEntity, store: Entity, cx: &mut Context, ) -> Self { diff --git a/crates/signed_app/src/lib.rs b/crates/signed_app/src/lib.rs index 5ac65d4..43c1ae1 100644 --- a/crates/signed_app/src/lib.rs +++ b/crates/signed_app/src/lib.rs @@ -9,7 +9,6 @@ mod repo; mod settings; mod shared; mod titlebar; -mod workspace; pub use app::AppRoot; pub use app::helpers::apply_appearance_mode; diff --git a/crates/signed_app/src/pull_requests/detail.rs b/crates/signed_app/src/pull_requests/detail.rs index 23f98fb..ec94902 100644 --- a/crates/signed_app/src/pull_requests/detail.rs +++ b/crates/signed_app/src/pull_requests/detail.rs @@ -27,13 +27,13 @@ use ui::theme::ActiveTheme as _; use utils::relative_time; use super::dialogs; +use crate::app::AppRoot; use crate::issues::helpers::{comment_form, comments_section, sidebar_section}; use crate::repo::commit_diff::{CommitDiffView, DiffPane}; use crate::shared::event_list::commit_list; use crate::shared::log_dropped; use crate::shared::panels::open_detail; use crate::shared::widgets::status_badge; -use crate::workspace::Workspace; const NOT_FOUND: &str = "Pull request not found"; @@ -49,7 +49,7 @@ struct PrBinding { } pub struct PullRequestDetailView { - workspace: WeakEntity, + workspace: WeakEntity, store: Entity, pr_id: EventId, update_dialog: Option>, @@ -72,7 +72,7 @@ pub struct PullRequestDetailView { impl PullRequestDetailView { pub fn new( - workspace: WeakEntity, + workspace: WeakEntity, store: Entity, pr_id: EventId, _window: &mut Window, diff --git a/crates/signed_app/src/pull_requests/mod.rs b/crates/signed_app/src/pull_requests/mod.rs index 0407b0d..3ad6cff 100644 --- a/crates/signed_app/src/pull_requests/mod.rs +++ b/crates/signed_app/src/pull_requests/mod.rs @@ -29,15 +29,15 @@ use helpers::PullRequestFilter; pub use new::NewPullRequestView; pub use send_patch::SendPatchView; +use crate::app::AppRoot; use crate::issues::helpers::{StatusCounts, filter_by_status}; use crate::repo::helpers::short_id; use crate::shared::event_list::{EventHandler, EventList, EventRow, filter_toolbar}; use crate::shared::log_dropped; use crate::shared::panels::open_detail; -use crate::workspace::Workspace; pub struct PullRequestsView { - workspace: WeakEntity, + workspace: WeakEntity, store: Entity, filter: PullRequestFilter, visible_prs: Vec, @@ -51,7 +51,7 @@ pub struct PullRequestsView { impl PullRequestsView { pub fn new( - workspace: WeakEntity, + workspace: WeakEntity, store: Entity, cx: &mut Context, ) -> Self { diff --git a/crates/signed_app/src/pull_requests/new.rs b/crates/signed_app/src/pull_requests/new.rs index 9cd51d2..05ecdaf 100644 --- a/crates/signed_app/src/pull_requests/new.rs +++ b/crates/signed_app/src/pull_requests/new.rs @@ -23,16 +23,16 @@ use ui::spinner::Spinner; use ui::tabs::{Tab, Tabs}; use super::helpers::{ForkCompare, truncate_label}; +use crate::app::AppRoot; use crate::repo::commit_diff::{CommitDiffView, DiffPane}; use crate::repo::helpers::announcement_display_name; use crate::shared::event_list::commit_list; use crate::shared::log_dropped; use crate::shared::panels::open_detail; use crate::shared::progress::error_row; -use crate::workspace::Workspace; pub struct NewPullRequestView { - workspace: WeakEntity, + workspace: WeakEntity, panel_id: SharedString, pub(super) store: Entity, pub(super) repo_path: Option, @@ -61,7 +61,7 @@ pub struct NewPullRequestView { impl NewPullRequestView { pub fn new( - workspace: WeakEntity, + workspace: WeakEntity, panel_id: SharedString, store: Entity, window: &mut Window, diff --git a/crates/signed_app/src/pull_requests/send_patch.rs b/crates/signed_app/src/pull_requests/send_patch.rs index 20e947c..71e96e1 100644 --- a/crates/signed_app/src/pull_requests/send_patch.rs +++ b/crates/signed_app/src/pull_requests/send_patch.rs @@ -11,12 +11,12 @@ use ui::input::{Input, InputState}; use ui::layout::panel::Panel; use ui::theme::ActiveTheme as _; +use crate::app::AppRoot; use crate::shared::log_dropped; use crate::shared::progress::error_row; -use crate::workspace::Workspace; pub struct SendPatchView { - workspace: WeakEntity, + workspace: WeakEntity, panel_id: SharedString, store: Entity, subject: Entity, @@ -28,7 +28,7 @@ pub struct SendPatchView { impl SendPatchView { pub fn new( - workspace: WeakEntity, + workspace: WeakEntity, panel_id: SharedString, store: Entity, _window: &mut Window, diff --git a/crates/signed_app/src/repo/actions.rs b/crates/signed_app/src/repo/actions.rs index 9794660..66c1922 100644 --- a/crates/signed_app/src/repo/actions.rs +++ b/crates/signed_app/src/repo/actions.rs @@ -3,9 +3,9 @@ use nostr::prelude::EventId; use signed_core::{Announcement, RepoAddr}; use signed_state::RepoStore; +use crate::app::AppRoot; use crate::repo::RepoDetailView; use crate::shared::panels; -use crate::workspace::Workspace; /// An item of a repository to open from outside its detail panel. pub enum RepoItem { @@ -16,7 +16,7 @@ pub enum RepoItem { /// Opens a repository as the main panel, keyed by its address. pub fn open_repo_panel( - workspace: &WeakEntity, + workspace: &WeakEntity, addr: &RepoAddr, hint: Option<&Announcement>, window: &mut Window, @@ -42,7 +42,7 @@ pub fn open_repo_panel( /// Opens an issue or pull request using a store built here. pub fn open_repo_item( - workspace: &WeakEntity, + workspace: &WeakEntity, addr: &RepoAddr, hint: Option<&Announcement>, item: RepoItem, diff --git a/crates/signed_app/src/repo/dialogs/create.rs b/crates/signed_app/src/repo/dialogs/create.rs index 9584cca..611c2d3 100644 --- a/crates/signed_app/src/repo/dialogs/create.rs +++ b/crates/signed_app/src/repo/dialogs/create.rs @@ -16,15 +16,15 @@ use ui::input::{Input, InputState}; use ui::tooltip::Tooltip; use super::grasp_servers::{GraspServersState, grasp_servers_field, load_user_grasp_servers}; +use crate::app::AppRoot; 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::workspace::Workspace; /// Progress of the create-repository flow, so async results can be rendered. pub struct CreateRepoDialog { - workspace: WeakEntity, + workspace: WeakEntity, handle: DialogHandle, name_input: Entity, desc_input: Entity, @@ -37,11 +37,7 @@ pub struct CreateRepoDialog { } impl CreateRepoDialog { - pub fn new( - workspace: WeakEntity, - window: &mut Window, - cx: &mut App, - ) -> Entity { + pub fn new(workspace: WeakEntity, window: &mut Window, cx: &mut App) -> Entity { let settings = SettingsStore::global(cx); let grasp_settings = settings.read(cx).settings().grasp_servers.clone(); diff --git a/crates/signed_app/src/repo/history.rs b/crates/signed_app/src/repo/history.rs index 9df0217..362fa5d 100644 --- a/crates/signed_app/src/repo/history.rs +++ b/crates/signed_app/src/repo/history.rs @@ -20,13 +20,13 @@ use ui::tooltip::Tooltip; use super::RepoDetailView; use super::commit_diff::CommitDiffView; +use crate::app::AppRoot; use crate::shared::event_list::commit_list; use crate::shared::panels; -use crate::workspace::Workspace; pub struct RepoHistoryView { store: Entity, - workspace: WeakEntity, + workspace: WeakEntity, detail: WeakEntity, sync_directory: Entity, worktree: Option, @@ -43,7 +43,7 @@ pub struct RepoHistoryView { impl RepoHistoryView { pub fn new( store: Entity, - workspace: WeakEntity, + workspace: WeakEntity, detail: WeakEntity, sync_directory: Entity, ) -> Self { diff --git a/crates/signed_app/src/repo/mod.rs b/crates/signed_app/src/repo/mod.rs index 6bc1ff1..beb6deb 100644 --- a/crates/signed_app/src/repo/mod.rs +++ b/crates/signed_app/src/repo/mod.rs @@ -50,11 +50,11 @@ use history::RepoHistoryView; use refs::RefSwitcher; use utils::relative_time_secs; +use crate::app::AppRoot; 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; #[derive(Clone, Copy, PartialEq, Eq)] pub(super) enum RepoSection { @@ -73,7 +73,7 @@ pub enum RefKind { } pub struct RepoDetailView { - pub(super) workspace: WeakEntity, + pub(super) workspace: WeakEntity, pub(super) store: Entity, about: Option>, init: Option>, @@ -112,7 +112,7 @@ pub struct RepoDetailView { impl RepoDetailView { pub fn new( - workspace: WeakEntity, + workspace: WeakEntity, addr: RepoAddr, hint: Option, window: &mut Window, @@ -123,7 +123,7 @@ impl RepoDetailView { } pub fn new_local( - workspace: WeakEntity, + workspace: WeakEntity, local_path: PathBuf, nip34: Option, window: &mut Window, @@ -135,7 +135,7 @@ impl RepoDetailView { /// A local repository whose detected binding matches an announcement. pub fn new_local_announced( - workspace: WeakEntity, + workspace: WeakEntity, announcement: Announcement, local_path: PathBuf, window: &mut Window, @@ -147,7 +147,7 @@ impl RepoDetailView { } fn new_common( - workspace: WeakEntity, + workspace: WeakEntity, store: Entity, window: &mut Window, cx: &mut Context, diff --git a/crates/signed_app/src/shared/panels.rs b/crates/signed_app/src/shared/panels.rs index 70758a6..62330b2 100644 --- a/crates/signed_app/src/shared/panels.rs +++ b/crates/signed_app/src/shared/panels.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use gpui::{App, Entity, SharedString, WeakEntity, Window}; use ui::layout::panel::{Panel, PanelView}; -use crate::workspace::Workspace; +use crate::app::AppRoot; /// Which slot of the workspace a panel opens into. #[derive(Clone, Copy)] @@ -14,7 +14,7 @@ enum PanelSlot { impl PanelSlot { /// Whether the slot already holds a panel under `id`. - fn has(self, workspace: &WeakEntity, id: &SharedString, cx: &App) -> bool { + fn has(self, workspace: &WeakEntity, id: &SharedString, cx: &App) -> bool { match self { PanelSlot::Main => workspace .read_with(cx, |workspace, _| workspace.has_main(id)) @@ -26,7 +26,7 @@ impl PanelSlot { } /// Activates the slot's cached panel under `id`. - fn activate(self, workspace: &WeakEntity, id: &SharedString, cx: &mut App) { + fn activate(self, workspace: &WeakEntity, id: &SharedString, cx: &mut App) { let result = match self { PanelSlot::Main => { workspace.update(cx, |workspace, cx| workspace.activate_main(id, cx)) @@ -43,7 +43,7 @@ impl PanelSlot { /// Shows the panel in the slot. fn show( self, - workspace: &WeakEntity, + workspace: &WeakEntity, id: SharedString, view: Arc, cx: &mut App, @@ -65,7 +65,7 @@ impl PanelSlot { /// Opens a panel in `slot`, reusing it when it is already cached. fn open_slot( slot: PanelSlot, - workspace: &WeakEntity, + workspace: &WeakEntity, id: impl Into, build: F, window: &mut Window, @@ -87,7 +87,7 @@ fn open_slot( /// Opens a repository in the main panel, reusing it when it is already cached. pub fn open_primary( - workspace: &WeakEntity, + workspace: &WeakEntity, id: impl Into, build: F, window: &mut Window, @@ -101,7 +101,7 @@ pub fn open_primary( /// Opens a detail panel beside the main panel, reusing it when it is already cached. pub fn open_detail( - workspace: &WeakEntity, + workspace: &WeakEntity, id: impl Into, build: F, window: &mut Window, diff --git a/crates/signed_app/src/titlebar/mod.rs b/crates/signed_app/src/titlebar/mod.rs index 9e90da9..0a1ba1d 100644 --- a/crates/signed_app/src/titlebar/mod.rs +++ b/crates/signed_app/src/titlebar/mod.rs @@ -15,18 +15,16 @@ use ui::avatar::Avatar; 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::RootContext as _; use ui::menu::{Menu, MenuItem}; -use ui::tabs::{Tab, Tabs, TabsVariant}; use ui::tooltip::Tooltip; +use crate::app::AppRoot; use crate::explore::ExploreView; use crate::identity::exit::ExitDialog; use crate::repo::{RepoDetailView, actions, dialogs}; use crate::settings::SettingsDialog; use crate::shared::{log_dropped, panels}; -use crate::workspace::Workspace; mod repository_switcher; @@ -59,7 +57,7 @@ pub(crate) struct CreateRepository; pub struct TitlebarView { weak: WeakEntity, - workspace: WeakEntity, + app: WeakEntity, /// User's announced repositories. announcements: Arc>, /// Local repositories found by the scan that are not announced yet. @@ -72,11 +70,7 @@ pub struct TitlebarView { } impl TitlebarView { - pub fn new( - workspace: WeakEntity, - window: &mut Window, - cx: &mut Context, - ) -> Self { + pub fn new(app: WeakEntity, window: &mut Window, cx: &mut Context) -> Self { let backend = Backend::global(cx); let repos = RepoListStore::global(cx); let local = LocalReposStore::global(cx); @@ -150,7 +144,7 @@ impl TitlebarView { Self { weak: cx.entity().downgrade(), - workspace, + app, announcements: Arc::new(Vec::new()), local_repos: Arc::new(Vec::new()), directory, @@ -298,57 +292,6 @@ impl TitlebarView { self.repository_switcher.clone() } - /// Tabs for the repositories opened in the main panel. - pub fn render_repo_tabs(&self, workspace: &Workspace) -> Option { - let open = workspace.open_mains(); - - if open.is_empty() { - return None; - } - - let tabs: Vec<_> = open - .iter() - .map(|id| (id.clone(), workspace.main_view(id))) - .collect(); - - let selected = workspace - .active_main_id() - .and_then(|id| open.iter().position(|open_id| open_id == id)) - .unwrap_or(usize::MAX); - - let change_workspace = self.workspace.clone(); - let close_workspace = self.workspace.clone(); - let open_for_change = open.to_vec(); - - Some( - Tabs::new("signed-repo-tabs") - .variant(TabsVariant::Titlebar) - .overflow_scroll(true) - .selected(selected) - .tabs(move |_, cx| { - tabs.iter() - .map(|(id, view)| match view { - Some(view) => view.tab(id.clone().into(), cx).closeable(true), - None => Tab::new(id.clone(), id.clone()).closeable(true), - }) - .collect() - }) - .on_change(move |index, _, cx| { - let Some(id) = open_for_change.get(index).cloned() else { - return; - }; - log_dropped(change_workspace.update(cx, |workspace, cx| { - workspace.activate_main(&id, cx); - })); - }) - .on_close(move |index, _, cx| { - log_dropped(close_workspace.update(cx, |workspace, cx| { - workspace.close_main_at(index, cx); - })); - }), - ) - } - pub(crate) fn activate_shortcut( &mut self, index: usize, @@ -379,7 +322,7 @@ impl TitlebarView { } fn open_create(&mut self, window: &mut Window, cx: &mut Context) { - let view = dialogs::create::CreateRepoDialog::new(self.workspace.clone(), window, cx); + let view = dialogs::create::CreateRepoDialog::new(self.app.clone(), window, cx); cx.open_dialog(view); cx.notify(); } @@ -387,7 +330,7 @@ impl TitlebarView { fn activate(&mut self, index: usize, window: &mut Window, cx: &mut Context) { if let Some(announcement) = self.announcements.get(index) { let addr = announcement.addr(); - actions::open_repo_panel(&self.workspace, &addr, Some(announcement), window, cx); + actions::open_repo_panel(&self.app, &addr, Some(announcement), window, cx); return; } @@ -416,7 +359,7 @@ impl TitlebarView { if let Some(announcement) = announcement { let id = SharedString::from(format!("repo:{}", announcement.addr())); - let workspace = self.workspace.clone(); + let workspace = self.app.clone(); let workspace_for_build = workspace.clone(); panels::open_primary( &workspace, @@ -439,7 +382,7 @@ impl TitlebarView { } let id = SharedString::from(format!("repo-local:{}", path.display())); - let workspace = self.workspace.clone(); + let workspace = self.app.clone(); let workspace_for_build = workspace.clone(); panels::open_primary( @@ -471,7 +414,7 @@ impl TitlebarView { } pub fn render_explore(&self) -> AnyElement { - let workspace = self.workspace.clone(); + let workspace = self.app.clone(); Button::new("open-explore") .icon(IconRole::Grid) diff --git a/crates/signed_app/src/workspace.rs b/crates/signed_app/src/workspace.rs deleted file mode 100644 index 5dcad26..0000000 --- a/crates/signed_app/src/workspace.rs +++ /dev/null @@ -1,244 +0,0 @@ -use std::collections::HashMap; -use std::sync::Arc; - -use gpui::prelude::FluentBuilder as _; -use gpui::{ - App, Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div, -}; -use ui::common::v_flex; -use ui::layout::panel::{Panel, PanelView}; -use ui::tabs::{Tab, Tabs, TabsVariant}; - -use crate::shared::log_dropped; - -/// The repository shown in the main panel and the detail panels opened beside it. -#[derive(Default)] -pub struct Workspace { - main: Option, - main_order: Vec, - main_views: HashMap>, - details: Vec, - detail_views: HashMap>, - active_detail: Option, -} - -impl Workspace { - pub fn new() -> Self { - Self::default() - } - - pub fn active_main(&self) -> Option> { - self.main - .as_ref() - .and_then(|id| self.main_views.get(id)) - .cloned() - } - - pub fn active_main_id(&self) -> Option<&SharedString> { - self.main.as_ref() - } - - pub fn open_mains(&self) -> &[SharedString] { - &self.main_order - } - - /// The open main panel with this id. - pub fn main_view(&self, id: &SharedString) -> Option> { - self.main_views.get(id).cloned() - } - - pub fn active_detail(&self) -> Option> { - self.active_detail - .as_ref() - .and_then(|id| self.detail_views.get(id)) - .cloned() - } - - pub fn has_main(&self, id: &SharedString) -> bool { - self.main_views.contains_key(id) - } - - pub fn has_details(&self) -> bool { - !self.details.is_empty() - } - - pub fn has_detail(&self, id: &SharedString) -> bool { - self.detail_views.contains_key(id) - } - - pub fn show_main( - &mut self, - id: SharedString, - view: Arc, - cx: &mut Context, - ) { - if self.main.as_ref() != Some(&id) { - self.clear_details(); - } - if !self.main_views.contains_key(&id) { - self.main_order.push(id.clone()); - } - self.main_views.insert(id.clone(), view); - self.main = Some(id); - cx.notify(); - } - - pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context) { - if self.main.as_ref() != Some(id) { - self.clear_details(); - } - self.main = Some(id.clone()); - cx.notify(); - } - - pub fn activate_detail(&mut self, id: &SharedString, cx: &mut Context) { - if self.detail_views.contains_key(id) { - self.active_detail = Some(id.clone()); - cx.notify(); - } - } - - pub fn show_detail( - &mut self, - id: SharedString, - view: Arc, - cx: &mut Context, - ) { - if !self.details.contains(&id) { - self.details.push(id.clone()); - } - self.detail_views.insert(id.clone(), view); - self.active_detail = Some(id); - cx.notify(); - } - - pub fn close_detail(&mut self, id: &SharedString, cx: &mut Context) { - let Some(index) = self.details.iter().position(|open| open == id) else { - return; - }; - self.close_detail_at(index, cx); - } - - pub fn close_main_at(&mut self, index: usize, cx: &mut Context) { - if index >= self.main_order.len() { - return; - } - - let id = self.main_order.remove(index); - self.main_views.remove(&id); - - if self.main.as_ref() == Some(&id) { - self.main = self - .main_order - .get(index) - .or_else(|| { - index - .checked_sub(1) - .and_then(|prev| self.main_order.get(prev)) - }) - .cloned(); - self.clear_details(); - } - - cx.notify(); - } - - pub fn clear(&mut self, cx: &mut Context) { - self.main = None; - self.main_order.clear(); - self.main_views.clear(); - self.clear_details(); - cx.notify(); - } - - fn clear_details(&mut self) { - self.details.clear(); - self.detail_views.clear(); - self.active_detail = None; - } - - fn close_detail_at(&mut self, index: usize, cx: &mut Context) { - if index >= self.details.len() { - return; - } - - let id = self.details.remove(index); - self.detail_views.remove(&id); - - if self.active_detail.as_ref() == Some(&id) { - self.active_detail = self - .details - .get(index) - .or_else(|| index.checked_sub(1).and_then(|prev| self.details.get(prev))) - .cloned(); - } - - cx.notify(); - } - - fn activate_detail_at(&mut self, index: usize, cx: &mut Context) { - let Some(id) = self.details.get(index) else { - return; - }; - self.active_detail = Some(id.clone()); - cx.notify(); - } -} - -impl Panel for Workspace { - fn title(&self, _cx: &App) -> SharedString { - SharedString::from("Workspace") - } -} - -impl Render for Workspace { - fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { - let selected = self - .active_detail - .as_ref() - .and_then(|active| self.details.iter().position(|id| id == active)) - .unwrap_or(usize::MAX); - - let details: Vec<_> = self - .details - .iter() - .map(|id| (id.clone(), self.detail_views.get(id).cloned())) - .collect(); - - let weak = cx.entity().downgrade(); - let body = self.active_detail().map(|view| view.view()); - - v_flex() - .size_full() - .gap_2() - .when(self.details.len() > 1, |this| { - let close_weak = weak.clone(); - this.child( - Tabs::new("signed-detail-tabs") - .variant(TabsVariant::Pill) - .overflow_scroll(true) - .selected(selected) - .tabs(move |_, cx| { - details - .iter() - .map(|(id, view)| match view { - Some(view) => view.tab(id.clone().into(), cx).closeable(true), - None => Tab::new(id.clone(), id.clone()).closeable(true), - }) - .collect() - }) - .on_change(move |index, _, cx| { - log_dropped(weak.update(cx, |workspace, cx| { - workspace.activate_detail_at(index, cx); - })); - }) - .on_close(move |index, _, cx| { - log_dropped(close_weak.update(cx, |workspace, cx| { - workspace.close_detail_at(index, cx); - })); - }), - ) - }) - .child(div().flex_1().min_h_0().w_full().children(body)) - } -}