From 6fb866d89ef6e18afe392c316db63cdc3484b013 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Tue, 17 Nov 2026 20:32:05 +0700 Subject: [PATCH] update --- crates/signed_app/src/app/mod.rs | 9 +- crates/signed_app/src/app/placeholder.rs | 8 +- crates/signed_app/src/app/sign_in.rs | 9 +- crates/signed_app/src/explore/helpers.rs | 11 +- crates/signed_app/src/explore/mod.rs | 235 ++++++++++-------- crates/signed_app/src/inbox/mod.rs | 6 +- crates/signed_app/src/issues/detail.rs | 14 +- crates/signed_app/src/issues/mod.rs | 13 +- crates/signed_app/src/pull_requests/detail.rs | 17 +- crates/signed_app/src/pull_requests/mod.rs | 15 +- crates/signed_app/src/pull_requests/new.rs | 13 +- .../src/pull_requests/send_patch.rs | 14 +- crates/signed_app/src/repo/actions.rs | 8 - crates/signed_app/src/repo/commit_diff.rs | 11 +- crates/signed_app/src/repo/header.rs | 1 - crates/signed_app/src/repo/history.rs | 3 - crates/signed_app/src/repo/mod.rs | 49 +++- crates/signed_app/src/shared/panels.rs | 4 +- crates/signed_app/src/titlebar/mod.rs | 51 ++-- crates/signed_app/src/workspace.rs | 45 ++-- crates/ui/src/layout/panel.rs | 60 +++++ crates/ui/tests/panel.rs | 118 ++++++++- 22 files changed, 486 insertions(+), 228 deletions(-) diff --git a/crates/signed_app/src/app/mod.rs b/crates/signed_app/src/app/mod.rs index 4a66983..f2d052c 100644 --- a/crates/signed_app/src/app/mod.rs +++ b/crates/signed_app/src/app/mod.rs @@ -119,10 +119,11 @@ impl Render for AppRoot { .child(titlebar_view.render_explore()) .child(titlebar_view.repository_switcher()); - let tab_bar = h_flex().gap_1().when_some( - titlebar_view.render_repo_tabs(workspace.open_mains(), workspace.active_main_id()), - |bar, tabs| bar.child(tabs), - ); + let tab_bar = h_flex() + .gap_1() + .when_some(titlebar_view.render_repo_tabs(workspace), |bar, tabs| { + bar.child(tabs) + }); div() .relative() diff --git a/crates/signed_app/src/app/placeholder.rs b/crates/signed_app/src/app/placeholder.rs index 62633b0..e9b7c0e 100644 --- a/crates/signed_app/src/app/placeholder.rs +++ b/crates/signed_app/src/app/placeholder.rs @@ -1,5 +1,5 @@ use gpui::{ - Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div, + App, Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div, }; use ui::empty_state::EmptyState; use ui::layout::panel::Panel; @@ -17,7 +17,11 @@ impl PlaceholderView { } } -impl Panel for PlaceholderView {} +impl Panel for PlaceholderView { + fn title(&self, _cx: &App) -> SharedString { + self.title.clone() + } +} impl Render for PlaceholderView { fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/app/sign_in.rs b/crates/signed_app/src/app/sign_in.rs index 5eccd24..da39038 100644 --- a/crates/signed_app/src/app/sign_in.rs +++ b/crates/signed_app/src/app/sign_in.rs @@ -1,5 +1,6 @@ use gpui::{ - Context, FontWeight, IntoElement, ParentElement as _, Render, Styled as _, Window, div, + App, Context, FontWeight, IntoElement, ParentElement as _, Render, SharedString, Styled as _, + Window, div, }; use ui::button::{Button, ButtonVariant}; use ui::common::v_flex; @@ -15,7 +16,11 @@ impl SignedOutView { } } -impl Panel for SignedOutView {} +impl Panel for SignedOutView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Signed") + } +} impl Render for SignedOutView { fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/explore/helpers.rs b/crates/signed_app/src/explore/helpers.rs index a2d1ab6..1507530 100644 --- a/crates/signed_app/src/explore/helpers.rs +++ b/crates/signed_app/src/explore/helpers.rs @@ -19,9 +19,8 @@ const RECENT_COUNT: usize = 10; #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub(super) enum RepoFilter { - All, #[default] - Popular, + All, Recent, } @@ -43,14 +42,6 @@ impl RepoFilter { match self { Self::All => {} Self::Recent => indices.truncate(RECENT_COUNT), - Self::Popular => { - let counts = &store.counts; - let scores: Vec = announcements - .iter() - .map(|a| counts.get(&a.addr()).map_or(0, |c| c.score())) - .collect(); - indices.sort_by(|&a, &b| scores[b].cmp(&scores[a])); - } } indices diff --git a/crates/signed_app/src/explore/mod.rs b/crates/signed_app/src/explore/mod.rs index f7ef621..f236b44 100644 --- a/crates/signed_app/src/explore/mod.rs +++ b/crates/signed_app/src/explore/mod.rs @@ -1,10 +1,9 @@ mod helpers; -use gpui::prelude::FluentBuilder as _; use gpui::{ - AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, - ParentElement as _, Render, SharedString, Styled as _, Subscription, UniformListScrollHandle, - WeakEntity, Window, div, px, uniform_list, + AnyElement, App, AppContext as _, Context, Entity, IntoElement, ListAlignment, ListState, + ParentElement as _, Render, SharedString, Styled as _, Subscription, WeakEntity, Window, div, + list, px, }; use helpers::{RepoFilter, render_card}; use metadata::ProfileDirectory; @@ -17,7 +16,7 @@ use ui::input::{Input, InputState, InputStateEvent}; use ui::layout::panel::Panel; use ui::scrollbar::Scrollbar; use ui::segmented::{Segmented, SegmentedOption}; -use ui::theme::{ActiveTheme as _, TextRole, Typeset as _}; +use ui::theme::ActiveTheme as _; use crate::shared::panels; use crate::workspace::Workspace; @@ -28,7 +27,7 @@ const CARD_HEIGHT: f32 = 40. + 64. + 48. + 2. + 6.; pub struct ExploreView { store: Entity, workspace: WeakEntity, - scroll_handle: UniformListScrollHandle, + scroll_handle: ListState, filter: RepoFilter, /// Indices matching [`Self::filter`] into the store's `announcements`. visible: Vec, @@ -83,7 +82,7 @@ impl ExploreView { Self { store, workspace, - scroll_handle: UniformListScrollHandle::new(), + scroll_handle: ListState::new(0, ListAlignment::Top, px(320.)), filter: RepoFilter::default(), visible: Vec::new(), search, @@ -111,129 +110,158 @@ impl ExploreView { directory.track(owners, cx); }); + self.scroll_handle.reset(self.item_count()); cx.notify(); } - fn render_header(&self, count: usize, cx: &mut Context) -> AnyElement { - let muted = cx.theme().colors.muted_foreground; - let weak = cx.entity().downgrade(); + /// Header row, plus one row per card row, or the empty state when nothing matches. + fn item_count(&self) -> usize { + if self.visible.is_empty() { + 2 + } else { + self.visible.len().div_ceil(COLUMNS) + 1 + } + } - h_flex() - .px_4() - .py_2() + fn render_header( + search: Entity, + filter: RepoFilter, + weak: WeakEntity, + cx: &App, + ) -> AnyElement { + let muted = cx.theme().colors.muted_foreground; + + div() + .relative() + .h_96() .w_full() - .gap_3() .child( - v_flex() - .gap_1() - .child(div().typeset(TextRole::Caption2, cx).child("Repositories")) + div() + .absolute() + .inset_0() + .flex() + .items_center() + .justify_center() + .px_4() .child( - div() - .typeset(TextRole::Caption, cx) - .text_color(muted) - .child(SharedString::from(format!("Total: {count}"))), + div().w_full().max_w(px(480.)).child( + Input::new("explore-search", search) + .placeholder("Search...") + .fully_rounded(true) + .prefix(Icon::named(IconRole::Search).small().color(muted)), + ), ), ) .child( - div().w(px(180.)).child( - Input::new("explore-search", self.search.clone()) - .placeholder("Search...") - .prefix(Icon::named(IconRole::Search).small().color(muted)), - ), - ) - .child(div().flex_1()) - .child( - Segmented::new("explore-filter", self.filter) - .options(vec![ - SegmentedOption::new(RepoFilter::All, "All") - .icon(Icon::named(IconRole::Grid).small().color(muted)), - SegmentedOption::new(RepoFilter::Popular, "Popular") - .icon(Icon::named(IconRole::Trending).small().color(muted)), - SegmentedOption::new(RepoFilter::Recent, "Recent") - .icon(Icon::named(IconRole::Clock).small().color(muted)), - ]) - .on_change(move |filter, _, cx| { - if let Err(error) = weak.update(cx, |this, cx| { - this.filter = filter; - this.rebuild_rows(cx); - }) { - log::warn!("failed to change explore filter: {error}"); - } - }), + div() + .absolute() + .left_0() + .bottom_0() + .pl_4() + .pb_4() + .child( + Segmented::new("explore-filter", filter) + .options(vec![ + SegmentedOption::new(RepoFilter::All, "All") + .icon(Icon::named(IconRole::Grid).small().color(muted)), + SegmentedOption::new(RepoFilter::Recent, "Recent") + .icon(Icon::named(IconRole::Clock).small().color(muted)), + ]) + .on_change(move |filter, _, cx| { + if let Err(error) = weak.update(cx, |this, cx| { + this.filter = filter; + this.rebuild_rows(cx); + }) { + log::warn!("failed to change explore filter: {error}"); + } + }), + ), ) .into_any_element() } } -impl Panel for ExploreView {} +impl Panel for ExploreView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Explore") + } + + fn prefix(&self, cx: &App) -> Option { + Some( + Icon::named(IconRole::Grid) + .small() + .color(cx.theme().colors.muted_foreground) + .into_any_element(), + ) + } +} impl Render for ExploreView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let store = self.store.read(cx); let announcements = store.announcements.clone(); let last_activity = store.last_activity.clone(); - let count = self.visible.len(); let visible = self.visible.clone(); let directory = self.directory.clone(); let workspace = self.workspace.clone(); - let has_repos = count > 0; - let rows = visible.len().div_ceil(COLUMNS); + let has_repos = !visible.is_empty(); + let search = self.search.clone(); + let filter = self.filter; + let weak = cx.entity().downgrade(); + let scroll_handle = self.scroll_handle.clone(); - v_flex() + div() .relative() .size_full() - .child(self.render_header(count, cx)) - .when(!has_repos, |this| { - this.child( - v_flex() - .size_full() - .items_center() - .justify_center() - .child(EmptyState::new("No repositories match your search")), - ) - }) - .when(has_repos, |this| { - this.child( - uniform_list("explore-repos", rows, move |range, _, cx| { - range - .map(|row| { - let mut cards = Vec::with_capacity(COLUMNS); + .child( + list(self.scroll_handle.clone(), move |item, _, cx| { + if item == 0 { + return Self::render_header(search.clone(), filter, weak.clone(), cx); + } - for col in 0..COLUMNS { - let Some(&ix) = visible.get(row * COLUMNS + col) else { - break; - }; - let Some(announcement) = announcements.get(ix) else { - break; - }; - let activity = last_activity.get(&announcement.addr()).copied(); - cards.push(render_card( - ix, - announcement, - activity, - &announcements, - &directory, - &workspace, - cx, - )); - } + if !has_repos { + return v_flex() + .w_full() + .h(px(320.)) + .items_center() + .justify_center() + .child(EmptyState::new("No repositories match your search")) + .into_any_element(); + } - h_flex() - .id(row) - .w_full() - .h(px(CARD_HEIGHT)) - .gap_3() - .px_4() - .pt_4() - .children(cards) - .into_any_element() - }) - .collect() - }) - .track_scroll(&self.scroll_handle) - .size_full(), - ) - }) + let row = item - 1; + let mut cards = Vec::with_capacity(COLUMNS); + + for col in 0..COLUMNS { + let Some(&ix) = visible.get(row * COLUMNS + col) else { + break; + }; + let Some(announcement) = announcements.get(ix) else { + break; + }; + let activity = last_activity.get(&announcement.addr()).copied(); + cards.push(render_card( + ix, + announcement, + activity, + &announcements, + &directory, + &workspace, + cx, + )); + } + + h_flex() + .w_full() + .h(px(CARD_HEIGHT)) + .gap_3() + .px_4() + .pt_4() + .children(cards) + .into_any_element() + }) + .size_full(), + ) .child( div() .absolute() @@ -241,10 +269,7 @@ impl Render for ExploreView { .left_0() .right_0() .bottom_0() - .child(Scrollbar::vertical( - "explore-scroll", - self.scroll_handle.clone(), - )), + .child(Scrollbar::vertical("explore-scroll", scroll_handle)), ) } } diff --git a/crates/signed_app/src/inbox/mod.rs b/crates/signed_app/src/inbox/mod.rs index c7cc6ed..3d24deb 100644 --- a/crates/signed_app/src/inbox/mod.rs +++ b/crates/signed_app/src/inbox/mod.rs @@ -392,7 +392,11 @@ impl InboxView { } } -impl Panel for InboxView {} +impl Panel for InboxView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Inbox") + } +} impl Render for InboxView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/issues/detail.rs b/crates/signed_app/src/issues/detail.rs index c3850b4..1a9a0d0 100644 --- a/crates/signed_app/src/issues/detail.rs +++ b/crates/signed_app/src/issues/detail.rs @@ -1,5 +1,5 @@ use gpui::{ - Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, Render, + App, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, Render, ScrollHandle, SharedString, Styled as _, Subscription, Window, div, }; use metadata::ProfileDirectory; @@ -80,7 +80,17 @@ impl IssueDetailView { } } -impl Panel for IssueDetailView {} +impl Panel for IssueDetailView { + fn title(&self, _cx: &App) -> SharedString { + let hex = self.issue_id.to_hex(); + let short = hex.get(..8).unwrap_or(&hex); + SharedString::from(format!("Issue {short}")) + } + + fn loading(&self, cx: &App) -> bool { + !self.store.read(cx).loaded + } +} impl Render for IssueDetailView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/issues/mod.rs b/crates/signed_app/src/issues/mod.rs index a1d5565..e3e93e3 100644 --- a/crates/signed_app/src/issues/mod.rs +++ b/crates/signed_app/src/issues/mod.rs @@ -1,6 +1,6 @@ use gpui::prelude::FluentBuilder as _; use gpui::{ - AnyElement, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, + AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled as _, Subscription, UniformListScrollHandle, WeakEntity, Window, div, px, uniform_list, }; @@ -166,7 +166,11 @@ impl IssuesView { } } -impl Panel for IssuesView {} +impl Panel for IssuesView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Issues") + } +} impl Render for IssuesView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { @@ -215,15 +219,10 @@ impl Render for IssuesView { .on_click(move |_, window, cx| { let id = SharedString::from(format!("issue:{issue_id}")); - let hex = issue_id.to_hex(); - let short = hex.get(..8).unwrap_or(&hex); - let label = - SharedString::from(format!("Issue {short}")); let store = store.clone(); panels::open_detail( &workspace, id, - label, move |_, cx| { cx.new(|cx| { IssueDetailView::new( diff --git a/crates/signed_app/src/pull_requests/detail.rs b/crates/signed_app/src/pull_requests/detail.rs index 71ae672..6e08593 100644 --- a/crates/signed_app/src/pull_requests/detail.rs +++ b/crates/signed_app/src/pull_requests/detail.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use gpui::prelude::FluentBuilder as _; use gpui::{ - AnyElement, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, + AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Subscription, UniformListScrollHandle, WeakEntity, Window, div, uniform_list, }; @@ -288,13 +288,10 @@ impl PullRequestDetailView { None => SharedString::from(format!("commit:{}:{commit_id}", worktree.display())), }; let workspace = self.workspace.clone(); - let short = commit_id.get(..8).unwrap_or(commit_id); - let label = SharedString::from(format!("Commit {short}")); let commit_id = commit_id.to_owned(); crate::shared::panels::open_detail( &workspace, id, - label, move |window, cx| { cx.new(|cx| CommitDiffView::new(worktree.clone(), commit_id.clone(), window, cx)) }, @@ -579,7 +576,17 @@ impl PullRequestDetailView { } } -impl Panel for PullRequestDetailView {} +impl Panel for PullRequestDetailView { + fn title(&self, _cx: &App) -> SharedString { + let hex = self.pr_id.to_hex(); + let short = hex.get(..8).unwrap_or(&hex); + SharedString::from(format!("PR {short}")) + } + + fn loading(&self, _cx: &App) -> bool { + self.loading + } +} impl Render for PullRequestDetailView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/pull_requests/mod.rs b/crates/signed_app/src/pull_requests/mod.rs index a47a96a..1f40af6 100644 --- a/crates/signed_app/src/pull_requests/mod.rs +++ b/crates/signed_app/src/pull_requests/mod.rs @@ -1,6 +1,6 @@ use gpui::prelude::FluentBuilder as _; use gpui::{ - AnyElement, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, + AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled as _, Subscription, UniformListScrollHandle, WeakEntity, Window, div, px, uniform_list, }; @@ -145,7 +145,6 @@ impl PullRequestsView { crate::shared::panels::open_detail( &workspace, id.clone(), - "New pull request", move |window, cx| { cx.new(|cx| { NewPullRequestView::new( @@ -175,7 +174,6 @@ impl PullRequestsView { crate::shared::panels::open_detail( &workspace, id.clone(), - "Send patch", move |window, cx| { cx.new(|cx| { SendPatchView::new( @@ -279,7 +277,11 @@ impl PullRequestsView { } } -impl Panel for PullRequestsView {} +impl Panel for PullRequestsView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Pull requests") + } +} impl Render for PullRequestsView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { @@ -357,16 +359,11 @@ impl Render for PullRequestsView { .hover(|this| this.bg(cx.theme().colors.muted_hover)) .on_click(move |_, window, cx| { let id = SharedString::from(format!("pr:{pr_id}")); - let hex = pr_id.to_hex(); - let short = hex.get(..8).unwrap_or(&hex); - let label = - SharedString::from(format!("PR {short}")); let workspace_for_build = workspace.clone(); let store = store.clone(); crate::shared::panels::open_detail( &workspace, id, - label, move |window, cx| { cx.new(|cx| { PullRequestDetailView::new( diff --git a/crates/signed_app/src/pull_requests/new.rs b/crates/signed_app/src/pull_requests/new.rs index 7026b63..1e17a70 100644 --- a/crates/signed_app/src/pull_requests/new.rs +++ b/crates/signed_app/src/pull_requests/new.rs @@ -222,13 +222,10 @@ impl NewPullRequestView { None => SharedString::from(format!("commit:{}:{commit_id}", repo_path.display())), }; let workspace = self.workspace.clone(); - let short = commit_id.get(..8).unwrap_or(commit_id); - let label = SharedString::from(format!("Commit {short}")); let commit_id = commit_id.to_owned(); crate::shared::panels::open_detail( &workspace, id, - label, move |window, cx| { cx.new(|cx| CommitDiffView::new(repo_path.clone(), commit_id.clone(), window, cx)) }, @@ -443,7 +440,15 @@ impl NewPullRequestView { } } -impl Panel for NewPullRequestView {} +impl Panel for NewPullRequestView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("New pull request") + } + + fn loading(&self, _cx: &App) -> bool { + self.loading + } +} impl Render for NewPullRequestView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/pull_requests/send_patch.rs b/crates/signed_app/src/pull_requests/send_patch.rs index a2b937f..b09f569 100644 --- a/crates/signed_app/src/pull_requests/send_patch.rs +++ b/crates/signed_app/src/pull_requests/send_patch.rs @@ -1,7 +1,7 @@ use gpui::prelude::FluentBuilder as _; use gpui::{ - AnyElement, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, Render, - SharedString, Styled as _, WeakEntity, Window, div, + AnyElement, App, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _, + Render, SharedString, Styled as _, WeakEntity, Window, div, }; use signed_state::RepoStore; use ui::button::Button; @@ -120,7 +120,15 @@ impl SendPatchView { } } -impl Panel for SendPatchView {} +impl Panel for SendPatchView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Send patch") + } + + fn loading(&self, _cx: &App) -> bool { + self.submitting + } +} impl Render for SendPatchView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/repo/actions.rs b/crates/signed_app/src/repo/actions.rs index 0378564..ad13255 100644 --- a/crates/signed_app/src/repo/actions.rs +++ b/crates/signed_app/src/repo/actions.rs @@ -53,14 +53,10 @@ pub fn open_repo_item( match item { RepoItem::Issue(issue_id) => { let id = SharedString::from(format!("issue:{issue_id}")); - let hex = issue_id.to_hex(); - let short = hex.get(..8).unwrap_or(&hex); - let label = SharedString::from(format!("Issue {short}")); let store = store.clone(); panels::open_detail( workspace, id, - label, move |_, cx| { cx.new(|cx| { crate::issues::detail::IssueDetailView::new(store.clone(), issue_id, cx) @@ -72,15 +68,11 @@ pub fn open_repo_item( } RepoItem::PullRequest(pr_id) => { let id = SharedString::from(format!("pr:{pr_id}")); - let hex = pr_id.to_hex(); - let short = hex.get(..8).unwrap_or(&hex); - let label = SharedString::from(format!("PR {short}")); let workspace_for_build = workspace.clone(); let store = store.clone(); panels::open_detail( workspace, id, - label, move |window, cx| { cx.new(|cx| { crate::pull_requests::PullRequestDetailView::new( diff --git a/crates/signed_app/src/repo/commit_diff.rs b/crates/signed_app/src/repo/commit_diff.rs index a1ddb78..e7de44b 100644 --- a/crates/signed_app/src/repo/commit_diff.rs +++ b/crates/signed_app/src/repo/commit_diff.rs @@ -500,7 +500,16 @@ impl CommitDiffView { } } -impl Panel for CommitDiffView {} +impl Panel for CommitDiffView { + fn title(&self, _cx: &App) -> SharedString { + let short = self.commit.id.get(..8).unwrap_or(&self.commit.id); + SharedString::from(format!("Commit {short}")) + } + + fn loading(&self, _cx: &App) -> bool { + self.loading + } +} impl Render for CommitDiffView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/repo/header.rs b/crates/signed_app/src/repo/header.rs index ceb99d8..abaef99 100644 --- a/crates/signed_app/src/repo/header.rs +++ b/crates/signed_app/src/repo/header.rs @@ -88,7 +88,6 @@ impl RepoDetailView { crate::shared::panels::open_detail( &workspace, id.clone(), - "New pull request", move |window, cx| { cx.new(|cx| { NewPullRequestView::new( diff --git a/crates/signed_app/src/repo/history.rs b/crates/signed_app/src/repo/history.rs index 8984457..a3e2949 100644 --- a/crates/signed_app/src/repo/history.rs +++ b/crates/signed_app/src/repo/history.rs @@ -100,8 +100,6 @@ impl RepoHistoryView { }; let store = self.store.clone(); - let short = commit_id.get(..8).unwrap_or(commit_id); - let label = SharedString::from(format!("Commit {short}")); let commit_id = commit_id.to_owned(); let id = match store.read(cx).addr() { Some(addr) => SharedString::from(format!("commit:{addr}:{commit_id}")), @@ -112,7 +110,6 @@ impl RepoHistoryView { panels::open_detail( &workspace, id, - label, move |window, cx| { cx.new(|cx| CommitDiffView::new(worktree.clone(), commit_id.clone(), window, cx)) }, diff --git a/crates/signed_app/src/repo/mod.rs b/crates/signed_app/src/repo/mod.rs index d7e0588..3422a3c 100644 --- a/crates/signed_app/src/repo/mod.rs +++ b/crates/signed_app/src/repo/mod.rs @@ -16,7 +16,8 @@ use signed_state::{ Backend, CheckoutsStore, LocalReposStore, Mirrors, Nip34Binding, RepoStore, SyncStatusDirectory, }; use ui::alert::{Alert, AlertSeverity}; -use ui::common::{h_flex, v_flex}; +use ui::avatar::Avatar; +use ui::common::{Sizable as _, h_flex, v_flex}; use ui::icons::{Icon, IconRole}; use ui::layout::panel::Panel; use ui::sidebar::{Sidebar, SidebarEntry}; @@ -922,7 +923,51 @@ impl RepoDetailView { } } -impl Panel for RepoDetailView {} +impl Panel for RepoDetailView { + fn title(&self, cx: &App) -> SharedString { + let store = self.store.read(cx); + if let Some(name) = store.path.as_ref().and_then(|path| path.file_name()) { + return SharedString::from(name.to_string_lossy().into_owned()); + } + if let Some(announcement) = store.announcement.as_ref() { + return announcement + .name + .as_deref() + .map(SharedString::from) + .unwrap_or_else(|| SharedString::from(announcement.id.clone())); + } + SharedString::from("Repository") + } + + fn prefix(&self, cx: &App) -> Option { + let store = self.store.read(cx); + let key = match (store.announcement.as_ref(), store.path.as_ref()) { + (Some(announcement), _) => SharedString::from(format!("repo:{}", announcement.addr())), + (None, Some(path)) => SharedString::from(format!("repo-local:{}", path.display())), + (None, None) => SharedString::from("repo"), + }; + let owner = store + .nip34 + .as_ref() + .and_then(|binding| binding.owner) + .or_else(|| { + store + .announcement + .as_ref() + .map(|announcement| announcement.owner) + }); + let mut avatar = Avatar::new(key); + if let Some(owner) = owner { + let profile = self.directory.read(cx).profile(&owner, cx); + avatar = avatar.name(profile.name()).picture(profile.picture()); + } + Some(avatar.small().into_any_element()) + } + + fn loading(&self, _cx: &App) -> bool { + self.loading + } +} impl Render for RepoDetailView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { diff --git a/crates/signed_app/src/shared/panels.rs b/crates/signed_app/src/shared/panels.rs index e084146..4912f36 100644 --- a/crates/signed_app/src/shared/panels.rs +++ b/crates/signed_app/src/shared/panels.rs @@ -41,7 +41,6 @@ pub fn open_primary( pub fn open_detail( workspace: &WeakEntity, id: impl Into, - label: impl Into, build: F, window: &mut Window, cx: &mut App, @@ -50,7 +49,6 @@ pub fn open_detail( F: Fn(&mut Window, &mut App) -> Entity

+ 'static, { let id = id.into(); - let label = label.into(); if workspace .read_with(cx, |workspace, _| workspace.has_detail(&id)) @@ -66,7 +64,7 @@ pub fn open_detail( let view: Arc = Arc::new(build(window, cx)); if let Err(error) = workspace.update(cx, |workspace, cx| { - workspace.show_detail(id.clone(), label, view, cx) + workspace.show_detail(id.clone(), view, cx) }) { log::warn!("failed to open detail panel {id}: {error}"); } diff --git a/crates/signed_app/src/titlebar/mod.rs b/crates/signed_app/src/titlebar/mod.rs index 03a0548..31bee47 100644 --- a/crates/signed_app/src/titlebar/mod.rs +++ b/crates/signed_app/src/titlebar/mod.rs @@ -1,4 +1,3 @@ -use std::collections::HashMap; use std::rc::Rc; use std::sync::Arc; @@ -16,9 +15,9 @@ 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::menu::{Menu, MenuItem}; use ui::tabs::{Tab, Tabs, TabsVariant}; -use ui::theme::ActiveTheme as _; use ui::tooltip::Tooltip; use crate::explore::ExploreView; @@ -340,29 +339,24 @@ impl TitlebarView { } /// Tabs for the repositories opened in the main panel. - pub fn render_repo_tabs( - &self, - open: &[SharedString], - active: Option<&SharedString>, - ) -> Option { + pub fn render_repo_tabs(&self, workspace: &Workspace) -> Option { + let open = workspace.open_mains(); + if open.is_empty() { return None; } - let lookup: HashMap = self - .repository_entries() - .into_iter() - .map(|entry| (entry.key.clone(), entry)) + let tabs: Vec<_> = open + .iter() + .map(|id| (id.clone(), workspace.main_view(id))) .collect(); - let open_tabs = open.to_vec(); - let lookup_for_tabs = lookup; - let lookup_directory = self.directory.clone(); - let selected = active + let selected = workspace + .active_main_id() .and_then(|id| open.iter().position(|open_id| open_id == id)) .unwrap_or(usize::MAX); - let workspace = self.workspace.clone(); + let change_workspace = self.workspace.clone(); let close_workspace = self.workspace.clone(); let open_for_change = open.to_vec(); @@ -372,25 +366,10 @@ impl TitlebarView { .overflow_scroll(true) .selected(selected) .tabs(move |_, cx| { - open_tabs - .iter() - .map(|id| { - let entry = lookup_for_tabs.get(id); - let label = entry - .map(|entry| entry.label.clone()) - .unwrap_or_else(|| id.clone()); - - let mut tab = Tab::new(id.clone(), label).closeable(true); - if let Some(entry) = entry { - tab = tab.lead(entry.avatar(&lookup_directory, cx)); - } else if *id == ExploreView::PANEL_ID { - tab = tab.lead( - Icon::named(IconRole::Grid) - .small() - .color(cx.theme().colors.muted_foreground), - ); - } - tab + 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() }) @@ -398,7 +377,7 @@ impl TitlebarView { let Some(id) = open_for_change.get(index).cloned() else { return; }; - log_dropped(workspace.update(cx, |workspace, cx| { + log_dropped(change_workspace.update(cx, |workspace, cx| { workspace.activate_main(&id, cx); })); }) diff --git a/crates/signed_app/src/workspace.rs b/crates/signed_app/src/workspace.rs index b110866..21383ce 100644 --- a/crates/signed_app/src/workspace.rs +++ b/crates/signed_app/src/workspace.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::sync::Arc; use gpui::{ - Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div, + App, Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div, }; use ui::common::v_flex; use ui::layout::panel::{Panel, PanelView}; @@ -16,7 +16,7 @@ pub struct Workspace { main: Option, main_order: Vec, main_views: HashMap>, - details: Vec<(SharedString, SharedString)>, + details: Vec, detail_views: HashMap>, active_detail: Option, } @@ -41,6 +41,11 @@ impl Workspace { &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() @@ -95,13 +100,11 @@ impl Workspace { pub fn show_detail( &mut self, id: SharedString, - label: SharedString, view: Arc, cx: &mut Context, ) { - match self.details.iter_mut().find(|(open, _)| *open == id) { - Some((_, existing)) => *existing = label, - None => self.details.push((id.clone(), label)), + if !self.details.contains(&id) { + self.details.push(id.clone()); } self.detail_views.insert(id.clone(), view); self.active_detail = Some(id); @@ -109,7 +112,7 @@ impl Workspace { } pub fn close_detail(&mut self, id: &SharedString, cx: &mut Context) { - let Some(index) = self.details.iter().position(|(open, _)| open == id) else { + let Some(index) = self.details.iter().position(|open| open == id) else { return; }; self.close_detail_at(index, cx); @@ -158,7 +161,7 @@ impl Workspace { return; } - let (id, _) = self.details.remove(index); + let id = self.details.remove(index); self.detail_views.remove(&id); if self.active_detail.as_ref() == Some(&id) { @@ -166,14 +169,14 @@ impl Workspace { .details .get(index) .or_else(|| index.checked_sub(1).and_then(|prev| self.details.get(prev))) - .map(|(id, _)| id.clone()); + .cloned(); } cx.notify(); } fn activate_detail_at(&mut self, index: usize, cx: &mut Context) { - let Some((id, _)) = self.details.get(index) else { + let Some(id) = self.details.get(index) else { return; }; self.active_detail = Some(id.clone()); @@ -181,17 +184,26 @@ impl Workspace { } } -impl Panel for Workspace {} +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)) + .and_then(|active| self.details.iter().position(|id| id == active)) .unwrap_or(usize::MAX); - let details = self.details.clone(); + let details: Vec<_> = self + .details + .iter() + .map(|id| (id.clone(), self.detail_views.get(id).cloned())) + .collect(); + let weak = cx.entity().downgrade(); let close_weak = weak.clone(); @@ -199,10 +211,13 @@ impl Render for Workspace { .variant(TabsVariant::Pill) .overflow_scroll(true) .selected(selected) - .tabs(move |_, _| { + .tabs(move |_, cx| { details .iter() - .map(|(id, label)| Tab::new(id.clone(), label.clone()).closeable(true)) + .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| { diff --git a/crates/ui/src/layout/panel.rs b/crates/ui/src/layout/panel.rs index 77b3533..878229c 100644 --- a/crates/ui/src/layout/panel.rs +++ b/crates/ui/src/layout/panel.rs @@ -10,6 +10,7 @@ use gpui::{ Render, RenderOnce, SharedString, StyleRefinement, Styled as _, Window, div, px, }; +use crate::components::tabs::Tab; use crate::layout::StyledExt as _; use crate::layout::resizable::{Resizable, ResizableState}; use crate::theme::ActiveTheme as _; @@ -21,6 +22,19 @@ pub trait Panel: Render + 'static { fn kind(&self, cx: &App) -> Option { None } + + /// The title shown in the panel's tab. + fn title(&self, cx: &App) -> SharedString; + + /// The element drawn before the tab's title, e.g. an avatar or icon. + fn prefix(&self, cx: &App) -> Option { + None + } + + /// Whether the tab shows the loading spinner. + fn loading(&self, cx: &App) -> bool { + false + } } /// An object-safe handle to a panel, so apps can hold panels by kind. @@ -33,6 +47,28 @@ pub trait PanelView: 'static { fn kind(&self, cx: &App) -> Option { None } + + /// The panel's title, read live from the panel. + fn title(&self, cx: &App) -> SharedString; + + /// The panel's tab prefix, read live from the panel. + fn prefix(&self, cx: &App) -> Option { + None + } + + /// Whether the panel's tab shows the loading spinner. + fn loading(&self, cx: &App) -> bool { + false + } + + /// Builds the panel's tab from its title, prefix, and loading state. + fn tab(&self, key: ElementId, cx: &App) -> Tab { + let mut tab = Tab::new(key, self.title(cx)).busy(self.loading(cx)); + if let Some(prefix) = self.prefix(cx) { + tab = tab.lead(prefix); + } + tab + } } impl PanelView for Entity { @@ -43,6 +79,18 @@ impl PanelView for Entity { fn kind(&self, cx: &App) -> Option { self.read(cx).kind(cx) } + + fn title(&self, cx: &App) -> SharedString { + self.read(cx).title(cx) + } + + fn prefix(&self, cx: &App) -> Option { + self.read(cx).prefix(cx) + } + + fn loading(&self, cx: &App) -> bool { + self.read(cx).loading(cx) + } } impl PanelView for Arc

{ @@ -53,6 +101,18 @@ impl PanelView for Arc

{ fn kind(&self, cx: &App) -> Option { (**self).kind(cx) } + + fn title(&self, cx: &App) -> SharedString { + (**self).title(cx) + } + + fn prefix(&self, cx: &App) -> Option { + (**self).prefix(cx) + } + + fn loading(&self, cx: &App) -> bool { + (**self).loading(cx) + } } /// The layout the cached panel content lays out with. diff --git a/crates/ui/tests/panel.rs b/crates/ui/tests/panel.rs index 2fe2f3a..ed72b54 100644 --- a/crates/ui/tests/panel.rs +++ b/crates/ui/tests/panel.rs @@ -2,11 +2,14 @@ //! half-width islands with a drag divider that resizes them. use gpui::{ - AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, Modifiers, MouseButton, - MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, Render, SharedString, - Styled as _, TestAppContext, VisualTestContext, Window, div, point, px, size, + AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, + Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, + Render, SharedString, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px, + size, }; -use ui::layout::panel::{Panel, PanelRow}; +use ui::common::v_flex; +use ui::layout::panel::{Panel, PanelRow, PanelView}; +use ui::tabs::Tabs; use ui::theme::Theme; const WINDOW_WIDTH: f32 = 1024.0; @@ -34,7 +37,11 @@ impl Render for TagView { } } -impl Panel for TagView {} +impl Panel for TagView { + fn title(&self, _cx: &App) -> SharedString { + self.selector.clone() + } +} struct PanelHarness { split: bool, @@ -150,3 +157,104 @@ fn dragging_the_divider_resizes_the_panels(cx: &mut TestAppContext) { .expect("main panel laid out after the drag"); assert_eq!(after.size.width, before.size.width + px(100.0)); } + +struct MetaView { + loading: bool, + prefix: bool, +} + +impl Render for MetaView { + fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { + div().size_full() + } +} + +impl Panel for MetaView { + fn title(&self, _cx: &App) -> SharedString { + SharedString::from("Meta") + } + + fn prefix(&self, _cx: &App) -> Option { + self.prefix.then(|| { + div() + .debug_selector(|| "meta-prefix".to_string()) + .into_any_element() + }) + } + + fn loading(&self, _cx: &App) -> bool { + self.loading + } +} + +struct MetaStripHarness { + busy: Entity, + idle: Entity, + prefixed: Entity, +} + +impl Render for MetaStripHarness { + fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { + let busy = self.busy.clone(); + let idle = self.idle.clone(); + let prefixed = self.prefixed.clone(); + v_flex() + .size_full() + .gap_1() + .child( + Tabs::new("busy-strip") + .tabs(move |_, cx| vec![busy.tab("busy".into(), cx).closeable(false)]), + ) + .child( + Tabs::new("idle-strip") + .tabs(move |_, cx| vec![idle.tab("idle".into(), cx).closeable(false)]), + ) + .child( + Tabs::new("prefix-strip") + .tabs(move |_, cx| vec![prefixed.tab("prefix".into(), cx).closeable(false)]), + ) + } +} + +#[gpui::test] +fn panel_tab_uses_title_prefix_and_loading(cx: &mut TestAppContext) { + cx.update(Theme::init); + let busy = cx.new(|_| MetaView { + loading: true, + prefix: false, + }); + let idle = cx.new(|_| MetaView { + loading: false, + prefix: false, + }); + let prefixed = cx.new(|_| MetaView { + loading: false, + prefix: true, + }); + let window = cx.open_window(size(px(800.), px(600.)), |_, _| MetaStripHarness { + busy, + idle, + prefixed, + }); + let mut cx = VisualTestContext::from_window(window.into(), cx); + cx.run_until_parked(); + + assert!( + cx.debug_bounds("busy-strip-tab-Meta").is_some(), + "title becomes the tab label" + ); + assert!( + cx.debug_bounds("meta-prefix").is_some(), + "prefix becomes the tab lead" + ); + let busy = cx + .debug_bounds("busy-strip-tab-Meta") + .expect("busy tab laid out"); + let idle = cx + .debug_bounds("idle-strip-tab-Meta") + .expect("idle tab laid out"); + assert!( + busy.size.width > idle.size.width, + "loading adds the spinner to the tab" + ); +}