diff --git a/crates/pdfs-gui/src/app/pages/drive_galleries.rs b/crates/pdfs-gui/src/app/pages/drive_galleries.rs index 7b02e6b..bb7c8e0 100644 --- a/crates/pdfs-gui/src/app/pages/drive_galleries.rs +++ b/crates/pdfs-gui/src/app/pages/drive_galleries.rs @@ -5,6 +5,28 @@ use pdfs_core::control::{GalleryAction, GalleryAlbum, GalleryFolder, GalleryPlac const PAGE: usize = 150; /// Decoded thumbnails kept, the least recently shown dropped first. const THUMB_CACHE: usize = 256; +/// What a new content width asks of the rows. +#[derive(Debug, PartialEq, Eq)] +enum Reflow { + /// Nothing changed, or there is no width yet. + None, + /// The first real width: lay the rows out now, nothing is on screen yet. + Now, + /// A resize: wait for it to settle, since the rows are rebuilt from + /// scratch and a drag resizes on every frame. + Debounced, +} + +fn reflow_for(previous: i32, width: i32) -> Reflow { + if width <= 0 || width == previous { + Reflow::None + } else if previous <= 0 { + Reflow::Now + } else { + Reflow::Debounced + } +} + type ThumbnailKey = (u64, bool, String); type ThumbnailTarget = (glib::WeakRef, glib::WeakRef); @@ -45,6 +67,8 @@ pub(crate) struct Browser { albums: RefCell>, items: RefCell>, width: Cell, + /// The pending re-flow after a resize, replaced by each new width. + reflow: RefCell>, generation: Cell, total: Cell, loading: Cell, @@ -752,6 +776,7 @@ pub(crate) fn wire( albums: RefCell::new(Vec::new()), items: RefCell::new(Vec::new()), width: Cell::new(0), + reflow: RefCell::new(None), generation: Cell::new(0), total: Cell::new(0), loading: Cell::new(false), @@ -1007,8 +1032,27 @@ pub(crate) fn wire( return glib::ControlFlow::Break; }; let width = scroll.width(); - if width > 0 && browser.width.replace(width) != width { - browser.rebuild(); + match reflow_for(browser.width.get(), width) { + Reflow::None => {} + Reflow::Now => { + browser.width.set(width); + browser.rebuild(); + } + Reflow::Debounced => { + browser.width.set(width); + if let Some(source) = browser.reflow.borrow_mut().take() { + source.remove(); + } + let weak = Rc::downgrade(&browser); + let source = + glib::timeout_add_local_once(super::photos::RELAYOUT_DEBOUNCE, move || { + if let Some(browser) = weak.upgrade() { + browser.reflow.borrow_mut().take(); + browser.rebuild(); + } + }); + *browser.reflow.borrow_mut() = Some(source); + } } glib::ControlFlow::Continue } @@ -1197,3 +1241,18 @@ impl Browser { }); } } + +#[cfg(test)] +mod tests { + use super::{Reflow, reflow_for}; + + #[test] + fn the_first_width_lays_out_at_once_and_a_resize_waits() { + assert_eq!(reflow_for(0, 0), Reflow::None); + assert_eq!(reflow_for(0, 800), Reflow::Now); + assert_eq!(reflow_for(800, 800), Reflow::None); + assert_eq!(reflow_for(800, 760), Reflow::Debounced); + // A collapsed widget reports no width; keep the last layout. + assert_eq!(reflow_for(800, 0), Reflow::None); + } +}