blitz_dom/
debug.rs

1use parley::layout::PositionedLayoutItem;
2
3use crate::BaseDocument;
4
5impl BaseDocument {
6    pub fn print_taffy_tree(&self) {
7        taffy::print_tree(self, taffy::NodeId::from(0usize));
8    }
9
10    pub fn debug_log_node(&self, node_id: usize) {
11        let node = &self.nodes[node_id];
12
13        #[cfg(feature = "tracing")]
14        {
15            tracing::info!("Layout: {:?}", &node.final_layout);
16            tracing::info!("Style: {:?}", &node.style);
17        }
18
19        println!("\nNode {} {}", node.id, node.node_debug_str());
20
21        println!("Attrs:");
22
23        for attr in node.attrs().into_iter().flatten() {
24            println!("    {}: {}", attr.name.local, attr.value);
25        }
26
27        if node.flags.is_inline_root() {
28            let inline_layout = &node
29                .data
30                .downcast_element()
31                .unwrap()
32                .inline_layout_data
33                .as_ref()
34                .unwrap();
35
36            println!(
37                "Size: {}x{}",
38                inline_layout.layout.width(),
39                inline_layout.layout.height()
40            );
41            println!("Text content: {:?}", inline_layout.text);
42            println!("Inline Boxes:");
43            for ibox in inline_layout.layout.inline_boxes() {
44                print!("(id: {}) ", ibox.id);
45            }
46            println!();
47            println!("Lines:");
48            for (i, line) in inline_layout.layout.lines().enumerate() {
49                println!("Line {i}:");
50                for item in line.items() {
51                    print!("  ");
52                    match item {
53                        PositionedLayoutItem::GlyphRun(run) => {
54                            print!(
55                                "RUN (x: {}, w: {}) ",
56                                run.offset().round(),
57                                run.run().advance()
58                            )
59                        }
60                        PositionedLayoutItem::InlineBox(ibox) => print!(
61                            "BOX (id: {} x: {} y: {} w: {}, h: {})",
62                            ibox.id,
63                            ibox.x.round(),
64                            ibox.y.round(),
65                            ibox.width.round(),
66                            ibox.height.round()
67                        ),
68                    }
69                    println!();
70                }
71            }
72        }
73
74        let layout = &node.final_layout;
75        println!("Layout:");
76        println!(
77            "  x: {x} y: {y} w: {width} h: {height} content_w: {content_width} content_h: {content_height}",
78            x = layout.location.x,
79            y = layout.location.y,
80            width = layout.size.width,
81            height = layout.size.height,
82            content_width = layout.content_size.width,
83            content_height = layout.content_size.height,
84        );
85        println!(
86            "  border: l:{l} r:{r} t:{t} b:{b}",
87            l = layout.border.left,
88            r = layout.border.right,
89            t = layout.border.top,
90            b = layout.border.bottom,
91        );
92        println!(
93            "  padding: l:{l} r:{r} t:{t} b:{b}",
94            l = layout.padding.left,
95            r = layout.padding.right,
96            t = layout.padding.top,
97            b = layout.padding.bottom,
98        );
99        println!(
100            "  margin: l:{l} r:{r} t:{t} b:{b}",
101            l = layout.margin.left,
102            r = layout.margin.right,
103            t = layout.margin.top,
104            b = layout.margin.bottom,
105        );
106        println!("Parent: {:?}", node.parent);
107
108        let children: Vec<_> = node
109            .children
110            .iter()
111            .map(|id| &self.nodes[*id])
112            .map(|node| (node.id, node.order(), node.node_debug_str()))
113            .collect();
114        println!("Children: {children:?}");
115
116        println!("Layout Parent: {:?}", node.layout_parent.get());
117
118        let layout_children: Option<Vec<_>> = node.layout_children.borrow().as_ref().map(|lc| {
119            lc.iter()
120                .map(|id| &self.nodes[*id])
121                .map(|node| (node.id, node.order(), node.node_debug_str()))
122                .collect()
123        });
124        if let Some(layout_children) = layout_children {
125            println!("Layout Children: {layout_children:?}");
126        }
127
128        let paint_children: Option<Vec<_>> = node.paint_children.borrow().as_ref().map(|lc| {
129            lc.iter()
130                .map(|id| &self.nodes[*id])
131                .map(|node| (node.id, node.order(), node.node_debug_str()))
132                .collect()
133        });
134        if let Some(paint_children) = paint_children {
135            println!("Paint Children: {paint_children:?}");
136        }
137        // taffy::print_tree(&self.dom, node_id.into());
138    }
139}