Fix scrollview cache

This commit is contained in:
Alexandre Bury 2018-06-22 14:50:38 -07:00
parent 3a9ee0df41
commit 9a21273167

View File

@ -311,6 +311,20 @@ where
/// ///
/// Returns `(inner_size, size)` /// Returns `(inner_size, size)`
fn sizes(&mut self, constraint: Vec2) -> (Vec2, Vec2) { fn sizes(&mut self, constraint: Vec2) -> (Vec2, Vec2) {
// First: try the cache
if self
.size_cache
.map(|cache| cache.zip_map(constraint, SizeCache::accept).both())
.unwrap_or(false)
{
// The new constraint shouldn't change much,
// so we can re-use previous values
return (
self.inner_size,
self.size_cache.unwrap().map(|c| c.value),
);
}
// Attempt 1: try without scrollbars // Attempt 1: try without scrollbars
let (inner_size, size, scrollable) = let (inner_size, size, scrollable) =
self.sizes_when_scrolling(constraint, XY::new(false, false)); self.sizes_when_scrolling(constraint, XY::new(false, false));
@ -353,19 +367,6 @@ where
steps * self.offset / (self.inner_size + (1, 1) - available) steps * self.offset / (self.inner_size + (1, 1) - available)
} }
fn compute_inner_size(&mut self, constraint: Vec2) {
if self
.size_cache
.map(|cache| cache.zip_map(constraint, SizeCache::accept).both())
.unwrap_or(false)
{
return;
}
let (inner_size, _) = self.sizes(constraint);
self.inner_size = inner_size;
self.size_cache = Some(SizeCache::build(inner_size, constraint));
}
/// Apply the scrolling strategy to the current scroll position. /// Apply the scrolling strategy to the current scroll position.
fn adjust_scroll(&mut self) { fn adjust_scroll(&mut self) {
match self.scroll_strategy { match self.scroll_strategy {
@ -582,10 +583,14 @@ where
} }
fn layout(&mut self, size: Vec2) { fn layout(&mut self, size: Vec2) {
// Size is final now // Size is final now, negociations are over.
self.last_size = size; self.last_size = size;
self.compute_inner_size(size); // This is what we'd like
let (inner_size, self_size) = self.sizes(size);
self.inner_size = inner_size;
self.size_cache = Some(SizeCache::build(self_size, size));
self.inner.layout(self.inner_size); self.inner.layout(self.inner_size);