diff --git a/crates/chat_ui/src/lib.rs b/crates/chat_ui/src/lib.rs index 43c07d4..9fc609d 100644 --- a/crates/chat_ui/src/lib.rs +++ b/crates/chat_ui/src/lib.rs @@ -10,8 +10,8 @@ use gpui::{ AnyElement, App, AppContext, ClipboardItem, Context, Entity, EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, ListAlignment, ListOffset, ListState, MouseButton, ObjectFit, ParentElement, PathPromptOptions, Render, SharedString, SharedUri, - StatefulInteractiveElement, Styled, StyledImage, Subscription, Task, WeakEntity, Window, - deferred, div, img, list, px, red, relative, svg, white, + StatefulInteractiveElement, Styled, StyledImage, Subscription, Task, WeakEntity, Window, div, + img, list, px, red, relative, svg, white, }; use itertools::Itertools; use nostr_sdk::prelude::*; @@ -889,7 +889,7 @@ impl ChatPanel { ) .child(message.created_at.to_human_time()) .when(has_reports, |this| { - this.child(deferred(self.render_sent_reports(&id, cx))) + this.child(self.render_sent_reports(&id, cx)) }), ) .when(has_replies, |this| { @@ -1534,7 +1534,7 @@ impl Render for ChatPanel { this.upload(window, cx); })), ) - .child(Input::new(&self.input).appearance(false).text_sm().flex_1()) + .child(Input::new(&self.input).appearance(false).flex_1()) .child( h_flex() .pl_1() diff --git a/crates/device/src/lib.rs b/crates/device/src/lib.rs index d3dda01..dd69da1 100644 --- a/crates/device/src/lib.rs +++ b/crates/device/src/lib.rs @@ -89,14 +89,14 @@ impl DeviceRegistry { fn new(window: &mut Window, cx: &mut Context) -> Self { let nostr = NostrRegistry::global(cx); let settings = AppSettings::global(cx); - let is_nip4e_enabled = settings.read(cx).is_nip4e_enabled(); + let is_nip4e_enabled = settings.read(cx).is_nip4e_enabled(cx); let mut subscriptions = smallvec![]; subscriptions.push( // Subscribe to nostr state events cx.observe(&settings, move |this, settings, cx| { - if settings.read(cx).is_nip4e_enabled() { + if settings.read(cx).is_nip4e_enabled(cx) { this.get_announcement(cx); }; }), diff --git a/crates/settings/src/lib.rs b/crates/settings/src/lib.rs index e107994..9651e55 100644 --- a/crates/settings/src/lib.rs +++ b/crates/settings/src/lib.rs @@ -20,13 +20,15 @@ macro_rules! setting_accessors { $( paste::paste! { pub fn [](cx: &App) -> $type { - Self::global(cx).read(cx).values.$field.clone() + Self::global(cx).read(cx).inner.read(cx).$field.clone() } pub fn [](value: $type, cx: &mut App) { Self::global(cx).update(cx, |this, cx| { - this.values.$field = value; - cx.notify(); + this.inner.update(cx, |inner, cx| { + inner.$field = value; + cx.notify(); + }); }); } } @@ -185,7 +187,7 @@ impl Global for GlobalAppSettings {} /// Application settings pub struct AppSettings { /// Settings - values: Settings, + inner: Entity, /// Event subscriptions _subscriptions: SmallVec<[Subscription; 2]>, @@ -203,11 +205,12 @@ impl AppSettings { } fn new(window: &mut Window, cx: &mut Context) -> Self { + let inner = cx.new(|_| Settings::default()); let mut subscriptions = smallvec![]; subscriptions.push( // Observe and automatically save settings on changes - cx.observe_self(|this, cx| { + cx.observe(&inner, |this, _inner, cx| { this.save(cx); }), ); @@ -218,15 +221,17 @@ impl AppSettings { }); Self { - values: Settings::default(), + inner, _subscriptions: subscriptions, } } /// Update settings fn set_settings(&mut self, settings: Settings, cx: &mut Context) { - self.values = settings; - cx.notify(); + self.inner.update(cx, |this, cx| { + *this = settings; + cx.notify(); + }); } /// Load settings @@ -256,19 +261,16 @@ impl AppSettings { /// Save settings pub fn save(&mut self, cx: &mut Context) { - let settings = self.values.clone(); + let settings = self.inner.read(cx); - let task: Task> = cx.background_spawn(async move { - let path = config_dir().join(".settings"); - let content = serde_json::to_string(&settings)?; - - // Write settings to file - smol::fs::write(&path, content).await?; - - Ok(()) - }); - - task.detach(); + if let Ok(content) = serde_json::to_string(&settings) { + cx.background_spawn(async move { + let path = config_dir().join(".settings"); + // Write settings to file + smol::fs::write(&path, content).await.ok(); + }) + .detach(); + } } /// Set theme @@ -277,8 +279,10 @@ impl AppSettings { T: Into, { // Update settings - self.values.theme = Some(theme.into()); - cx.notify(); + self.inner.update(cx, |this, cx| { + this.theme = Some(theme.into()); + cx.notify(); + }); // Apply the new theme self.apply_theme(window, cx); @@ -286,16 +290,17 @@ impl AppSettings { /// Reset theme pub fn reset_theme(&mut self, window: &mut Window, cx: &mut Context) { - self.values.theme = None; - cx.notify(); - + self.inner.update(cx, |this, cx| { + this.theme = None; + cx.notify(); + }); self.apply_theme(window, cx); } /// Apply theme pub fn apply_theme(&mut self, window: &mut Window, cx: &mut Context) { - if let Some(name) = self.values.theme.as_ref() { - let mode = self.values.theme_mode; + if let Some(name) = self.inner.read(cx).theme.as_ref() { + let mode = self.inner.read(cx).theme_mode; if let Ok(new_theme) = ThemeFamily::from_assets(name) { Theme::apply_theme(Rc::new(new_theme), Some(window), cx); @@ -309,30 +314,22 @@ impl AppSettings { } /// Check if decoupling encryption key is enabled - pub fn is_nip4e_enabled(&self) -> bool { - self.values.encryption_key + pub fn is_nip4e_enabled(&self, cx: &App) -> bool { + self.inner.read(cx).encryption_key } /// Check if the given relay is already authenticated - pub fn trusted_relay(&self, url: &RelayUrl, _cx: &App) -> bool { - self.values.trusted_relays.iter().any(|relay| { + pub fn trusted_relay(&self, url: &RelayUrl, cx: &App) -> bool { + self.inner.read(cx).trusted_relays.iter().any(|relay| { relay.as_str_without_trailing_slash() == url.as_str_without_trailing_slash() }) } /// Add a relay to the trusted list pub fn add_trusted_relay(&mut self, url: &RelayUrl, cx: &mut Context) { - self.values.trusted_relays.insert(url.clone()); - cx.notify(); - } - - /// Add a room configuration - pub fn add_room_config(&mut self, id: u64, config: RoomConfig, cx: &mut Context) { - self.values - .room_configs - .entry(id) - .and_modify(|this| *this = config) - .or_default(); - cx.notify(); + self.inner.update(cx, |this, cx| { + this.trusted_relays.insert(url.clone()); + cx.notify(); + }); } }