From af740f462c1a19f78f5fa57ae499788192035eb9 Mon Sep 17 00:00:00 2001 From: reya Date: Sat, 13 Dec 2025 09:20:36 +0700 Subject: [PATCH] feat: simple dock layout --- Cargo.lock | 69 +++++++++++++++++++------------ Cargo.toml | 3 +- crates/account/src/lib.rs | 55 ++++++++++++++++++++++-- crates/common/src/constants.rs | 3 ++ crates/lume/src/main.rs | 2 + crates/lume/src/panels/mod.rs | 1 + crates/lume/src/panels/startup.rs | 41 ++++++++++++++++++ crates/lume/src/sidebar/mod.rs | 41 ++++++++++++++++++ crates/lume/src/title_bar.rs | 2 + crates/lume/src/workspace.rs | 50 ++++++++++++++++++++-- crates/state/Cargo.toml | 1 + crates/state/src/lib.rs | 7 +++- 12 files changed, 238 insertions(+), 37 deletions(-) create mode 100644 crates/lume/src/panels/mod.rs create mode 100644 crates/lume/src/panels/startup.rs create mode 100644 crates/lume/src/sidebar/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 1a7a430a..ffc05c56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1050,9 +1050,9 @@ dependencies = [ [[package]] name = "cmake" -version = "0.1.54" +version = "0.1.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0" +checksum = "d49d74c227b6cc9f3c51a2c7c667a05b6453f7f0f952a5f8e4493bb9e731d68e" dependencies = [ "cc", ] @@ -1131,7 +1131,7 @@ dependencies = [ [[package]] name = "collections" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "indexmap", "rustc-hash 2.1.1", @@ -1520,7 +1520,7 @@ dependencies = [ [[package]] name = "derive_refineable" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "proc-macro2", "quote", @@ -1789,7 +1789,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -2391,7 +2391,7 @@ dependencies = [ [[package]] name = "gpui" version = "0.2.2" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "anyhow", "as-raw-xcb-connection", @@ -2433,6 +2433,7 @@ dependencies = [ "libc", "log", "lyon", + "mach2", "media", "metal", "naga", @@ -2490,7 +2491,7 @@ dependencies = [ [[package]] name = "gpui-component" version = "0.5.0" -source = "git+https://github.com/longbridge/gpui-component#67608353f227f4f40d8d150f33adc0c891be318b" +source = "git+https://github.com/longbridge/gpui-component#aeaa6b61455607c41620955850b06c086ca40b08" dependencies = [ "aho-corasick", "anyhow", @@ -2528,7 +2529,7 @@ dependencies = [ [[package]] name = "gpui-component-macros" version = "0.5.0" -source = "git+https://github.com/longbridge/gpui-component#67608353f227f4f40d8d150f33adc0c891be318b" +source = "git+https://github.com/longbridge/gpui-component#aeaa6b61455607c41620955850b06c086ca40b08" dependencies = [ "proc-macro2", "quote", @@ -2550,7 +2551,7 @@ dependencies = [ [[package]] name = "gpui_macros" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -2561,7 +2562,7 @@ dependencies = [ [[package]] name = "gpui_tokio" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "anyhow", "gpui", @@ -2788,7 +2789,7 @@ dependencies = [ [[package]] name = "http_client" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "anyhow", "async-compression", @@ -2813,7 +2814,7 @@ dependencies = [ [[package]] name = "http_client_tls" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "rustls", "rustls-platform-verifier", @@ -3604,6 +3605,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5cab8f2cadc416a82d2e783a1946388b31654d391d1c7d92cc1f03e295b1deb" dependencies = [ + "serde", "unicode-id", ] @@ -3656,7 +3658,7 @@ dependencies = [ [[package]] name = "media" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "anyhow", "bindgen", @@ -3945,6 +3947,18 @@ dependencies = [ "nostr", ] +[[package]] +name = "nostr-gossip-memory" +version = "0.44.0" +source = "git+https://github.com/rust-nostr/nostr#8ef9fb157cc4c895c80bfba8fc9c5f0879aba36c" +dependencies = [ + "indexmap", + "lru", + "nostr", + "nostr-gossip", + "tokio", +] + [[package]] name = "nostr-lmdb" version = "0.44.0" @@ -4033,7 +4047,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -4514,7 +4528,7 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" [[package]] name = "perf" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "collections", "serde", @@ -4899,7 +4913,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.60.2", + "windows-sys 0.59.0", ] [[package]] @@ -5132,7 +5146,7 @@ dependencies = [ [[package]] name = "refineable" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "derive_refineable", ] @@ -5213,7 +5227,7 @@ dependencies = [ [[package]] name = "reqwest_client" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "anyhow", "bytes", @@ -5412,7 +5426,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.11.0", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -6078,6 +6092,7 @@ dependencies = [ "common", "gpui", "log", + "nostr-gossip-memory", "nostr-lmdb", "nostr-sdk", "rustls", @@ -6191,7 +6206,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sum_tree" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "arrayvec", "log", @@ -6440,7 +6455,7 @@ dependencies = [ "getrandom 0.3.4", "once_cell", "rustix 1.1.2", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7147,7 +7162,7 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] name = "util" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "anyhow", "async-fs", @@ -7183,7 +7198,7 @@ dependencies = [ [[package]] name = "util_macros" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "perf", "quote", @@ -7605,7 +7620,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.48.0", ] [[package]] @@ -8666,7 +8681,7 @@ dependencies = [ [[package]] name = "zlog" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "anyhow", "chrono", @@ -8677,7 +8692,7 @@ dependencies = [ [[package]] name = "ztracing" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" dependencies = [ "tracing", "tracing-subscriber", @@ -8688,7 +8703,7 @@ dependencies = [ [[package]] name = "ztracing_macro" version = "0.1.0" -source = "git+https://github.com/zed-industries/zed#0a1e5f93a08512806e3812a05fb1f884d6673295" +source = "git+https://github.com/zed-industries/zed#e860252185bbefc30b1a9a051167a42c549bd6b0" [[package]] name = "zune-core" diff --git a/Cargo.toml b/Cargo.toml index 4dd3456d..fb6815c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,8 @@ reqwest_client = { git = "https://github.com/zed-industries/zed" } # Nostr nostr-lmdb = { git = "https://github.com/rust-nostr/nostr" } nostr-connect = { git = "https://github.com/rust-nostr/nostr" } -nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = [ "nip96", "nip59", "nip49", "nip44" ] } +nostr-gossip-memory = { git = "https://github.com/rust-nostr/nostr" } +nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = [ "all-nips", "pow-multi-thread" ] } # Others anyhow = "1.0.44" diff --git a/crates/account/src/lib.rs b/crates/account/src/lib.rs index 6fa94b9f..7f04ddc9 100644 --- a/crates/account/src/lib.rs +++ b/crates/account/src/lib.rs @@ -1,6 +1,7 @@ use std::env; -use gpui::{App, AppContext, Context, Entity, Global, Task}; +use anyhow::Error; +use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task}; use nostr_sdk::prelude::*; use smallvec::{smallvec, SmallVec}; use state::NostrRegistry; @@ -17,8 +18,11 @@ pub struct Account { /// The public key of the account public_key: Option, + /// Event subscriptions + _subscriptions: SmallVec<[Subscription; 1]>, + /// Tasks for asynchronous operations - _tasks: SmallVec<[Task<()>; 1]>, + _tasks: SmallVec<[Task>; 1]>, } impl Account { @@ -51,11 +55,12 @@ impl Account { let args: Vec = env::args().collect(); let account = args.get(1).and_then(|s| Keys::parse(s).ok()); + let mut subscriptions = smallvec![]; let mut tasks = smallvec![]; if let Some(keys) = account { tasks.push( - // Background + // Set signer in background cx.spawn(async move |this, cx| { let public_key = keys.public_key(); @@ -72,13 +77,22 @@ impl Account { this.public_key = Some(public_key); cx.notify(); }) - .ok(); }), ); } + subscriptions.push( + // Listen for public key set + cx.observe_self(move |this, cx| { + if let Some(public_key) = this.public_key { + this.init(public_key, cx); + } + }), + ); + Self { public_key: None, + _subscriptions: subscriptions, _tasks: tasks, } } @@ -93,4 +107,37 @@ impl Account { // This method is only called when user is logged in, so unwrap safely self.public_key.unwrap() } + + fn init(&mut self, public_key: PublicKey, cx: &mut Context) { + let nostr = NostrRegistry::global(cx); + let client = nostr.read(cx).client(); + + let task: Task> = cx.background_spawn(async move { + let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE); + + // Construct a filter to get the user's metadata + let filter = Filter::new() + .kind(Kind::Metadata) + .author(public_key) + .limit(1); + + // Subscribe to the user metadata + client.subscribe(filter, Some(opts)).await?; + + // Construct a filter to get the user's contact list + let filter = Filter::new() + .kind(Kind::ContactList) + .author(public_key) + .limit(1); + + // Subscribe to the user's contact list + client.subscribe(filter, Some(opts)).await?; + + log::info!("Subscribed to user metadata and contact list"); + + Ok(()) + }); + + self._tasks.push(task); + } } diff --git a/crates/common/src/constants.rs b/crates/common/src/constants.rs index 0755b6db..6fa9260c 100644 --- a/crates/common/src/constants.rs +++ b/crates/common/src/constants.rs @@ -19,3 +19,6 @@ pub const SEARCH_RELAYS: [&str; 3] = [ /// Default relay for Nostr Connect pub const NOSTR_CONNECT_RELAY: &str = "wss://relay.nsec.app"; + +/// Default width of the sidebar. +pub const DEFAULT_SIDEBAR_WIDTH: f32 = 240.; diff --git a/crates/lume/src/main.rs b/crates/lume/src/main.rs index 79f7d9df..8d689b70 100644 --- a/crates/lume/src/main.rs +++ b/crates/lume/src/main.rs @@ -14,6 +14,8 @@ use crate::workspace::Workspace; mod actions; mod menus; +mod panels; +mod sidebar; mod themes; mod title_bar; mod workspace; diff --git a/crates/lume/src/panels/mod.rs b/crates/lume/src/panels/mod.rs new file mode 100644 index 00000000..564d8c8c --- /dev/null +++ b/crates/lume/src/panels/mod.rs @@ -0,0 +1 @@ +pub mod startup; diff --git a/crates/lume/src/panels/startup.rs b/crates/lume/src/panels/startup.rs new file mode 100644 index 00000000..aee127cc --- /dev/null +++ b/crates/lume/src/panels/startup.rs @@ -0,0 +1,41 @@ +use gpui::{ + div, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, IntoElement, + ParentElement, Render, Window, +}; +use gpui_component::dock::{Panel, PanelEvent}; + +pub fn init(window: &mut Window, cx: &mut App) -> Entity { + cx.new(|cx| Startup::new(window, cx)) +} + +pub struct Startup { + focus_handle: FocusHandle, +} + +impl Startup { + fn new(_window: &mut Window, cx: &mut Context) -> Self { + Self { + focus_handle: cx.focus_handle(), + } + } +} + +impl Panel for Startup { + fn panel_name(&self) -> &'static str { + "Startup" + } +} + +impl EventEmitter for Startup {} + +impl Focusable for Startup { + fn focus_handle(&self, _cx: &App) -> FocusHandle { + self.focus_handle.clone() + } +} + +impl Render for Startup { + fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { + div().child("Startup") + } +} diff --git a/crates/lume/src/sidebar/mod.rs b/crates/lume/src/sidebar/mod.rs new file mode 100644 index 00000000..128c22d0 --- /dev/null +++ b/crates/lume/src/sidebar/mod.rs @@ -0,0 +1,41 @@ +use gpui::{ + div, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, IntoElement, + ParentElement, Render, Window, +}; +use gpui_component::dock::{Panel, PanelEvent}; + +pub fn init(window: &mut Window, cx: &mut App) -> Entity { + cx.new(|cx| Sidebar::new(window, cx)) +} + +pub struct Sidebar { + focus_handle: FocusHandle, +} + +impl Sidebar { + fn new(_window: &mut Window, cx: &mut Context) -> Self { + Self { + focus_handle: cx.focus_handle(), + } + } +} + +impl Panel for Sidebar { + fn panel_name(&self) -> &'static str { + "Sidebar" + } +} + +impl EventEmitter for Sidebar {} + +impl Focusable for Sidebar { + fn focus_handle(&self, _cx: &App) -> FocusHandle { + self.focus_handle.clone() + } +} + +impl Render for Sidebar { + fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { + div().child("Sidebar") + } +} diff --git a/crates/lume/src/title_bar.rs b/crates/lume/src/title_bar.rs index 56044607..72c1cc7b 100644 --- a/crates/lume/src/title_bar.rs +++ b/crates/lume/src/title_bar.rs @@ -9,6 +9,7 @@ use gpui_component::TitleBar; use crate::menus; +#[allow(clippy::type_complexity)] pub struct AppTitleBar { /// The app menu bar app_menu_bar: Entity, @@ -35,6 +36,7 @@ impl AppTitleBar { } } + #[allow(dead_code)] pub fn child(mut self, f: F) -> Self where E: IntoElement, diff --git a/crates/lume/src/workspace.rs b/crates/lume/src/workspace.rs index 31aae5bf..b92638e9 100644 --- a/crates/lume/src/workspace.rs +++ b/crates/lume/src/workspace.rs @@ -1,12 +1,17 @@ -use common::CLIENT_NAME; +use std::sync::Arc; + +use account::Account; +use common::{CLIENT_NAME, DEFAULT_SIDEBAR_WIDTH}; use gpui::{ - div, AppContext, Context, Entity, InteractiveElement, IntoElement, ParentElement, Render, - Styled, Subscription, Window, + div, px, AppContext, Axis, Context, Entity, InteractiveElement, IntoElement, ParentElement, + Render, Styled, Subscription, Window, }; -use gpui_component::dock::DockArea; +use gpui_component::dock::{DockArea, DockItem}; use gpui_component::{v_flex, Root, Theme}; use smallvec::{smallvec, SmallVec}; +use crate::panels::startup; +use crate::sidebar; use crate::title_bar::AppTitleBar; #[derive(Debug)] @@ -25,6 +30,8 @@ impl Workspace { pub fn new(window: &mut Window, cx: &mut Context) -> Self { let dock = cx.new(|cx| DockArea::new("dock", None, window, cx)); let title_bar = cx.new(|cx| AppTitleBar::new(CLIENT_NAME, window, cx)); + let account = Account::global(cx); + let mut subscriptions = smallvec![]; subscriptions.push( @@ -34,12 +41,47 @@ impl Workspace { }), ); + subscriptions.push( + // Observe account entity changes + cx.observe_in(&account, window, move |this, state, window, cx| { + if state.read(cx).has_account() { + this.init_app_layout(window, cx); + } + }), + ); + Self { dock, title_bar, _subscriptions: subscriptions, } } + + fn init_app_layout(&self, window: &mut Window, cx: &mut Context) { + let weak_dock = self.dock.downgrade(); + + let sidebar = Arc::new(sidebar::init(window, cx)); + let startup = Arc::new(startup::init(window, cx)); + + // Construct left dock (sidebar) + let left = DockItem::panel(sidebar); + + // Construct center dock + let center = DockItem::split_with_sizes( + Axis::Vertical, + vec![DockItem::tabs(vec![startup], &weak_dock, window, cx)], + vec![None], + &weak_dock, + window, + cx, + ); + + // Update dock layout + self.dock.update(cx, |this, cx| { + this.set_left_dock(left, Some(px(DEFAULT_SIDEBAR_WIDTH)), true, window, cx); + this.set_center(center, window, cx); + }); + } } impl Render for Workspace { diff --git a/crates/state/Cargo.toml b/crates/state/Cargo.toml index 9abb248a..8d0e7c14 100644 --- a/crates/state/Cargo.toml +++ b/crates/state/Cargo.toml @@ -9,6 +9,7 @@ common = { path = "../common" } nostr-sdk.workspace = true nostr-lmdb.workspace = true +nostr-gossip-memory.workspace = true gpui.workspace = true smol.workspace = true diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs index a03b0183..85add8df 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -2,6 +2,7 @@ use std::time::Duration; use common::{config_dir, BOOTSTRAP_RELAYS, SEARCH_RELAYS}; use gpui::{App, AppContext, Context, Entity, Global, Task}; +use nostr_gossip_memory::prelude::*; use nostr_lmdb::NostrLmdb; use nostr_sdk::prelude::*; use smallvec::{smallvec, SmallVec}; @@ -61,7 +62,11 @@ impl NostrRegistry { }); // Construct the nostr client - let client = ClientBuilder::default().database(lmdb).opts(opts).build(); + let client = ClientBuilder::default() + .database(lmdb) + .gossip(NostrGossipMemory::unbounded()) + .opts(opts) + .build(); let mut tasks = smallvec![];