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 "letter_spacing",
172 AttributeType::Measure(f32::from(self.text_style.letter_spacing)),
173 ),
174 ];
175
176 for shadow in self.style.shadows.iter() {
177 attributes.push(("shadow", AttributeType::Shadow(shadow)));
178 }
179
180 for text_shadow in self.text_style.text_shadows.iter() {
181 attributes.push(("text_shadow", AttributeType::TextShadow(text_shadow)));
182 }
183
184 attributes
185 }
186}
187
188pub enum AttributeType<'a> {
189 Color(Color),
190 Fill(Fill),
191 Size(&'a Size),
192 VisibleSize(&'a VisibleSize),
193 Measure(f32),
194 Measures(Gaps),
195 CornerRadius(CornerRadius),
196 Direction(&'a Direction),
197 Position(&'a Position),
198 Content(&'a Content),
199 Alignment(&'a Alignment),
200 Shadow(&'a Shadow),
201 TextShadow(&'a TextShadow),
202 Text(String),
203 Border(&'a Border),
204 TextAlignment(&'a TextAlign),
205 TextOverflow(&'a TextOverflow),
206 TextHeightBehavior(&'a TextHeightBehavior),
207 FontSlant(FontSlant),
208 TextDecoration(TextDecoration),
209 Length(Length),
210}
211
212impl<'a> From<&'a Fill> for AttributeType<'a> {
213 fn from(fill: &'a Fill) -> Self {
214 match fill.as_color() {
215 Some(color) => AttributeType::Color(color),
216 None => AttributeType::Fill(fill.clone()),
217 }
218 }
219}