use repose_core::{Modifier, Rect, ViewId, ViewKind};
use slotmap::new_key_type;
use smallvec::SmallVec;
new_key_type! {
pub struct NodeId;
}
#[derive(Clone)]
pub struct TreeNode {
pub id: NodeId,
pub view_id: ViewId,
pub kind: ViewKind,
pub modifier: Modifier,
pub children: SmallVec<[NodeId; 4]>,
pub parent: Option<NodeId>,
pub content_hash: u64,
pub subtree_hash: u64,
pub layout_cache: Option<LayoutCache>,
pub generation: u64,
pub user_key: Option<u64>,
pub depth: u32,
}
impl TreeNode {
pub fn new(
id: NodeId,
view_id: ViewId,
kind: ViewKind,
modifier: Modifier,
generation: u64,
) -> Self {
Self {
id,
view_id,
kind,
modifier,
children: SmallVec::new(),
parent: None,
content_hash: 0,
subtree_hash: 0,
layout_cache: None,
generation,
user_key: None,
depth: 0,
}
}
pub fn has_valid_layout(&self) -> bool {
self.layout_cache.is_some()
}
pub fn invalidate_layout(&mut self) {
self.layout_cache = None;
}
pub fn cached_rect(&self) -> Option<Rect> {
self.layout_cache.as_ref().map(|c| c.rect)
}
pub fn set_layout(&mut self, rect: Rect) {
self.layout_cache = Some(LayoutCache {
rect,
screen_rect: Rect::default(),
constraints: LayoutConstraints::default(),
generation: self.generation,
});
}
}
#[derive(Clone, Debug)]
pub struct LayoutCache {
pub rect: Rect,
pub screen_rect: Rect,
pub constraints: LayoutConstraints,
pub generation: u64,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LayoutConstraints {
pub min_width: f32,
pub max_width: f32,
pub min_height: f32,
pub max_height: f32,
}
impl Default for LayoutConstraints {
fn default() -> Self {
Self {
min_width: 0.0,
max_width: f32::INFINITY,
min_height: 0.0,
max_height: f32::INFINITY,
}
}
}
impl LayoutConstraints {
pub fn fixed(width: f32, height: f32) -> Self {
Self {
min_width: width,
max_width: width,
min_height: height,
max_height: height,
}
}
pub fn tight(size: (f32, f32)) -> Self {
Self::fixed(size.0, size.1)
}
}
#[derive(Clone, Debug, Default)]
pub struct TreeStats {
pub total_nodes: usize,
pub dirty_nodes: usize,
pub reconciled_nodes: usize,
pub skipped_nodes: usize,
pub created_nodes: usize,
pub removed_nodes: usize,
pub layout_cache_hits: usize,
pub layout_cache_misses: usize,
}