1use crate::{settings_content::SettingsContent, settings_store::SettingsStore};
2use collections::HashSet;
3use fs::{Fs, PathEventKind};
4use futures::{StreamExt, channel::mpsc};
5use gpui::{App, BackgroundExecutor, ReadGlobal};
6use std::{path::PathBuf, sync::Arc, time::Duration};
7
8pub const EMPTY_THEME_NAME: &str = "empty-theme";
9
10/// Settings for visual tests that use proper fonts instead of Courier.
11/// Uses Helvetica Neue for UI (sans-serif) and Menlo for code (monospace),
12/// which are available on all macOS systems.
13#[cfg(any(test, feature = "test-support"))]
14pub fn visual_test_settings() -> String {
15 let mut value =
16 crate::parse_json_with_comments::<serde_json::Value>(crate::default_settings().as_ref())
17 .unwrap();
18 util::merge_non_null_json_value_into(
19 serde_json::json!({
20 "ui_font_family": ".SystemUIFont",
21 "ui_font_features": {},
22 "ui_font_size": 14,
23 "ui_font_fallback": [],
24 "buffer_font_family": "Menlo",
25 "buffer_font_features": {},
26 "buffer_font_size": 14,
27 "buffer_font_fallbacks": [],
28 "theme": EMPTY_THEME_NAME,
29 }),
30 &mut value,
31 );
32 value.as_object_mut().unwrap().remove("languages");
33 serde_json::to_string(&value).unwrap()
34}
35
36#[cfg(any(test, feature = "test-support"))]
37pub fn test_settings() -> String {
38 let mut value =
39 crate::parse_json_with_comments::<serde_json::Value>(crate::default_settings().as_ref())
40 .unwrap();
41 #[cfg(not(target_os = "windows"))]
42 util::merge_non_null_json_value_into(
43 serde_json::json!({
44 "ui_font_family": "Courier",
45 "ui_font_features": {},
46 "ui_font_size": 14,
47 "ui_font_fallback": [],
48 "buffer_font_family": "Courier",
49 "buffer_font_features": {},
50 "buffer_font_size": 14,
51 "buffer_font_fallbacks": [],
52 "theme": EMPTY_THEME_NAME,
53 }),
54 &mut value,
55 );
56 #[cfg(target_os = "windows")]
57 util::merge_non_null_json_value_into(
58 serde_json::json!({
59 "ui_font_family": "Courier New",
60 "ui_font_features": {},
61 "ui_font_size": 14,
62 "ui_font_fallback": [],
63 "buffer_font_family": "Courier New",
64 "buffer_font_features": {},
65 "buffer_font_size": 14,
66 "buffer_font_fallbacks": [],
67 "theme": EMPTY_THEME_NAME,
68 }),
69 &mut value,
70 );
71 value.as_object_mut().unwrap().remove("languages");
72 serde_json::to_string(&value).unwrap()
73}
74
75pub fn watch_config_file(
76 executor: &BackgroundExecutor,
77 fs: Arc<dyn Fs>,
78 path: PathBuf,
79) -> mpsc::UnboundedReceiver<String> {
80 let (tx, rx) = mpsc::unbounded();
81 executor
82 .spawn(async move {
83 let (events, _) = fs.watch(&path, Duration::from_millis(100)).await;
84 futures::pin_mut!(events);
85
86 let contents = fs.load(&path).await.unwrap_or_default();
87 if tx.unbounded_send(contents).is_err() {
88 return;
89 }
90
91 loop {
92 if events.next().await.is_none() {
93 break;
94 }
95
96 if let Ok(contents) = fs.load(&path).await
97 && tx.unbounded_send(contents).is_err()
98 {
99 break;
100 }
101 }
102 })
103 .detach();
104 rx
105}
106
107pub fn watch_config_dir(
108 executor: &BackgroundExecutor,
109 fs: Arc<dyn Fs>,
110 dir_path: PathBuf,
111 config_paths: HashSet<PathBuf>,
112) -> mpsc::UnboundedReceiver<String> {
113 let (tx, rx) = mpsc::unbounded();
114 executor
115 .spawn(async move {
116 for file_path in &config_paths {
117 if fs.metadata(file_path).await.is_ok_and(|v| v.is_some())
118 && let Ok(contents) = fs.load(file_path).await
119 && tx.unbounded_send(contents).is_err()
120 {
121 return;
122 }
123 }
124
125 let (events, _) = fs.watch(&dir_path, Duration::from_millis(100)).await;
126 futures::pin_mut!(events);
127
128 while let Some(event_batch) = events.next().await {
129 for event in event_batch {
130 if config_paths.contains(&event.path) {
131 match event.kind {
132 Some(PathEventKind::Removed) => {
133 if tx.unbounded_send(String::new()).is_err() {
134 return;
135 }
136 }
137 Some(PathEventKind::Created) | Some(PathEventKind::Changed) => {
138 if let Ok(contents) = fs.load(&event.path).await
139 && tx.unbounded_send(contents).is_err()
140 {
141 return;
142 }
143 }
144 _ => {}
145 }
146 }
147 }
148 }
149 })
150 .detach();
151
152 rx
153}
154
155pub fn update_settings_file(
156 fs: Arc<dyn Fs>,
157 cx: &App,
158 update: impl 'static + Send + FnOnce(&mut SettingsContent, &App),
159) {
160 SettingsStore::global(cx).update_settings_file(fs, update);
161}