use std::sync::Arc; use gpui::{ AnyElement, App, AppContext as _, Context, IntoElement, ParentElement as _, SharedString, Styled as _, Subscription, WeakEntity, Window, px, }; 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 ui::avatar::Avatar; use ui::button::{Button, ButtonVariant}; use ui::command_palette::{Command, CommandPalette, CommandPaletteState}; use ui::common::{Sizable as _, h_flex}; use ui::icons::{Icon, IconRole}; use ui::menu::{Menu, MenuItem}; use ui::tabs::{Tab, Tabs, TabsVariant}; use ui::theme::ActiveTheme as _; use ui::tooltip::Tooltip; use crate::repo::{RepoDetailView, actions, dialogs}; use crate::shared::{log_dropped, panels}; use crate::workspace::Workspace; use crate::{identity, settings}; mod helpers; pub struct TitlebarView { weak: WeakEntity, workspace: WeakEntity, /// User's announced repositories. announcements: Arc>, /// Local repositories found by the scan that are not announced yet. local_repos: Arc>, /// Profiles rendered by the titlebar tabs. directory: gpui::Entity, /// The command palette opened from the titlebar. palette: gpui::Entity, user_menu_open: bool, _subscriptions: Vec, } impl TitlebarView { pub fn new( workspace: WeakEntity, window: &mut Window, cx: &mut Context, ) -> 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 palette = Self::new_palette(workspace.clone(), window, cx); let entity = cx.entity(); let this = Self { weak: entity.downgrade(), workspace, announcements: Arc::new(Vec::new()), local_repos: Arc::new(Vec::new()), directory: ProfileDirectory::new(cx), palette, user_menu_open: false, _subscriptions: subscriptions, }; cx.defer(move |cx| { entity.update(cx, |this, cx| { if this.refresh(cx) { cx.notify(); } }); }); this } fn new_palette( workspace: WeakEntity, window: &mut Window, cx: &mut Context, ) -> gpui::Entity { cx.new(|cx| { let matcher = |query: &str, commands: &[Command]| { let needle = query.to_lowercase(); commands .iter() .filter(|command| command.label.to_lowercase().contains(&needle)) .cloned() .collect() }; CommandPaletteState::new( CommandPalette::new("signed-command-palette", Self::palette_commands(), matcher) .on_execute(move |command, window, cx| match command.id.as_ref() { "create-repo" => dialogs::create::open(workspace.clone(), window, cx), "rescan-repos" => { let local_repos = LocalReposStore::global(cx); local_repos.update(cx, |store, cx| store.rescan(cx)); } "open-settings" => settings::open(cx), "exit" => identity::helpers::open_exit(cx), _ => {} }), window, cx, ) }) } fn palette_commands() -> Vec { vec![ Command::new("create-repo", "Create repository").icon(Icon::named(IconRole::Plus)), Command::new("rescan-repos", "Rescan for local repositories") .icon(Icon::named(IconRole::Refresh)), Command::new("open-settings", "Settings").icon(Icon::named(IconRole::Settings)), Command::new("exit", "Exit"), ] } pub fn palette(&self) -> gpui::Entity { self.palette.clone() } fn refresh(&mut self, cx: &mut Context) -> bool { let backend = Backend::global(cx); let user = backend.read(cx).current_user(); let (announcements, local_repos) = { 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) }; let announcements_changed = *self.announcements != announcements; let local_changed = *self.local_repos != local_repos; self.announcements = Arc::new(announcements); self.local_repos = Arc::new(local_repos); let mut profiles: Vec = user.into_iter().collect(); let owners: Vec = 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 } pub fn render_tabs(&self, cx: &App) -> Option { let backend = Backend::global(cx); backend.read(cx).current_user()?; 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 = 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::Titlebar) .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()) .kind("Local Repositories") .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) { 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, ) { 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 { 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 { let backend = Backend::global(cx); backend.read(cx).current_user()?; let workspace = self.workspace.clone(); Some( h_flex() .gap_1() .child( Button::new("open-command-palette") .icon(Icon::named(IconRole::Search)) .variant(ButtonVariant::Ghost) .tooltip(Tooltip::text("Command palette")) .on_click({ let palette = self.palette.clone(); move |_, window, cx| { palette.update(cx, |palette, cx| palette.toggle(window, 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())), } }