diff --git a/CHANGELOG.md b/CHANGELOG.md index a9ded47..cfbd79a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ Releases up to 0.3.5 are described on the [GitHub releases page](https://github. ## [Unreleased] +### Fixed +- **Folders open the way they were left.** The sidebar remembers which folders are collapsed in each vault, in the settings file, instead of opening every folder expanded at each start. Renaming a folder keeps it collapsed; folders deleted since are forgotten. + ## [0.5.2] - 2026-11-11 ### Fixed diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 94301ec..ebc993f 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -262,6 +262,45 @@ pub async fn forget_vault( .await } +/// Remembers which sidebar folders are collapsed in a vault, so they open the +/// same way next time. Folders that no longer exist are dropped. +#[tauri::command] +pub async fn save_collapsed_folders( + vault_id: String, + folders: Vec, + app: AppHandle, +) -> Result<(), String> { + on_worker(app, move |state, _app| { + let root = state + .settings + .read() + .vaults + .iter() + .find(|vault| vault.id == vault_id) + .map(|vault| vault.path.clone()) + .ok_or("errors.vaultNotFound")?; + let folders = vault::collapsed_folders(&root, folders); + let mut settings = state.settings.write(); + let vault = settings + .vaults + .iter_mut() + .find(|vault| vault.id == vault_id) + .ok_or("errors.vaultNotFound")?; + if vault.collapsed_folders == folders { + return Ok(()); + } + let previous = std::mem::replace(&mut vault.collapsed_folders, folders); + if let Err(error) = settings.save() { + if let Some(vault) = settings.vaults.iter_mut().find(|v| v.id == vault_id) { + vault.collapsed_folders = previous; + } + return Err(error.to_string()); + } + Ok(()) + }) + .await +} + #[tauri::command] pub async fn list_notes(app: AppHandle) -> Result, String> { on_worker(app, move |state, _app| { diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 4639483..752ebc7 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -49,6 +49,10 @@ pub struct VaultConfig { pub credentials_locked: bool, #[serde(default)] pub peers: Vec, + /// Folders the sidebar shows collapsed, as vault-relative paths. Any other + /// folder opens expanded, as before this was remembered. + #[serde(default)] + pub collapsed_folders: Vec, /// Only set on copies sent to the interface: the folder no longer exists. #[serde( default, @@ -75,6 +79,7 @@ impl VaultConfig { secret_key: URL_SAFE_NO_PAD.encode(rand::rng().random::<[u8; 32]>()), pairing_token: new_pairing_token(), peers: Vec::new(), + collapsed_folders: Vec::new(), credentials_locked: false, missing: false, } @@ -897,6 +902,7 @@ mod tests { pairing_token: "".to_string(), credentials_locked: false, peers: Vec::new(), + collapsed_folders: Vec::new(), missing: false, }; assert!(vault.ensure_keys()); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 019fcdd..a83efa9 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -342,6 +342,7 @@ pub fn run() { commands::select_vault, commands::create_vault, commands::forget_vault, + commands::save_collapsed_folders, commands::list_notes, commands::read_note, commands::save_note, diff --git a/src-tauri/src/retention.rs b/src-tauri/src/retention.rs index cd646bd..4b9664b 100644 --- a/src-tauri/src/retention.rs +++ b/src-tauri/src/retention.rs @@ -66,17 +66,15 @@ pub(crate) fn cutoff(now: u64, days: Option) -> Option { days.map(|days| now.saturating_sub(days as u64 * 86_400_000)) } fn is_link(metadata: &fs::Metadata) -> bool { - if metadata.file_type().is_symlink() { - return true; - } + // Junctions are reparse points that Windows does not report as symlinks. #[cfg(windows)] - { + let reparse_point = { use std::os::windows::fs::MetadataExt; - if metadata.file_attributes() & 0x400 != 0 { - return true; - } - } - false + metadata.file_attributes() & 0x400 != 0 + }; + #[cfg(not(windows))] + let reparse_point = false; + metadata.file_type().is_symlink() || reparse_point } /// Never follow a link/junction or recursively delete beyond the chosen vault. pub(crate) fn owned_path(root: &Path, target: &Path, tree: bool) -> anyhow::Result { diff --git a/src-tauri/src/vault.rs b/src-tauri/src/vault.rs index f1f30a6..8a28cc8 100644 --- a/src-tauri/src/vault.rs +++ b/src-tauri/src/vault.rs @@ -271,6 +271,31 @@ pub fn rename_item(root: &Path, old_relative: &str, new_relative: &str) -> anyho ) } +/// Most folders the sidebar remembers as collapsed in one vault. +pub const MAX_COLLAPSED_FOLDERS: usize = 10_000; + +/// The collapsed folders worth remembering: valid vault-relative paths, once +/// each, without folders that were renamed or deleted since. While the vault +/// folder is unavailable nothing can be checked, so valid paths are all kept. +pub fn collapsed_folders(root: &Path, folders: Vec) -> Vec { + let check_disk = root.is_dir(); + folders + .into_iter() + .map(|folder| { + folder + .trim() + .replace('\\', "/") + .trim_matches('/') + .to_owned() + }) + .filter(|folder| !folder.is_empty()) + .filter(|folder| safe_join(root, folder).is_ok_and(|path| !check_disk || path.is_dir())) + .collect::>() + .into_iter() + .take(MAX_COLLAPSED_FOLDERS) + .collect() +} + /// Hashes of files by path, valid while size and modification time match. static FILE_HASHES: std::sync::OnceLock< parking_lot::Mutex>, @@ -438,6 +463,38 @@ mod tests { assert!(safe_join(root, ".hidden/file.md").is_err()); } + #[test] + fn collapsed_folders_keep_existing_folders_once() { + let dir = tempfile::tempdir().unwrap(); + fs::create_dir_all(dir.path().join("Projects/Old")).unwrap(); + fs::create_dir_all(dir.path().join("Journal")).unwrap(); + fs::write(dir.path().join("note.md"), "# Note").unwrap(); + let kept = collapsed_folders( + dir.path(), + [ + "Projects/Old", + "Journal", + "Journal/", + "Projects\\Old", + "Removed", + "note.md", + "../outside", + ".hidden", + "", + ] + .map(String::from) + .to_vec(), + ); + assert_eq!(kept, ["Journal", "Projects/Old"]); + + // An unplugged drive must not erase the remembered folders. + let missing = dir.path().join("unplugged"); + assert_eq!( + collapsed_folders(&missing, vec!["Journal".into(), "../outside".into()]), + ["Journal"] + ); + } + #[test] fn test_is_markdown() { assert!(is_markdown(Path::new("note.md"))); diff --git a/src/lib/api.ts b/src/lib/api.ts index ae9e36c..6b30881 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -73,6 +73,11 @@ export async function forgetVault(vaultId: string): Promise { + return await invoke('save_collapsed_folders', { vaultId, folders }); +} + export async function createVault(path: string, name?: string): Promise { return await invoke('create_vault', { pathStr: path, name }); } diff --git a/src/lib/components/Sidebar.svelte b/src/lib/components/Sidebar.svelte index 3936534..558f6c9 100644 --- a/src/lib/components/Sidebar.svelte +++ b/src/lib/components/Sidebar.svelte @@ -1,7 +1,7 @@