Skip to main content

torin/
custom_measurer.rs

1use std::{
2    any::Any,
3    rc::Rc,
4};
5
6use crate::{
7    geometry::Size2D,
8    node::Node,
9    prelude::{
10        Area,
11        Length,
12    },
13    torin::Torin,
14    tree_adapter::{
15        LayoutNode,
16        NodeKey,
17    },
18};
19
20/// Result of a [LayoutMeasurer::post_measure] step.
21pub struct PostMeasure<Key: NodeKey> {
22    /// Corrected content size, re-applied through the Node's min/max sizing.
23    pub content_size: Option<Size2D>,
24    /// `(x, y)` offsets to move each visible child's subtree by.
25    pub offsets: Vec<(Key, Length, Length)>,
26    /// Children whose subtree must be hidden from painting and events.
27    pub hidden_children: Vec<Key>,
28}
29
30impl<Key: NodeKey> Default for PostMeasure<Key> {
31    fn default() -> Self {
32        Self {
33            content_size: None,
34            offsets: Vec::new(),
35            hidden_children: Vec::new(),
36        }
37    }
38}
39
40pub trait LayoutMeasurer<Key: NodeKey> {
41    fn measure(
42        &mut self,
43        node_id: Key,
44        node: &Node,
45        size: &Size2D,
46    ) -> Option<(Size2D, Rc<dyn Any>)>;
47
48    fn should_hook_measurement(&mut self, node_id: Key) -> bool;
49
50    fn should_measure_inner_children(&mut self, node_id: Key) -> bool;
51
52    /// Whether [LayoutMeasurer::post_measure] should run for this node.
53    fn should_post_measure(&mut self, _node_id: Key) -> bool {
54        false
55    }
56
57    /// Runs after a node and its children are measured.
58    fn post_measure(
59        &mut self,
60        _node_id: Key,
61        _node_layout: &LayoutNode,
62        _children: &[Key],
63        _layout: &Torin<Key>,
64    ) -> PostMeasure<Key> {
65        PostMeasure::default()
66    }
67
68    fn notify_layout_references(
69        &mut self,
70        _node_id: Key,
71        _area: Area,
72        _visible_area: Area,
73        _inner_sizes: Size2D,
74    ) {
75    }
76}
77
78// No-op measurer, use it when you don't need one.
79pub struct NoopMeasurer;
80
81impl LayoutMeasurer<usize> for NoopMeasurer {
82    fn measure(
83        &mut self,
84        _node_id: usize,
85        _node: &Node,
86        _size: &Size2D,
87    ) -> Option<(Size2D, Rc<dyn Any>)> {
88        None
89    }
90
91    fn should_hook_measurement(&mut self, _node_id: usize) -> bool {
92        false
93    }
94
95    fn should_measure_inner_children(&mut self, _node_id: usize) -> bool {
96        false
97    }
98}