tuix_core 0.2.0

Core parts of tuix
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use crate::state::style::*;
use crate::{Entity, EventHandler, State, Widget};

use std::{cell::RefCell, marker::PhantomData};
use std::rc::Rc;

/// Contains an entity id and a mutable reference to state and can be used to set properties of a widget at build time
pub struct Builder<'a,T> {
    pub entity: Entity,
    pub state: &'a mut State,
    phantom: std::marker::PhantomData<T>,
}

impl<'a,T> Builder<'a,T> {

    /// Creates a new Builder
    pub(crate) fn new(state: &'a mut State, entity: Entity) -> Self {
        Builder::<T> { entity, state, phantom: PhantomData}
    }

    /// Builds the widget into State
    pub(crate) fn build(mut self, event_handler: T) -> Entity
    where
        T: EventHandler + 'static + Sized,
    {
        self.state
            .event_handlers
            .insert(self.entity, Box::new(event_handler));

        self.entity
    }

    /// Returns a mutable reference to the State
    pub fn state(&mut self) -> &mut State {
        self.state
    }

    /// Returns the entity id contained within the builder
    pub fn entity(&self) -> Entity {
        self.entity
    }

    /// Sets the general callback for pressing a widget
    pub fn on_press<F>(mut self, mut handler: F) -> Self
    where 
        T: Widget,
        F: FnMut(&mut T, &mut State, Entity) + 'static,
    {
        self.state.callbacks.insert(self.entity, Box::new(move |callback, state, entity| {
            if let Some(callback) = callback.downcast::<T>() {
                (handler)(callback, state, entity);
            } else {
                println!("Failed");
            }
        }));

        self
    }

    /// Adds a class name to the entity
    pub fn class(mut self, class_name: &str) -> Self {
        //self.state.style.insert_class(self.entity, class);
        self.entity.class(self.state, class_name);

        self
    }

    pub fn set_name(mut self, name: &str) -> Self {
        self.state.style.name.insert(self.entity(), name.to_string());
    
        self
    }

    /// Sets the element name of the entity
    pub fn set_element(mut self, element: &str) -> Self {
        //self.state.style.insert_element(self.entity, element);

        self.entity.set_element(self.state, element);

        self
    }

    /// Sets the id of the entity
    pub fn set_id(mut self, id: &str) -> Self {
        //self.state.style.insert_id(self.entity, id);

        todo!();
        self
    }

    pub fn set_disabled(mut self, value: bool) -> Self {
        self.entity().set_disabled(self.state, value);

        self
    }


    /// Sets whether the entity can be hovered
    pub fn set_hoverable(mut self, value: bool) -> Self {
        self.state.data.set_hoverable(self.entity, value);

        self
    }

    /// Sets whether the entity can be focused
    pub fn set_focusable(mut self, value: bool) -> Self {
        self.state.data.set_focusable(self.entity, value);

        self
    }

    /// Sets the opacity of the entity
    pub fn set_opacity(mut self, value: f32) -> Self {
        self.state.style.opacity.insert(self.entity, Opacity(value));

        self
    }

    /// Sets the checked state of the entity
    pub fn set_checked(mut self, value: bool) -> Self {
        self.entity().set_checked(self.state, value);

        self
    }

    /// Sets the z-order of the entity
    pub fn set_z_order(mut self, value: i32) -> Self {
        self.state.style.z_order.insert(self.entity, value);

        self
    }

    /// Sets the clip widget of the entity. The clip bounds of the entity are set to the bounds of the clip widget
    pub fn set_clip_widget(mut self, value: Entity) -> Self {
        self.state.style.clip_widget.insert(self.entity, value);

        self
    }

    // Sets the text displayed within the entity
    pub fn set_text(mut self, value: &str) -> Self {
        self.state.style.text.insert(self.entity, value.to_owned());

        self
    }

    pub fn set_font(mut self, value: &str) -> Self {
        self.state.style.font.insert(self.entity, value.to_owned());

        self
    }

