From edb34abc6024a355b1bb917918042d6776649c5e Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sun, 15 Nov 2026 15:43:39 +0700 Subject: [PATCH] Move repository navigation into a sidebar - Replace the repository tabs and header Issues/PR menus with sidebar sections. - Keep the file tree visible beside Files, Commits, Issues, and Pull requests. - Switch back to Files when a tree folder or file is activated. --- crates/signed_app/src/repo/files.rs | 141 +++++++++---------- crates/signed_app/src/repo/header.rs | 193 +-------------------------- crates/signed_app/src/repo/mod.rs | 193 ++++++++++++++++++++++++--- 3 files changed, 234 insertions(+), 293 deletions(-) diff --git a/crates/signed_app/src/repo/files.rs b/crates/signed_app/src/repo/files.rs index a203b1c..d44b0a9 100644 --- a/crates/signed_app/src/repo/files.rs +++ b/crates/signed_app/src/repo/files.rs @@ -4,8 +4,8 @@ use std::path::{Component, Path, PathBuf}; use anyhow::Error; use gpui::prelude::FluentBuilder as _; use gpui::{ - AnyElement, AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, - SharedString, Styled as _, Task, WeakEntity, Window, div, px, + AnyElement, App, AppContext as _, Context, Entity, EventEmitter, IntoElement, + ParentElement as _, SharedString, Styled as _, Task, WeakEntity, Window, div, px, }; use gpui_native_kit::common::{h_flex, v_flex}; use gpui_native_kit::data::tree::{Tree, TreeItem}; @@ -19,11 +19,16 @@ use super::helpers::{code_language, is_markdown_path, preview_spinner, source_ha use crate::shared::tree::{TreeItemSeed, TreeRow, collect_expanded, flatten}; use crate::shared::widgets::empty_state; -const TREE_WIDTH: f32 = 240.; const MAX_PREVIEW_BYTES: usize = 1024 * 1024; const MAX_PREVIEWED_FILES: usize = 32; const MAX_PREVIEW_CACHE_BYTES: usize = 8 * 1024 * 1024; +/// The tree picked a folder or file, so the repository panel should show Files. +#[derive(Clone, Copy, Debug)] +pub enum RepoFilesEvent { + Activated, +} + enum FileContent { Text(String), Binary, @@ -68,6 +73,8 @@ pub struct RepoFilesView { tasks: Vec>>, } +impl EventEmitter for RepoFilesView {} + impl RepoFilesView { pub fn new(_cx: &mut Context) -> Self { Self { @@ -237,81 +244,72 @@ impl RepoFilesView { } else { self.open_file(&id, window, cx); } + cx.emit(RepoFilesEvent::Activated); cx.notify(); } - fn render_tree_column( - &self, - rows: Vec, - selected: Option, - view: WeakEntity, - cx: &mut Context, - ) -> impl IntoElement { + pub fn render_tree(&self, view: WeakEntity) -> AnyElement { + let rows = self.rows.clone(); + let selected = self.selected_index; let change_view = view.clone(); let disclosure_view = view; - v_flex() - .h_full() - .w(px(TREE_WIDTH)) - .p_2() - .flex_none() - .border_r_1() - .border_color(cx.theme().colors.border) + div() + .flex_1() + .min_h_0() + .w_full() .child( - div().flex_1().min_h_0().child( - Tree::new("repo-tree") - .selected(selected) - .items(move |_, _| { - rows.iter() - .map(|row| { - let item = TreeItem::new(row.id.clone(), row.label.clone()) - .depth(row.depth) - .expandable(row.expandable) - .expanded(row.expanded); + Tree::new("repo-tree") + .selected(selected) + .items(move |_, _| { + rows.iter() + .map(|row| { + let item = TreeItem::new(row.id.clone(), row.label.clone()) + .depth(row.depth) + .expandable(row.expandable) + .expanded(row.expanded); - if row.expandable { - item.icon(Icon::named(IconRole::Folder)) - } else { - item.icon(Icon::named(IconRole::File)) - } + if row.expandable { + item.icon(Icon::named(IconRole::Folder)) + } else { + item.icon(Icon::named(IconRole::File)) + } + }) + .collect() + }) + .on_change({ + let view = change_view; + move |index, window, cx| { + if let Some(index) = index + && let Err(error) = view.update(cx, |this, cx| { + this.activate_row(index, window, cx); }) - .collect() - }) - .on_change({ - let view = change_view; - move |index, window, cx| { - if let Some(index) = index - && let Err(error) = view.update(cx, |this, cx| { - this.activate_row(index, window, cx); - }) - { - log::warn!("failed to activate tree row: {error}"); - } + { + log::warn!("failed to activate tree row: {error}"); } - }) - .on_disclosure({ - 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.id.clone()); - if let Some(id) = id { - this.toggle_folder(&id); - cx.notify(); - } - }) { - log::warn!("failed to toggle folder: {error}"); + } + }) + .on_disclosure({ + 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.id.clone()); + if let Some(id) = id { + this.toggle_folder(&id); + cx.emit(RepoFilesEvent::Activated); + cx.notify(); } + }) { + log::warn!("failed to toggle folder: {error}"); } - }), - ), + } + }), ) + .into_any_element() } - fn render_content_column( - &self, - pane_title: SharedString, - cx: &mut Context, - ) -> impl IntoElement { + pub fn render_content(&self, cx: &App) -> AnyElement { + let pane_title = self.pane_title(); let body: AnyElement = if let Some(path) = self.selected_file.clone() { match self.files.get(path.as_ref()) { Some(FileContent::Text(_)) => { @@ -383,6 +381,7 @@ impl RepoFilesView { }), ) .child(div().flex_1().min_h_0().child(body)) + .into_any_element() } fn set_markdown( @@ -709,19 +708,3 @@ impl RepoFilesView { self.tasks.push(task); } } - -impl Render for RepoFilesView { - fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { - let rows = self.rows.clone(); - let selected = self.selected_index; - let view = cx.entity().downgrade(); - let pane_title = self.pane_title(); - - h_flex() - .flex_1() - .w_full() - .overflow_hidden() - .child(self.render_tree_column(rows, selected, view, cx)) - .child(self.render_content_column(pane_title, cx)) - } -} diff --git a/crates/signed_app/src/repo/header.rs b/crates/signed_app/src/repo/header.rs index a964f4e..091da80 100644 --- a/crates/signed_app/src/repo/header.rs +++ b/crates/signed_app/src/repo/header.rs @@ -10,7 +10,6 @@ use gpui_native_kit::avatar::Avatar; use gpui_native_kit::button::{Button, ButtonVariant}; use gpui_native_kit::clipboard::ClipboardButton; use gpui_native_kit::common::{Sizable as _, h_flex, v_flex}; -use gpui_native_kit::data::tabs::{Tab, Tabs}; use gpui_native_kit::dropdown_menu::DropdownMenu; use gpui_native_kit::icons::{Icon, IconRole}; use gpui_native_kit::menu::MenuItem; @@ -25,8 +24,7 @@ use super::dialogs; use super::helpers::{ ShareTargets, bound_repo_label, clone_group, fork_row, nostr_clone_url, repo_display_name, }; -use crate::issues::IssuesView; -use crate::pull_requests::{NewPullRequestView, PullRequestsView, SendPatchView}; +use crate::pull_requests::NewPullRequestView; impl RepoDetailView { pub(super) fn open_upstream(&mut self, window: &mut Window, cx: &mut Context) { @@ -49,28 +47,6 @@ impl RepoDetailView { dialogs::init::open(local_path, cx.entity().downgrade(), window, cx); } - fn open_pulls(&mut self, window: &mut Window, cx: &mut Context) { - let Some(addr) = self.store.read(cx).addr().cloned() else { - return; - }; - - let id = SharedString::from(format!("pulls:{addr}")); - let workspace = self.workspace.clone(); - let workspace_for_build = workspace.clone(); - let store = self.store.clone(); - - crate::shared::panels::open_detail( - &workspace, - id, - "Pull requests", - move |_, cx| { - cx.new(|cx| PullRequestsView::new(workspace_for_build.clone(), store.clone(), cx)) - }, - window, - cx, - ); - } - pub(super) fn open_new_pull_request(&mut self, window: &mut Window, cx: &mut Context) { let Some(addr) = self.store.read(cx).addr().cloned() else { return; @@ -101,57 +77,6 @@ impl RepoDetailView { ); } - fn open_send_patch(&mut self, window: &mut Window, cx: &mut Context) { - let Some(addr) = self.store.read(cx).addr().cloned() else { - return; - }; - - let id = SharedString::from(format!("send-patch:{addr}")); - let workspace = self.workspace.clone(); - let workspace_for_build = workspace.clone(); - let store = self.store.clone(); - - crate::shared::panels::open_detail( - &workspace, - id.clone(), - "Send patch", - move |window, cx| { - cx.new(|cx| { - SendPatchView::new( - workspace_for_build.clone(), - id.clone(), - store.clone(), - window, - cx, - ) - }) - }, - window, - cx, - ); - } - - fn open_issues(&mut self, window: &mut Window, cx: &mut Context) { - let Some(addr) = self.store.read(cx).addr().cloned() else { - return; - }; - let id = SharedString::from(format!("issues:{addr}")); - let workspace = self.workspace.clone(); - let workspace_for_build = workspace.clone(); - let store = self.store.clone(); - - crate::shared::panels::open_detail( - &workspace, - id, - "Issues", - move |_, cx| { - cx.new(|cx| IssuesView::new(workspace_for_build.clone(), store.clone(), cx)) - }, - window, - cx, - ); - } - pub(super) fn render_header(&mut self, cx: &mut Context) -> AnyElement { if self.store.read(cx).addr().is_none() { return self.render_local_header(cx); @@ -249,90 +174,6 @@ impl RepoDetailView { .flex_none() .gap_2() .justify_end() - .child({ - let weak_pulls = cx.entity().downgrade(); - let weak_pulls_open = cx.entity().downgrade(); - let _store_for_pulls = self.store.clone(); - let pulls_count = self.store.read(cx).pull_request_count(); - - DropdownMenu::new("pulls") - .label(format!("Pull requests {pulls_count}")) - .icon(Icon::named(CustomIconName::GitPullRequest)) - .variant(ButtonVariant::Secondary) - .open(self.pulls_menu_open) - .on_click(move |_, window, cx| { - if let Err(error) = weak_pulls.update(cx, |this, cx| { - this.open_pulls(window, cx); - }) { - log::warn!("failed to open pull requests: {error}"); - } - }) - .on_open_change(move |open, _, cx| { - if let Err(error) = weak_pulls_open.update(cx, |this, cx| { - this.pulls_menu_open = open; - cx.notify(); - }) { - log::warn!("failed to update pulls menu: {error}"); - } - }) - .items({ - let weak_new = cx.entity().downgrade(); - let weak_patch = cx.entity().downgrade(); - move |_, _| { - let weak_new = weak_new.clone(); - let weak_patch = weak_patch.clone(); - vec![ - MenuItem::new("New pull request").handler(move |window, cx| { - crate::shared::log_dropped(weak_new.update(cx, |this, cx| { - this.open_new_pull_request(window, cx); - })); - }), - MenuItem::new("Send patch").handler(move |window, cx| { - crate::shared::log_dropped(weak_patch.update(cx, |this, cx| { - this.open_send_patch(window, cx); - })); - }), - ] - } - }) - }) - .child({ - let weak_issues = cx.entity().downgrade(); - let weak_issues_open = cx.entity().downgrade(); - let store_for_issues = self.store.clone(); - let issues_count = self.store.read(cx).issue_count(); - - DropdownMenu::new("issues") - .label(format!("Issues {issues_count}")) - .icon(Icon::named(CustomIconName::GitIssueDone)) - .variant(ButtonVariant::Secondary) - .open(self.issues_menu_open) - .on_click(move |_, window, cx| { - if let Err(error) = weak_issues.update(cx, |this, cx| { - this.open_issues(window, cx); - }) { - log::warn!("failed to open issues: {error}"); - } - }) - .on_open_change(move |open, _, cx| { - if let Err(error) = - weak_issues_open.update(cx, |this, cx| { - this.issues_menu_open = open; - cx.notify(); - }) - { - log::warn!("failed to update issues menu: {error}"); - } - }) - .items(move |_, _| { - vec![MenuItem::new("New issue").handler({ - let store = store_for_issues.clone(); - move |_, cx| { - crate::issues::new_issue::open(store.clone(), cx); - } - })] - }) - }) .child( DropdownMenu::new("share") .icon(Icon::named(IconRole::Copy)) @@ -526,7 +367,6 @@ impl RepoDetailView { ), ), ) - .child(self.render_tabs(cx)) .into_any_element() } @@ -593,37 +433,6 @@ impl RepoDetailView { ) .child(action), ) - .child(self.render_tabs(cx)) - .into_any_element() - } - - fn render_tabs(&self, cx: &mut Context) -> AnyElement { - let count = self.history.read(cx).commit_count(); - let muted = cx.theme().colors.muted_foreground; - let weak = cx.entity().downgrade(); - let commits_label = match count { - Some(count) => SharedString::from(format!("Commits ({count})")), - None => SharedString::from("Commits"), - }; - - Tabs::new("repo-tabs") - .selected(self.active_tab) - .tabs(move |_, _| { - vec![ - Tab::new("files", "Files") - .lead(Icon::named(CustomIconName::GitFile).color(muted)), - Tab::new("commits", commits_label.clone()) - .lead(Icon::named(CustomIconName::GitCommit).color(muted)), - ] - }) - .on_change(move |index, _, cx| { - if let Err(error) = weak.update(cx, |this, cx| { - this.active_tab = index; - cx.notify(); - }) { - log::warn!("failed to switch repo tab: {error}"); - } - }) .into_any_element() } diff --git a/crates/signed_app/src/repo/mod.rs b/crates/signed_app/src/repo/mod.rs index baf0e61..214522a 100644 --- a/crates/signed_app/src/repo/mod.rs +++ b/crates/signed_app/src/repo/mod.rs @@ -6,8 +6,8 @@ use assets::CustomIconName; use gpui::prelude::FluentBuilder as _; use gpui::{ AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, - ParentElement as _, PathPromptOptions, Render, SharedString, Styled as _, Subscription, Task, - WeakEntity, Window, div, + ParentElement as _, PathPromptOptions, Render, SharedString, StatefulInteractiveElement as _, + Styled as _, Subscription, Task, WeakEntity, Window, div, px, }; use gpui_native_kit::alert::{Alert, AlertSeverity}; use gpui_native_kit::badge::Badge; @@ -42,9 +42,32 @@ use helpers::{RepoData, load_repo_data}; use history::RepoHistoryView; use refs::RefSwitcher; +use crate::issues::IssuesView; +use crate::pull_requests::PullRequestsView; use crate::shared::tree::{build_tree_items, sorted_worktree_paths}; use crate::workspace::Workspace; +const REPO_SIDEBAR_WIDTH: f32 = 240.; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub(super) enum RepoSection { + Files, + Commits, + Issues, + PullRequests, +} + +impl RepoSection { + fn id(self) -> &'static str { + match self { + Self::Files => "repo-section-files", + Self::Commits => "repo-section-commits", + Self::Issues => "repo-section-issues", + Self::PullRequests => "repo-section-pulls", + } + } +} + #[derive(Clone, Copy, PartialEq, Eq)] pub enum RefKind { /// HEAD stays attached. @@ -58,12 +81,16 @@ pub struct RepoDetailView { pub(super) store: Entity, /// A repository opened by address alone starts without an announcement. repo_started: bool, - /// The Files tab, which owns the explorer, previews and the per-file commit map. + /// The file explorer and preview. files: Entity, - /// The Commits tab, which walks HEAD and opens commit diffs. + /// The commit history section. pub(super) history: Entity, - /// The active tab: Files or Commits. - pub(super) active_tab: usize, + /// The issues list shown in the repository panel. + issues: Entity, + /// The pull request list shown in the repository panel. + pulls: Entity, + /// The section selected in the repository sidebar. + active_section: RepoSection, /// The checked-out worktree path. worktree: Option, loading: bool, @@ -79,8 +106,6 @@ pub struct RepoDetailView { pub(super) share_open: bool, pub(super) repo_menu_open: bool, pub(super) clone_open: bool, - pub(super) issues_menu_open: bool, - pub(super) pulls_menu_open: bool, _subscriptions: Vec, } @@ -129,14 +154,24 @@ impl RepoDetailView { let checkouts = CheckoutsStore::global(cx); let files = cx.new(RepoFilesView::new); let history = cx.new(|_| RepoHistoryView::new(store.clone(), workspace.clone())); + let issues = cx.new(|cx| IssuesView::new(workspace.clone(), store.clone(), cx)); + let pulls = cx.new(|cx| PullRequestsView::new(workspace.clone(), store.clone(), cx)); let weak = cx.entity().downgrade(); let refs = RefSwitcher::new(weak.clone(), window, cx); - let subscriptions = vec![cx.observe(&checkouts, |this, _checkouts, cx| { - if this.refresh_statuses(cx) { - cx.notify(); - } - })]; + let subscriptions = vec![ + cx.observe(&checkouts, |this, _checkouts, cx| { + if this.refresh_statuses(cx) { + cx.notify(); + } + }), + cx.subscribe(&files, |this, _files, _event, cx| { + if this.active_section != RepoSection::Files { + this.active_section = RepoSection::Files; + cx.notify(); + } + }), + ]; let store_for_attach = store.clone(); window.defer(cx, move |window, cx| { @@ -152,7 +187,9 @@ impl RepoDetailView { repo_started: false, files, history, - active_tab: 0, + issues, + pulls, + active_section: RepoSection::Files, worktree: None, loading: true, error: None, @@ -165,8 +202,6 @@ impl RepoDetailView { share_open: false, repo_menu_open: false, clone_open: false, - issues_menu_open: false, - pulls_menu_open: false, _subscriptions: subscriptions, } } @@ -1157,7 +1192,7 @@ impl RepoDetailView { ) } - fn render_files_tab(&self, cx: &mut Context) -> AnyElement { + fn render_files_tab(&self, cx: &App) -> AnyElement { if self.loading { let label = if self.store.read(cx).path.is_some() { "Loading repository..." @@ -1181,7 +1216,109 @@ impl RepoDetailView { .into_any_element(); } - self.files.clone().into_any_element() + self.files.read(cx).render_content(cx) + } + + fn render_sidebar(&self, cx: &mut Context) -> AnyElement { + let tree = self.files.read(cx).render_tree(self.files.downgrade()); + let commits = self + .history + .read(cx) + .commit_count() + .map(|count| SharedString::from(format!("Commits ({count})"))) + .unwrap_or_else(|| SharedString::from("Commits")); + + let mut sections = v_flex() + .p_2() + .gap_1() + .child(self.render_section_row( + RepoSection::Files, + "Files", + Icon::named(CustomIconName::GitFile), + cx, + )) + .child(self.render_section_row( + RepoSection::Commits, + commits, + Icon::named(CustomIconName::GitCommit), + cx, + )); + + if self.store.read(cx).addr().is_some() { + sections = sections + .child(self.render_section_row( + RepoSection::Issues, + "Issues", + Icon::named(CustomIconName::GitIssueDone), + cx, + )) + .child(self.render_section_row( + RepoSection::PullRequests, + "Pull requests", + Icon::named(CustomIconName::GitPullRequest), + cx, + )); + } + + v_flex() + .w(px(REPO_SIDEBAR_WIDTH)) + .h_full() + .flex_none() + .border_r_1() + .border_color(cx.theme().colors.border) + .child(sections) + .child(div().flex_1().min_h_0().px_2().pb_2().child(tree)) + .into_any_element() + } + + fn render_section_row( + &self, + section: RepoSection, + label: impl Into, + icon: Icon, + cx: &mut Context, + ) -> AnyElement { + let label = label.into(); + let active = self.active_section == section; + let color = if active { + cx.theme().colors.foreground + } else { + cx.theme().colors.muted_foreground + }; + + h_flex() + .id(section.id()) + .w_full() + .h_8() + .px_2() + .gap_2() + .rounded(cx.theme().radius.control) + .cursor_pointer() + .when(active, |this| this.bg(cx.theme().colors.muted_hover)) + .when(!active, |this| { + this.hover(|this| this.bg(cx.theme().colors.muted)) + }) + .child(icon.size(px(14.)).color(color)) + .child( + div() + .typeset(TextRole::Body, cx) + .text_color(color) + .child(label), + ) + .on_click(cx.listener(move |this, _, _, cx| { + this.active_section = section; + cx.notify(); + })) + .into_any_element() + } + + fn render_content(&mut self, cx: &mut Context) -> AnyElement { + match self.active_section { + RepoSection::Files => self.render_files_tab(cx), + RepoSection::Commits => self.history.clone().into_any_element(), + RepoSection::Issues => self.issues.clone().into_any_element(), + RepoSection::PullRequests => self.pulls.clone().into_any_element(), + } } } @@ -1232,9 +1369,21 @@ impl Render for RepoDetailView { }), ) }) - .map(|this| match self.active_tab { - 0 => this.child(self.render_files_tab(cx)), - _ => this.child(self.history.clone()), - }) + .child( + h_flex() + .flex_1() + .min_h_0() + .w_full() + .overflow_hidden() + .child(self.render_sidebar(cx)) + .child( + v_flex() + .flex_1() + .min_w_0() + .h_full() + .overflow_hidden() + .child(self.render_content(cx)), + ), + ) } }