layuit 1.0.0

A UI layout library for Rust
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Containers that use ratios, but maintain minimum size requirements.
//!
//! [`AspectRatio`] maintains a runtime-configurable horizontal:vertical ratio.
//!
//! [`HSplit`] and [`VSplit`] distribute a percentage of the horizontal or vertical space to the
//! left or top child, and give the rest to the other child.
//!
//! [`Percent`] gives a child a percentage of the available space. It can be configured to extend
//! the minimum size to ensure the percentage is always maintained.
//!
//! ## Strict vs non-strict [`Percent`]
//!
//! `Percent` features a field called `strict`. While normally disabled, when enabled, the minimum
//! size of the `Percent` will grow to ensure that the child's minimum is exactly the percentage of
//! the `Percent`'s minimum. When disabled, the minimum size will be the child's minimum size, and
//! the `Percent` will not ensure the percentage is always maintained if it is too small.
//!
//! ```rust
//! use layuit::{UiTree, Rect};
//! use layuit::proportional::Percent;
//! use layuit::padding::Spacer;
//! use layuit::overlap::Overlap;
//!
//! // Both nodes hold a Spacer with a size of 10x10 and occupy the full space. Default alignment is
//! // (Begin, Begin)
//!
//! let strict = Percent::new()
//!     .with_child(Spacer::sized(0.0, 0.0, 10.0, 10.0), &mut tree)
//!     .with_percent((0.5, 0.5))
//!     .with_strict(true);
//!
//! let non_strict = Percent::new()
//!     .with_child(Spacer::sized(0.0, 0.0, 10.0, 10.0), &mut tree)
//!     .with_percent((0.5, 0.5))
//!     .with_strict(false);
//!
//! // Both nodes will be shrunk, guaranteed.
//! let mut stack = HStack::new()
//!     .with_child(strict, &mut tree)
//!     .with_child(non_strict, &mut tree);
//!
//! let mut tree = UiTre::new(stack);
//!
//! tree.calculate_layout(Rect::new(0.0, 0.0, 30.0, 20.0));
//!
//! // Final results:
//!
//! // Strict has a size of 20x20, since its minimum grew to ensure the percentage is always upheld.
//!
//! // Non_strict has a size of 10x10, since its minimum remained the same as the spacer, and the
//! // percentage was not upheld.
//! ```
use thunderdome::Index as TdIndex;

use crate::{Alignment, Anchor, NodeCache, Rect, UiNode, UiTree};

/// Expands the horizontal or vertical dimensions of a child to maintain an aspect ratio.
///
/// An anchor must be specified to determine where the child should be placed after the aspect ratio
/// is applied.
///
/// Once the child is added, it cannot be removed.
pub struct AspectRatio {
    ratio: f32,

    /// The position to place the shrunken space. The child is then aligned within the new space.
    pub anchor: (Anchor, Anchor),

    align: (Alignment, Alignment),
    child: Option<TdIndex>,
}

impl AspectRatio {
    /// Creates a new `AspectRatio` with no child, default anchoring, a 1:1 ratio, and ([`Begin`],
    /// [`Begin`]) alignment.
    ///
    /// [`Begin`]: Alignment::Begin
    pub fn new() -> Self {
        Self {
            ratio: 1.0,
            anchor: (Anchor::Center, Anchor::Center),
            align: (Alignment::Begin, Alignment::Begin),
            child: None,
        }
    }

    /// Create a child node and bind it to the node.
    ///
    /// # Panics
    /// If there is already a child node.
    pub fn with_child(mut self, child: impl UiNode, tree: &mut UiTree) -> Self {
        assert!(self.child.is_none());
        self.child = Some(tree.add_node(child));
        self
    }

    /// Set the horizontal and vertical alignment.
    pub fn with_align(mut self, align: (Alignment, Alignment)) -> Self {
        self.align = align;
        self
    }

    /// Set the horizontal and vertical anchor.
    pub fn with_anchor(mut self, anchor: (Anchor, Anchor)) -> Self {
        self.anchor = anchor;
        self
    }

