Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): remember previous recordings folders

Aditya Garud7b307c449a22
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index 19b3740a4..6eab8ad8f 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -3916,8 +3916,10 @@ impl SettingsWindow {
false,
cx,
|this, _window, cx| {
- this.settings.recordings_path = None;
- this.write("recordingsPath", Value::Null, cx);
+ if store::set_recordings_path(None) {
+ this.settings = GeneralSettings::load();
+ cx.notify();
+ }
},
));
}
@@ -3945,8 +3947,10 @@ impl SettingsWindow {
};
this.update(cx, |this, cx| {
let path = path.to_string_lossy().to_string();
- this.settings.recordings_path = Some(path.clone());
- this.write("recordingsPath", Value::String(path), cx);
+ if store::set_recordings_path(Some(&path)) {
+ this.settings = GeneralSettings::load();
+ cx.notify();
+ }
})
.ok();
})
diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs
index 01fa3a508..e6c421720 100644
--- a/apps/desktop-gpui/src/store.rs
+++ b/apps/desktop-gpui/src/store.rs
@@ -1052,6 +1052,40 @@ pub struct GeneralSettings {
/// The section names, so the write calls read as the store keys they are.
pub const GENERAL_SETTINGS: &str = "general_settings";
pub const RECORDING_START_SAFETY: &str = "recording_start_safety";
+
+/// Changes the recordings folder and remembers the old one, like Tauri's
+/// `remember_previous_recordings_path`, so the library keeps listing projects
+/// left in earlier folders.
+pub fn set_recordings_path(path: Option<&str>) -> bool {
+ const MAX_PREVIOUS_PATHS: usize = 20;
+
+ update_store_section(GENERAL_SETTINGS, |settings| {
+ let mut previous_paths = strings_at(settings, "previousRecordingsPaths");
+ if let Some(path) = path {
+ previous_paths.retain(|previous| previous != path);
+ }
+ if let Some(previous) = opt_string_at(settings, "recordingsPath")
+ && Some(previous.as_str()) != path
+ && !previous_paths.contains(&previous)
+ {
+ previous_paths.push(previous);
+ let len = previous_paths.len();
+ if len > MAX_PREVIOUS_PATHS {
+ previous_paths.drain(..len - MAX_PREVIOUS_PATHS);
+ }
+ }
+ settings.insert(
+ "recordingsPath".into(),
+ path.map_or(Value::Null, Value::from),
+ );
+ settings.insert(
+ "previousRecordingsPaths".into(),
+ Value::from(previous_paths),
+ );
+ true
+ })
+}
+
/// `audioEnhancementStore` (`apps/desktop/src/store.ts`): whether new Studio
/// recordings start with Studio Sound on. Defaults on; the editor's Audio tab
/// and the Recording settings card both write it.
@@ -2379,6 +2413,127 @@ mod tests {
assert_eq!(store.read()["auth"]["user_id"], "a");
}
+ #[test]
+ fn recordings_folder_changes_remember_previous_locations() {
+ let store = TempStore::new(
+ "recordings-folder-history",
+ Some(
+ r#"{"general_settings":{"recordingsPath":"old","previousRecordingsPaths":["older","new"],"maxFps":30,"future":true},"auth":{"user_id":"keep"}}"#,
+ ),
+ );
+
+ assert!(set_recordings_path(Some("new")));
+ let reloaded = store_section(GENERAL_SETTINGS);
+ assert_eq!(reloaded["recordingsPath"], "new");
+ assert_eq!(
+ strings_at(&reloaded, "previousRecordingsPaths"),
+ ["older", "old"]
+ );
+
+ assert!(set_recordings_path(Some("new")));
+ assert_eq!(store_section(GENERAL_SETTINGS), reloaded);
+
+ assert!(set_recordings_path(None));
+ let reloaded = store_section(GENERAL_SETTINGS);
+ assert_eq!(reloaded["recordingsPath"], Value::Null);
+ assert_eq!(
+ strings_at(&reloaded, "previousRecordingsPaths"),
+ ["older", "old", "new"]
+ );
+
+ assert!(set_recordings_path(Some("old")));
+ let reloaded = store_section(GENERAL_SETTINGS);
+ assert_eq!(reloaded["recordingsPath"], "old");
+ assert_eq!(
+ strings_at(&reloaded, "previousRecordingsPaths"),
+ ["older", "new"]
+ );
+ assert_eq!(reloaded["maxFps"], 30);
+ assert_eq!(store.read()[GENERAL_SETTINGS]["future"], true);
+ assert_eq!(store.read()["auth"]["user_id"], "keep");
+ }
+
+ #[test]
+ fn recordings_folder_changes_limit_history_without_duplicates() {
+ let _store = TempStore::new("recordings-folder-history-limit", None);
+ for index in 0..25 {
+ assert!(set_recordings_path(Some(&format!("folder-{index}"))));
+ }
+ let expected = (4..24)
+ .map(|index| format!("folder-{index}"))
+ .collect::<Vec<_>>();
+ assert_eq!(
+ strings_at(&store_section(GENERAL_SETTINGS), "previousRecordingsPaths"),
+ expected
+ );
+
+ assert!(set_recordings_path(Some("folder-23")));
+ assert!(set_recordings_path(Some("folder-24")));
+ assert_eq!(
+ strings_at(&store_section(GENERAL_SETTINGS), "previousRecordingsPaths"),
+ expected
+ );
+ }
+
+ #[test]
+ fn recordings_folder_changes_merge_concurrent_updates() {
+ let store = TempStore::new(
+ "recordings-folder-concurrent",
+ Some(r#"{"general_settings":{"recordingsPath":"original","maxFps":30}}"#),
+ );
+ let barrier = Arc::new(std::sync::Barrier::new(8));
+ let workers = (0..8)
+ .map(|index| {
+ let path = store.path.clone();
+ let barrier = barrier.clone();
+ std::thread::spawn(move || {
+ set_test_tauri_store_path(Some(path));
+ barrier.wait();
+ let saved = set_recordings_path(Some(&format!("folder-{index}")));
+ set_test_tauri_store_path(None);
+ saved
+ })
+ })
+ .collect::<Vec<_>>();
+ for worker in workers {
+ assert!(worker.join().unwrap());
+ }
+ let reloaded = store_section(GENERAL_SETTINGS);
+ let mut paths = strings_at(&reloaded, "previousRecordingsPaths");
+ assert_eq!(paths.remove(0), "original");
+ paths.push(opt_string_at(&reloaded, "recordingsPath").unwrap());
+ paths.sort();
+ assert_eq!(
+ paths,
+ (0..8)
+ .map(|index| format!("folder-{index}"))
+ .collect::<Vec<_>>()
+ );
+ assert_eq!(reloaded["maxFps"], 30);
+ }
+
+ #[test]
+ fn recordings_folder_changes_preserve_preferences_when_saving_fails() {
+ let contents = r#"{"general_settings":{"recordingsPath":"old","maxFps":30}}"#;
+ let store = TempStore::new("recordings-folder-write-failure", Some(contents));
+ let staged = store.path.with_extension("gpui-tmp");
+ std::fs::create_dir(&staged).unwrap();
+ let saved = set_recordings_path(Some("new"));
+ std::fs::remove_dir(staged).unwrap();
+
+ assert!(!saved);
+ assert_eq!(std::fs::read_to_string(&store.path).unwrap(), contents);
+ assert_eq!(store_section(GENERAL_SETTINGS)["recordingsPath"], "old");
+ }
+
+ #[test]
+ fn recordings_folder_changes_preserve_unreadable_stores() {
+ let store = TempStore::new("recordings-folder-corrupt", Some("not JSON"));
+ assert!(!set_recordings_path(Some("new")));
+ assert!(!set_recordings_path(None));
+ assert_eq!(std::fs::read_to_string(&store.path).unwrap(), "not JSON");
+ }
+
#[test]
fn notification_sounds_follow_tauri_settings_without_writing() {
for (contents, enabled) in [