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;
use futures::lock::Mutex;
use parking_lot::Mutex;
use postage::watch;
use std::sync::Arc;
use zrpc::ForegroundRouter;

View File

@ -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::{

View File

@ -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,

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>> {
if let Some(theme) = self.themes.lock().get(name) {
return Ok(theme.clone());

View File

@ -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);
}
}
}