    /// Set the horizontal:vertical ratio to be maintained.
    ///
    /// # Panics
    /// If `ratio <= 0.0`.
    pub fn with_ratio(mut self, ratio: f32) -> Self {
        assert!(ratio > 0.0);
        self.ratio = ratio;
        self
    }

    /// Returns the current ratio.
    pub fn get_ratio(&self) -> f32 {
        self.ratio
    }

    /// Set the horizontal:vertical ratio to be maintained.
    ///
    /// # Panics
    /// If `ratio <= 0.0`.
    pub fn set_ratio(&mut self, ratio: f32) {
        assert!(ratio > 0.0);
        self.ratio = ratio;
    }

    /// Get the tree index of the child.
    pub fn get_child(&self) -> Option<TdIndex> {
        self.child
    }
}

impl Default for AspectRatio {
    fn default() -> Self {
        Self::new()
    }
}

impl UiNode for AspectRatio {
    fn get_align(&self) -> (Alignment, Alignment) {
        self.align
    }

    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment) {
        (&mut self.align.0, &mut self.align.1)
    }

    fn calculate_min_size(&self, tree: &UiTree) -> (f32, f32) {
        if let Some(child) = self.child {
            let child_min = tree.get_cache(child).expect("Child not in cache").min_size;

            // Prevent division by zero
            if child_min.0 == 0.0 || child_min.1 == 0.0 {
                return (0.0, 0.0);
            }

            let child_ratio = child_min.0 / child_min.1;
            if child_ratio > self.ratio {
                // Keep width, increase height
                (child_min.0, child_min.0 / self.ratio)
            } else {
                // Keep height, increase width
                (child_min.1 * self.ratio, child_min.1)
            }
        } else {
            (0.0, 0.0)
        }
    }

    fn calculate_rects(&self, cache: &NodeCache, tree: &UiTree) -> Vec<Rect> {
        if let Some(child) = self.child {
            let child_min = tree.get_cache(child).expect("Child not in cache").min_size;

            // Prevent division by zero
            if child_min.0 == 0.0 || child_min.1 == 0.0 {
                return vec![Rect::new(cache.rect.x, cache.rect.y, 0.0, 0.0)];
            }

            let child_ratio = child_min.0 / child_min.1;

            let (w, h) = if child_ratio > self.ratio {
                // Keep width, increase height
                (child_min.0, child_min.0 / self.ratio)
            } else {
                // Keep height, increase width
                (child_min.1 * self.ratio, child_min.1)
            };

            let w = w.max(child_min.0);
            let h = h.max(child_min.1);

            let shrunk = cache.rect.anchor(self.anchor, (w, h));
            let space = shrunk.align(self.align, child_min);
            vec![space]
        } else {
            vec![]
        }
    }

    fn get_children(&self) -> Vec<TdIndex> {
        self.child.into_iter().collect()
    }
}

/// Divides the space between two children horizontally, giving the left child a proportion of the
/// space.
///
/// Both nodes receive their minimum size. If a child would not receive its minimum size, the
/// percentage is bypassed.
pub struct HSplit {
    /// The space between the two children.
    pub spacing: f32,

    percent: f32,

    align: (Alignment, Alignment),
    left: Option<TdIndex>,
    right: Option<TdIndex>,
}

impl HSplit {
    /// Creates a new `HSplit` with no children, 0 spacing, 50/50 split, and ([`Begin`], [`Begin`])
    /// alignment.
    ///
    /// [`Begin`]: Alignment::Begin
    pub fn new() -> Self {
        Self {
            spacing: 0.0,
            percent: 0.5,
            align: (Alignment::Begin, Alignment::Begin),
            left: None,
            right: None,
        }
    }

    /// Create two child nodes and bind them to the node.
    ///
    /// # Panics
    /// If there are already child nodes.
    pub fn with_children(
        mut self,
        left: impl UiNode,
        right: impl UiNode,
        tree: &mut UiTree,
    ) -> Self {
        assert!(self.left.is_none() && self.right.is_none());
        self.left = Some(tree.add_node(left));
        self.right = Some(tree.add_node(right));
        self
    }

