torin/
custom_measurer.rs

1use std::{
2    any::Any,
3    rc::Rc,
4};
5
6use crate::{
7    geometry::Size2D,
8    node::Node,
9    prelude::Area,
10    tree_adapter::NodeKey,
11};
12
13pub trait LayoutMeasurer<Key: NodeKey> {
14    fn measure(
15        &mut self,
16        node_id: Key,
17        node: &Node,
18        size: &Size2D,
19    ) -> Option<(Size2D, Rc<dyn Any>)>;
20
21    fn should_hook_measurement(&mut self, node_id: Key) -> bool;
22
23    fn should_measure_inner_children(&mut self, node_id: Key) -> bool;
24
25    fn notify_layout_references(
26        &mut self,
27        _node_id: Key,
28        _area: Area,
29        _visible_area: Area,
30        _inner_sizes: Size2D,
31    ) {
32    }
33}
34
35// No-op measurer, use it when you don't need one.
36pub struct NoopMeasurer;
37
38impl LayoutMeasurer<usize> for NoopMeasurer {
39    fn measure(
40        &mut self,
41        _node_id: usize,
42        _node: &Node,
43        _size: &Size2D,
44    ) -> Option<(Size2D, Rc<dyn Any>)> {
45        None
46    }
47
48    fn should_hook_measurement(&mut self, _node_id: usize) -> bool {
49        false
50    }
51
52    fn should_measure_inner_children(&mut self, _node_id: usize) -> bool {
53        false
54    }
55}