chore: restructure ui components
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
use gpui::App;
|
||||
use settings::AppearanceMode;
|
||||
use ui::theme::{ThemeVariant, follow_system, set_mode_override};
|
||||
|
||||
/// Applies one appearance mode to the kit theme.
|
||||
pub fn apply_appearance_mode(mode: AppearanceMode, cx: &mut App) {
|
||||
match mode {
|
||||
AppearanceMode::System => follow_system(cx),
|
||||
AppearanceMode::Light => set_mode_override(Some(ThemeVariant::Light), cx),
|
||||
AppearanceMode::Dark => set_mode_override(Some(ThemeVariant::Dark), cx),
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,48 @@
|
||||
pub mod helpers;
|
||||
mod placeholder;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
ParentElement as _, Render, Styled as _, Subscription, WeakEntity, Window, div,
|
||||
AnyView, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
ParentElement as _, Render, SharedString, Styled as _, Subscription, WeakEntity, Window, div,
|
||||
};
|
||||
use helpers::apply_appearance_mode;
|
||||
use nostr::prelude::EventId;
|
||||
use settings::{AppearanceMode, SettingsStore};
|
||||
use signed_state::{Backend, BackendEvent};
|
||||
use signed_core::{Announcement, RepoAddr};
|
||||
use signed_state::{Backend, BackendEvent, RepoStore};
|
||||
use ui::common::{h_flex, v_flex};
|
||||
use ui::icons::IconRole;
|
||||
use ui::layout::panel::PanelStack;
|
||||
use ui::layout::root::RootContext as _;
|
||||
use ui::layout::panel::{PanelSlot, PanelStack};
|
||||
use ui::layout::root::{Root, RootContext as _};
|
||||
use ui::layout::titlebar::Titlebar;
|
||||
use ui::theme::{ThemeEngine, ThemeVariant};
|
||||
use ui::toast::Toast;
|
||||
|
||||
use crate::app::placeholder::PlaceholderView;
|
||||
use crate::explore::ExploreView;
|
||||
use crate::identity::import::ImportDialog;
|
||||
use crate::identity::onboarding::OnboardingDialog;
|
||||
use crate::identity::passphrase::PassphraseDialog;
|
||||
use crate::issues::IssuesView;
|
||||
use crate::issues::detail::IssueDetailView;
|
||||
use crate::pull_requests::{PullRequestDetailView, PullRequestsView};
|
||||
use crate::repo::RepoDetailView;
|
||||
use crate::shared::log_dropped;
|
||||
use crate::titlebar::{
|
||||
CreateRepository, REPOSITORY_KEY_CONTEXT, SwitchRepository, TitlebarView,
|
||||
bind_repository_shortcuts,
|
||||
};
|
||||
use crate::titlebar::{CreateRepository, REPOSITORY_KEY_CONTEXT, SwitchRepository, TitlebarView};
|
||||
|
||||
/// An item of a repository to open from outside its detail panel.
|
||||
pub enum RepoItem {
|
||||
Issue(EventId),
|
||||
PullRequest(EventId),
|
||||
Patch,
|
||||
}
|
||||
|
||||
/// The issue and pull-request tab views a repository panel embeds.
|
||||
pub struct RepoTabs {
|
||||
pub issues: AnyView,
|
||||
pub pulls: AnyView,
|
||||
/// Keep the tab totals flowing into the repository panel.
|
||||
pub(crate) _subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
pub struct AppRoot {
|
||||
panels: Entity<PanelStack>,
|
||||
@@ -34,35 +53,40 @@ pub struct AppRoot {
|
||||
|
||||
impl AppRoot {
|
||||
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
bind_repository_shortcuts(cx);
|
||||
TitlebarView::bind_keys(cx);
|
||||
|
||||
let app = cx.entity().downgrade();
|
||||
let app = cx.weak_entity();
|
||||
let panels = cx.new(|cx| PanelStack::new("signed-panel", cx));
|
||||
let titlebar = cx.new(|cx| TitlebarView::new(app.clone(), window, cx));
|
||||
|
||||
let empty = cx.new(|_| PlaceholderView::new("Select a repository"));
|
||||
panels.update(cx, |panels, cx| panels.set_empty(empty, None, cx));
|
||||
panels.update(cx, |panels, cx| {
|
||||
panels.set_empty(empty, None, 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();
|
||||
window.defer(cx, move |window, cx| {
|
||||
Self::reset_to_explore(&app, window, cx)
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
|this, _backend, event, window, cx| {
|
||||
match event {
|
||||
BackendEvent::PassphraseRequired => {
|
||||
this.open_passphrase(cx);
|
||||
}
|
||||
BackendEvent::Error(message) => {
|
||||
cx.toast(Toast::new(message).icon(IconRole::Warning));
|
||||
}
|
||||
BackendEvent::SignerChanged => {
|
||||
cx.defer_in(window, |this, window, cx| {
|
||||
this.reset_to_explore(window, cx);
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
},
|
||||
));
|
||||
|
||||
@@ -70,15 +94,16 @@ impl AppRoot {
|
||||
let appearance = store.read(cx).settings().appearance;
|
||||
if appearance != this.appearance {
|
||||
this.appearance = appearance;
|
||||
apply_appearance_mode(appearance, cx);
|
||||
Self::apply_appearance_mode(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)));
|
||||
let backend_deferred = backend.clone();
|
||||
cx.defer_in(window, move |this, window, cx| {
|
||||
if backend_deferred.read(cx).passphrase_required() {
|
||||
this.open_passphrase(cx);
|
||||
}
|
||||
Self::reset_to_explore(&app, window, cx);
|
||||
this.reset_to_explore(window, cx);
|
||||
});
|
||||
|
||||
Self {
|
||||
@@ -94,18 +119,129 @@ impl AppRoot {
|
||||
cx.open_dialog(dialog);
|
||||
}
|
||||
|
||||
/// Builds the window root with the app shell inside it.
|
||||
pub fn mount(window: &mut Window, cx: &mut App) -> Entity<Root> {
|
||||
let root = cx.new(|cx| Root::new(window, cx));
|
||||
let content = cx.new(|cx| AppRoot::new(window, cx));
|
||||
root.update(cx, |root, cx| {
|
||||
root.set_content(content, cx);
|
||||
});
|
||||
root
|
||||
}
|
||||
|
||||
/// Applies one appearance mode to the kit theme.
|
||||
pub fn apply_appearance_mode(mode: AppearanceMode, cx: &mut App) {
|
||||
match mode {
|
||||
AppearanceMode::System => ThemeEngine::follow_system(cx),
|
||||
AppearanceMode::Light => ThemeEngine::set_mode_override(Some(ThemeVariant::Light), cx),
|
||||
AppearanceMode::Dark => ThemeEngine::set_mode_override(Some(ThemeVariant::Dark), cx),
|
||||
}
|
||||
}
|
||||
|
||||
/// The panel stack: the main stack, the split stack, and the divider between them.
|
||||
pub fn panels(&self) -> &Entity<PanelStack> {
|
||||
&self.panels
|
||||
}
|
||||
|
||||
/// Empties the stack and opens it on the explore panel, where every session starts.
|
||||
fn reset_to_explore(app: &WeakEntity<Self>, window: &mut Window, cx: &mut App) {
|
||||
let result = app.update(cx, |this, cx| {
|
||||
this.panels.update(cx, |panels, cx| panels.clear(cx))
|
||||
fn reset_to_explore(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.panels.update(cx, |panels, cx| panels.clear(cx));
|
||||
ExploreView::open(&self.panels, cx.weak_entity(), window, cx);
|
||||
}
|
||||
|
||||
/// Builds the tab views for the repository served by `store`, observing their totals into `this`.
|
||||
pub(crate) fn repo_tabs(
|
||||
store: &Entity<RepoStore>,
|
||||
workspace: &WeakEntity<Self>,
|
||||
cx: &mut Context<RepoDetailView>,
|
||||
) -> RepoTabs {
|
||||
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 subscriptions = vec![
|
||||
cx.observe(&issues, |this, view, cx| {
|
||||
this.issue_count = view.read(cx).total_count()
|
||||
}),
|
||||
cx.observe(&pulls, |this, view, cx| {
|
||||
this.pull_count = view.read(cx).total_count()
|
||||
}),
|
||||
];
|
||||
|
||||
RepoTabs {
|
||||
issues: issues.into(),
|
||||
pulls: pulls.into(),
|
||||
_subscriptions: subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens a repository as the main panel, keyed by its address.
|
||||
pub(crate) fn open_repo_panel(
|
||||
&mut self,
|
||||
addr: &RepoAddr,
|
||||
hint: Option<&Announcement>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let id = SharedString::from(format!("repo:{addr}"));
|
||||
let addr = addr.clone();
|
||||
let hint = hint.cloned();
|
||||
let workspace = cx.weak_entity();
|
||||
|
||||
self.panels.update(cx, |panels, cx| {
|
||||
panels.open(
|
||||
PanelSlot::Main,
|
||||
id,
|
||||
move |window, cx| {
|
||||
let addr = addr.clone();
|
||||
let hint = hint.clone();
|
||||
cx.new(|cx| RepoDetailView::new(workspace.clone(), addr, hint, window, cx))
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
});
|
||||
log_dropped(result);
|
||||
ExploreView::open(app, window, cx);
|
||||
}
|
||||
|
||||
/// Opens an issue or pull request using a store built here.
|
||||
pub(crate) fn open_repo_item(
|
||||
&mut self,
|
||||
addr: &RepoAddr,
|
||||
hint: Option<&Announcement>,
|
||||
item: RepoItem,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
match item {
|
||||
RepoItem::Issue(issue_id) => {
|
||||
let store = cx.new(|cx| RepoStore::new(addr.clone(), hint.cloned(), cx));
|
||||
IssueDetailView::open(&self.panels, store, issue_id, window, cx);
|
||||
}
|
||||
RepoItem::PullRequest(pr_id) => {
|
||||
let store = cx.new(|cx| RepoStore::new(addr.clone(), hint.cloned(), cx));
|
||||
PullRequestDetailView::open(
|
||||
&self.panels,
|
||||
&cx.weak_entity(),
|
||||
store,
|
||||
pr_id,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
}
|
||||
// Opening a patch series directly is not a feature.
|
||||
RepoItem::Patch => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the dialog that imports an identity from a secret.
|
||||
pub(crate) fn open_import_dialog(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
|
||||
let dialog = ImportDialog::new(cx);
|
||||
cx.open_dialog(dialog);
|
||||
}
|
||||
|
||||
/// Opens the dialog that creates an identity.
|
||||
pub(crate) fn open_onboarding_dialog(&mut self, cx: &mut Context<Self>) {
|
||||
let dialog = OnboardingDialog::new(cx);
|
||||
cx.open_dialog(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,8 +255,12 @@ impl Render for AppRoot {
|
||||
.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);
|
||||
let titlebar = this.titlebar.downgrade();
|
||||
let index = action.index;
|
||||
window.defer(cx, move |window, cx| {
|
||||
log_dropped(titlebar.update(cx, |titlebar, cx| {
|
||||
titlebar.activate_shortcut(index, window, cx);
|
||||
}));
|
||||
});
|
||||
}))
|
||||
.on_action(cx.listener(|this, _: &CreateRepository, window, cx| {
|
||||
|
||||
Reference in New Issue
Block a user