diff --git a/Cargo.lock b/Cargo.lock index 27669ae..32dd35d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,7 +146,6 @@ dependencies = [ "gpui", "nostr-sdk", "state", - "ui", ] [[package]] diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index c38250b..276ff8b 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -44,16 +44,14 @@ fn main() { // Get client let client = get_client(); - let (signal_tx, mut signal_rx) = tokio::sync::mpsc::channel::(4096); + let (signal_tx, mut signal_rx) = tokio::sync::mpsc::channel::(2048); spawn(async move { // Add some bootstrap relays _ = client.add_relay("wss://relay.damus.io/").await; _ = client.add_relay("wss://relay.primal.net/").await; - _ = client.add_relay("wss://nos.lol/").await; - - _ = client.add_discovery_relay("wss://user.kindpag.es/").await; - _ = client.add_discovery_relay("wss://directory.yabu.me/").await; + _ = client.add_relay("wss://user.kindpag.es/").await; + _ = client.add_relay("wss://directory.yabu.me/").await; // Connect to all relays _ = client.connect().await @@ -217,6 +215,8 @@ fn main() { .with_assets(Assets) .with_http_client(Arc::new(reqwest_client::ReqwestClient::new())) .run(move |cx| { + // Initialize app global state + AppRegistry::set_global(cx); // Initialize chat global state ChatRegistry::set_global(cx); @@ -260,14 +260,10 @@ fn main() { }) .detach(); - let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx)); - let weak_root = root.downgrade(); let window_handle = window.window_handle(); + let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx)); let task = cx.read_credentials(KEYRING_SERVICE); - // Initialize app global state - AppRegistry::set_global(weak_root, cx); - cx.spawn(|mut cx| async move { if let Ok(Some((npub, secret))) = task.await { let (tx, rx) = oneshot::channel::(); @@ -295,24 +291,18 @@ fn main() { .detach(); if let Ok(profile) = rx.await { - cx.update_window(window_handle, |_, window, cx| { - cx.update_global::(|this, cx| { - this.set_user(Some(profile.clone())); - this.set_root_view( - app::init(profile, window, cx).into(), - cx, - ); + _ = cx.update_window(window_handle, |_, window, cx| { + window.replace_root(cx, |window, cx| { + Root::new(app::init(profile, window, cx).into(), window, cx) }); - }) - .unwrap(); + }); } } else { - cx.update_window(window_handle, |_, window, cx| { - cx.update_global::(|this, cx| { - this.set_root_view(onboarding::init(window, cx).into(), cx); + _ = cx.update_window(window_handle, |_, window, cx| { + window.replace_root(cx, |window, cx| { + Root::new(onboarding::init(window, cx).into(), window, cx) }); - }) - .unwrap(); + }); } }) .detach(); diff --git a/crates/app/src/views/app.rs b/crates/app/src/views/app.rs index d715aa8..19de353 100644 --- a/crates/app/src/views/app.rs +++ b/crates/app/src/views/app.rs @@ -226,15 +226,15 @@ impl AppView { } fn on_logout_action(&mut self, _action: &Logout, window: &mut Window, cx: &mut Context) { - cx.update_global::(|this, cx| { - cx.background_executor() - .spawn(async move { get_client().reset().await }) - .detach(); + cx.background_spawn(async move { get_client().reset().await }) + .detach(); - // Remove user + cx.update_global::(|this, _cx| { this.set_user(None); - // Update root view - this.set_root_view(onboarding::init(window, cx).into(), cx); + }); + + window.replace_root(cx, |window, cx| { + Root::new(onboarding::init(window, cx).into(), window, cx) }); } } diff --git a/crates/app/src/views/onboarding.rs b/crates/app/src/views/onboarding.rs index 5447356..1d7ad25 100644 --- a/crates/app/src/views/onboarding.rs +++ b/crates/app/src/views/onboarding.rs @@ -13,12 +13,12 @@ use ui::{ input::{InputEvent, TextInput}, notification::NotificationType, theme::{scale::ColorScaleStep, ActiveTheme}, - ContextModal, Size, StyledExt, + ContextModal, Root, Size, StyledExt, }; use super::app; -const ALPHA_MESSAGE: &str = "Coop is in the alpha stage; it doesn't store any credentials. You will need to log in again when you relanch."; +const ALPHA_MESSAGE: &str = "Coop is in the alpha stage; it doesn't store any credentials. You will need to log in again when you relaunch."; const JOIN_URL: &str = "https://start.njump.me/"; pub fn init(window: &mut Window, cx: &mut App) -> Entity { @@ -113,9 +113,12 @@ impl Onboarding { if let Ok(profile) = rx.await { _ = cx.update_window(window_handle, |_, window, cx| { - cx.update_global::(|this, cx| { + cx.update_global::(|this, _cx| { this.set_user(Some(profile.clone())); - this.set_root_view(app::init(profile, window, cx).into(), cx); + }); + + window.replace_root(cx, |window, cx| { + Root::new(app::init(profile, window, cx).into(), window, cx) }); }) } @@ -178,9 +181,12 @@ impl Onboarding { if let Ok(profile) = rx.await { _ = cx.update_window(window_handle, |_, window, cx| { - cx.update_global::(|this, cx| { + cx.update_global::(|this, _cx| { this.set_user(Some(profile.clone())); - this.set_root_view(app::init(profile, window, cx).into(), cx); + }); + + window.replace_root(cx, |window, cx| { + Root::new(app::init(profile, window, cx).into(), window, cx) }); }) } @@ -371,7 +377,7 @@ impl Render for Onboarding { ) .child( div() - .text_align(gpui::TextAlign::Center) + .text_center() .child( div() .text_lg() diff --git a/crates/app_state/Cargo.toml b/crates/app_state/Cargo.toml index b204e3d..3ccd504 100644 --- a/crates/app_state/Cargo.toml +++ b/crates/app_state/Cargo.toml @@ -7,7 +7,6 @@ publish = false [dependencies] common = { path = "../common" } state = { path = "../state" } -ui = { path = "../ui" } gpui.workspace = true nostr-sdk.workspace = true diff --git a/crates/app_state/src/registry.rs b/crates/app_state/src/registry.rs index 0c8d995..ed973d1 100644 --- a/crates/app_state/src/registry.rs +++ b/crates/app_state/src/registry.rs @@ -2,21 +2,19 @@ use common::{ constants::{ALL_MESSAGES_SUB_ID, NEW_MESSAGE_SUB_ID}, profile::NostrProfile, }; -use gpui::{AnyView, App, AppContext, Global, WeakEntity}; +use gpui::{App, AppContext, Global}; use nostr_sdk::prelude::*; use state::get_client; use std::time::Duration; -use ui::Root; pub struct AppRegistry { - root: WeakEntity, user: Option, } impl Global for AppRegistry {} impl AppRegistry { - pub fn set_global(root: WeakEntity, cx: &mut App) { + pub fn set_global(cx: &mut App) { cx.observe_global::(|cx| { if let Some(profile) = cx.global::().user() { let client = get_client(); @@ -49,7 +47,7 @@ impl AppRegistry { all_messages_sub_id, all_messages, Some(SubscribeAutoCloseOptions::default().exit_policy( - ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(5)), + ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(3)), )), ) .await; @@ -57,14 +55,14 @@ impl AppRegistry { // Subscribe for new message _ = client .subscribe_with_id(new_message_sub_id, new_message, None) - .await; + .await }) .detach(); } }) .detach(); - cx.set_global(Self { root, user: None }); + cx.set_global(Self { user: None }); } pub fn set_user(&mut self, profile: Option) { @@ -74,12 +72,4 @@ impl AppRegistry { pub fn user(&self) -> Option { self.user.clone() } - - pub fn set_root_view(&self, view: AnyView, cx: &mut App) { - if let Err(e) = self.root.update(cx, |this, cx| { - this.set_view(view, cx); - }) { - println!("Error: {}", e) - } - } } diff --git a/crates/common/src/qr.rs b/crates/common/src/qr.rs index 75ba393..cc611bd 100644 --- a/crates/common/src/qr.rs +++ b/crates/common/src/qr.rs @@ -7,7 +7,7 @@ pub fn create_qr(data: &str) -> Result { let config_dir = config_dir().expect("Config directory not found"); let path = config_dir.join("Coop/nostr_connect.png"); - qrcode_generator::to_png_to_file(data, QrCodeEcc::Low, 1024, &path)?; + qrcode_generator::to_png_to_file(data, QrCodeEcc::Low, 512, &path)?; Ok(path) } diff --git a/crates/ui/src/root.rs b/crates/ui/src/root.rs index c4083ab..59c91e4 100644 --- a/crates/ui/src/root.rs +++ b/crates/ui/src/root.rs @@ -212,12 +212,6 @@ impl Root { pub fn view(&self) -> &AnyView { &self.view } - - /// Set the root view of the Root. - pub fn set_view(&mut self, view: AnyView, cx: &mut Context) { - self.view = view; - cx.notify(); - } } impl Render for Root {