refactor: move panel stack state into app root
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run

This commit is contained in:
2026-11-23 08:44:05 +07:00
parent 40c8831bf5
commit 2d07bcd1ed
15 changed files with 647 additions and 374 deletions
+102 -47
View File
@@ -6,15 +6,15 @@ use std::sync::Arc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _,
Render, SharedString, Styled as _, Subscription, Window, div, px,
App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
ParentElement as _, Render, SharedString, Styled as _, Subscription, 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, PanelStack, PanelView};
use ui::layout::panel::{PanelRow, PanelView};
use ui::layout::root::RootContext as _;
use ui::layout::titlebar::Titlebar;
use ui::tabs::{Tabs, TabsVariant};
@@ -31,10 +31,14 @@ use crate::titlebar::{
};
pub struct AppRoot {
panel: Entity<PanelStack>,
/// The id of the main panel currently shown.
main: Option<SharedString>,
/// The open main panels in tab order.
mains: Vec<(SharedString, Arc<dyn PanelView>)>,
titlebar: Entity<TitlebarView>,
sign_in: Entity<SignedOutView>,
explore: Entity<ExploreView>,
empty: Entity<PlaceholderView>,
passphrase: Option<Entity<PassphraseDialog>>,
_subscriptions: Vec<Subscription>,
}
@@ -44,11 +48,10 @@ impl AppRoot {
bind_repository_shortcuts(cx);
let app = cx.entity().downgrade();
let empty = cx.new(|_| PlaceholderView::new("Select a repository"));
let panel = cx.new(|cx| PanelStack::new(empty.clone().into(), cx));
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(app.clone(), cx));
let empty = cx.new(|_| PlaceholderView::new("Select a repository"));
let backend = Backend::global(cx);
let settings = SettingsStore::global(cx);
@@ -67,12 +70,11 @@ impl AppRoot {
BackendEvent::SignerChanged => {
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);
}));
log_dropped(app.update(cx, |this, cx| this.clear(cx)));
if signed_in {
ExploreView::open(&app, window, cx);
}
@@ -94,10 +96,12 @@ impl AppRoot {
});
Self {
panel,
main: None,
mains: Vec::new(),
titlebar,
sign_in,
explore,
empty,
passphrase: None,
_subscriptions: subscriptions,
}
@@ -112,79 +116,127 @@ impl AppRoot {
cx.open_dialog(dialog);
}
/// The panel stack holding the main tile and the detail tiles.
pub fn panel(&self) -> Entity<PanelStack> {
self.panel.clone()
pub fn active_main(&self) -> Option<Arc<dyn PanelView>> {
let id = self.main.as_ref()?;
self.mains
.iter()
.find(|(open, _)| open == id)
.map(|(_, view)| view.clone())
}
pub fn open_main(
pub fn has_main(&self, id: &SharedString) -> bool {
self.mains.iter().any(|(open, _)| open == id)
}
/// Whether the active main panel owns an open sub panel with this id.
pub(crate) fn has_detail(&self, id: &SharedString, cx: &App) -> bool {
self.active_main()
.is_some_and(|main| main.sub_panels(cx).iter().any(|(open, _)| open == id))
}
pub fn show_main(
&mut self,
id: SharedString,
view: Arc<dyn PanelView>,
cx: &mut Context<Self>,
) {
self.panel
.update(cx, |panel, cx| panel.open_main(id, view, cx));
match self.mains.iter_mut().find(|(open, _)| *open == id) {
Some((_, open_view)) => *open_view = view,
None => self.mains.push((id.clone(), view)),
}
self.main = Some(id);
cx.notify();
}
pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context<Self>) {
self.main = Some(id.clone());
cx.notify();
}
/// Opens the panel as a sub panel of the active main panel.
pub fn open_detail(
&mut self,
id: SharedString,
view: Arc<dyn PanelView>,
cx: &mut Context<Self>,
) {
self.panel
.update(cx, |panel, cx| panel.open_split(id, view, cx));
let Some(active) = self.active_main() else {
log::warn!("no main panel open for detail {id}");
return;
};
let panel_id = id.clone();
if !active.open_sub(id, view, cx) {
log::warn!("active panel ignored sub panel {panel_id}");
}
}
/// Closes the active main panel's sub panel with `id`.
pub fn close_detail(&mut self, id: &SharedString, cx: &mut Context<Self>) {
self.panel.update(cx, |panel, cx| panel.close_split(id, cx));
}
pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context<Self>) {
self.panel
.update(cx, |panel, cx| panel.activate_main(id, cx));
if let Some(active) = self.active_main() {
active.close_sub(id, cx);
}
}
pub fn close_main_at(&mut self, index: usize, cx: &mut Context<Self>) {
self.panel
.update(cx, |panel, cx| panel.close_main_at(index, cx));
let Some((id, _)) = self.mains.get(index) else {
return;
};
let id = id.clone();
self.mains.remove(index);
if self.main.as_ref() == Some(&id) {
self.main = self
.mains
.get(index)
.or_else(|| index.checked_sub(1).and_then(|prev| self.mains.get(prev)))
.map(|(id, _)| id.clone());
}
cx.notify();
}
pub fn clear(&mut self, cx: &mut Context<Self>) {
self.panel.update(cx, |panel, cx| panel.clear(cx));
self.main = None;
self.mains.clear();
cx.notify();
}
/// Tabs for the repositories opened in the main panel.
fn render_repo_tabs(&self, cx: &mut Context<Self>) -> Option<Tabs> {
let panel = self.panel.read(cx);
if panel.mains().is_empty() {
if self.mains.is_empty() {
return None;
}
let selected = panel.active_main_index();
let ids: Vec<SharedString> = panel
.mains()
let tabs: Vec<_> = self
.mains
.iter()
.map(|panel| panel.id().clone())
.map(|(id, view)| (id.clone(), view.clone()))
.collect();
let panel = self.panel.clone();
let selected = self
.main
.as_ref()
.and_then(|id| self.mains.iter().position(|(open, _)| open == id))
.unwrap_or(usize::MAX);
let app = cx.entity().downgrade();
let close_app = app.clone();
let open: Vec<SharedString> = self.mains.iter().map(|(id, _)| id.clone()).collect();
Some(
Tabs::new("signed-repo-tabs")
.variant(TabsVariant::Titlebar)
.overflow_scroll(true)
.selected(selected)
.tabs(move |_, cx| panel.read(cx).main_tabs(cx))
.tabs(move |_, cx| {
tabs.iter()
.map(|(id, view)| view.tab(id.clone().into(), cx).closeable(true))
.collect()
})
.on_change(move |index, _, cx| {
let Some(id) = ids.get(index) else {
let Some(id) = open.get(index).cloned() else {
return;
};
let id = id.clone();
log_dropped(app.update(cx, |app, cx| {
app.activate_main(&id, cx);
}));
@@ -205,6 +257,8 @@ impl Render for AppRoot {
let repo_tabs = self.render_repo_tabs(cx);
let titlebar_view = self.titlebar.read(cx);
let main = self.active_main();
let has_main = main.is_some();
div()
.relative()
@@ -242,14 +296,15 @@ impl Render for AppRoot {
titlebar.suffix(menu)
}),
)
.child(if signed_in {
self.panel.clone().into_any_element()
} else {
PanelRow::new("signed-out-panel")
.main(self.sign_in.clone())
.split_at(self.explore.clone(), px(300.))
.into_any_element()
}),
.child(
PanelRow::new("signed-panel")
.when_some(main, |row, main| row.main(main))
.when(!has_main && signed_in, |row| row.main(self.empty.clone()))
.when(!has_main && !signed_in, |row| {
row.main(self.sign_in.clone())
.split_at(self.explore.clone(), px(300.))
}),
),
)
}
}