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            "ui_font_fallback": [],
 22            "buffer_font_family": "Courier",
 23            "buffer_font_features": {},
 24            "buffer_font_size": 14,
 25            "buffer_font_fallback": [],
 26            "theme": EMPTY_THEME_NAME,
 27        }),
 28        &mut value,
 29    );
 30    value.as_object_mut().unwrap().remove("languages");
 31    serde_json::to_string(&value).unwrap()
 32}
 33
 34pub fn watch_config_file(
 35    executor: &BackgroundExecutor,
 36    fs: Arc<dyn Fs>,
 37    path: PathBuf,
 38) -> mpsc::UnboundedReceiver<String> {
 39    let (tx, rx) = mpsc::unbounded();
 40    executor
 41        .spawn(async move {
 42            let (events, _) = fs.watch(&path, Duration::from_millis(100)).await;
 43            futures::pin_mut!(events);
 44
 45            let contents = fs.load(&path).await.unwrap_or_default();
 46            if tx.unbounded_send(contents).is_err() {
 47                return;
 48            }
 49
 50            loop {
 51                if events.next().await.is_none() {
 52                    break;
 53                }
 54
 55                if let Ok(contents) = fs.load(&path).await {
 56                    if tx.unbounded_send(contents).is_err() {
 57                        break;
 58                    }
 59                }
 60            }
 61        })
 62        .detach();
 63    rx
 64}
 65
 66pub fn handle_settings_file_changes(
 67    mut user_settings_file_rx: mpsc::UnboundedReceiver<String>,
 68    cx: &mut AppContext,
 69) {
 70    let user_settings_content = cx
 71        .background_executor()
 72        .block(user_settings_file_rx.next())
 73        .unwrap();
 74    SettingsStore::update_global(cx, |store, cx| {
 75        store
 76            .set_user_settings(&user_settings_content, cx)
 77            .log_err();
 78    });
 79    cx.spawn(move |mut cx| async move {
 80        while let Some(user_settings_content) = user_settings_file_rx.next().await {
 81            let result = cx.update_global(|store: &mut SettingsStore, cx| {
 82                store
 83                    .set_user_settings(&user_settings_content, cx)
 84                    .log_err();
 85                cx.refresh();
 86            });
 87            if result.is_err() {
 88                break; // App dropped
 89            }
 90        }
 91    })
 92    .detach();
 93}
 94
 95pub fn update_settings_file<T: Settings>(
 96    fs: Arc<dyn Fs>,
 97    cx: &AppContext,
 98    update: impl 'static + Send + FnOnce(&mut T::FileContent, &AppContext),
 99) {
100    SettingsStore::global(cx).update_settings_file::<T>(fs, update);
101}