    // Sets the tooltip associated with the entity
    pub fn set_tooltip(mut self, value: &str) -> Self {
        self.state
            .style
            .tooltip
            .insert(self.entity, value.to_string());

        self
    }

    // pub fn set_tooltip<F>(mut self, value: &str) -> Self 
    // where F: FnOnce(&mut State, Entity)
    // {
    //     self.state
    //         .style
    //         .tooltip
    //         .insert(self.entity, value.to_string());

    //     self
    // }

    // Display
    /// Sets the display type of the entity
    pub fn set_display(mut self, value: Display) -> Self {
        self.state.style.display.insert(self.entity, value);

        self
    }

    pub fn set_visibility(mut self, value: Visibility) -> Self {
        self.state.style.visibility.insert(self.entity, value);

        self
    }

    pub fn set_overflow(mut self, value: Overflow) -> Self {
        self.state.style.overflow.insert(self.entity, value);

        self
    }

    // Background
    pub fn set_background_color(mut self, value: Color) -> Self {
        self.state.style.background_color.insert(self.entity, value);

        self
    }

    pub fn set_background_image(mut self, value: Rc<()>) -> Self {
        self.state
            .style
            .background_image
            .insert(self.entity, value.clone());

        self
    }

    pub fn set_background_gradient(mut self, value: LinearGradient) -> Self {
        self.state
            .style
            .background_gradient
            .insert(self.entity, value);

        self
    }

    // Outer Shadow
    pub fn set_outer_shadow_h_offset(mut self, value: Units) -> Self {
        self.state
            .style
            .outer_shadow_h_offset
            .insert(self.entity, value);

        self
    }

    pub fn set_outer_shadow_v_offset(mut self, value: Units) -> Self {
        self.state
            .style
            .outer_shadow_v_offset
            .insert(self.entity, value);

        self
    }

    pub fn set_outer_shadow_color(mut self, value: Color) -> Self {
        self.state.style.outer_shadow_color.insert(self.entity, value);

        self
    }

    pub fn set_outer_shadow_blur(mut self, value: Units) -> Self {
        self.state.style.outer_shadow_blur.insert(self.entity, value);

        self
    }

    // Inner Shadow
    pub fn set_inner_shadow_h_offset(mut self, value: Units) -> Self {
        self.state
            .style
            .inner_shadow_h_offset
            .insert(self.entity, value);

        self
    }

    pub fn set_inner_shadow_v_offset(mut self, value: Units) -> Self {
        self.state
            .style
            .inner_shadow_v_offset
            .insert(self.entity, value);

        self
    }

    pub fn set_inner_shadow_color(mut self, value: Color) -> Self {
        self.state.style.inner_shadow_color.insert(self.entity, value);

        self
    }

    pub fn set_inner_shadow_blur(mut self, value: Units) -> Self {
        self.state.style.inner_shadow_blur.insert(self.entity, value);

        self
    }

    // Positioning

    pub fn set_space(mut self, value: Units) -> Self {
        self.state.style.left.insert(self.entity, value);
        self.state.style.right.insert(self.entity, value);
        self.state.style.top.insert(self.entity, value);
        self.state.style.bottom.insert(self.entity, value);

        self
    }

    pub fn set_left(mut self, value: Units) -> Self {
        self.entity.set_left(self.state, value);

        self
    }

    pub fn set_right(mut self, value: Units) -> Self {
        self.entity.set_right(self.state, value);

        self
    }

    pub fn set_top(mut self, value: Units) -> Self {
        self.entity.set_top(self.state, value);
        
        self
    }

    pub fn set_bottom(mut self, value: Units) -> Self {
        self.entity.set_bottom(self.state, value);
        
        self
    }

    // Size

    pub fn set_width(mut self, value: Units) -> Self {
        self.entity.set_width(self.state, value);

        self
    }

    pub fn set_height(mut self, value: Units) -> Self {
        self.entity.set_height(self.state, value);

        self
    }

    // Size Constraints

    pub fn set_min_width(mut self, value: Units) -> Self {
        self.state.style.min_width.insert(self.entity, value);

        self
    }

