1pub mod assets;
2pub mod language;
3pub mod menus;
4#[cfg(any(test, feature = "test-support"))]
5pub mod test;
6pub mod theme_selector;
7
8pub use buffer;
9use buffer::LanguageRegistry;
10use chat_panel::ChatPanel;
11pub use client;
12pub use editor;
13use gpui::{
14 action,
15 geometry::{rect::RectF, vector::vec2f},
16 keymap::Binding,
17 platform::WindowOptions,
18 ModelHandle, MutableAppContext, PathPromptOptions, Task, ViewContext,
19};
20use parking_lot::Mutex;
21pub use people_panel;
22use people_panel::PeoplePanel;
23use postage::watch;
24pub use project::{self, fs};
25use project_panel::ProjectPanel;
26use std::{path::PathBuf, sync::Arc};
27use theme::ThemeRegistry;
28pub use workspace;
29use workspace::{Settings, Workspace, WorkspaceParams};
30
31action!(About);
32action!(Open, Arc<AppState>);
33action!(OpenPaths, OpenParams);
34action!(Quit);
35action!(AdjustBufferFontSize, f32);
36
37const MIN_FONT_SIZE: f32 = 6.0;
38
39pub struct AppState {
40 pub settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
41 pub settings: watch::Receiver<Settings>,
42 pub languages: Arc<LanguageRegistry>,
43 pub themes: Arc<ThemeRegistry>,
44 pub client: Arc<client::Client>,
45 pub user_store: ModelHandle<client::UserStore>,
46 pub fs: Arc<dyn fs::Fs>,
47 pub channel_list: ModelHandle<client::ChannelList>,
48}
49
50#[derive(Clone)]
51pub struct OpenParams {
52 pub paths: Vec<PathBuf>,
53 pub app_state: Arc<AppState>,
54}
55
56pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
57 cx.add_global_action(open);
58 cx.add_global_action(|action: &OpenPaths, cx: &mut MutableAppContext| {
59 open_paths(action, cx).detach()
60 });
61 cx.add_global_action(open_new);
62 cx.add_global_action(quit);
63
64 cx.add_global_action({
65 let settings_tx = app_state.settings_tx.clone();
66
67 move |action: &AdjustBufferFontSize, cx| {
68 let mut settings_tx = settings_tx.lock();
69 let new_size = (settings_tx.borrow().buffer_font_size + action.0).max(MIN_FONT_SIZE);
70 settings_tx.borrow_mut().buffer_font_size = new_size;
71 cx.refresh_windows();
72 }
73 });
74
75 cx.add_bindings(vec![
76 Binding::new("cmd-=", AdjustBufferFontSize(1.), None),
77 Binding::new("cmd--", AdjustBufferFontSize(-1.), None),
78 ])
79}
80
81fn open(action: &Open, cx: &mut MutableAppContext) {
82 let app_state = action.0.clone();
83 cx.prompt_for_paths(
84 PathPromptOptions {
85 files: true,
86 directories: true,
87 multiple: true,
88 },
89 move |paths, cx| {
90 if let Some(paths) = paths {
91 cx.dispatch_global_action(OpenPaths(OpenParams { paths, app_state }));
92 }
93 },
94 );
95}
96
97fn open_paths(action: &OpenPaths, cx: &mut MutableAppContext) -> Task<()> {
98 log::info!("open paths {:?}", action.0.paths);
99
100 // Open paths in existing workspace if possible
101 for window_id in cx.window_ids().collect::<Vec<_>>() {
102 if let Some(handle) = cx.root_view::<Workspace>(window_id) {
103 let task = handle.update(cx, |view, cx| {
104 if view.contains_paths(&action.0.paths, cx.as_ref()) {
105 log::info!("open paths on existing workspace");
106 Some(view.open_paths(&action.0.paths, cx))
107 } else {
108 None
109 }
110 });
111
112 if let Some(task) = task {
113 return task;
114 }
115 }
116 }
117
118 log::info!("open new workspace");
119
120 // Add a new workspace if necessary
121 let app_state = &action.0.app_state;
122 let (_, workspace) = cx.add_window(window_options(), |cx| {
123 build_workspace(&WorkspaceParams::from(app_state.as_ref()), cx)
124 });
125 workspace.update(cx, |workspace, cx| {
126 workspace.open_paths(&action.0.paths, cx)
127 })
128}
129
130fn open_new(action: &workspace::OpenNew, cx: &mut MutableAppContext) {
131 cx.add_window(window_options(), |cx| {
132 let mut workspace = build_workspace(&action.0, cx);
133 workspace.open_new_file(&action, cx);
134 workspace
135 });
136}
137
138fn build_workspace(params: &WorkspaceParams, cx: &mut ViewContext<Workspace>) -> Workspace {
139 let mut workspace = Workspace::new(params, cx);
140 let project = workspace.project().clone();
141 workspace.left_sidebar_mut().add_item(
142 "icons/folder-tree-16.svg",
143 ProjectPanel::new(project, params.settings.clone(), cx).into(),
144 );
145 workspace.right_sidebar_mut().add_item(
146 "icons/user-16.svg",
147 cx.add_view(|cx| PeoplePanel::new(params.user_store.clone(), params.settings.clone(), cx))
148 .into(),
149 );
150 workspace.right_sidebar_mut().add_item(
151 "icons/comment-16.svg",
152 cx.add_view(|cx| {
153 ChatPanel::new(
154 params.client.clone(),
155 params.channel_list.clone(),
156 params.settings.clone(),
157 cx,
158 )
159 })
160 .into(),
161 );
162 workspace
163}
164
165fn window_options() -> WindowOptions<'static> {
166 WindowOptions {
167 bounds: RectF::new(vec2f(0., 0.), vec2f(1024., 768.)),
168 title: None,
169 titlebar_appears_transparent: true,
170 traffic_light_position: Some(vec2f(8., 8.)),
171 }
172}
173
174fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) {
175 cx.platform().quit();
176}
177
178impl<'a> From<&'a AppState> for WorkspaceParams {
179 fn from(state: &'a AppState) -> Self {
180 Self {
181 client: state.client.clone(),
182 fs: state.fs.clone(),
183 languages: state.languages.clone(),
184 settings: state.settings.clone(),
185 user_store: state.user_store.clone(),
186 channel_list: state.channel_list.clone(),
187 }
188 }
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194 use serde_json::json;
195 use test::test_app_state;
196 use theme::DEFAULT_THEME_NAME;
197 use util::test::temp_tree;
198 use workspace::ItemView;
199
200 #[gpui::test]
201 async fn test_open_paths_action(mut cx: gpui::TestAppContext) {
202 let app_state = cx.update(test_app_state);
203 let dir = temp_tree(json!({
204 "a": {
205 "aa": null,
206 "ab": null,
207 },
208 "b": {
209 "ba": null,
210 "bb": null,
211 },
212 "c": {
213 "ca": null,
214 "cb": null,
215 },
216 }));
217
218 cx.update(|cx| {
219 open_paths(
220 &OpenPaths(OpenParams {
221 paths: vec![
222 dir.path().join("a").to_path_buf(),
223 dir.path().join("b").to_path_buf(),
224 ],
225 app_state: app_state.clone(),
226 }),
227 cx,
228 )
229 })
230 .await;
231 assert_eq!(cx.window_ids().len(), 1);
232
233 cx.update(|cx| {
234 open_paths(
235 &OpenPaths(OpenParams {
236 paths: vec![dir.path().join("a").to_path_buf()],
237 app_state: app_state.clone(),
238 }),
239 cx,
240 )
241 })
242 .await;
243 assert_eq!(cx.window_ids().len(), 1);
244 let workspace_1 = cx.root_view::<Workspace>(cx.window_ids()[0]).unwrap();
245 workspace_1.read_with(&cx, |workspace, cx| {
246 assert_eq!(workspace.worktrees(cx).len(), 2)
247 });
248
249 cx.update(|cx| {
250 open_paths(
251 &OpenPaths(OpenParams {
252 paths: vec![
253 dir.path().join("b").to_path_buf(),
254 dir.path().join("c").to_path_buf(),
255 ],
256 app_state: app_state.clone(),
257 }),
258 cx,
259 )
260 })
261 .await;
262 assert_eq!(cx.window_ids().len(), 2);
263 }
264
265 #[gpui::test]
266 async fn test_new_empty_workspace(mut cx: gpui::TestAppContext) {
267 let app_state = cx.update(test_app_state);
268 cx.update(|cx| init(&app_state, cx));
269 cx.dispatch_global_action(workspace::OpenNew(app_state.as_ref().into()));
270 let window_id = *cx.window_ids().first().unwrap();
271 let workspace = cx.root_view::<Workspace>(window_id).unwrap();
272 let editor = workspace.update(&mut cx, |workspace, cx| {
273 workspace
274 .active_item(cx)
275 .unwrap()
276 .to_any()
277 .downcast::<editor::Editor>()
278 .unwrap()
279 });
280
281 editor.update(&mut cx, |editor, cx| {
282 assert!(editor.text(cx).is_empty());
283 });
284
285 workspace.update(&mut cx, |workspace, cx| {
286 workspace.save_active_item(&workspace::Save, cx)
287 });
288
289 app_state.fs.as_fake().insert_dir("/root").await.unwrap();
290 cx.simulate_new_path_selection(|_| Some(PathBuf::from("/root/the-new-name")));
291
292 editor
293 .condition(&cx, |editor, cx| editor.title(cx) == "the-new-name")
294 .await;
295 editor.update(&mut cx, |editor, cx| {
296 assert!(!editor.is_dirty(cx));
297 });
298 }
299
300 #[gpui::test]
301 fn test_bundled_themes(cx: &mut MutableAppContext) {
302 let app_state = test_app_state(cx);
303 let mut has_default_theme = false;
304 for theme_name in app_state.themes.list() {
305 let theme = app_state.themes.get(&theme_name).unwrap();
306 if theme.name == DEFAULT_THEME_NAME {
307 has_default_theme = true;
308 }
309 assert_eq!(theme.name, theme_name);
310 }
311 assert!(has_default_theme);
312 }
313}