Always refresh the windows when the settings change

Max Brunsfeld created

Change summary

crates/theme_selector/src/theme_selector.rs | 22 +++++++++-------------
crates/zed/src/main.rs                      | 18 +++++++++++++++++-
crates/zed/src/zed.rs                       |  4 +---
3 files changed, 27 insertions(+), 17 deletions(-)

Detailed changes

crates/theme_selector/src/theme_selector.rs 🔗

@@ -110,13 +110,12 @@ impl ThemeSelector {
         });
     }
 
-    fn reload(_: &mut Workspace, action: &Reload, cx: &mut ViewContext<Workspace>) {
+    fn reload(_: &mut Workspace, action: &Reload, _: &mut ViewContext<Workspace>) {
         let current_theme_name = action.0.settings.borrow().theme.name.clone();
         action.0.themes.clear();
         match action.0.themes.get(&current_theme_name) {
             Ok(theme) => {
                 action.0.settings_tx.lock().borrow_mut().theme = theme;
-                cx.refresh_windows();
                 log::info!("reloaded theme {}", current_theme_name);
             }
             Err(error) => {
@@ -137,7 +136,7 @@ impl ThemeSelector {
         self.list_state
             .scroll_to(ScrollTarget::Show(self.selected_index));
 
-        self.show_selected_theme(cx);
+        self.show_selected_theme();
         cx.notify();
     }
 
@@ -148,16 +147,14 @@ impl ThemeSelector {
         self.list_state
             .scroll_to(ScrollTarget::Show(self.selected_index));
 
-        self.show_selected_theme(cx);
+        self.show_selected_theme();
         cx.notify();
     }
 
-    fn show_selected_theme(&mut self, cx: &mut MutableAppContext) {
+    fn show_selected_theme(&mut self) {
         if let Some(mat) = self.matches.get(self.selected_index) {
             match self.themes.get(&mat.string) {
-                Ok(theme) => {
-                    self.set_theme(theme, cx);
-                }
+                Ok(theme) => self.set_theme(theme),
                 Err(error) => {
                     log::error!("error loading theme {}: {}", mat.string, error)
                 }
@@ -177,9 +174,8 @@ impl ThemeSelector {
         self.settings_tx.lock().borrow().theme.clone()
     }
 
-    fn set_theme(&self, theme: Arc<Theme>, cx: &mut MutableAppContext) {
+    fn set_theme(&self, theme: Arc<Theme>) {
         self.settings_tx.lock().borrow_mut().theme = theme;
-        cx.refresh_windows();
     }
 
     fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
@@ -248,7 +244,7 @@ impl ThemeSelector {
             editor::Event::Edited => {
                 self.update_matches(cx);
                 self.select_if_matching(&self.current_theme().name);
-                self.show_selected_theme(cx);
+                self.show_selected_theme();
             }
             editor::Event::Blurred => cx.emit(Event::Dismissed),
             _ => {}
@@ -322,9 +318,9 @@ impl ThemeSelector {
 impl Entity for ThemeSelector {
     type Event = Event;
 
-    fn release(&mut self, cx: &mut MutableAppContext) {
+    fn release(&mut self, _: &mut MutableAppContext) {
         if !self.selection_completed {
-            self.set_theme(self.original_theme.clone(), cx);
+            self.set_theme(self.original_theme.clone());
         }
     }
 }

crates/zed/src/main.rs 🔗

@@ -4,10 +4,11 @@
 use anyhow::{anyhow, Context, Result};
 use client::{self, http, ChannelList, UserStore};
 use fs::OpenOptions;
-use futures::channel::oneshot;
+use futures::{channel::oneshot, StreamExt};
 use gpui::{App, AssetSource, Task};
 use log::LevelFilter;
 use parking_lot::Mutex;
+use postage::{prelude::Stream, watch};
 use project::Fs;
 use simplelog::SimpleLogger;
 use smol::process::Command;
@@ -103,6 +104,8 @@ fn main() {
             cx.font_cache().clone(),
         );
 
+        refresh_window_on_settings_change(settings.clone(), cx);
+
         languages.set_language_server_download_dir(zed_dir);
         languages.set_theme(&settings.borrow().theme.editor.syntax);
 
@@ -246,3 +249,16 @@ fn load_settings_file(
         .detach();
     rx
 }
+
+fn refresh_window_on_settings_change(
+    mut settings_rx: watch::Receiver<Settings>,
+    cx: &mut gpui::MutableAppContext,
+) {
+    settings_rx.try_recv().ok();
+    cx.spawn(|mut cx| async move {
+        while settings_rx.next().await.is_some() {
+            cx.update(|cx| cx.refresh_windows());
+        }
+    })
+    .detach();
+}

crates/zed/src/zed.rs 🔗

@@ -34,12 +34,10 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
     cx.add_global_action(quit);
     cx.add_global_action({
         let settings_tx = app_state.settings_tx.clone();
-
-        move |action: &AdjustBufferFontSize, cx| {
+        move |action: &AdjustBufferFontSize, _| {
             let mut settings_tx = settings_tx.lock();
             let new_size = (settings_tx.borrow().buffer_font_size + action.0).max(MIN_FONT_SIZE);
             settings_tx.borrow_mut().buffer_font_size = new_size;
-            cx.refresh_windows();
         }
     });