Store settings as a global via a gpui app_state

This commit is contained in:
Max Brunsfeld
2022-03-11 15:58:58 -08:00
parent 2103eec463
commit 48848de82c
25 changed files with 406 additions and 733 deletions
+14 -22
View File
@@ -8,7 +8,6 @@ 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;
@@ -93,23 +92,29 @@ fn main() {
.detach_and_log_err(cx);
let settings_file = cx.background().block(settings_file).unwrap();
let (settings_tx, settings) = Settings::from_files(
let mut settings_rx = Settings::from_files(
default_settings,
vec![settings_file],
cx.background().clone(),
themes.clone(),
cx.font_cache().clone(),
);
refresh_window_on_settings_change(settings.clone(), cx);
let settings = cx.background().block(settings_rx.next()).unwrap();
cx.spawn(|mut cx| async move {
while let Some(settings) = settings_rx.next().await {
cx.update(|cx| {
cx.update_app_state(|s, _| *s = settings);
cx.refresh_windows();
});
}
})
.detach();
languages.set_language_server_download_dir(zed::ROOT_PATH.clone());
languages.set_theme(&settings.borrow().theme.editor.syntax);
languages.set_theme(&settings.theme.editor.syntax);
cx.add_app_state(settings);
let app_state = Arc::new(AppState {
languages: Arc::new(languages),
settings_tx,
settings,
themes,
channel_list,
client,
@@ -121,7 +126,7 @@ fn main() {
});
journal::init(app_state.clone(), cx);
zed::init(&app_state, cx);
theme_selector::init(app_state.as_ref().into(), cx);
theme_selector::init(app_state.themes.clone(), cx);
cx.set_menus(menus::menus(&app_state.clone()));
@@ -242,16 +247,3 @@ fn load_settings_file(app: &App, fs: Arc<dyn Fs>) -> oneshot::Receiver<SettingsF
.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();
}
+2 -5
View File
@@ -2,8 +2,6 @@ use crate::{assets::Assets, build_window_options, build_workspace, AppState};
use client::{test::FakeHttpClient, ChannelList, Client, UserStore};
use gpui::MutableAppContext;
use language::LanguageRegistry;
use parking_lot::Mutex;
use postage::watch;
use project::fs::FakeFs;
use std::sync::Arc;
use theme::ThemeRegistry;
@@ -18,9 +16,10 @@ fn init_logger() {
}
pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {
let settings = Settings::test(cx);
let mut path_openers = Vec::new();
editor::init(cx, &mut path_openers);
let (settings_tx, settings) = watch::channel_with(Settings::test(cx));
cx.add_app_state(settings);
let themes = ThemeRegistry::new(Assets, cx.font_cache().clone());
let http = FakeHttpClient::with_404_response();
let client = Client::new(http.clone());
@@ -35,8 +34,6 @@ pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {
Some(tree_sitter_rust::language()),
)));
Arc::new(AppState {
settings_tx: Arc::new(Mutex::new(settings_tx)),
settings,
themes,
languages: Arc::new(languages),
channel_list: cx.add_model(|cx| ChannelList::new(user_store.clone(), client.clone(), cx)),
+14 -31
View File
@@ -23,7 +23,7 @@ pub use project::{self, fs};
use project_panel::ProjectPanel;
use std::{path::PathBuf, sync::Arc};
pub use workspace;
use workspace::{AppState, Workspace, WorkspaceParams};
use workspace::{AppState, Settings, Workspace, WorkspaceParams};
action!(About);
action!(Quit);
@@ -42,11 +42,12 @@ lazy_static! {
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, _| {
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;
move |action: &AdjustBufferFontSize, cx| {
cx.update_app_state::<Settings, _, _>(|settings, cx| {
settings.buffer_font_size =
(settings.buffer_font_size + action.0).max(MIN_FONT_SIZE);
cx.refresh_windows();
});
}
});
@@ -89,7 +90,6 @@ pub fn build_workspace(
client: app_state.client.clone(),
fs: app_state.fs.clone(),
languages: app_state.languages.clone(),
settings: app_state.settings.clone(),
user_store: app_state.user_store.clone(),
channel_list: app_state.channel_list.clone(),
path_openers: app_state.path_openers.clone(),
@@ -98,7 +98,7 @@ pub fn build_workspace(
let project = workspace.project().clone();
workspace.left_sidebar_mut().add_item(
"icons/folder-tree-16.svg",
ProjectPanel::new(project, app_state.settings.clone(), cx).into(),
ProjectPanel::new(project, cx).into(),
);
workspace.right_sidebar_mut().add_item(
"icons/user-16.svg",
@@ -108,35 +108,18 @@ pub fn build_workspace(
workspace.right_sidebar_mut().add_item(
"icons/comment-16.svg",
cx.add_view(|cx| {
ChatPanel::new(
app_state.client.clone(),
app_state.channel_list.clone(),
app_state.settings.clone(),
cx,
)
ChatPanel::new(app_state.client.clone(), app_state.channel_list.clone(), cx)
})
.into(),
);
let diagnostic_message =
cx.add_view(|_| editor::items::DiagnosticMessage::new(app_state.settings.clone()));
let diagnostic_summary = cx.add_view(|cx| {
diagnostics::items::DiagnosticSummary::new(
workspace.project(),
app_state.settings.clone(),
cx,
)
});
let diagnostic_message = cx.add_view(|_| editor::items::DiagnosticMessage::new());
let diagnostic_summary =
cx.add_view(|cx| diagnostics::items::DiagnosticSummary::new(workspace.project(), cx));
let lsp_status = cx.add_view(|cx| {
workspace::lsp_status::LspStatus::new(
workspace.project(),
app_state.languages.clone(),
app_state.settings.clone(),
cx,
)
workspace::lsp_status::LspStatus::new(workspace.project(), app_state.languages.clone(), cx)
});
let cursor_position =
cx.add_view(|_| editor::items::CursorPosition::new(app_state.settings.clone()));
let cursor_position = cx.add_view(|_| editor::items::CursorPosition::new());
workspace.status_bar().update(cx, |status_bar, cx| {
status_bar.add_left_item(diagnostic_summary, cx);
status_bar.add_left_item(diagnostic_message, cx);