pub mod helpers; mod placeholder; mod sign_in; 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, }; 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::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 { panel: Entity, titlebar: Entity, sign_in: Entity, explore: Entity, passphrase: Option>, _subscriptions: Vec, } impl AppRoot { pub fn new(window: &mut Window, cx: &mut Context) -> Self { 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 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(); 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 { panel, titlebar, sign_in, explore, passphrase: None, _subscriptions: subscriptions, } } fn open_passphrase(&mut self, cx: &mut Context) { if self.passphrase.is_some() { return; } let dialog = PassphraseDialog::new(cx); self.passphrase = Some(dialog.clone()); cx.open_dialog(dialog); } /// The panel stack holding the main tile and the detail tiles. pub fn panel(&self) -> Entity { self.panel.clone() } pub fn open_main( &mut self, id: SharedString, view: Arc, cx: &mut Context, ) { self.panel .update(cx, |panel, cx| panel.open_main(id, view, cx)); } pub fn open_detail( &mut self, id: SharedString, view: Arc, cx: &mut Context, ) { self.panel .update(cx, |panel, cx| panel.open_split(id, view, cx)); } pub fn close_detail(&mut self, id: &SharedString, cx: &mut Context) { self.panel.update(cx, |panel, cx| panel.close_split(id, cx)); } pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context) { self.panel .update(cx, |panel, cx| panel.activate_main(id, cx)); } pub fn close_main_at(&mut self, index: usize, cx: &mut Context) { self.panel .update(cx, |panel, cx| panel.close_main_at(index, cx)); } pub fn clear(&mut self, cx: &mut Context) { self.panel.update(cx, |panel, cx| panel.clear(cx)); } /// Tabs for the repositories opened in the main panel. fn render_repo_tabs(&self, cx: &mut Context) -> Option { let panel = self.panel.read(cx); if panel.mains().is_empty() { return None; } let selected = panel.active_main_index(); let ids: Vec = panel .mains() .iter() .map(|panel| panel.id().clone()) .collect(); let panel = self.panel.clone(); let app = cx.entity().downgrade(); let close_app = app.clone(); Some( Tabs::new("signed-repo-tabs") .variant(TabsVariant::Titlebar) .overflow_scroll(true) .selected(selected) .tabs(move |_, cx| panel.read(cx).main_tabs(cx)) .on_change(move |index, _, cx| { let Some(id) = ids.get(index) else { return; }; let id = id.clone(); 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) -> 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); 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(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() }), ) } }