add repository shortcuts and extract repository switcher
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
Rust / build (macos-latest, stable) (pull_request) Waiting to run
Rust / build (ubuntu-latest, stable) (pull_request) Waiting to run
Rust / build (windows-latest, stable) (pull_request) Waiting to run
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
Rust / build (macos-latest, stable) (pull_request) Waiting to run
Rust / build (ubuntu-latest, stable) (pull_request) Waiting to run
Rust / build (windows-latest, stable) (pull_request) Waiting to run
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{
|
||||
AnyElement, App, AppContext as _, Context, IntoElement, ParentElement as _, SharedString,
|
||||
Styled as _, Subscription, WeakEntity, Window, px,
|
||||
AnyElement, App, AppContext as _, Context, Entity, IntoElement, KeyBinding,
|
||||
KeyBindingContextPredicate, ParentElement as _, SharedString, Styled as _, Subscription,
|
||||
WeakEntity, Window, px,
|
||||
};
|
||||
use helpers::local_avatar;
|
||||
use metadata::{Profile, ProfileDirectory};
|
||||
use nostr::prelude::PublicKey;
|
||||
use repository_switcher::RepositoryEntry;
|
||||
pub(crate) use repository_switcher::RepositorySwitcher;
|
||||
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::common::h_flex;
|
||||
use ui::icons::{Icon, IconRole};
|
||||
use ui::menu::{Menu, MenuItem};
|
||||
use ui::tabs::{Tab, Tabs, TabsVariant};
|
||||
@@ -24,7 +28,77 @@ use crate::shared::{log_dropped, panels};
|
||||
use crate::workspace::Workspace;
|
||||
use crate::{identity, settings};
|
||||
|
||||
mod helpers;
|
||||
mod repository_switcher;
|
||||
|
||||
/// The key context the repository shortcuts bind against.
|
||||
pub(crate) const REPOSITORY_KEY_CONTEXT: &str = "Signed";
|
||||
const CREATE_KEYSTROKE: &str = "secondary-n";
|
||||
const REPOSITORY_KEYSTROKES: [&str; 9] = [
|
||||
"secondary-1",
|
||||
"secondary-2",
|
||||
"secondary-3",
|
||||
"secondary-4",
|
||||
"secondary-5",
|
||||
"secondary-6",
|
||||
"secondary-7",
|
||||
"secondary-8",
|
||||
"secondary-9",
|
||||
];
|
||||
|
||||
/// Opens the repository at this index in the combined announced/local list.
|
||||
#[derive(Clone, PartialEq, gpui::Action)]
|
||||
#[action(namespace = signed, no_json)]
|
||||
pub(crate) struct SwitchRepository {
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
/// Opens the create-repository dialog.
|
||||
#[derive(Clone, PartialEq, gpui::Action)]
|
||||
#[action(namespace = signed, no_json)]
|
||||
pub(crate) struct CreateRepository;
|
||||
|
||||
pub(crate) fn bind_repository_shortcuts(cx: &mut App) {
|
||||
let context = match KeyBindingContextPredicate::parse(REPOSITORY_KEY_CONTEXT) {
|
||||
Ok(context) => Some(Rc::new(context)),
|
||||
Err(error) => {
|
||||
log::error!("failed to parse repository key context: {error}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let bindings = REPOSITORY_KEYSTROKES
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(index, keystroke)| {
|
||||
KeyBinding::load(
|
||||
keystroke,
|
||||
Box::new(SwitchRepository { index }),
|
||||
context.clone(),
|
||||
false,
|
||||
None,
|
||||
cx,
|
||||
)
|
||||
})
|
||||
.chain(std::iter::once(KeyBinding::load(
|
||||
CREATE_KEYSTROKE,
|
||||
Box::new(CreateRepository),
|
||||
context.clone(),
|
||||
false,
|
||||
None,
|
||||
cx,
|
||||
)))
|
||||
.collect::<Result<Vec<_>, _>>();
|
||||
|
||||
let bindings = match bindings {
|
||||
Ok(bindings) => bindings,
|
||||
Err(error) => {
|
||||
log::error!("failed to parse repository shortcut keystrokes: {error}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
cx.bind_keys(bindings);
|
||||
}
|
||||
|
||||
pub struct TitlebarView {
|
||||
weak: WeakEntity<Self>,
|
||||
@@ -33,10 +107,11 @@ pub struct TitlebarView {
|
||||
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>,
|
||||
/// Profiles rendered by the repository switcher.
|
||||
directory: Entity<ProfileDirectory>,
|
||||
/// The command palette opened from the titlebar.
|
||||
palette: gpui::Entity<CommandPaletteState>,
|
||||
palette: Entity<CommandPaletteState>,
|
||||
repository_switcher: Entity<RepositorySwitcher>,
|
||||
user_menu_open: bool,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
@@ -51,8 +126,42 @@ impl TitlebarView {
|
||||
let repos = RepoListStore::global(cx);
|
||||
let local = LocalReposStore::global(cx);
|
||||
|
||||
let palette = Self::new_palette(workspace.clone(), window, cx);
|
||||
let directory = ProfileDirectory::new(cx);
|
||||
|
||||
let entity = cx.entity();
|
||||
let weak = entity.downgrade();
|
||||
let mut subscriptions = Vec::new();
|
||||
|
||||
let repository_switcher = cx.new(|cx| {
|
||||
RepositorySwitcher::new(
|
||||
directory.clone(),
|
||||
{
|
||||
let weak = weak.clone();
|
||||
move |index, window, cx| {
|
||||
log_dropped(weak.update(cx, |this, cx| {
|
||||
this.activate(index, window, cx);
|
||||
}));
|
||||
}
|
||||
},
|
||||
{
|
||||
let weak = weak.clone();
|
||||
move |window, cx| {
|
||||
log_dropped(weak.update(cx, |this, cx| {
|
||||
this.create_repository(window, cx);
|
||||
}));
|
||||
}
|
||||
},
|
||||
|cx| {
|
||||
let local_repos = LocalReposStore::global(cx);
|
||||
local_repos.update(cx, |store, cx| {
|
||||
store.rescan(cx);
|
||||
});
|
||||
},
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
||||
subscriptions.push(cx.subscribe(&backend, |this, _backend, event, cx| {
|
||||
let signer_changed = matches!(event, BackendEvent::SignerChanged);
|
||||
let signer_required = matches!(event, BackendEvent::SignerRequired);
|
||||
@@ -74,21 +183,6 @@ impl TitlebarView {
|
||||
}
|
||||
}));
|
||||
|
||||
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) {
|
||||
@@ -97,14 +191,24 @@ impl TitlebarView {
|
||||
});
|
||||
});
|
||||
|
||||
this
|
||||
Self {
|
||||
weak: cx.entity().downgrade(),
|
||||
workspace,
|
||||
announcements: Arc::new(Vec::new()),
|
||||
local_repos: Arc::new(Vec::new()),
|
||||
directory,
|
||||
palette,
|
||||
repository_switcher,
|
||||
user_menu_open: false,
|
||||
_subscriptions: subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
fn new_palette(
|
||||
workspace: WeakEntity<Workspace>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> gpui::Entity<CommandPaletteState> {
|
||||
) -> Entity<CommandPaletteState> {
|
||||
cx.new(|cx| {
|
||||
let matcher = |query: &str, commands: &[Command]| {
|
||||
let needle = query.to_lowercase();
|
||||
@@ -116,7 +220,7 @@ impl TitlebarView {
|
||||
};
|
||||
|
||||
CommandPaletteState::new(
|
||||
CommandPalette::new("signed-command-palette", Self::palette_commands(), matcher)
|
||||
CommandPalette::new("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" => {
|
||||
@@ -143,7 +247,7 @@ impl TitlebarView {
|
||||
]
|
||||
}
|
||||
|
||||
pub fn palette(&self) -> gpui::Entity<CommandPaletteState> {
|
||||
pub fn palette(&self) -> Entity<CommandPaletteState> {
|
||||
self.palette.clone()
|
||||
}
|
||||
|
||||
@@ -173,57 +277,94 @@ impl TitlebarView {
|
||||
self.local_repos = Arc::new(local_repos);
|
||||
|
||||
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);
|
||||
profiles.extend(
|
||||
self.local_repos
|
||||
.iter()
|
||||
.filter_map(|entry| {
|
||||
entry
|
||||
.nip34
|
||||
.as_ref()
|
||||
.and_then(|binding| binding.owner)
|
||||
.filter(|owner| !profiles.contains(owner))
|
||||
})
|
||||
.collect::<Vec<PublicKey>>(),
|
||||
);
|
||||
|
||||
self.directory.update(cx, |directory, cx| {
|
||||
directory.track(profiles, cx);
|
||||
});
|
||||
|
||||
if announcements_changed || local_changed {
|
||||
let entries = self.repository_entries();
|
||||
self.repository_switcher.update(cx, |switcher, cx| {
|
||||
switcher.set_entries(entries, cx);
|
||||
});
|
||||
}
|
||||
|
||||
announcements_changed || local_changed
|
||||
}
|
||||
|
||||
pub fn render_tabs(&self, cx: &App) -> Option<Tabs> {
|
||||
let backend = Backend::global(cx);
|
||||
backend.read(cx).current_user()?;
|
||||
fn repository_entries(&self) -> Vec<RepositoryEntry> {
|
||||
let mut entries = Vec::with_capacity(self.announcements.len() + self.local_repos.len());
|
||||
|
||||
let announcements = self.announcements.clone();
|
||||
let local_repos = self.local_repos.clone();
|
||||
for (index, announcement) in self.announcements.iter().enumerate() {
|
||||
let label = announcement
|
||||
.name
|
||||
.as_deref()
|
||||
.map(SharedString::from)
|
||||
.unwrap_or_else(|| SharedString::from(announcement.id.clone()));
|
||||
|
||||
if announcements.is_empty() && local_repos.is_empty() {
|
||||
entries.push(RepositoryEntry {
|
||||
index,
|
||||
key: announcement_key(announcement),
|
||||
label,
|
||||
owner: Some(announcement.owner),
|
||||
});
|
||||
}
|
||||
|
||||
for (local_index, entry) in self.local_repos.iter().enumerate() {
|
||||
let owner = entry.nip34.as_ref().and_then(|binding| binding.owner);
|
||||
|
||||
entries.push(RepositoryEntry {
|
||||
index: self.announcements.len() + local_index,
|
||||
key: local_key(entry),
|
||||
label: entry.name(),
|
||||
owner,
|
||||
});
|
||||
}
|
||||
|
||||
entries
|
||||
}
|
||||
|
||||
pub(crate) fn repository_switcher(&self) -> Entity<RepositorySwitcher> {
|
||||
self.repository_switcher.clone()
|
||||
}
|
||||
|
||||
/// Tabs for the repositories opened in the main panel.
|
||||
pub fn render_repo_tabs(
|
||||
&self,
|
||||
open: &[SharedString],
|
||||
active: Option<&SharedString>,
|
||||
) -> Option<Tabs> {
|
||||
if open.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let keys: Vec<SharedString> = announcements
|
||||
.iter()
|
||||
.map(announcement_key)
|
||||
.chain(local_repos.iter().map(local_key))
|
||||
let lookup: HashMap<SharedString, RepositoryEntry> = self
|
||||
.repository_entries()
|
||||
.into_iter()
|
||||
.map(|entry| (entry.key.clone(), entry))
|
||||
.collect();
|
||||
|
||||
let active = self
|
||||
.workspace
|
||||
.read_with(cx, |workspace, _| workspace.active_main_id().cloned())
|
||||
.ok()
|
||||
.flatten();
|
||||
|
||||
let open_tabs = open.to_vec();
|
||||
let lookup_for_tabs = lookup;
|
||||
let lookup_directory = self.directory.clone();
|
||||
let selected = active
|
||||
.as_ref()
|
||||
.and_then(|id| keys.iter().position(|key| key == id))
|
||||
.and_then(|id| open.iter().position(|open_id| open_id == id))
|
||||
.unwrap_or(usize::MAX);
|
||||
|
||||
let weak = self.weak.clone();
|
||||
let directory = self.directory.clone();
|
||||
let workspace = self.workspace.clone();
|
||||
let open_for_change = open.to_vec();
|
||||
|
||||
Some(
|
||||
Tabs::new("signed-repo-tabs")
|
||||
@@ -231,41 +372,50 @@ impl TitlebarView {
|
||||
.overflow_scroll(true)
|
||||
.selected(selected)
|
||||
.tabs(move |_, cx| {
|
||||
let mut tabs = Vec::with_capacity(announcements.len() + local_repos.len());
|
||||
open_tabs
|
||||
.iter()
|
||||
.map(|id| {
|
||||
let entry = lookup_for_tabs.get(id);
|
||||
let label = entry
|
||||
.map(|entry| entry.label.clone())
|
||||
.unwrap_or_else(|| id.clone());
|
||||
|
||||
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
|
||||
let mut tab = Tab::new(id.clone(), label);
|
||||
if let Some(entry) = entry {
|
||||
tab = tab.lead(entry.avatar(&lookup_directory, cx));
|
||||
}
|
||||
tab
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.on_change(move |index, window, cx| {
|
||||
log_dropped(weak.update(cx, |this, cx| this.activate(index, window, cx)));
|
||||
.on_change(move |index, _, cx| {
|
||||
let Some(id) = open_for_change.get(index).cloned() else {
|
||||
return;
|
||||
};
|
||||
log_dropped(workspace.update(cx, |workspace, cx| {
|
||||
workspace.activate_main(&id, cx);
|
||||
}));
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn activate_shortcut(
|
||||
&mut self,
|
||||
index: usize,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.repository_switcher
|
||||
.update(cx, |switcher, cx| switcher.close(cx));
|
||||
self.activate(index, window, cx);
|
||||
}
|
||||
|
||||
pub(crate) fn create_repository(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.repository_switcher
|
||||
.update(cx, |switcher, cx| switcher.close(cx));
|
||||
dialogs::create::open(self.workspace.clone(), 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();
|
||||
@@ -401,38 +551,33 @@ impl TitlebarView {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn render_actions(&self, cx: &App) -> Option<AnyElement> {
|
||||
let backend = Backend::global(cx);
|
||||
backend.read(cx).current_user()?;
|
||||
|
||||
pub fn render_actions(&self) -> AnyElement {
|
||||
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(),
|
||||
)
|
||||
h_flex()
|
||||
.gap_1()
|
||||
.child(
|
||||
Button::new("open-command-palette")
|
||||
.icon(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(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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user