| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | // cSpell: ignore fillcolor fontcolor graphviz tpdf |
| 5 | |
| 6 | use std::{ |
| 7 | collections::HashMap, |
| 8 | rc::{Rc, Weak}, |
| 9 | }; |
| 10 | |
| 11 | use i_slint_compiler::object_tree::{Component, Element, ElementRc}; |
| 12 | |
| 13 | #[derive (Clone)] |
| 14 | struct ElementInfo { |
| 15 | id: usize, |
| 16 | component_id: String, |
| 17 | node_definition: String, |
| 18 | } |
| 19 | |
| 20 | impl ElementInfo { |
| 21 | fn node_id(&self) -> String { |
| 22 | format!("n {}" , self.id) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | pub struct ElementMap(HashMap<*mut Element, String>); |
| 27 | |
| 28 | impl ElementMap { |
| 29 | #[allow (unused)] |
| 30 | pub fn node_name(&self, element: &ElementRc) -> Option<&String> { |
| 31 | self.0.get(&element.as_ptr()) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[derive (Default)] |
| 36 | struct State { |
| 37 | components: HashMap<*const Component, String>, |
| 38 | elements: HashMap<*mut Element, ElementInfo>, |
| 39 | current_node_number: usize, |
| 40 | mark_up: Option<ElementRc>, |
| 41 | } |
| 42 | |
| 43 | impl State { |
| 44 | fn generate_subgraph(&self, name: &str, elements: &[ElementInfo]) -> Vec<String> { |
| 45 | let mut result = Vec::new(); |
| 46 | |
| 47 | let spaces = if name == "root" { |
| 48 | " " |
| 49 | } else { |
| 50 | result.push(format!(" subgraph cluster_ {name} {{" )); |
| 51 | result.push(format!(" label = \"{name}\"" )); |
| 52 | result.push(" color = black" .to_string()); |
| 53 | " " |
| 54 | }; |
| 55 | |
| 56 | for ei in elements { |
| 57 | if ei.component_id == name { |
| 58 | result.push(format!(" {spaces}{}" , ei.node_definition)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if name != "root" { |
| 63 | result.push(" }" .to_string()); |
| 64 | } |
| 65 | |
| 66 | result |
| 67 | } |
| 68 | |
| 69 | fn generate(&self, connections: Vec<String>) -> String { |
| 70 | let mut result = vec!["digraph ElementTree {" .to_string()]; |
| 71 | |
| 72 | let mut values: Vec<_> = self.components.values().cloned().collect(); |
| 73 | values.sort_unstable(); |
| 74 | let mut elements: Vec<_> = self.elements.values().cloned().collect(); |
| 75 | elements.sort_unstable_by(|left, right| left.id.partial_cmp(&right.id).unwrap()); |
| 76 | |
| 77 | for cn in &values { |
| 78 | result.extend_from_slice(&self.generate_subgraph(cn, &elements)); |
| 79 | } |
| 80 | |
| 81 | result.extend_from_slice(&connections); |
| 82 | result.push("}" .to_string()); |
| 83 | |
| 84 | result.join(" \n" ) |
| 85 | } |
| 86 | |
| 87 | fn get_node_number(&mut self) -> usize { |
| 88 | let result = self.current_node_number; |
| 89 | self.current_node_number += 1; |
| 90 | result |
| 91 | } |
| 92 | |
| 93 | fn element_info(&mut self, element: &ElementRc) -> Option<ElementInfo> { |
| 94 | self.elements.get(&element.as_ptr()).cloned() |
| 95 | } |
| 96 | |
| 97 | fn register_component(&mut self, comp: &Rc<Component>, name: String) { |
| 98 | self.components.entry(Rc::as_ptr(comp)).or_insert(name); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// This function prints a tree of `Element`s starting below `element` in |
| 103 | /// graphviz format. You may optionally pass one element to highlight in the |
| 104 | /// output. |
| 105 | /// |
| 106 | /// Arrows moving from one component to the next are printed in blue. Elements |
| 107 | /// lowered from a Layout are printed as boxes, all other elements are round. |
| 108 | /// |
| 109 | /// The function returns a map from `ElementRc` to a String containing the |
| 110 | /// node name of that element in the dot file. This is nice as it allows to |
| 111 | /// follow some process as it traverses the tree without having to regenerate |
| 112 | /// the output all the time. |
| 113 | /// |
| 114 | /// You can cut and paste the output into |
| 115 | /// <https://dreampuf.github.io/GraphvizOnline/> |
| 116 | /// or into some text file and then run `dot -Tpdf /path/to/file > graph.pdf` |
| 117 | /// to generate a PDF file out of it. |
| 118 | #[allow (unused)] |
| 119 | pub fn as_dot(element: &ElementRc, mark_up: Option<ElementRc>) -> ElementMap { |
| 120 | let mut state: State = State { mark_up, ..State::default() }; |
| 121 | state.register_component( |
| 122 | &Weak::upgrade(&element.borrow().enclosing_component).unwrap(), |
| 123 | name:"root" .to_string(), |
| 124 | ); |
| 125 | |
| 126 | let (_, connections: Vec) = recurse_into_element(&mut state, element); |
| 127 | |
| 128 | i_slint_core::debug_log!( |
| 129 | "Statistics: {} elements in {} components" , |
| 130 | state.elements.len(), |
| 131 | state.components.len() |
| 132 | ); |
| 133 | i_slint_core::debug_log!(" {}" , state.generate(connections)); |
| 134 | |
| 135 | let map: HashMap<_, _> = state.elements.iter().map(|(k: &*mut Element, v: &ElementInfo)| (*k, v.node_id())).collect(); |
| 136 | ElementMap(map) |
| 137 | } |
| 138 | |
| 139 | fn recurse_into_element(state: &mut State, element: &ElementRc) -> (usize, Vec<String>) { |
| 140 | if let Some(existing) = state.element_info(element) { |
| 141 | return (existing.id, Vec::new()); |
| 142 | } |
| 143 | |
| 144 | let node_number = state.get_node_number(); |
| 145 | add_element_node(state, element, node_number); |
| 146 | |
| 147 | let e = element.borrow(); |
| 148 | |
| 149 | let mut lines = Vec::new(); |
| 150 | |
| 151 | if let i_slint_compiler::langtype::ElementType::Component(comp) = &e.base_type { |
| 152 | state.register_component(comp, format!("c {}" , state.components.len())); |
| 153 | let component_root = comp.root_element.clone(); |
| 154 | let (root_id, component_lines) = recurse_into_element(state, &component_root); |
| 155 | lines.extend_from_slice(&component_lines); |
| 156 | lines.push(format!(" n {node_number} -> n {root_id} [color = blue]" )); |
| 157 | } |
| 158 | |
| 159 | for c in &e.children { |
| 160 | let (child_node_number, child_result) = recurse_into_element(state, c); |
| 161 | lines.extend_from_slice(&child_result); |
| 162 | lines.push(format!(" n {node_number} -> n {child_node_number}" )); |
| 163 | } |
| 164 | |
| 165 | (node_number, lines) |
| 166 | } |
| 167 | |
| 168 | fn add_element_node(state: &mut State, element: &ElementRc, node_number: usize) { |
| 169 | let e = element.borrow(); |
| 170 | let layout = if e.debug.iter().any(|d| d.layout.is_some()) { ",shape = box" } else { "" }; |
| 171 | let repeated = if e.repeated.is_some() { ",color = blue" } else { "" }; |
| 172 | let component = if matches!(e.base_type, i_slint_compiler::langtype::ElementType::Component(_)) |
| 173 | { |
| 174 | ",fontcolor = blue" |
| 175 | } else { |
| 176 | "" |
| 177 | }; |
| 178 | let mark_up = if let Some(mu) = &state.mark_up { |
| 179 | if Rc::ptr_eq(element, mu) { |
| 180 | ",style=filled,fillcolor=lightgrey" |
| 181 | } else { |
| 182 | "" |
| 183 | } |
| 184 | } else { |
| 185 | "" |
| 186 | }; |
| 187 | |
| 188 | let component_id = state.components.get(&e.enclosing_component.as_ptr()).unwrap().clone(); |
| 189 | |
| 190 | state.elements.insert( |
| 191 | element.as_ptr(), |
| 192 | ElementInfo { |
| 193 | id: node_number, |
| 194 | component_id, |
| 195 | node_definition: format!( |
| 196 | " \"n {node_number}\" [label= \"n {node_number}: {}: {}\"{}{}{}{}]" , |
| 197 | e.id, e.base_type, repeated, layout, component, mark_up |
| 198 | ), |
| 199 | }, |
| 200 | ); |
| 201 | } |
| 202 | |