Add setting to make projects online/offline by default

Max Brunsfeld created

Change summary

crates/collab/src/integration_tests.rs |  3 +
crates/project/src/project.rs          | 32 ++++++++++++++--------------
crates/settings/src/settings.rs        |  9 +++++++
3 files changed, 27 insertions(+), 17 deletions(-)

Detailed changes

crates/collab/src/integration_tests.rs 🔗

@@ -4635,7 +4635,8 @@ impl TestServer {
 
     async fn create_client(&mut self, cx: &mut TestAppContext, name: &str) -> TestClient {
         cx.update(|cx| {
-            let settings = Settings::test(cx);
+            let mut settings = Settings::test(cx);
+            settings.projects_online_by_default = false;
             cx.set_global(settings);
         });
 

crates/project/src/project.rs 🔗

@@ -562,9 +562,14 @@ impl Project {
 
         let db = self.project_store.read(cx).db.clone();
         let keys = self.db_keys_for_online_state(cx);
+        let online_by_default = cx.global::<Settings>().projects_online_by_default;
         let read_online = cx.background().spawn(async move {
             let values = db.read(keys)?;
-            anyhow::Ok(values.into_iter().all(|e| e.is_some()))
+            anyhow::Ok(
+                values
+                    .into_iter()
+                    .all(|e| e.map_or(online_by_default, |e| e == [true as u8])),
+            )
         });
         cx.spawn(|this, mut cx| async move {
             let online = read_online.await.log_err().unwrap_or(false);
@@ -592,11 +597,8 @@ impl Project {
         let keys = self.db_keys_for_online_state(cx);
         let is_online = self.is_online();
         cx.background().spawn(async move {
-            if is_online {
-                db.write(keys.into_iter().map(|key| (key, &[])))
-            } else {
-                db.delete(keys)
-            }
+            let value = &[is_online as u8];
+            db.write(keys.into_iter().map(|key| (key, value)))
         })
     }
 
@@ -882,17 +884,15 @@ impl Project {
         self.worktrees
             .iter()
             .filter_map(|worktree| {
-                worktree.upgrade(&cx).map(|worktree| {
-                    format!(
+                let worktree = worktree.upgrade(&cx)?.read(cx);
+                if worktree.is_visible() {
+                    Some(format!(
                         "project-path-online:{}",
-                        worktree
-                            .read(cx)
-                            .as_local()
-                            .unwrap()
-                            .abs_path()
-                            .to_string_lossy()
-                    )
-                })
+                        worktree.as_local().unwrap().abs_path().to_string_lossy()
+                    ))
+                } else {
+                    None
+                }
             })
             .collect::<Vec<_>>()
     }

crates/settings/src/settings.rs 🔗

@@ -19,6 +19,7 @@ pub use keymap_file::{keymap_file_json_schema, KeymapFileContent};
 
 #[derive(Clone)]
 pub struct Settings {
+    pub projects_online_by_default: bool,
     pub buffer_font_family: FamilyId,
     pub buffer_font_size: f32,
     pub default_buffer_font_size: f32,
@@ -49,6 +50,8 @@ pub enum SoftWrap {
 
 #[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
 pub struct SettingsFileContent {
+    #[serde(default)]
+    pub projects_online_by_default: Option<bool>,
     #[serde(default)]
     pub buffer_font_family: Option<String>,
     #[serde(default)]
@@ -81,6 +84,7 @@ impl Settings {
             preferred_line_length: 80,
             language_overrides: Default::default(),
             format_on_save: true,
+            projects_online_by_default: true,
             theme,
         })
     }
@@ -135,6 +139,7 @@ impl Settings {
             preferred_line_length: 80,
             format_on_save: true,
             language_overrides: Default::default(),
+            projects_online_by_default: true,
             theme: gpui::fonts::with_font_cache(cx.font_cache().clone(), || Default::default()),
         }
     }
@@ -164,6 +169,10 @@ impl Settings {
             }
         }
 
+        merge(
+            &mut self.projects_online_by_default,
+            data.projects_online_by_default,
+        );
         merge(&mut self.buffer_font_size, data.buffer_font_size);
         merge(&mut self.default_buffer_font_size, data.buffer_font_size);
         merge(&mut self.vim_mode, data.vim_mode);