    /// Creates and binds a child node to the left slot, or the right, if the left is occupied.
    ///
    /// # Panics
    /// If both left and right are set.
    pub fn with_child(mut self, child: impl UiNode, tree: &mut UiTree) -> Self {
        if self.left.is_none() {
            self.left = Some(tree.add_node(child));
        } else if self.right.is_none() {
            self.right = Some(tree.add_node(child));
        } else {
            panic!("Cannot add child when both children are bound");
        }
        self
    }

    /// Set the horizontal and vertical alignment.
    pub fn with_align(mut self, align: (Alignment, Alignment)) -> Self {
        self.align = align;
        self
    }

    /// Set the spacing between the two children
    pub fn with_spacing(mut self, spacing: f32) -> Self {
        self.spacing = spacing;
        self
    }

    /// Set the percentage of space to give to the first child
    ///
    /// # Panics
    /// If the percentage is not between 0.0 and 1.0
    pub fn with_percent(mut self, percent: f32) -> Self {
        assert!(matches!(percent, 0.0..=1.0));
        self.percent = percent;
        self
    }

    /// Get the percentage of space to give to the first child
    pub fn get_percent(&self) -> f32 {
        self.percent
    }

    /// Set the percentage of space to give to the first child
    ///
    /// # Panics
    /// If the percentage is not between 0.0 and 1.0
    pub fn set_percent(&mut self, percent: f32) {
        assert!(matches!(percent, 0.0..=1.0));
        self.percent = percent;
    }

    /// Get the index of the left child
    ///
    /// # Panics
    /// If the left slot is not set
    pub fn get_left_index(&self) -> TdIndex {
        self.left.expect("Left slot not set")
    }

    /// Get the index of the right child
    ///
    /// # Panics
    /// If the right slot is not set
    pub fn get_right_index(&self) -> TdIndex {
        self.right.expect("Right slot not set")
    }
}

impl Default for HSplit {
    fn default() -> Self {
        Self::new()
    }
}

impl UiNode for HSplit {
    fn get_align(&self) -> (Alignment, Alignment) {
        self.align
    }

    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment) {
        (&mut self.align.0, &mut self.align.1)
    }

    fn calculate_min_size(&self, tree: &UiTree) -> (f32, f32) {
        if let Some((left, right)) = self.left.zip(self.right) {
            let left_min = tree
                .get_cache(left)
                .expect("Left child not in cache")
                .min_size;
            let right_min = tree
                .get_cache(right)
                .expect("Right child not in cache")
                .min_size;
            (
                left_min.0 + right_min.0 + self.spacing,
                left_min.1.max(right_min.1),
            )
        } else {
            (0.0, 0.0)
        }
    }

    fn calculate_rects(&self, cache: &NodeCache, tree: &UiTree) -> Vec<Rect> {
        if let Some((left, right)) = self.left.zip(self.right) {
            let left_min = tree
                .get_cache(left)
                .expect("Left child not in cache")
                .min_size;
            let right_min = tree
                .get_cache(right)
                .expect("Right child not in cache")
                .min_size;

            let div_left = (cache.rect.w - self.spacing) * self.percent;

            // If there is not enough space for the left child, give it enough space and give the
            // right child the rest.

            if div_left < left_min.0 {
                let div_right = cache.rect.w - left_min.0 - self.spacing;
                let x_right = cache.rect.x + left_min.0 + self.spacing;

                let left_space = Rect::new(cache.rect.x, cache.rect.y, left_min.0, cache.rect.h)
                    .align(self.align, left_min);
                let right_space = Rect::new(x_right, cache.rect.y, div_right, cache.rect.h)
                    .align(self.align, right_min);
                return vec![left_space, right_space];
            }

            let div_right = cache.rect.w - div_left - self.spacing;

            // If there is not enough space for the right child, give it enough space and give the
            // left child the rest.

            if div_right < right_min.0 {
                let div_left = cache.rect.w - right_min.0 - self.spacing;
                let x_right = cache.rect.x + div_left + self.spacing;

                let left_space = Rect::new(cache.rect.x, cache.rect.y, div_left, cache.rect.h)
                    .align(self.align, left_min);
                let right_space = Rect::new(x_right, cache.rect.y, right_min.0, cache.rect.h)
                    .align(self.align, right_min);
                return vec![left_space, right_space];
            }

            let x_right = cache.rect.x + div_left + self.spacing;

            // If there is enough space for both children, use the percentage to divide the space.

            let left_space = Rect::new(cache.rect.x, cache.rect.y, div_left, cache.rect.h)
                .align(self.align, left_min);
            let right_space = Rect::new(x_right, cache.rect.y, div_right, cache.rect.h)
                .align(self.align, right_min);
            vec![left_space, right_space]
        } else {
            vec![]
        }
    }

    fn get_children(&self) -> Vec<TdIndex> {
        if let Some((left, right)) = self.left.zip(self.right) {
            vec![left, right]
        } else {
            vec![]
        }
    }
}

