From 16988ccc2aff896ec1d4b76e9bbb79182545fb6e Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 27 Apr 2025 17:28:52 +0200 Subject: [PATCH] Only reset SearchPage instances on library change Fixes #21 --- src/window.rs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/window.rs b/src/window.rs index cf768f0..ce2f4ab 100644 --- a/src/window.rs +++ b/src/window.rs @@ -299,13 +299,13 @@ impl Window { #[weak(rename_to = obj)] self, move |_| { - obj.reset_view(); + obj.initial_view(); } )); navigation.replace(&[empty_page.into()]); } else { - self.reset_view(); + self.initial_view(); } Ok(()) @@ -323,8 +323,9 @@ impl Window { Ok(()) } - fn reset_view(&self) { + fn initial_view(&self) { let navigation = self.imp().navigation_view.get(); + navigation.replace(&[SearchPage::new( &self.imp().toast_overlay, &navigation, @@ -334,4 +335,36 @@ impl Window { ) .into()]); } + + fn reset_view(&self) { + let navigation = self.imp().navigation_view.get(); + + // Get all pages that are not instances of SearchPage. + let mut navigation_stack = navigation + .navigation_stack() + .iter::() + .filter_map(|page| match page { + Ok(page) => match page.downcast_ref::() { + Some(_) => None, + None => Some(page), + }, + Err(_) => None, + }) + .collect::>(); + + navigation_stack.insert( + 0, + SearchPage::new( + &self.imp().toast_overlay, + &navigation, + self.imp().library.borrow().as_ref().unwrap(), + &self.imp().player, + LibraryQuery::default(), + ) + .into(), + ); + + // Readd all pages except for instances of SearchPage and add a new SearchPage as the root. + navigation.replace(&navigation_stack); + } }