Always refresh the windows when the settings change

This commit is contained in:
Max Brunsfeld 2022-03-10 15:34:04 -08:00
parent 00056fbe88
commit f32107eb8e
3 changed files with 27 additions and 17 deletions

View file

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

View file

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

View file

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