new layout (wip) (#34)
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

Reviewed-on: #34
This commit was merged in pull request #34.
This commit is contained in:
2026-11-19 07:43:44 +00:00
parent a249174026
commit 5a597f43c4
368 changed files with 38791 additions and 18423 deletions
+628
View File
@@ -0,0 +1,628 @@
use std::rc::Rc;
use std::sync::Arc;
use gpui::{
AnyElement, AnyView, App, AppContext as _, Context, Entity, IntoElement, KeyBinding,
KeyBindingContextPredicate, ParentElement as _, SharedString, Subscription, WeakEntity, Window,
};
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::icons::{Icon, IconRole};
use ui::layout::panel::PanelView;
use ui::menu::{Menu, MenuItem};
use ui::tabs::{Tab, Tabs, TabsVariant};
use ui::tooltip::Tooltip;
use crate::explore::ExploreView;
use crate::identity::exit::ExitDialog;
use crate::repo::{RepoDetailView, actions, dialogs};
use crate::settings::SettingsDialog;
use crate::shared::{log_dropped, panels};
use crate::workspace::Workspace;
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>,
workspace: WeakEntity<Workspace>,
/// 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 repository switcher.
directory: Entity<ProfileDirectory>,
/// The command palette opened from the titlebar.
palette: Entity<CommandPaletteState>,
repository_switcher: Entity<RepositorySwitcher>,
settings: Option<Entity<SettingsDialog>>,
exit: Option<Entity<ExitDialog>>,
create: Option<Entity<dialogs::create::CreateRepoDialog>>,
user_menu_open: bool,
_subscriptions: Vec<Subscription>,
}
impl TitlebarView {
pub fn new(
workspace: WeakEntity<Workspace>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let backend = Backend::global(cx);
let repos = RepoListStore::global(cx);
let local = LocalReposStore::global(cx);
let weak = cx.entity().downgrade();
let palette = Self::new_palette(weak.clone(), window, cx);
let directory = ProfileDirectory::new(cx);
let entity = cx.entity();
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);
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();
}
}));
cx.defer(move |cx| {
entity.update(cx, |this, cx| {
if this.refresh(cx) {
cx.notify();
}
});
});
Self {
weak: cx.entity().downgrade(),
workspace,
announcements: Arc::new(Vec::new()),
local_repos: Arc::new(Vec::new()),
directory,
palette,
repository_switcher,
settings: None,
exit: None,
create: None,
user_menu_open: false,
_subscriptions: subscriptions,
}
}
fn new_palette(
weak: WeakEntity<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Entity<CommandPaletteState> {
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("command-palette", Self::palette_commands(), matcher)
.on_execute(move |command, window, cx| match command.id.as_ref() {
"create-repo" => {
log_dropped(weak.update(cx, |this, cx| this.open_create(window, cx)));
}
"rescan-repos" => {
let local_repos = LocalReposStore::global(cx);
local_repos.update(cx, |store, cx| store.rescan(cx));
}
"open-settings" => {
log_dropped(weak.update(cx, |this, cx| this.open_settings(cx)));
}
"exit" => {
log_dropped(weak.update(cx, |this, cx| this.open_exit(cx)));
}
_ => {}
}),
window,
cx,
)
})
}
fn palette_commands() -> Vec<Command> {
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) -> Entity<CommandPaletteState> {
self.palette.clone()
}
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) = {
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<PublicKey> = user.into_iter().collect();
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
}
fn repository_entries(&self) -> Vec<RepositoryEntry> {
let mut entries = Vec::with_capacity(self.announcements.len() + self.local_repos.len());
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()));
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, workspace: &Workspace) -> Option<Tabs> {
let open = workspace.open_mains();
if open.is_empty() {
return None;
}
let tabs: Vec<_> = open
.iter()
.map(|id| (id.clone(), workspace.main_view(id)))
.collect();
let selected = workspace
.active_main_id()
.and_then(|id| open.iter().position(|open_id| open_id == id))
.unwrap_or(usize::MAX);
let change_workspace = self.workspace.clone();
let close_workspace = self.workspace.clone();
let open_for_change = open.to_vec();
Some(
Tabs::new("signed-repo-tabs")
.variant(TabsVariant::Titlebar)
.overflow_scroll(true)
.selected(selected)
.tabs(move |_, cx| {
tabs.iter()
.map(|(id, view)| match view {
Some(view) => view.tab(id.clone().into(), cx).closeable(true),
None => Tab::new(id.clone(), id.clone()).closeable(true),
})
.collect()
})
.on_change(move |index, _, cx| {
let Some(id) = open_for_change.get(index).cloned() else {
return;
};
log_dropped(change_workspace.update(cx, |workspace, cx| {
workspace.activate_main(&id, cx);
}));
})
.on_close(move |index, _, cx| {
log_dropped(close_workspace.update(cx, |workspace, cx| {
workspace.close_main_at(index, 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));
self.open_create(window, cx);
}
fn open_settings(&mut self, cx: &mut Context<Self>) {
self.settings = Some(SettingsDialog::new(cx));
cx.notify();
}
fn open_exit(&mut self, cx: &mut Context<Self>) {
self.exit = Some(ExitDialog::new(cx));
cx.notify();
}
fn open_create(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let view = dialogs::create::CreateRepoDialog::new(self.workspace.clone(), window, cx);
self.create = Some(view);
cx.notify();
}
/// The dialogs this titlebar opens, rendered at the window root.
pub fn dialogs(&self) -> Vec<AnyView> {
[
self.settings.clone().map(Into::into),
self.exit.clone().map(Into::into),
self.create.clone().map(Into::into),
]
.into_iter()
.flatten()
.collect()
}
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())
}
pub fn render_explore(&self) -> AnyElement {
let workspace = self.workspace.clone();
Button::new("open-explore")
.icon(IconRole::Grid)
.variant(ButtonVariant::Ghost)
.fully_rounded(true)
.tooltip(Tooltip::text("Explore repositories"))
.on_click(move |_, window, cx| {
ExploreView::open(&workspace, window, cx);
})
.into_any_element()
}
pub fn render_new_repository(&self) -> AnyElement {
let weak = self.weak.clone();
Button::new("add-repo")
.icon(IconRole::Plus)
.variant(ButtonVariant::Ghost)
.fully_rounded(true)
.tooltip(Tooltip::text("Create repository"))
.on_click(move |_, window, cx| {
log_dropped(weak.update(cx, |this, cx| this.open_create(window, 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();
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 |_, _| {
Button::new("user-menu-trigger")
.variant(ButtonVariant::Ghost)
.fully_rounded(true)
.padded(false)
.accessibility_label(name.clone())
.child(
Avatar::new(public_key)
.name(name.clone())
.picture(picture.clone())
.fully_rounded(true),
)
.into_any_element()
}
})
.items(move |_, _| {
vec![
MenuItem::new("Settings").handler({
let weak = weak.clone();
move |_, cx| {
log_dropped(weak.update(cx, |this, cx| this.open_settings(cx)));
}
}),
MenuItem::separator(),
MenuItem::new("Exit").destructive(true).handler({
let weak = weak.clone();
move |_, cx| {
log_dropped(weak.update(cx, |this, cx| this.open_exit(cx)));
}
}),
]
})
}
}
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())),
}
}