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            {
120                let background = &self.style.background;
121                let fill = match *background {
122                    Fill::Color(background) => AttributeType::Color(background),
123                    Fill::LinearGradient(_) | Fill::RadialGradient(_) | Fill::ConicGradient(_) => {
124                        AttributeType::Gradient(background.clone())
125                    }
126                };
127                ("background", fill)
128            },
129            (
130                "corner_radius",
131                AttributeType::CornerRadius(self.style.corner_radius),
132            ),
133        ];
134
135        let shadows = &self.style.shadows;
136        for shadow in shadows.iter() {
137            attributes.push(("shadow", AttributeType::Shadow(shadow)));
138        }
139
140        let borders = &self.style.borders;
141        for border in borders.iter() {
142            attributes.push(("border", AttributeType::Border(border)));
143        }
144
145        attributes
146    }
147
148    fn text_style_attributes(&'_ self) -> Vec<(&'_ str, AttributeType<'_>)> {
149        let mut attributes = vec![
150            ("color", AttributeType::Color(self.text_style.color)),
151            (
152                "font_family",
153                AttributeType::Text(self.text_style.font_families.join(", ")),
154            ),
155            (
156                "font_size",
157                AttributeType::Measure(f32::from(self.text_style.font_size)),
158            ),
159            (
160                "text_align",
161                AttributeType::TextAlignment(&self.text_style.text_align),
162            ),
163            (
164                "text_overflow",
165                AttributeType::TextOverflow(&self.text_style.text_overflow),
166            ),
167            (
168                "text_height",
169                AttributeType::TextHeightBehavior(&self.text_style.text_height),
170            ),
171            (
172                "font_slant",
173                AttributeType::FontSlant(self.text_style.font_slant),
174            ),
175            (
176                "font_weight",
177                AttributeType::Measure(self.text_style.font_weight.into()),
178            ),
179            (
180                "font_width",
181                AttributeType::Measure(self.text_style.font_width.into()),
182            ),
183            (
184                "text_decoration",
185                AttributeType::TextDecoration(self.text_style.text_decoration),
186            ),
187        ];
188
189        for shadow in self.style.shadows.iter() {
190            attributes.push(("shadow", AttributeType::Shadow(shadow)));
191        }
192
193        for text_shadow in self.text_style.text_shadows.iter() {
194            attributes.push(("text_shadow", AttributeType::TextShadow(text_shadow)));
195        }
196
197        attributes
198    }
199}
200
201pub enum AttributeType<'a> {
202    Color(Color),
203    OptionalColor(Option<Color>),
204    Gradient(Fill),
205    Size(&'a Size),
206    VisibleSize(&'a VisibleSize),
207    Measure(f32),
208    OptionalMeasure(Option<f32>),
209    Measures(Gaps),
210    CornerRadius(CornerRadius),
211    Direction(&'a Direction),
212    Position(&'a Position),
213    Content(&'a Content),
214    Alignment(&'a Alignment),
215    Shadow(&'a Shadow),
216    TextShadow(&'a TextShadow),
217    Text(String),
218    Border(&'a Border),
219    TextAlignment(&'a TextAlign),
220    TextOverflow(&'a TextOverflow),
221    TextHeightBehavior(&'a TextHeightBehavior),
222    FontSlant(FontSlant),
223    TextDecoration(TextDecoration),
224    Length(Length),
225    Layer(i16),
226    CursorMode(CursorMode),
227    VerticalAlign(VerticalAlign),
228}