- Move repository navigation from the dock sidebar to titlebar tabs. - Route main and detail panels through Workspace. - Move sign-in into the main panel and remove unlinked navigation.
389 lines
13 KiB
Rust
389 lines
13 KiB
Rust
use std::sync::Arc;
|
|
|
|
use gpui::{
|
|
AnyElement, App, AppContext as _, Context, IntoElement, ParentElement as _, SharedString,
|
|
Styled as _, Subscription, WeakEntity, Window, px,
|
|
};
|
|
use gpui_native_kit::avatar::Avatar;
|
|
use gpui_native_kit::button::{Button, ButtonVariant};
|
|
use gpui_native_kit::common::{Sizable as _, h_flex};
|
|
use gpui_native_kit::data::tabs::{Tab, Tabs, TabsVariant};
|
|
use gpui_native_kit::icons::{Icon, IconRole};
|
|
use gpui_native_kit::menu::{Menu, MenuItem};
|
|
use gpui_native_kit::theme::ActiveTheme as _;
|
|
use gpui_native_kit::tooltip::Tooltip;
|
|
use helpers::local_avatar;
|
|
use metadata::{Profile, ProfileDirectory};
|
|
use nostr::prelude::PublicKey;
|
|
use signed_core::Announcement;
|
|
use signed_state::{Backend, BackendEvent, LocalReposStore, RepoListStore, ResolvedLocalRepo};
|
|
|
|
use crate::repo::RepoDetailView;
|
|
use crate::repo::actions;
|
|
use crate::repo::dialogs;
|
|
use crate::shared::log_dropped;
|
|
use crate::shared::panels;
|
|
use crate::workspace::Workspace;
|
|
use crate::{identity, settings};
|
|
|
|
mod helpers;
|
|
|
|
pub struct TitlebarView {
|
|
weak: WeakEntity<Self>,
|
|
workspace: WeakEntity<Workspace>,
|
|
scanning: bool,
|
|
/// User's announced repositories.
|
|
announcements: Arc<Vec<Announcement>>,
|
|
/// Local repositories found by the scan that are not announced yet.
|
|
local_repos: Arc<Vec<ResolvedLocalRepo>>,
|
|
/// Profiles rendered by the titlebar tabs.
|
|
directory: gpui::Entity<ProfileDirectory>,
|
|
user_menu_open: bool,
|
|
_subscriptions: Vec<Subscription>,
|
|
}
|
|
|
|
impl TitlebarView {
|
|
pub fn new(workspace: WeakEntity<Workspace>, cx: &mut Context<Self>) -> Self {
|
|
let backend = Backend::global(cx);
|
|
let repos = RepoListStore::global(cx);
|
|
let local = LocalReposStore::global(cx);
|
|
|
|
let mut subscriptions = Vec::new();
|
|
|
|
subscriptions.push(cx.subscribe(&backend, |this, _backend, event, cx| {
|
|
let signer_changed = matches!(event, BackendEvent::SignerChanged);
|
|
let signer_required = matches!(event, BackendEvent::SignerRequired);
|
|
|
|
if this.refresh(cx) || signer_changed || signer_required {
|
|
cx.notify();
|
|
}
|
|
}));
|
|
|
|
subscriptions.push(cx.observe(&repos, |this, _repos, cx| {
|
|
if this.refresh(cx) {
|
|
cx.notify();
|
|
}
|
|
}));
|
|
|
|
subscriptions.push(cx.observe(&local, |this, _local, cx| {
|
|
if this.refresh(cx) {
|
|
cx.notify();
|
|
}
|
|
}));
|
|
|
|
let mut this = Self {
|
|
weak: cx.entity().downgrade(),
|
|
workspace,
|
|
scanning: false,
|
|
announcements: Arc::new(Vec::new()),
|
|
local_repos: Arc::new(Vec::new()),
|
|
directory: ProfileDirectory::new(cx),
|
|
user_menu_open: false,
|
|
_subscriptions: subscriptions,
|
|
};
|
|
this.refresh(cx);
|
|
this
|
|
}
|
|
|
|
fn refresh(&mut self, cx: &mut Context<Self>) -> bool {
|
|
let backend = Backend::global(cx);
|
|
let user = backend.read(cx).current_user();
|
|
|
|
let (announcements, local_repos, scanning) = {
|
|
let repo_list = RepoListStore::global(cx).read(cx);
|
|
|
|
let announcements = user
|
|
.as_ref()
|
|
.map(|user| repo_list.announcements_of(user))
|
|
.unwrap_or_default();
|
|
|
|
let local = LocalReposStore::global(cx).read(cx);
|
|
let local_repos =
|
|
LocalReposStore::resolve(&local.repos, &repo_list.announcements, &announcements);
|
|
|
|
(announcements, local_repos, local.scanning)
|
|
};
|
|
|
|
let announcements_changed = *self.announcements != announcements;
|
|
let local_changed = *self.local_repos != local_repos;
|
|
let scanning_changed = self.scanning != scanning;
|
|
|
|
self.announcements = Arc::new(announcements);
|
|
self.local_repos = Arc::new(local_repos);
|
|
self.scanning = scanning;
|
|
|
|
let mut profiles: Vec<PublicKey> = user.into_iter().collect();
|
|
let owners: Vec<PublicKey> = self
|
|
.local_repos
|
|
.iter()
|
|
.filter_map(|entry| {
|
|
entry
|
|
.nip34
|
|
.as_ref()
|
|
.and_then(|binding| binding.owner)
|
|
.filter(|owner| !profiles.contains(owner))
|
|
})
|
|
.collect();
|
|
|
|
profiles.extend(owners);
|
|
|
|
self.directory.update(cx, |directory, cx| {
|
|
directory.track(profiles, cx);
|
|
});
|
|
|
|
announcements_changed || local_changed || scanning_changed
|
|
}
|
|
|
|
pub fn render_tabs(&self, cx: &App) -> Option<Tabs> {
|
|
if Backend::global(cx).read(cx).current_user().is_none() {
|
|
return None;
|
|
}
|
|
|
|
let announcements = self.announcements.clone();
|
|
let local_repos = self.local_repos.clone();
|
|
|
|
if announcements.is_empty() && local_repos.is_empty() {
|
|
return None;
|
|
}
|
|
|
|
let keys: Vec<SharedString> = announcements
|
|
.iter()
|
|
.map(announcement_key)
|
|
.chain(local_repos.iter().map(local_key))
|
|
.collect();
|
|
|
|
let active = self
|
|
.workspace
|
|
.read_with(cx, |workspace, _| workspace.active_main_id().cloned())
|
|
.ok()
|
|
.flatten();
|
|
let selected = active
|
|
.as_ref()
|
|
.and_then(|id| keys.iter().position(|key| key == id))
|
|
.unwrap_or(usize::MAX);
|
|
|
|
let weak = self.weak.clone();
|
|
let directory = self.directory.clone();
|
|
|
|
Some(
|
|
Tabs::new("signed-repo-tabs")
|
|
.variant(TabsVariant::Pill)
|
|
.overflow_scroll(true)
|
|
.selected(selected)
|
|
.tabs(move |_, cx| {
|
|
let mut tabs = Vec::with_capacity(announcements.len() + local_repos.len());
|
|
|
|
for announcement in announcements.iter() {
|
|
let profile = directory.read(cx).profile(&announcement.owner, cx);
|
|
let label = announcement
|
|
.name
|
|
.as_deref()
|
|
.map(SharedString::from)
|
|
.unwrap_or_else(|| SharedString::from(announcement.id.clone()));
|
|
tabs.push(
|
|
Tab::new(announcement_key(announcement), label).lead(
|
|
Avatar::new(format!("{}:{}", announcement.owner, announcement.id))
|
|
.name(profile.name())
|
|
.picture(profile.picture())
|
|
.small(),
|
|
),
|
|
);
|
|
}
|
|
|
|
for entry in local_repos.iter() {
|
|
tabs.push(
|
|
Tab::new(local_key(entry), entry.name())
|
|
.lead(local_avatar(entry, &directory, cx)),
|
|
);
|
|
}
|
|
|
|
tabs
|
|
})
|
|
.on_change(move |index, window, cx| {
|
|
log_dropped(weak.update(cx, |this, cx| this.activate(index, window, cx)));
|
|
}),
|
|
)
|
|
}
|
|
|
|
fn activate(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) {
|
|
if let Some(announcement) = self.announcements.get(index) {
|
|
let addr = announcement.addr();
|
|
actions::open_repo_panel(&self.workspace, &addr, Some(announcement), window, cx);
|
|
return;
|
|
}
|
|
|
|
let Some(entry) = index
|
|
.checked_sub(self.announcements.len())
|
|
.and_then(|local| self.local_repos.get(local))
|
|
.cloned()
|
|
else {
|
|
return;
|
|
};
|
|
self.open_local_entry(entry, window, cx);
|
|
}
|
|
|
|
/// A local repository opens as the announced repository when its binding matches one.
|
|
fn open_local_entry(
|
|
&mut self,
|
|
entry: ResolvedLocalRepo,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
let ResolvedLocalRepo {
|
|
path,
|
|
nip34,
|
|
announcement,
|
|
} = entry;
|
|
|
|
if let Some(announcement) = announcement {
|
|
let id = SharedString::from(format!("repo:{}", announcement.addr()));
|
|
let workspace = self.workspace.clone();
|
|
let workspace_for_build = workspace.clone();
|
|
panels::open_primary(
|
|
&workspace,
|
|
id,
|
|
move |window, cx| {
|
|
cx.new(|cx| {
|
|
RepoDetailView::new_local_announced(
|
|
workspace_for_build.clone(),
|
|
announcement.clone(),
|
|
path.clone(),
|
|
window,
|
|
cx,
|
|
)
|
|
})
|
|
},
|
|
window,
|
|
cx,
|
|
);
|
|
return;
|
|
}
|
|
|
|
let id = SharedString::from(format!("repo-local:{}", path.display()));
|
|
let workspace = self.workspace.clone();
|
|
let workspace_for_build = workspace.clone();
|
|
|
|
panels::open_primary(
|
|
&workspace,
|
|
id,
|
|
move |window, cx| {
|
|
cx.new(|cx| {
|
|
RepoDetailView::new_local(
|
|
workspace_for_build.clone(),
|
|
path.clone(),
|
|
nip34.clone(),
|
|
window,
|
|
cx,
|
|
)
|
|
})
|
|
},
|
|
window,
|
|
cx,
|
|
);
|
|
}
|
|
|
|
pub fn render_user_menu(&self, cx: &App) -> Option<AnyElement> {
|
|
let profile = Backend::global(cx)
|
|
.read(cx)
|
|
.current_user()
|
|
.map(|public_key| self.directory.read(cx).profile(&public_key, cx))?;
|
|
|
|
Some(self.render_user(&profile, cx).into_any_element())
|
|
}
|
|
|
|
fn render_user(&self, profile: &Profile, cx: &App) -> impl IntoElement {
|
|
let name = profile.name();
|
|
let picture = profile.picture();
|
|
let public_key = profile.public_key();
|
|
let weak = self.weak.clone();
|
|
let muted = cx.theme().colors.muted_foreground;
|
|
|
|
Menu::new("user-menu")
|
|
.open(self.user_menu_open)
|
|
.on_change({
|
|
let weak = weak.clone();
|
|
move |open, _, cx| {
|
|
log_dropped(weak.update(cx, |this, cx| {
|
|
this.user_menu_open = open;
|
|
cx.notify();
|
|
}));
|
|
}
|
|
})
|
|
.trigger({
|
|
let name = name.clone();
|
|
let picture = picture.clone();
|
|
move |_, _| {
|
|
h_flex()
|
|
.gap_1()
|
|
.items_center()
|
|
.child(
|
|
Avatar::new(public_key)
|
|
.name(name.clone())
|
|
.picture(picture.clone()),
|
|
)
|
|
.child(
|
|
Icon::named(IconRole::ChevronDown)
|
|
.size(px(12.))
|
|
.color(muted),
|
|
)
|
|
.into_any_element()
|
|
}
|
|
})
|
|
.items(|_, _| {
|
|
vec![
|
|
MenuItem::new("Settings").handler(|_, cx| settings::open(cx)),
|
|
MenuItem::separator(),
|
|
MenuItem::new("Exit")
|
|
.destructive(true)
|
|
.handler(|_, cx| identity::helpers::open_exit(cx)),
|
|
]
|
|
})
|
|
}
|
|
|
|
pub fn render_actions(&self, cx: &App) -> Option<AnyElement> {
|
|
if Backend::global(cx).read(cx).current_user().is_none() {
|
|
return None;
|
|
}
|
|
|
|
let workspace = self.workspace.clone();
|
|
|
|
Some(
|
|
h_flex()
|
|
.gap_1()
|
|
.child(
|
|
Button::new("rescan-repos")
|
|
.icon(Icon::named(IconRole::Loader))
|
|
.variant(ButtonVariant::Ghost)
|
|
.loading(self.scanning)
|
|
.disabled(self.scanning)
|
|
.tooltip(Tooltip::text("Rescan for local repositories"))
|
|
.on_click(|_, _, cx| {
|
|
let local_repos = LocalReposStore::global(cx);
|
|
local_repos.update(cx, |store, cx| store.rescan(cx));
|
|
}),
|
|
)
|
|
.child(
|
|
Button::new("add-repo")
|
|
.icon(Icon::named(IconRole::Plus))
|
|
.variant(ButtonVariant::Ghost)
|
|
.tooltip(Tooltip::text("Create repository"))
|
|
.on_click(move |_, window, cx| {
|
|
dialogs::create::open(workspace.clone(), window, cx);
|
|
}),
|
|
)
|
|
.into_any_element(),
|
|
)
|
|
}
|
|
}
|
|
|
|
fn announcement_key(announcement: &Announcement) -> SharedString {
|
|
SharedString::from(format!("repo:{}", announcement.addr()))
|
|
}
|
|
|
|
fn local_key(entry: &ResolvedLocalRepo) -> SharedString {
|
|
match entry.announcement.as_ref() {
|
|
Some(announcement) => announcement_key(announcement),
|
|
None => SharedString::from(format!("repo-local:{}", entry.path.display())),
|
|
}
|
|
}
|