craft_core 0.1.1

Core library for the Craft GUI framework.
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use crate::components::ComponentSpecification;
use crate::devtools::dev_tools_colors::{BORDER_COLOR, FIELD_NAME_COLOR, FIELD_VALUE_COLOR, ROW_BACKGROUND_COLOR};
use crate::elements::element::Element;
use crate::elements::{Container, ElementStyles, Text};
use crate::style::style_flags::StyleFlags;
use crate::style::Display::Flex;
use crate::style::{FlexDirection, Unit};
use crate::Color;
use taffy::Overflow;

fn format_option<T: std::fmt::Debug>(option: Option<T>) -> String {
    option.map_or("None".to_string(), |value| format!("{:?}", value))
}

fn field_row(
    field_name: &str,
    field_name_color: Color,
    field_value: &str,
    field_value_color: Color,
) -> ComponentSpecification {
    Container::new()
        .push(Text::new(field_name.to_lowercase().as_str()).color(field_name_color))
        .push(Text::new(field_value.to_lowercase().as_str()).color(field_value_color))
        .padding("0px", "10px", "0px", "10px")
        .component()
}

pub(crate) fn styles_window_view(selected_element: Option<&dyn Element>) -> ComponentSpecification {
    let mut styles_window = Container::new()
        .width(Unit::Percentage(100.0))
        .display(Flex)
        .flex_direction(FlexDirection::Column)
        .height("50%")
        .max_height("50%")
        .overflow(Overflow::Scroll)
        .background(ROW_BACKGROUND_COLOR)
        .push(Container::new().border_width("2px", "0px", "2px", "0px").border_color(BORDER_COLOR).push(
            Text::new("Styles Window").color(Color::from_rgb8(230, 230, 230)).padding("10px", "0px", "10px", "10px"),
        ))
        .component();

    if let Some(selected_element) = selected_element {
        let style = selected_element.style();

        // Font Family
        if style.dirty_flags.contains(StyleFlags::FONT_FAMILY) && style.font_family().is_some() {
            styles_window = styles_window.push(field_row(
                "Font Family: ",
                FIELD_NAME_COLOR,
                style.font_family().unwrap(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Box Sizing
        if style.dirty_flags.contains(StyleFlags::BOX_SIZING) {
            styles_window = styles_window.push(field_row(
                "Box Sizing: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.box_sizing()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Scrollbar Width
        if style.dirty_flags.contains(StyleFlags::SCROLLBAR_WIDTH) {
            styles_window = styles_window.push(field_row(
                "Scrollbar Width: ",
                FIELD_NAME_COLOR,
                style.scrollbar_width().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Position
        if style.dirty_flags.contains(StyleFlags::POSITION) {
            styles_window = styles_window.push(field_row(
                "Position: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.position()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Margin
        if style.dirty_flags.contains(StyleFlags::MARGIN) {
            styles_window = styles_window.push(field_row(
                "Margin Top: ",
                FIELD_NAME_COLOR,
                style.margin().top.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Margin Right: ",
                FIELD_NAME_COLOR,
                style.margin().right.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Margin Bottom: ",
                FIELD_NAME_COLOR,
                style.margin().bottom.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Margin Left: ",
                FIELD_NAME_COLOR,
                style.margin().left.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Padding
        if style.dirty_flags.contains(StyleFlags::PADDING) {
            styles_window = styles_window.push(field_row(
                "Padding Top: ",
                FIELD_NAME_COLOR,
                style.padding().top.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Padding Right: ",
                FIELD_NAME_COLOR,
                style.padding().right.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Padding Bottom: ",
                FIELD_NAME_COLOR,
                style.padding().bottom.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Padding Left: ",
                FIELD_NAME_COLOR,
                style.padding().left.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Gap
        if style.dirty_flags.contains(StyleFlags::GAP) {
            styles_window = styles_window.push(field_row(
                "Row Gap: ",
                FIELD_NAME_COLOR,
                style.gap()[0].to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Column Gap: ",
                FIELD_NAME_COLOR,
                style.gap()[1].to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Inset
        if style.dirty_flags.contains(StyleFlags::INSET) {
            styles_window = styles_window.push(field_row(
                "Inset Top: ",
                FIELD_NAME_COLOR,
                style.inset().top.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Inset Right: ",
                FIELD_NAME_COLOR,
                style.inset().right.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Inset Bottom: ",
                FIELD_NAME_COLOR,
                style.inset().bottom.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
            styles_window = styles_window.push(field_row(
                "Inset Left: ",
                FIELD_NAME_COLOR,
                style.inset().left.to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Width
        if style.dirty_flags.contains(StyleFlags::WIDTH) {
            styles_window = styles_window.push(field_row(
                "Width: ",
                FIELD_NAME_COLOR,
                style.width().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Height
        if style.dirty_flags.contains(StyleFlags::HEIGHT) {
            styles_window = styles_window.push(field_row(
                "Height: ",
                FIELD_NAME_COLOR,
                style.height().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Max Width
        if style.dirty_flags.contains(StyleFlags::MAX_WIDTH) {
            styles_window = styles_window.push(field_row(
                "Max Width: ",
                FIELD_NAME_COLOR,
                style.max_width().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Max Height
        if style.dirty_flags.contains(StyleFlags::MAX_HEIGHT) {
            styles_window = styles_window.push(field_row(
                "Max Height: ",
                FIELD_NAME_COLOR,
                style.max_height().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Min Width
        if style.dirty_flags.contains(StyleFlags::MIN_WIDTH) {
            styles_window = styles_window.push(field_row(
                "Min Width: ",
                FIELD_NAME_COLOR,
                style.min_width().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Min Height
        if style.dirty_flags.contains(StyleFlags::MIN_HEIGHT) {
            styles_window = styles_window.push(field_row(
                "Min Height: ",
                FIELD_NAME_COLOR,
                style.min_height().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // X
        if style.dirty_flags.contains(StyleFlags::X) {
            styles_window = styles_window.push(field_row(
                "X: ",
                FIELD_NAME_COLOR,
                style.x().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Y
        if style.dirty_flags.contains(StyleFlags::Y) {
            styles_window = styles_window.push(field_row(
                "Y: ",
                FIELD_NAME_COLOR,
                style.y().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Display
        if style.dirty_flags.contains(StyleFlags::DISPLAY) {
            styles_window = styles_window.push(field_row(
                "Display: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.display()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Wrap
        if style.dirty_flags.contains(StyleFlags::WRAP) {
            styles_window = styles_window.push(field_row(
                "Wrap: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.wrap()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Align Items
        if style.dirty_flags.contains(StyleFlags::ALIGN_ITEMS) {
            styles_window = styles_window.push(field_row(
                "Align Items: ",
                FIELD_NAME_COLOR,
                format_option(style.align_items()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Justify Content
        if style.dirty_flags.contains(StyleFlags::JUSTIFY_CONTENT) {
            styles_window = styles_window.push(field_row(
                "Justify Content: ",
                FIELD_NAME_COLOR,
                format_option(style.justify_content()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Flex Direction
        if style.dirty_flags.contains(StyleFlags::FLEX_DIRECTION) {
            styles_window = styles_window.push(field_row(
                "Flex Direction: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.flex_direction()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Flex Grow
        if style.dirty_flags.contains(StyleFlags::FLEX_GROW) {
            styles_window = styles_window.push(field_row(
                "Flex Grow: ",
                FIELD_NAME_COLOR,
                style.flex_grow().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Flex Shrink
        if style.dirty_flags.contains(StyleFlags::FLEX_SHRINK) {
            styles_window = styles_window.push(field_row(
                "Flex Shrink: ",
                FIELD_NAME_COLOR,
                style.flex_shrink().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Flex Basis
        if style.dirty_flags.contains(StyleFlags::FLEX_BASIS) {
            styles_window = styles_window.push(field_row(
                "Flex Basis: ",
                FIELD_NAME_COLOR,
                style.flex_basis().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Color
        if style.dirty_flags.contains(StyleFlags::COLOR) {
            styles_window = styles_window.push(field_row(
                "Color: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.color()).as_str(),
                FIELD_VALUE_COLOR,
            ))
        }

        // Background Color
        if style.dirty_flags.contains(StyleFlags::BACKGROUND) {
            styles_window = styles_window.push(field_row(
                "Background: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.background()).as_str(),
                FIELD_VALUE_COLOR,
            ))
        }

        // Font Size
        if style.dirty_flags.contains(StyleFlags::FONT_SIZE) {
            styles_window = styles_window.push(field_row(
                "Font Size: ",
                FIELD_NAME_COLOR,
                style.font_size().to_string().as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Font Weight
        if style.dirty_flags.contains(StyleFlags::FONT_WEIGHT) {
            styles_window = styles_window.push(field_row(
                "Font Weight: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.font_weight()).as_str(),
                FIELD_VALUE_COLOR,
            ))
        }

        // Font Style
        if style.dirty_flags.contains(StyleFlags::FONT_STYLE) {
            styles_window = styles_window.push(field_row(
                "Font Style: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.font_style()).as_str(),
                FIELD_VALUE_COLOR,
            ))
        }

        // Overflow
        if style.dirty_flags.contains(StyleFlags::OVERFLOW) {
            styles_window = styles_window.push(field_row(
                "Overflow: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.overflow()).as_str(),
                FIELD_VALUE_COLOR,
            ))
        }

        // Border Color
        if style.dirty_flags.contains(StyleFlags::BORDER_COLOR) {
            styles_window = styles_window.push(field_row(
                "Border Color: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.border_color()).as_str(),
                FIELD_VALUE_COLOR,
            ))
        }

        // Border Width
        if style.dirty_flags.contains(StyleFlags::BORDER_WIDTH) {
            styles_window = styles_window.push(field_row(
                "Border Width: ",
                FIELD_NAME_COLOR,
                style.border_width().to_array().map(|bw| bw.to_string()).join(", ").as_str(),
                FIELD_VALUE_COLOR,
            ));
        }

        // Border Radius
        if style.dirty_flags.contains(StyleFlags::BORDER_RADIUS) {
            styles_window = styles_window.push(field_row(
                "Border Radius: ",
                FIELD_NAME_COLOR,
                format!("{:?}", style.border_radius()).as_str(),
                FIELD_VALUE_COLOR,
            ));
        }
    }

    styles_window
}