mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-21 02:23:48 +01:00
perf(gui): debounce the Drive gallery re-flow on resize
The Drive gallery rebuilt its whole row model on every frame the width changed, so dragging the window edge rebuilt it dozens of times a second. Lay out the first width at once as before, and wait for a resize to settle (80 ms, like the timeline) before rebuilding.
This commit is contained in:
1 parent
960c6afe2d
commit
890adbc189
1 file changed
+61
-2
@@ -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<gtk4::Picture>, glib::WeakRef<gtk4::Image>);
|
||||
|
||||
@@ -45,6 +67,8 @@ pub(crate) struct Browser {
|
||||
albums: RefCell<Vec<GalleryAlbum>>,
|
||||
items: RefCell<Vec<PhotoItem>>,
|
||||
width: Cell<i32>,
|
||||
/// The pending re-flow after a resize, replaced by each new width.
|
||||
reflow: RefCell<Option<glib::SourceId>>,
|
||||
generation: Cell<u64>,
|
||||
total: Cell<usize>,
|
||||
loading: Cell<bool>,
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user