    pub fn set_max_width(mut self, value: Units) -> Self {
        self.state.style.max_width.insert(self.entity, value);

        self
    }

    pub fn set_min_height(mut self, value: Units) -> Self {
        self.state.style.min_height.insert(self.entity, value);

        self
    }

    pub fn set_max_height(mut self, value: Units) -> Self {
        self.state.style.max_height.insert(self.entity, value);

        self
    }

    pub fn set_min_left(mut self, value: Units) -> Self {
        self.state.style.min_left.insert(self.entity, value);
        self
    }

    pub fn set_min_right(mut self, value: Units) -> Self {
        self.state.style.min_right.insert(self.entity, value);
        self
    }

    pub fn set_min_top(mut self, value: Units) -> Self {
        self.state.style.min_top.insert(self.entity, value);
        self
    }

    pub fn set_min_bottom(mut self, value: Units) -> Self {
        self.state.style.min_bottom.insert(self.entity, value);
        self
    }

    pub fn set_max_left(mut self, value: Units) -> Self {
        self.state.style.max_left.insert(self.entity, value);
        self
    }

    pub fn set_max_right(mut self, value: Units) -> Self {
        self.state.style.max_right.insert(self.entity, value);
        self
    }

    pub fn set_max_top(mut self, value: Units) -> Self {
        self.state.style.max_top.insert(self.entity, value);
        self
    }

    pub fn set_max_bottom(mut self, value: Units) -> Self {
        self.state.style.max_bottom.insert(self.entity, value);
        self
    }

    // Border

    pub fn set_border_color(mut self, value: Color) -> Self {
        self.state.style.border_color.insert(self.entity, value);

        self
    }

    pub fn set_border_width(mut self, value: Units) -> Self {
        self.state.style.border_width.insert(self.entity, value);

        self
    }

    pub fn set_border_corner_shape(mut self, value: BorderCornerShape) -> Self {
        self.state.style.border_shape_top_left.insert(self.entity, value);
        self.state.style.border_shape_top_right.insert(self.entity, value);
        self.state.style.border_shape_bottom_left.insert(self.entity, value);
        self.state.style.border_shape_bottom_right.insert(self.entity, value);

        self
    }

    pub fn set_border_top_left_shape(mut self, value: BorderCornerShape) -> Self {
        self.state.style.border_shape_top_left.insert(self.entity, value);

        self
    }

    pub fn set_border_top_right_shape(mut self, value: BorderCornerShape) -> Self {
        self.state.style.border_shape_top_right.insert(self.entity, value);

        self
    }

    pub fn set_border_bottom_left_shape(mut self, value: BorderCornerShape) -> Self {
        self.state.style.border_shape_bottom_left.insert(self.entity, value);

        self
    }

    pub fn set_border_bottom_right_shape(mut self, value: BorderCornerShape) -> Self {
        self.state.style.border_shape_bottom_right.insert(self.entity, value);

        self
    }

    pub fn set_border_radius(mut self, value: Units) -> Self {
        self.state
            .style
            .border_radius_top_left
            .insert(self.entity, value);
        self.state
            .style
            .border_radius_top_right
            .insert(self.entity, value);
        self.state
            .style
            .border_radius_bottom_left
            .insert(self.entity, value);
        self.state
            .style
            .border_radius_bottom_right
            .insert(self.entity, value);

        self
    }

    pub fn set_border_radius_top_left(mut self, value: Units) -> Self {
        self.state
            .style
            .border_radius_top_left
            .insert(self.entity, value);

        self
    }

    pub fn set_border_radius_top_right(mut self, value: Units) -> Self {
        self.state
            .style
            .border_radius_top_right
            .insert(self.entity, value);

        self
    }

    pub fn set_border_radius_bottom_left(mut self, value: Units) -> Self {
        self.state
            .style
            .border_radius_bottom_left
            .insert(self.entity, value);

        self
    }

