diff --git a/crates/snowbound/src/recycle.rs b/crates/snowbound/src/recycle.rs index 74589433cb2243cdcf117fcf097f9e45fe38f641..d4408d5d1f5c45db4c86101db4f24d0e38567897 100644 --- a/crates/snowbound/src/recycle.rs +++ b/crates/snowbound/src/recycle.rs @@ -165,16 +165,7 @@ impl State { } { return Ok(Loaded::Page(show, replica.page(show)?)); } - // A section without pages shows no tab: the notebook shows instead. - let path = library - .first_section() - .ok_or("The notebook has no sections")?; - if let Some((_, replica)) = held.iter().find(|(held, _)| *held == path) { - return crate::split::held_page(library, path, replica, None); - } - let section = library.open_async(&path, notify(proxy)).await?; - let (session, space, page) = read_session(section, library, path, None)?; - Ok(Loaded::Section(Box::new(session), space, page)) + notebook_shown(library, proxy, &held).await }); Ok(()) } @@ -213,6 +204,24 @@ impl State { } } +/// The notebook's first section, shown in place of a section of the bin left without pages, +/// which shows no tab; through its replica in `held` where a parked pane holds it. +pub(crate) async fn notebook_shown( + library: Arc, + proxy: crate::EventLoopProxy, + held: &[(String, Arc)], +) -> Result> { + let path = library + .first_section() + .ok_or("The notebook has no sections")?; + if let Some((_, replica)) = held.iter().find(|(held, _)| *held == path) { + return crate::split::held_page(library, path, replica, None); + } + let section = library.open_async(&path, notify(proxy)).await?; + let (session, space, page) = read_session(section, library, path, None)?; + Ok(Loaded::Section(Box::new(session), space, page)) +} + /// Does `request` to the bin's section `replica` holds, in `library`, as `author`: a page /// restored goes to its section as one op, keeping its identity, a copy under a new one, and /// what leaves the bin goes from it as another. A section a pane shows is reached through diff --git a/crates/snowbound/src/undo.rs b/crates/snowbound/src/undo.rs index 31137f0cc9d33a7c127796c6195527857c1a5ae4..66222b3c860f6eb66c3589b083c6a14dba41110e 100644 --- a/crates/snowbound/src/undo.rs +++ b/crates/snowbound/src/undo.rs @@ -349,7 +349,9 @@ struct Site { /// path. held: Vec<(String, Arc)>, author: String, - fresh: PageCreation, + /// The page a section left without pages gains; none in the recycle bin, which OneNote + /// leaves empty. + fresh: Option, } /// A change to pages done: the change taking it back, the page to show, and pages gone. @@ -375,7 +377,7 @@ impl Site { } /// The page shown once `gone` leave `listed`, preferring `prefer`, and the page `ops` gain - /// where none stays. + /// where none stays; in the recycle bin, which gains none, a page gone. fn after( &self, listed: &[(ExGuid, String, u32)], @@ -383,13 +385,13 @@ impl Site { prefer: Option, ops: &mut Vec, ) -> (ExGuid, Option) { - match neighbor(listed, gone, prefer) { - Some(show) => (show, None), - None => { - let space = self.fresh.space(); - ops.push(Op::Section(SectionOp::Create(self.fresh.clone()))); - (space, Some(space)) + match (neighbor(listed, gone, prefer), &self.fresh) { + (Some(show), _) => (show, None), + (None, Some(fresh)) => { + ops.push(Op::Section(SectionOp::Create(fresh.clone()))); + (fresh.space(), Some(fresh.space())) } + (None, None) => (gone[0], None), } } @@ -1137,18 +1139,20 @@ impl State { library: Arc::clone(&session.library), held, author: self.author.clone(), - fresh: self.dated_page(None, "")?, + fresh: (!self.in_recycle_bin()) + .then(|| self.dated_page(None, "")) + .transpose()?, }; let shown = self.pane.showing.space; let original = Arc::clone(&site.library); - let proxy = self.proxy.clone(); + let (proxy, held) = (self.proxy.clone(), self.held(&original.location)); self.undo.busy += 1; self.pane.switching = Some((None, web_time::Instant::now())); self.load_async(move || async move { let applied = site.change(change, shown).await; - let show = match &applied { - Ok(Some(applied)) => applied.show, - _ => shown, + let (show, emptied) = match &applied { + Ok(Some(applied)) => (applied.show, applied.gone.contains(&applied.show)), + _ => (shown, false), }; let failed = applied.as_ref().err().map(ToString::to_string); let (stale, applied) = (failed.is_none(), applied.ok().flatten()); @@ -1211,6 +1215,10 @@ impl State { if let Some(failed) = failed { return Err(failed.into()); } + if emptied { + return crate::recycle::notebook_shown(Arc::clone(&site.library), proxy, &held) + .await; + } Ok(Loaded::Page(show, site.replica.page(show)?)) }); Ok(()) @@ -1489,7 +1497,7 @@ mod tests { library: Arc::clone(&self.library), held: Vec::new(), author: AUTHOR.into(), - fresh: dated(), + fresh: Some(dated()), } } @@ -1752,6 +1760,22 @@ mod tests { assert_eq!(fixture.titles(), titled(&["First", "Third"])); } + /// The last page sent out of a section of the recycle bin leaves it without pages, as + /// OneNote leaves its Deleted Pages, where another section gains a dated one. + #[test] + fn the_last_page_sent_out_of_the_recycle_bin_leaves_none_behind() { + let fixture = Fixture::new("send-binned", &["Only"]); + let target = fixture.library.section_identity("B.one").unwrap(); + let page = fixture.space("Only"); + let mut site = fixture.site(); + site.fresh = None; + let sent = pollster::block_on(site.change(Change::Send { page, target }, page)) + .unwrap() + .unwrap(); + assert_eq!((sent.show, sent.gone), (page, vec![page])); + assert!(fixture.titles().is_empty()); + } + /// A page edited while it is on its way to another section stays, with the edit, beside /// the copy that arrived there, with nothing for Undo to take back; an edit to another page /// meanwhile lets it go. Both hold once each section is opened again.