/// Divides the space between two children vertically, giving the top child a proportion of the
/// space.
///
/// Both nodes receive their minimum size. If a child would not receive its minimum size, the
/// percentage is bypassed.
pub struct VSplit {
    /// The space between the two children.
    pub spacing: f32,

    percent: f32,

    align: (Alignment, Alignment),
    top: Option<TdIndex>,
    bot: Option<TdIndex>,
}

impl VSplit {
    /// Creates a new `VSplit` with no children, 0 spacing, 50/50 split, and ([`Begin`], [`Begin`])
    /// alignment.
    ///
    /// [`Begin`]: Alignment::Begin
    pub fn new() -> Self {
        Self {
            spacing: 0.0,
            percent: 0.5,
            align: (Alignment::Begin, Alignment::Begin),
            top: None,
            bot: None,
        }
    }

    /// Create two child nodes and bind them to the node.
    ///
    /// # Panics
    /// If there are already child nodes.
    pub fn with_children(
        mut self,
        top: impl UiNode,
        bottom: impl UiNode,
        tree: &mut UiTree,
    ) -> Self {
        assert!(self.top.is_none() && self.bot.is_none());
        self.top = Some(tree.add_node(top));
        self.bot = Some(tree.add_node(bottom));
        self
    }

    /// Creates and binds a child node to the top slot, or the bottom, if the top is occupied.
    ///
    /// # Panics
    /// If both left and right are set.
    pub fn with_child(mut self, child: impl UiNode, tree: &mut UiTree) -> Self {
        if self.top.is_none() {
            self.top = Some(tree.add_node(child));
        } else if self.bot.is_none() {
            self.bot = Some(tree.add_node(child));
        } else {
            panic!("Cannot add child when both children are bound");
        }
        self
    }

    /// Set the horizontal and vertical alignment.
    pub fn with_align(mut self, align: (Alignment, Alignment)) -> Self {
        self.align = align;
        self
    }

    /// Set the spacing between the two children.
    pub fn with_spacing(mut self, spacing: f32) -> Self {
        self.spacing = spacing;
        self
    }

    /// Set the percentage of space to give to the first child
    ///
    /// # Panics
    /// If the percentage is not between 0.0 and 1.0
    pub fn with_percent(mut self, percent: f32) -> Self {
        assert!(matches!(percent, 0.0..=1.0));
        self.percent = percent;
        self
    }

    /// Get the percentage of space to give to the first child.
    pub fn get_percent(&self) -> f32 {
        self.percent
    }

    /// Set the percentage of space to give to the first child
    ///
    /// # Panics
    /// If the percentage is not between 0.0 and 1.0
    pub fn set_percent(&mut self, percent: f32) {
        assert!(matches!(percent, 0.0..=1.0));
        self.percent = percent;
    }

    /// Returns the tree index of the top node.
    ///
    /// # Panics
    /// If the top node is not set.
    pub fn get_top_index(&self) -> TdIndex {
        self.top.expect("Top slot not set")
    }

    /// Returns the tree index of the bottom node.
    ///
    /// # Panics
    /// If the top node is not set.
    pub fn get_bottom_index(&self) -> TdIndex {
        self.bot.expect("Bottom slot not set")
    }
}

impl Default for VSplit {
    fn default() -> Self {
        Self::new()
    }
}

