Skip to main content

freya_devtools/
node_info.rs

1use freya_core::{
2    integration::*,
3    prelude::{
4        Border,
5        Color,
6        CornerRadius,
7        Fill,
8        FontSlant,
9        Shadow,
10        TextAlign,
11        TextDecoration,
12        TextHeightBehavior,
13        TextOverflow,
14        TextShadow,
15    },
16};
17use serde::{
18    Deserialize,
19    Serialize,
20};
21use torin::{
22    alignment::Alignment,
23    direction::Direction,
24    gaps::Gaps,
25    geometry::Length,
26    prelude::{
27        Area,
28        AreaOf,
29        Content,
30        Inner,
31        Position,
32        VisibleSize,
33    },
34    size::Size,
35};
36
37#[derive(Deserialize, Serialize, Clone, PartialEq, Debug)]
38pub struct NodeInfo {
39    pub window_id: u64,
40    pub is_window: bool,
41    pub node_id: NodeId,
42    pub parent_id: Option<NodeId>,
43    pub children_len: usize,
44    pub height: u16,
45    pub layer: i16,
46    pub state: NodeState,
47    pub area: Area,
48    pub inner_area: AreaOf<Inner>,
49}
50
51#[derive(Clone, PartialEq, Debug, serde::Serialize, serde::Deserialize)]
52pub struct NodeState {
53    pub style: StyleState,
54    pub text_style: TextStyleState,
55    pub layout: torin::node::Node,
56    pub accessibility: AccessibilityData,
57}
58
59impl NodeState {
60    pub fn layout_attributes(&'_ self) -> Vec<(&'_ str, AttributeType<'_>)> {
61        vec![
62            ("width", AttributeType::Size(&self.layout.width)),
63            ("height", AttributeType::Size(&self.layout.height)),
64            ("min_width", AttributeType::Size(&self.layout.minimum_width)),
65            (
66                "min_height",
67                AttributeType::Size(&self.layout.minimum_height),
68            ),
69            ("max_width", AttributeType::Size(&self.layout.maximum_width)),
70            (
71                "max_height",
72                AttributeType::Size(&self.layout.maximum_height),
73            ),
74            (
75                "visible_width",
76                AttributeType::VisibleSize(&self.layout.visible_width),
77            ),
78            (
79                "visible_height",
80                AttributeType::VisibleSize(&self.layout.visible_height),
81            ),
82            (
83                "direction",
84                AttributeType::Direction(&self.layout.direction),
85            ),
86            ("padding", AttributeType::Measures(self.layout.padding)),
87            ("margin", AttributeType::Measures(self.layout.margin)),
88            ("position", AttributeType::Position(&self.layout.position)),
89            (
90                "main_alignment",
91                AttributeType::Alignment(&self.layout.main_alignment),
92            ),
93            (
94                "cross_alignment",
95                AttributeType::Alignment(&self.layout.cross_alignment),
96            ),
97            (
98                "offset_x",
99                AttributeType::Measure(self.layout.offset_x.get()),
100            ),
101            (
102                "offset_y",
103                AttributeType::Measure(self.layout.offset_y.get()),
104            ),
105            ("content", AttributeType::Content(&self.layout.content)),
106            ("spacing", AttributeType::Length(self.layout.spacing)),
107        ]
108    }
109    pub fn style_attributes(&'_ self) -> Vec<(&'_ str, AttributeType<'_>)> {
110        let mut attributes = vec![
111            ("background", AttributeType::from(&self.style.background)),
112            (
113                "corner_radius",
114                AttributeType::CornerRadius(self.style.corner_radius),
115            ),
116        ];
117
118        let shadows = &self.style.shadows;
119        for shadow in shadows.iter() {
120            attributes.push(("shadow", AttributeType::Shadow(shadow)));
121        }
122
123        let borders = &self.style.borders;
124        for border in borders.iter() {
125            attributes.push(("border", AttributeType::Border(border)));
126        }
127
128        attributes
129    }
130
131    pub fn text_style_attributes(&'_ self) -> Vec<(&'_ str, AttributeType<'_>)> {
132        let mut attributes = vec![
133            ("color", AttributeType::from(&self.text_style.color)),
134            (
135                "font_family",
136                AttributeType::Text(self.text_style.font_families.join(", ")),
137            ),
138            (
139                "font_size",
140                AttributeType::Measure(f32::from(self.text_style.font_size)),
141            ),
142            (
143                "text_align",
144                AttributeType::TextAlignment(&self.text_style.text_align),
145            ),
146            (
147                "text_overflow",
148                AttributeType::TextOverflow(&self.text_style.text_overflow),
149            ),
150            (
151                "text_height",
152                AttributeType::TextHeightBehavior(&self.text_style.text_height),
153            ),
154            (
155                "font_slant",
156                AttributeType::FontSlant(self.text_style.font_slant),
157            ),
158            (
159                "font_weight",
160                AttributeType::Measure(self.text_style.font_weight.into()),
161            ),
162            (
163                "font_width",
164                AttributeType::Measure(self.text_style.font_width.into()),
165            ),
166            (
167                "text_decoration",
168                AttributeType::TextDecoration(self.text_style.text_decoration),
169            ),
170        ];
171
172        for shadow in self.style.shadows.iter() {
173            attributes.push(("shadow", AttributeType::Shadow(shadow)));
174        }
175
176        for text_shadow in self.text_style.text_shadows.iter() {
177            attributes.push(("text_shadow", AttributeType::TextShadow(text_shadow)));
178        }
179
180        attributes
181    }
182}
183
184pub enum AttributeType<'a> {
185    Color(Color),
186    Fill(Fill),
187    Size(&'a Size),
188    VisibleSize(&'a VisibleSize),
189    Measure(f32),
190    Measures(Gaps),
191    CornerRadius(CornerRadius),
192    Direction(&'a Direction),
193    Position(&'a Position),
194    Content(&'a Content),
195    Alignment(&'a Alignment),
196    Shadow(&'a Shadow),
197    TextShadow(&'a TextShadow),
198    Text(String),
199    Border(&'a Border),
200    TextAlignment(&'a TextAlign),
201    TextOverflow(&'a TextOverflow),
202    TextHeightBehavior(&'a TextHeightBehavior),
203    FontSlant(FontSlant),
204    TextDecoration(TextDecoration),
205    Length(Length),
206}
207
208impl<'a> From<&'a Fill> for AttributeType<'a> {
209    fn from(fill: &'a Fill) -> Self {
210        match fill.as_color() {
211            Some(color) => AttributeType::Color(color),
212            None => AttributeType::Fill(fill.clone()),
213        }
214    }
215}