freya_devtools/
node_info.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use freya_core::{
    node_state_snapshot::NodeState,
    values::{
        Border,
        CornerRadius,
        Fill,
        Shadow,
        SvgPaint,
        TextAlign,
        TextOverflow,
        TextShadow,
    },
};
use freya_native_core::{
    NodeId,
    tags::TagName,
};
use serde::{
    Deserialize,
    Serialize,
};
use torin::{
    alignment::Alignment,
    direction::Direction,
    gaps::Gaps,
    prelude::{
        Content,
        LayoutNode,
        Position,
        VisibleSize,
    },
    size::Size,
};

#[derive(Deserialize, Serialize, Clone, PartialEq, Debug)]
pub struct NodeInfo {
    pub window_id: u64,
    pub is_window: bool,
    pub node_id: NodeId,
    pub parent_id: Option<NodeId>,
    pub children_len: usize,
    pub tag: TagName,
    pub height: u16,
    pub state: NodeState,
    pub layout_node: LayoutNode,
}

pub trait NodeStateAttributes {
    fn layout_attributes(&self) -> Vec<(&str, AttributeType)>;
    fn font_style_attributes(&self) -> Vec<(&str, AttributeType)>;
    fn style_attributes(&self) -> Vec<(&str, AttributeType)>;
    fn svg_attributes(&self) -> Vec<(&str, AttributeType)>;
}

impl NodeStateAttributes for NodeState {
    fn layout_attributes(&self) -> Vec<(&str, AttributeType)> {
        vec![
            ("width", AttributeType::Size(&self.layout.width)),
            ("height", AttributeType::Size(&self.layout.height)),
            ("min_width", AttributeType::Size(&self.layout.minimum_width)),
            (
                "min_height",
                AttributeType::Size(&self.layout.minimum_height),
            ),
            ("max_width", AttributeType::Size(&self.layout.maximum_width)),
            (
                "max_height",
                AttributeType::Size(&self.layout.maximum_height),
            ),
            (
                "visible_width",
                AttributeType::VisibleSize(&self.layout.visible_width),
            ),
            (
                "visible_height",
                AttributeType::VisibleSize(&self.layout.visible_height),
            ),
            (
                "direction",
                AttributeType::Direction(&self.layout.direction),
            ),
            ("padding", AttributeType::Measures(self.layout.padding)),
            ("margin", AttributeType::Measures(self.layout.margin)),
            ("position", AttributeType::Position(&self.layout.position)),
            (
                "main_alignment",
                AttributeType::Alignment(&self.layout.main_alignment),
            ),
            (
                "cross_alignment",
                AttributeType::Alignment(&self.layout.cross_alignment),
            ),
            (
                "offset_x",
                AttributeType::Measure(self.layout.offset_x.get()),
            ),
            (
                "offset_y",
                AttributeType::Measure(self.layout.offset_y.get()),
            ),
            ("content", AttributeType::Content(&self.layout.content)),
        ]
    }
    fn style_attributes(&self) -> Vec<(&str, AttributeType)> {
        let mut attributes = vec![
            {
                let background = &self.style.background;
                let fill = match *background {
                    Fill::Color(_) => AttributeType::Color(background.clone()),
                    Fill::LinearGradient(_) => AttributeType::Gradient(background.clone()),
                    Fill::RadialGradient(_) => AttributeType::Gradient(background.clone()),
                    Fill::ConicGradient(_) => AttributeType::Gradient(background.clone()),
                };
                ("background", fill)
            },
            (
                "corner_radius",
                AttributeType::CornerRadius(self.style.corner_radius),
            ),
        ];

        let shadows = &self.style.shadows;
        for shadow in shadows.iter() {
            attributes.push(("shadow", AttributeType::Shadow(shadow)));
        }

        let borders = &self.style.borders;
        for border in borders.iter() {
            attributes.push(("border", AttributeType::Border(border)));
        }

        attributes
    }

    fn font_style_attributes(&self) -> Vec<(&str, AttributeType)> {
        let mut attributes = vec![
            ("color", AttributeType::Color(self.font_style.color.into())),
            (
                "font_family",
                AttributeType::Text(self.font_style.font_family.join(",")),
            ),
            (
                "font_size",
                AttributeType::Measure(self.font_style.font_size),
            ),
            (
                "line_height",
                AttributeType::OptionalMeasure(self.font_style.line_height),
            ),
            (
                "text_align",
                AttributeType::TextAlignment(&self.font_style.text_align),
            ),
            (
                "text_overflow",
                AttributeType::TextOverflow(&self.font_style.text_overflow),
            ),
        ];

        let text_shadows = &self.font_style.text_shadows;

        for text_shadow in text_shadows.iter() {
            attributes.push(("text_shadow", AttributeType::TextShadow(text_shadow)));
        }

        attributes
    }

    fn svg_attributes(&self) -> Vec<(&str, AttributeType)> {
        vec![
            (
                "svg_fill",
                AttributeType::OptionalColor(self.svg.svg_fill.and_then(|fill| match fill {
                    SvgPaint::None => None,
                    SvgPaint::CurrentColor => Some(Fill::Color(self.font_style.color)),
                    SvgPaint::Color(color) => Some(Fill::Color(color)),
                })),
            ),
            (
                "svg_stroke",
                AttributeType::OptionalColor(self.svg.svg_stroke.and_then(|stroke| match stroke {
                    SvgPaint::None => None,
                    SvgPaint::CurrentColor => Some(Fill::Color(self.font_style.color)),
                    SvgPaint::Color(color) => Some(Fill::Color(color)),
                })),
            ),
        ]
    }
}

pub enum AttributeType<'a> {
    Color(Fill),
    OptionalColor(Option<Fill>),
    Gradient(Fill),
    Size(&'a Size),
    VisibleSize(&'a VisibleSize),
    Measure(f32),
    OptionalMeasure(Option<f32>),
    Measures(Gaps),
    CornerRadius(CornerRadius),
    Direction(&'a Direction),
    Position(&'a Position),
    Content(&'a Content),
    Alignment(&'a Alignment),
    Shadow(&'a Shadow),
    TextShadow(&'a TextShadow),
    Text(String),
    Border(&'a Border),
    TextAlignment(&'a TextAlign),
    TextOverflow(&'a TextOverflow),
}