From 4d947580b1fd786301e7ba18b0f3b5cefa366ed8 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 4 Aug 2021 15:52:23 -0600 Subject: [PATCH] Reload current theme on cmd-k shift-T Co-Authored-By: Max Brunsfeld --- 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(-) diff --git a/zed/src/lib.rs b/zed/src/lib.rs index 0236fdd0330a00f521f15e2b6dcabdd4f5a47c0e..24196a42226caf68640f246b88a612ebc77a4327 100644 --- a/zed/src/lib.rs +++ b/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; diff --git a/zed/src/main.rs b/zed/src/main.rs index 84a249b2f70323ea9b417af60ae1b9a9be4b2d3f..b488dbe95b5c9947fec80bc76e430203657dcd52 100644 --- a/zed/src/main.rs +++ b/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::{ diff --git a/zed/src/test.rs b/zed/src/test.rs index 7d7d6e3ed6ab1b2898a8d6bc1afb79d2dcbcde47..e2ddff054ea1b288404bbcc4bfc75d462e540b43 100644 --- a/zed/src/test.rs +++ b/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, diff --git a/zed/src/theme.rs b/zed/src/theme.rs index f5c01b2759ebb2ff405590a136496ac4689fcdd7..b1c09bf7c862fe1cc6e5384defafb223a46507a1 100644 --- a/zed/src/theme.rs +++ b/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> { if let Some(theme) = self.themes.lock().get(name) { return Ok(theme.clone()); diff --git a/zed/src/theme_selector.rs b/zed/src/theme_selector.rs index ec50e92a6e81decb50e0649e6693d8caeca6ca4d..9aa35cb3959f3695abcc28b2b3bdc9908b84732f 100644 --- a/zed/src/theme_selector.rs +++ b/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) { 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, cx: &mut ViewContext) { + let current_theme_name = app_state.settings.borrow().theme.name.clone(); + app_state.themes.clear(); + match app_state.themes.get(¤t_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) { 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); } } }