impl UiNode for VSplit {
    fn get_align(&self) -> (Alignment, Alignment) {
        self.align
    }

    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment) {
        (&mut self.align.0, &mut self.align.1)
    }

    fn calculate_min_size(&self, tree: &UiTree) -> (f32, f32) {
        if let Some((top, bot)) = self.top.zip(self.bot) {
            let top_min = tree
                .get_cache(top)
                .expect("Top child not in cache")
                .min_size;
            let bot_min = tree
                .get_cache(bot)
                .expect("Bottom child not in cache")
                .min_size;
            (
                top_min.0.max(bot_min.0),
                top_min.1 + bot_min.1 + self.spacing,
            )
        } else {
            (0.0, 0.0)
        }
    }

    fn calculate_rects(&self, cache: &NodeCache, tree: &UiTree) -> Vec<Rect> {
        if let Some((top, bot)) = self.top.zip(self.bot) {
            let top_min = tree
                .get_cache(top)
                .expect("Top child not in cache")
                .min_size;
            let bot_min = tree
                .get_cache(bot)
                .expect("Bottom child not in cache")
                .min_size;

            let div_top = (cache.rect.h - self.spacing) * self.percent;

            if div_top < top_min.1 {
                let div_bot = cache.rect.h - top_min.1 - self.spacing;
                let y_bot = cache.rect.y + top_min.1 + self.spacing;

                let top_space = Rect::new(cache.rect.x, cache.rect.y, cache.rect.w, top_min.1)
                    .align(self.align, top_min);
                let bot_space = Rect::new(cache.rect.x, y_bot, cache.rect.w, div_bot)
                    .align(self.align, bot_min);
                return vec![top_space, bot_space];
            }

            let div_bot = cache.rect.h - div_top - self.spacing;

            if div_bot < bot_min.1 {
                let div_top = cache.rect.h - bot_min.1 - self.spacing;
                let y_bot = cache.rect.y + div_top + self.spacing;

                let top_space = Rect::new(cache.rect.x, cache.rect.y, cache.rect.w, div_top)
                    .align(self.align, top_min);
                let bot_space = Rect::new(cache.rect.x, y_bot, cache.rect.w, bot_min.1)
                    .align(self.align, bot_min);
                return vec![top_space, bot_space];
            }

            let y_bot = cache.rect.y + div_top + self.spacing;

            let top_space = Rect::new(cache.rect.x, cache.rect.y, cache.rect.w, div_top)
                .align(self.align, top_min);
            let bot_space =
                Rect::new(cache.rect.x, y_bot, cache.rect.w, div_bot).align(self.align, bot_min);
            vec![top_space, bot_space]
        } else {
            vec![]
        }
    }

    fn get_children(&self) -> Vec<TdIndex> {
        if let Some((top, bot)) = self.top.zip(self.bot) {
            vec![top, bot]
        } else {
            vec![]
        }
    }
}

/// Assigns a percentage of the available space to the child.
///
/// If `strict` is not enabled, and there is not enough space to maintain the percentage, it will be
/// bypassed. If `strict` is enabled, the minimum size will grow to ensure the percentage is always
/// maintained.
///
/// Once the child is added, it cannot be removed.
pub struct Percent {
    /// If `true`, the minimum size grows to ensure the percentage is always maintained.
    pub strict: bool,
    percent: (f32, f32),
    pub anchor: (Anchor, Anchor),

    align: (Alignment, Alignment),
    child: Option<TdIndex>,
}

impl Percent {
    /// Creates a new `Percent` with no child, no alignment, default anchoring, a (100%, 100%)
    /// percent, `strict` disabled, and ([`Begin`], [`Begin`]) alignment.
    ///
    /// [`Begin`]: Alignment::Begin
    pub fn new() -> Self {
        Self {
            strict: false,
            percent: (1.0, 1.0),
            anchor: (Anchor::Begin, Anchor::Begin),
            align: (Alignment::Begin, Alignment::Begin),
            child: None,
        }
    }

    /// Set the child of the `Percent`.
    ///
    /// # Panics
    /// If the child is already set
    pub fn with_child(mut self, child: impl UiNode, tree: &mut UiTree) -> Self {
        assert!(self.child.is_none());
        self.child = Some(tree.add_node(child));
        self
    }

