Skip to main content

freya_devtools/
node_info.rs

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