Files
signed/crates/signed_app/src/app/mod.rs
T
reya 2d07bcd1ed
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
refactor: move panel stack state into app root
2026-11-23 08:44:05 +07:00

311 lines
11 KiB
Rust

pub mod helpers;
mod placeholder;
mod sign_in;
use std::sync::Arc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
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, PanelView};
use ui::layout::root::RootContext as _;
use ui::layout::titlebar::Titlebar;
use ui::tabs::{Tabs, TabsVariant};
use ui::toast::Toast;
use crate::app::placeholder::PlaceholderView;
use crate::app::sign_in::SignedOutView;
use crate::explore::ExploreView;
use crate::identity::passphrase::PassphraseDialog;
use crate::shared::log_dropped;
use crate::titlebar::{
CreateRepository, REPOSITORY_KEY_CONTEXT, SwitchRepository, TitlebarView,
bind_repository_shortcuts,
};
pub struct AppRoot {
/// 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>,
}
impl AppRoot {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
bind_repository_shortcuts(cx);
let app = cx.entity().downgrade();
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);
let mut subscriptions = Vec::new();
subscriptions.push(cx.subscribe_in(
&backend,
window,
|this, backend, event, window, cx| match event {
BackendEvent::PassphraseRequired => {
this.open_passphrase(cx);
}
BackendEvent::Error(message) => {
cx.toast(Toast::new(message.clone()).icon(IconRole::Warning));
}
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)));
if signed_in {
ExploreView::open(&app, window, cx);
}
});
}
_ => {}
},
));
subscriptions.push(cx.observe(&settings, |_this, store, cx| {
let settings = store.read(cx).settings().clone();
apply_appearance_mode(settings.appearance, cx);
}));
window.defer(cx, move |_window, cx| {
if backend.read(cx).passphrase_required() {
log_dropped(app.update(cx, |this, cx| this.open_passphrase(cx)));
}
});
Self {
main: None,
mains: Vec::new(),
titlebar,
sign_in,
explore,
empty,
passphrase: None,
_subscriptions: subscriptions,
}
}
fn open_passphrase(&mut self, cx: &mut Context<Self>) {
if self.passphrase.is_some() {
return;
}
let dialog = PassphraseDialog::new(cx);
self.passphrase = Some(dialog.clone());
cx.open_dialog(dialog);
}
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 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>,
) {
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>,
) {
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>) {
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>) {
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.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> {
if self.mains.is_empty() {
return None;
}
let tabs: Vec<_> = self
.mains
.iter()
.map(|(id, view)| (id.clone(), view.clone()))
.collect();
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| {
tabs.iter()
.map(|(id, view)| view.tab(id.clone().into(), cx).closeable(true))
.collect()
})
.on_change(move |index, _, cx| {
let Some(id) = open.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 {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
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 main = self.active_main();
let has_main = main.is_some();
div()
.relative()
.size_full()
.key_context(REPOSITORY_KEY_CONTEXT)
.on_action(cx.listener(|this, action: &SwitchRepository, window, cx| {
this.titlebar.update(cx, |titlebar, cx| {
titlebar.activate_shortcut(action.index, window, cx);
});
}))
.on_action(cx.listener(|this, _: &CreateRepository, window, cx| {
this.titlebar.update(cx, |titlebar, cx| {
titlebar.create_repository(window, cx);
});
}))
.child(
v_flex()
.size_full()
.overflow_hidden()
.child(
Titlebar::new("signed-titlebar")
.prefix(
h_flex()
.gap_1()
.child(titlebar_view.render_new_repository())
.child(titlebar_view.render_explore())
.child(titlebar_view.repository_switcher()),
)
.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)
}),
)
.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.))
}),
),
)
}
}