    /// Set the horizontal and vertical alignment.
    pub fn with_align(mut self, align: (Alignment, Alignment)) -> Self {
        self.align = align;
        self
    }

    /// Set the horizontal and vertical anchor.
    pub fn with_anchor(mut self, anchor: (Anchor, Anchor)) -> Self {
        self.anchor = anchor;
        self
    }

    /// Set the percentage of space to give to the first child
    ///
    /// # Panics
    /// If the percentage is not between 0.0 and 1.0 or if `strict` is enabled and the percent is 0
    pub fn with_percent(mut self, percent: (f32, f32)) -> Self {
        assert!(matches!(percent, (0.0..=1.0, 0.0..=1.0)));
        if self.strict {
            assert!(
                percent.0 > 0.0 && percent.1 > 0.0,
                "Percent must be greater than 0 when strict is enabled"
            );
        }
        self.percent = percent;
        self
    }

    /// Set whether the minimum size grows to ensure the percentage is always maintained.
    ///
    /// # Panics
    /// If `strict` is enabled and the percent is 0
    pub fn with_strict(mut self, strict: bool) -> Self {
        if strict {
            assert!(
                self.percent.0 > 0.0 && self.percent.1 > 0.0,
                "Percent must be greater than 0 when strict is enabled"
            );
        }
        self.strict = strict;
        self
    }

    /// Set the percentage of space to give to the first child
    ///
    /// # Panics
    /// If the percentage is not between 0.0 and 1.0 or if `strict` is enabled and the percent is 0
    pub fn set_percent(&mut self, percent: (f32, f32)) {
        assert!(matches!(percent, (0.0..=1.0, 0.0..=1.0)));
        if self.strict {
            assert!(
                percent.0 > 0.0 && percent.1 > 0.0,
                "Percent must be greater than 0 when strict is enabled"
            );
        }
        self.percent = percent;
    }

    /// Set whether the minimum size grows to ensure the percentage is always maintained.
    ///
    /// # Panics
    /// If `strict` is enabled and the percent is 0
    pub fn set_strict(&mut self, strict: bool) {
        if strict {
            assert!(
                self.percent.0 > 0.0 && self.percent.1 > 0.0,
                "Percent must be greater than 0 when strict is enabled"
            );
        }
        self.strict = strict;
    }

    /// Get the percentage of space to give to the first child
    pub fn get_percent(&self) -> (f32, f32) {
        self.percent
    }

    /// Get the tree index of the child.
    pub fn get_child(&self) -> Option<TdIndex> {
        self.child
    }
}

impl Default for Percent {
    fn default() -> Self {
        Self::new()
    }
}

impl UiNode for Percent {
    fn get_align(&self) -> (Alignment, Alignment) {
        self.align
    }

    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment) {
        (&mut self.align.0, &mut self.align.1)
    }

    fn calculate_min_size(&self, tree: &UiTree) -> (f32, f32) {
        if let Some(child) = self.child {
            let child_min = tree.get_cache(child).expect("Child not in cache").min_size;
            if self.strict {
                // If percent is 50%, the minimum size doubles.
                (child_min.0 / self.percent.0, child_min.1 / self.percent.1)
            } else {
                child_min
            }
        } else {
            (0.0, 0.0)
        }
    }

    fn calculate_rects(&self, cache: &NodeCache, tree: &UiTree) -> Vec<Rect> {
        if let Some(child) = self.child {
            let child_min = tree.get_cache(child).expect("Child not in cache").min_size;
            // Child gets enough space but can get up to the percent.
            let w = child_min.0.max(cache.rect.w * self.percent.0);
            let h = child_min.1.max(cache.rect.h * self.percent.1);

            let shrunk = cache.rect.anchor(self.anchor, (w, h));
            let space = shrunk.align(self.align, child_min);
            vec![space]
        } else {
            vec![]
        }
    }

    fn get_children(&self) -> Vec<TdIndex> {
        if let Some(child) = self.child {
            vec![child]
        } else {
            vec![]
        }
    }
}