Skip to main content

astrelis_ui/
layout.rs

1//! Layout cache for storing computed layout information.
2
3use crate::tree::{LayoutRect, NodeId};
4use astrelis_core::alloc::HashMap;
5
6/// Cache for layout computations.
7#[derive(Debug, Clone, Default)]
8pub struct LayoutCache {
9    layouts: HashMap<NodeId, LayoutRect>,
10}
11
12impl LayoutCache {
13    /// Create a new layout cache.
14    pub fn new() -> Self {
15        Self {
16            layouts: HashMap::new(),
17        }
18    }
19
20    /// Store layout for a node.
21    pub fn set(&mut self, node_id: NodeId, layout: LayoutRect) {
22        self.layouts.insert(node_id, layout);
23    }
24
25    /// Get layout for a node.
26    pub fn get(&self, node_id: NodeId) -> Option<&LayoutRect> {
27        self.layouts.get(&node_id)
28    }
29
30    /// Check if a node has cached layout.
31    pub fn contains(&self, node_id: NodeId) -> bool {
32        self.layouts.contains_key(&node_id)
33    }
34
35    /// Clear all cached layouts.
36    pub fn clear(&mut self) {
37        self.layouts.clear();
38    }
39
40    /// Get the number of cached layouts.
41    pub fn len(&self) -> usize {
42        self.layouts.len()
43    }
44
45    /// Check if cache is empty.
46    pub fn is_empty(&self) -> bool {
47        self.layouts.is_empty()
48    }
49}