    pub fn set_border_radius_bottom_right(mut self, value: Units) -> Self {
        self.state
            .style
            .border_radius_bottom_right
            .insert(self.entity, value);

        self
    }

    pub fn set_color(mut self, value: Color) -> Self {
        self.state.style.font_color.insert(self.entity, value);

        self
    }

    pub fn set_font_size(mut self, value: f32) -> Self {
        self.state.style.font_size.insert(self.entity, value);

        self
    }

    pub fn set_next_focus(mut self, value: Entity) -> Self {
        if let Some(entity) = self.state.style.focus_order.get_mut(self.entity) {
            entity.next = value;
        } else {
            self.state.style.focus_order.insert(
                self.entity,
                FocusOrder {
                    next: value,
                    ..Default::default()
                },
            );
        }

        self
    }

    pub fn set_prev_focus(mut self, value: Entity) -> Self {
        if let Some(entity) = self.state.style.focus_order.get_mut(self.entity) {
            entity.prev = value;
        } else {
            self.state.style.focus_order.insert(
                self.entity,
                FocusOrder {
                    prev: value,
                    ..Default::default()
                },
            );
        }

        self
    }

    pub fn set_focus(mut self, next: Entity, prev: Entity) -> Self {
        if let Some(entity) = self.state.style.focus_order.get_mut(self.entity) {
            entity.next = next;
            entity.prev = prev;
        } else {
            self.state
                .style
                .focus_order
                .insert(self.entity, FocusOrder { next, prev });
        }

        self
    }

    pub fn set_rotate(mut self, rotate: f32) -> Self {
        self.state.style.rotate.insert(self.entity, rotate);

        self
    }

    pub fn set_scale(mut self, scale: f32) -> Self {
        self.state.style.scale.insert(self.entity, scale);

        self
    }

    pub fn set_child_left(mut self, value: Units) -> Self {
        self.state.style.child_left.insert(self.entity, value);
        self
    }

    pub fn set_child_right(mut self, value: Units) -> Self {
        self.state.style.child_right.insert(self.entity, value);
        self
    }

    pub fn set_child_top(mut self, value: Units) -> Self {
        self.state.style.child_top.insert(self.entity, value);
        self
    }

    pub fn set_child_bottom(mut self, value: Units) -> Self {
        self.state.style.child_bottom.insert(self.entity, value);
        self
    }

    pub fn set_row_between(mut self, value: Units) -> Self {
        self.state.style.row_between.insert(self.entity, value);
        self
    }


    pub fn set_col_between(mut self, value: Units) -> Self {
        self.state.style.col_between.insert(self.entity, value);
        self
    }


    pub fn set_child_space(mut self, value: Units) -> Self {
        self.state.style.child_left.insert(self.entity, value);
        self.state.style.child_right.insert(self.entity, value);
        self.state.style.child_top.insert(self.entity, value);
        self.state.style.child_bottom.insert(self.entity, value);
        self
    }

    pub fn set_position_type(mut self, value: PositionType) -> Self {
        self.state.style.positioning_type.insert(self.entity, value);
        self
    }

    pub fn set_layout_type(mut self, value: LayoutType) -> Self {
        self.state.style.layout_type.insert(self.entity, value);
        self
    }

    pub fn set_row_index(mut self, value: usize) -> Self {
        self.state.style.row_index.insert(self.entity, value);

        self
    }

    pub fn set_col_index(mut self, value: usize) -> Self {
        self.state.style.col_index.insert(self.entity, value);

        self
    }

    pub fn set_row_span(mut self, value: usize) -> Self {
        self.state.style.row_span.insert(self.entity, value);

        self
    }

    pub fn set_col_span(mut self, value: usize) -> Self {
        self.state.style.col_span.insert(self.entity, value);

        self
    }

    pub fn set_grid_rows(mut self, value: Vec<Units>) -> Self {
        self.state.style.grid_rows.insert(self.entity(), value);

        self
    }

    pub fn set_grid_cols(mut self, value: Vec<Units>) -> Self {
        self.state.style.grid_cols.insert(self.entity(), value);

        self
    }
}