diff --git a/packages/frontend/desktop/src/sidebar/tests.rs b/packages/frontend/desktop/src/sidebar/tests.rs index 5654e8023f..484f7421e8 100644 --- a/packages/frontend/desktop/src/sidebar/tests.rs +++ b/packages/frontend/desktop/src/sidebar/tests.rs @@ -340,6 +340,75 @@ fn web_sidebar_starts_at_the_top_and_has_one_collapse_control() { app.shell(|shell, _, _| assert!(shell.state.sidebar.open)); } +#[test] +fn favorite_folder_documents_expand_linked_rows_independently() { + use affine_store::workspace_db::{FavoriteKind, FavoriteRef}; + + let app = TestShell::new(); + app.shell(|view, _, cx| { + for id in ["f-desktop", "f-eng"] { + assert!( + view.state + .store + .toggle_favorite_target(&FavoriteRef::new(FavoriteKind::Folder, id)) + ); + } + view.state.sidebar.set_show_linked_doc_in_sidebar(true); + cx.notify(); + }); + app.frame(); + + let parent = "desktop.sidebar.fav_folder.f-eng"; + let nested = "desktop.sidebar.fav_folder.f-eng.f-desktop"; + let top = "desktop.sidebar.fav_folder.f-desktop"; + let next = "desktop.sidebar.fav.doc-arch"; + let bounds = |id: &str| app.shell(|_, window, _| window.frame_snapshot().elements[id].bounds); + let click_doc_arrow = |folder: &str, depth: f32| { + let row = bounds(folder); + app.window.simulate_click(point( + px(row.x + 16.0 + depth * 20.0), + px(row.y + 32.0 + row.height / 2.0), + )); + app.frame(); + }; + + app.click(parent); + app.click(nested); + click_doc_arrow(nested, 2.0); + app.click(top); + assert_eq!(bounds(next).y - bounds(top).y, 96.0); + + let assert_linked_rows = |nested_open: bool, top_open: bool| { + app.shell(|_, window, _| { + let frame = window.frame_snapshot(); + for (folder_path, expanded) in + [("f-eng/f-desktop", nested_open), ("f-desktop", top_open)] + { + let path = format!("fav-fdoc/{folder_path}:link-arch-1/doc-arch"); + assert!( + frame + .elements + .contains_key(&format!("desktop.sidebar.fav_doc.{path}")) + ); + for doc in ["doc-sync", "doc-board"] { + let id = format!("desktop.sidebar.linked_doc.{path}/{doc}"); + assert_eq!(frame.elements.contains_key(&id), expanded, "{id}"); + } + } + }); + }; + assert_linked_rows(true, false); + for (folder, depth, nested_open, top_open) in [ + (top, 1.0, true, true), + (nested, 2.0, false, true), + (nested, 2.0, true, true), + (top, 1.0, true, false), + ] { + click_doc_arrow(folder, depth); + assert_linked_rows(nested_open, top_open); + } +} + #[test] fn sidebar_menus_anchor_below_their_triggers_and_dismiss_in_each_layout() { for (width, web_chrome) in [(1280, false), (1280, true), (390, true)] { diff --git a/packages/frontend/desktop/src/sidebar_tree/favorites.rs b/packages/frontend/desktop/src/sidebar_tree/favorites.rs index abad03cc88..f94d0c18da 100644 --- a/packages/frontend/desktop/src/sidebar_tree/favorites.rs +++ b/packages/frontend/desktop/src/sidebar_tree/favorites.rs @@ -60,7 +60,10 @@ impl Builder<'_> { match child { FolderChild::Folder(child) => self.push_folder(child, ancestors), FolderChild::Doc { link, doc } => { - let root = DocRoot::InFavorites(Box::new(DocRoot::Folder(link.clone()))); + let root = DocRoot::FavoriteFolder { + ancestors: ancestors.clone(), + link: link.clone(), + }; self.push_doc(root, doc, Some(id), depth + 1); } } @@ -229,15 +232,21 @@ mod tests { .insert(FavoriteExpansion::Group(collection_ref.clone())); assert_eq!(sidebar.expanded_folders, organize); let doc = |root: DocRoot, id: &str, folder: Option<&str>, depth| SidebarRow::FavoriteDoc { - path: DocPath::new(DocRoot::InFavorites(Box::new(root)), id), + path: DocPath::new(root, id), folder_id: folder.map(str::to_owned), depth, }; + let collection_root = + DocRoot::InFavorites(Box::new(DocRoot::Collection(collection.clone()))); let members: Vec = store .docs_for_collection(&collection) .iter() - .map(|meta| doc(DocRoot::Collection(collection.clone()), &meta.id, None, 1)) + .map(|meta| doc(collection_root.clone(), &meta.id, None, 1)) .collect(); + let folder_root = |link: &str| DocRoot::FavoriteFolder { + ancestors: vec!["f-eng".into(), "f-desktop".into()], + link: link.into(), + }; let mut expected = vec![ SidebarRow::FavoriteGroup(tag_ref.clone()), SidebarRow::FavoriteFolder { @@ -250,18 +259,8 @@ mod tests { ancestors: vec!["f-eng".into()], depth: 1, }, - doc( - DocRoot::Folder("link-arch-1".into()), - "doc-arch", - Some("f-desktop"), - 2, - ), - doc( - DocRoot::Folder("link-sync-1".into()), - "doc-sync", - Some("f-desktop"), - 2, - ), + doc(folder_root("link-arch-1"), "doc-arch", Some("f-desktop"), 2), + doc(folder_root("link-sync-1"), "doc-sync", Some("f-desktop"), 2), SidebarRow::FavoriteGroup(collection_ref.clone()), ]; expected.extend(members); diff --git a/packages/frontend/desktop/src/sidebar_tree/linked.rs b/packages/frontend/desktop/src/sidebar_tree/linked.rs index c171dc2b5d..0a570849e2 100644 --- a/packages/frontend/desktop/src/sidebar_tree/linked.rs +++ b/packages/frontend/desktop/src/sidebar_tree/linked.rs @@ -13,10 +13,28 @@ pub(crate) enum DocRoot { Folder(String), Collection(String), Tag(String), - /// A placement below a folder, collection, or tag of the Favorites section. + FavoriteFolder { + ancestors: Vec, + link: String, + }, + /// A placement below a collection or tag of the Favorites section. InFavorites(Box), } +fn write_path_segment(f: &mut fmt::Formatter<'_>, id: &str) -> fmt::Result { + let mut start = 0; + for (index, separator) in id.match_indices(['%', '/', ':']) { + f.write_str(&id[start..index])?; + f.write_str(match separator { + "%" => "%25", + "/" => "%2F", + _ => "%3A", + })?; + start = index + 1; + } + f.write_str(&id[start..]) +} + impl fmt::Display for DocRoot { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -24,6 +42,15 @@ impl fmt::Display for DocRoot { Self::Folder(link) => write!(f, "fdoc:{link}"), Self::Collection(id) => write!(f, "col:{id}"), Self::Tag(id) => write!(f, "tag:{id}"), + Self::FavoriteFolder { ancestors, link } => { + f.write_str("fav-fdoc")?; + for id in ancestors { + f.write_str("/")?; + write_path_segment(f, id)?; + } + f.write_str(":")?; + write_path_segment(f, link) + } Self::InFavorites(root) => write!(f, "fav-{root}"), } } @@ -72,12 +99,46 @@ impl fmt::Display for DocPath { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.root)?; for doc in &self.docs { - write!(f, "/{doc}")?; + f.write_str("/")?; + if matches!(self.root, DocRoot::FavoriteFolder { .. }) { + write_path_segment(f, doc)?; + } else { + f.write_str(doc)?; + } } Ok(()) } } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn favorite_folder_doc_ids_keep_path_segments_distinct() { + let path = |ancestors: &[&str], link: &str, doc: &str| { + DocPath::new( + DocRoot::FavoriteFolder { + ancestors: ancestors.iter().map(|id| (*id).into()).collect(), + link: link.into(), + }, + doc, + ) + }; + let paths = [ + path(&["a/b"], "c", "d"), + path(&["a%2Fb"], "c", "d"), + path(&["a", "b"], "c", "d"), + path(&["a"], "b:c", "d"), + path(&["a:b"], "c", "d"), + path(&["a"], "b", "c/d"), + path(&["a"], "b", "c").child("d"), + ]; + let ids: std::collections::BTreeSet<_> = paths.iter().map(ToString::to_string).collect(); + assert_eq!(ids.len(), paths.len()); + } +} + /// Whether the row at `path` shows the expand control. pub(crate) fn can_expand(sidebar: &SidebarState, path: &DocPath) -> bool { sidebar.show_linked_doc_in_sidebar() && !path.repeats_ancestor()