Reload current theme on cmd-k shift-T

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2021-08-04 15:52:23 -06:00
parent 8238c87481
commit 4d947580b1
5 changed files with 28 additions and 15 deletions

View File

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

View File

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

View File

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

View File

@ -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>> { pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
if let Some(theme) = self.themes.lock().get(name) { if let Some(theme) = self.themes.lock().get(name) {
return Ok(theme.clone()); return Ok(theme.clone());

View File

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