Reload current theme on cmd-k shift-T

Nathan Sobo and Max Brunsfeld created

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>

Change summary

zed/src/lib.rs            |  2 +-
zed/src/main.rs           |  2 +-
zed/src/test.rs           |  2 +-
zed/src/theme.rs          |  5 +++++
zed/src/theme_selector.rs | 32 ++++++++++++++++++++------------
5 files changed, 28 insertions(+), 15 deletions(-)

Detailed changes

zed/src/lib.rs 🔗

@@ -19,7 +19,7 @@ pub mod worktree;
 
 pub use settings::Settings;
 
-use futures::lock::Mutex;
+use parking_lot::Mutex;
 use postage::watch;
 use std::sync::Arc;
 use zrpc::ForegroundRouter;

zed/src/main.rs 🔗

@@ -2,8 +2,8 @@
 #![allow(non_snake_case)]
 
 use fs::OpenOptions;
-use futures::lock::Mutex;
 use log::LevelFilter;
+use parking_lot::Mutex;
 use simplelog::SimpleLogger;
 use std::{fs, path::PathBuf, sync::Arc};
 use zed::{

zed/src/test.rs 🔗

@@ -6,8 +6,8 @@ use crate::{
     time::ReplicaId,
     AppState,
 };
-use futures::lock::Mutex;
 use gpui::{AppContext, Entity, ModelHandle};
+use parking_lot::Mutex;
 use smol::channel;
 use std::{
     marker::PhantomData,

zed/src/theme.rs 🔗

@@ -126,6 +126,11 @@ impl ThemeRegistry {
         })
     }
 
+    pub fn clear(&self) {
+        self.theme_data.lock().clear();
+        self.themes.lock().clear();
+    }
+
     pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
         if let Some(theme) = self.themes.lock().get(name) {
             return Ok(theme.clone());

zed/src/theme_selector.rs 🔗

@@ -7,7 +7,6 @@ use crate::{
     worktree::fuzzy::{match_strings, StringMatch, StringMatchCandidate},
     AppState, Settings,
 };
-use futures::lock::Mutex;
 use gpui::{
     elements::{
         Align, ChildView, ConstrainedBox, Container, Expanded, Flex, Label, ParentElement,
@@ -17,6 +16,7 @@ use gpui::{
     AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, View,
     ViewContext, ViewHandle,
 };
+use parking_lot::Mutex;
 use postage::watch;
 
 pub struct ThemeSelector {
@@ -31,13 +31,14 @@ pub struct ThemeSelector {
 
 pub fn init(cx: &mut MutableAppContext, app_state: &Arc<AppState>) {
     cx.add_action("theme_selector:confirm", ThemeSelector::confirm);
-    // cx.add_action("file_finder:select", ThemeSelector::select);
     cx.add_action("menu:select_prev", ThemeSelector::select_prev);
     cx.add_action("menu:select_next", ThemeSelector::select_next);
     cx.add_action("theme_selector:toggle", ThemeSelector::toggle);
+    cx.add_action("theme_selector:reload", ThemeSelector::reload);
 
     cx.add_bindings(vec![
         Binding::new("cmd-k cmd-t", "theme_selector:toggle", None).with_arg(app_state.clone()),
+        Binding::new("cmd-k shift-T", "theme_selector:reload", None).with_arg(app_state.clone()),
         Binding::new("escape", "theme_selector:toggle", Some("ThemeSelector"))
             .with_arg(app_state.clone()),
         Binding::new("enter", "theme_selector:confirm", Some("ThemeSelector")),
@@ -90,19 +91,26 @@ impl ThemeSelector {
         });
     }
 
+    fn reload(_: &mut Workspace, app_state: &Arc<AppState>, cx: &mut ViewContext<Workspace>) {
+        let current_theme_name = app_state.settings.borrow().theme.name.clone();
+        app_state.themes.clear();
+        match app_state.themes.get(&current_theme_name) {
+            Ok(theme) => {
+                cx.notify_all();
+                app_state.settings_tx.lock().borrow_mut().theme = theme;
+            }
+            Err(error) => {
+                log::error!("failed to load theme {}: {:?}", current_theme_name, error)
+            }
+        }
+    }
+
     fn confirm(&mut self, _: &(), cx: &mut ViewContext<Self>) {
         if let Some(mat) = self.matches.get(self.selected_index) {
             if let Ok(theme) = self.registry.get(&mat.string) {
-                let settings_tx = self.settings_tx.clone();
-                cx.spawn(|this, mut cx| async move {
-                    let mut settings_tx = settings_tx.lock().await;
-                    this.update(&mut cx, |_, cx| {
-                        settings_tx.borrow_mut().theme = theme;
-                        cx.notify_all();
-                        cx.emit(Event::Dismissed);
-                    })
-                })
-                .detach();
+                self.settings_tx.lock().borrow_mut().theme = theme;
+                cx.notify_all();
+                cx.emit(Event::Dismissed);
             }
         }
     }