pub struct CoherenceCache { /* private fields */ }Expand description
Stores previous layout allocations for temporal coherence.
When terminal size changes, the layout solver re-computes positions from scratch. Without coherence, rounding tie-breaks can cause widgets to “jump” between frames even when the total size change is small.
The CoherenceCache feeds previous allocations to
round_layout_stable so that tie-breaking
favours keeping elements where they were.
§Usage
use ftui_layout::{CoherenceCache, CoherenceId, round_layout_stable, Constraint, Direction};
let mut coherence = CoherenceCache::new(64);
let id = CoherenceId::new(&constraints, Direction::Horizontal);
let prev = coherence.get(&id);
let alloc = round_layout_stable(&targets, total, prev);
coherence.store(id, alloc.clone());§Eviction
Entries are evicted on a least-recently-stored basis when the cache reaches capacity. The cache does not grow unboundedly.
Implementations§
Source§impl CoherenceCache
impl CoherenceCache
Sourcepub fn new(max_entries: usize) -> Self
pub fn new(max_entries: usize) -> Self
Create a new coherence cache with the specified capacity.
Sourcepub fn get(&self, id: &CoherenceId) -> Option<Vec<u16>>
pub fn get(&self, id: &CoherenceId) -> Option<Vec<u16>>
Retrieve the previous allocation for a layout, if available.
Returns Some(allocation) suitable for passing directly to
round_layout_stable.
Sourcepub fn store(&mut self, id: CoherenceId, allocation: Vec<u16>)
pub fn store(&mut self, id: CoherenceId, allocation: Vec<u16>)
Store a layout allocation for future coherence lookups.
If the cache is at capacity, evicts the oldest entry.
Sourcepub fn displacement(&self, id: &CoherenceId, new_alloc: &[u16]) -> (u64, u32)
pub fn displacement(&self, id: &CoherenceId, new_alloc: &[u16]) -> (u64, u32)
Compute displacement between a new allocation and the previously stored one.
Returns (sum_displacement, max_displacement) in cells.
If no previous allocation exists for the ID, returns (0, 0).