settings_file.rs

 1use crate::{settings_store::SettingsStore, Settings};
 2use fs::Fs;
 3use futures::{channel::mpsc, StreamExt};
 4use gpui::{AppContext, BackgroundExecutor, ReadGlobal, UpdateGlobal};
 5use std::{path::PathBuf, sync::Arc, time::Duration};
 6use util::ResultExt;
 7
 8pub const EMPTY_THEME_NAME: &str = "empty-theme";
 9
10#[cfg(any(test, feature = "test-support"))]
11pub fn test_settings() -> String {
12    let mut value = crate::settings_store::parse_json_with_comments::<serde_json::Value>(
13        crate::default_settings().as_ref(),
14    )
15    .unwrap();
16    util::merge_non_null_json_value_into(
17        serde_json::json!({
18            "ui_font_family": "Courier",
19            "ui_font_features": {},
20            "ui_font_size": 14,
21            "buffer_font_family": "Courier",
22            "buffer_font_features": {},
23            "buffer_font_size": 14,
24            "theme": EMPTY_THEME_NAME,
25        }),
26        &mut value,
27    );
28    value.as_object_mut().unwrap().remove("languages");
29    serde_json::to_string(&value).unwrap()
30}
31
32pub fn watch_config_file(
33    executor: &BackgroundExecutor,
34    fs: Arc<dyn Fs>,
35    path: PathBuf,
36) -> mpsc::UnboundedReceiver<String> {
37    let (tx, rx) = mpsc::unbounded();
38    executor
39        .spawn(async move {
40            let (events, _) = fs.watch(&path, Duration::from_millis(100)).await;
41            futures::pin_mut!(events);
42
43            let contents = fs.load(&path).await.unwrap_or_default();
44            if tx.unbounded_send(contents).is_err() {
45                return;
46            }
47
48            loop {
49                if events.next().await.is_none() {
50                    break;
51                }
52
53                if let Ok(contents) = fs.load(&path).await {
54                    if tx.unbounded_send(contents).is_err() {
55                        break;
56                    }
57                }
58            }
59        })
60        .detach();
61    rx
62}
63
64pub fn handle_settings_file_changes(
65    mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
66    cx: &mut AppContext,
67) {
68    let user_settings_content = cx
69        .background_executor()
70        .block(user_settings_file_rx.next())
71        .unwrap();
72    SettingsStore::update_global(cx, |store, cx| {
73        store
74            .set_user_settings(&user_settings_content, cx)
75            .log_err();
76    });
77    cx.spawn(move |mut cx| async move {
78        while let Some(user_settings_content) = user_settings_file_rx.next().await {
79            let result = cx.update_global(|store: &mut SettingsStore, cx| {
80                store
81                    .set_user_settings(&user_settings_content, cx)
82                    .log_err();
83                cx.refresh();
84            });
85            if result.is_err() {
86                break; // App dropped
87            }
88        }
89    })
90    .detach();
91}
92
93pub fn update_settings_file<T: Settings>(
94    fs: Arc<dyn Fs>,
95    cx: &AppContext,
96    update: impl 'static + Send + FnOnce(&mut T::FileContent, &AppContext),
97) {
98    SettingsStore::global(cx).update_settings_file::<T>(fs, update);
99}