layuit 3.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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
//! # Layuit
//!
//! A renderer-agnostic UI layout system. Layuit handles computing the size and position of various
//! [`UiNode`]s in a [`UiTree`]. Layuit does not handle rendering, but provides tools to define a
//! renderer.
//!
//! Layuit provides several organizational nodes such as [`HStack`] and [`Margin`], but allows users
//! to create their own nodes.
//!
//! ## Core concepts
//!
//! - **[`UiTree`]**: Owns the [`UiNode`]s and layout information in an arena and handles
//!   computation and access.
//! - **[`UiNode`]**: A trait implemented by all UI nodes, containing alignment and any number of
//!   children.
//! - **[`Rect`]**: A rectangle in space, represented with `f32` coordinates.
//! - **[`Alignment`]**: An alignment primarily used for determining node placement.
//! - **[`NodeIndex`]**: A tree index, used to access the [`UiNode`]s in a [`UiTree`], but does not
//!   have "ownership".
//! - **[`OwnedIndex`]**: A non-duplicable tree index, used to refer to children, with "ownership".
//!
//! ## Layout process
//!
//! Layout runs in two passes, when [`UiTree::calculate_layout`] is called:
//!
//! 1. **Bottom-up: minimum size.** Children are visited before their parent. Each node computes its
//!    minimum size based on its children through [`calculate_min_size`] and stores it in its
//!    [`NodeCache::min_size`].
//!
//! 2. **Top-down: rectangles.** Starting from the root, each node computes the position and size of
//!    its immediate children through [`calculate_rects`]. Each child then uses its restricted
//!    [`Rect`] to do the same for its children. The [`NodeCache::rect`] field is populated with
//!    the resulting [`Rect`]s.
//!
//! The majority of the layout process can be thought of as drawing boxes on a sheet of paper. Boxes
//! cannot normally cross, but are allowed to touch. Several nodes change that behavior:
//!
//! - [`Overlap`] intentionally allows the boxes of children to overlap each other, as long as they
//!   stay inside.
//!
//! - [`Clip`] allows a box to be bigger than that of its parent, but requires the box's viewable
//!   area to be constrained to that of the `Clip` by the renderer.
//!
//! - [`Hider`] allows a child to be completely excluded from the layout process, but requires the
//!   renderer to ignore it.
//!
//! ## Caveats
//!
//! **The cache is stale before [`UiTree::calculate_layout`] is called**, and becomes stale if
//! children are added, removed, moved, or otherwise changed. The cache always produces valid
//! results, but they may be out of date or set to 0.
//!
//! **Minimum size is a practice, not a requirement**. When implementing custom nodes, be wary of
//! ensuring each node's minimum size is enforced. This can easily become a problem if the space
//! required by the entire tree is smaller than the one provided to [`UiTree::calculate_layout`].
//!
//! **No threads or async.** Due to using `dyn UiNode` and [`UiNode`] not requiring `Send + Sync`,
//! [`UiTree`] and [`PartialTree`] are `!Send + !Sync`.
//!
//! ## Implementing custom nodes
//!
//! Custom nodes are essential to using Layuit. Without them, no meaningful UI can be rendered.
//! However, it is important to ensure you follow the rules:
//!
//! 1. **Children must be accurately reported.** Failure to report children will result in them not
//!    being updated during [`UiTree::calculate_layout`] or removed during [`UiTree::remove_node`].
//!
//! 2. **Minimum size must be correctly calculated.** Under-representing the minimum size can and
//!    often will result in nodes overflowing into each other.
//!
//! 3. **Rectangles must be properly assigned.** Similar to #2, it is the responsibility of the
//!    *parent* node to ensure that each node get both enough space and not too much. Failing to do
//!    so will result in nodes overlapping.
//!
//! Probably the most important custom node is the `Label`:
//!
//! ```rust
//! use layuit::{Alignment, UiTree, UiNode};
//!
//! pub struct Label {
//!     text: String,
//!     cached_size: (f32, f32),
//!
//!     align: (Alignment, Alignment),
//! }
//!
//! /* Label methods and constructors... */
//!
//! impl UiNode for Label {
//!     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) {
//!         self.cached_size
//!     }
//!
//!     // calculate_rects and get_children are omitted for leaf nodes
//! }
//! ```
//!
//! However, you are not restricted to just leaf nodes. You can create containers.
//!
//! ## Using the `ui!` macro
//!
//! The [`ui!`] macro is a convenience for creating trees with a simple syntax, avoiding rewriting
//! `.with_align((...))` and `.with_child(...)` in every node, for every child. It does come with
//! the limitation that you cannot create your entire tree this way; your root node must be created
//! manually.
//!
//! Additionally, you can create variables outside the macro and assign the indices of nodes created
//! by the macro to them.
//!
//! ```rust
//! use layuit::padding::{Spacer, Minimum};
//! use layuit::stack::HStack;
//!
//! let spacer3;
//! let mut tree = layuit::ui!(
//!     %%,
//!     +|+ HStack::new() => [
//!         -|< Spacer::sized((10.0, 10.0)),
//!         -|- Minimum::new().with_min((20.0, 20.0)) => [
//!             -|- Spacer::sized((10.0, 10.0))
//!         ],
//!         spacer3 = -|> Spacer::sized((10.0, 10.0))
//!     ]
//! );
//!
//! tree
//!     .get_cast_mut::<Spacer>(spacer3)
//!     .unwrap()
//!     .set_size((20.0, 20.0));
//!
//!
//! // HStack (Full, Full)
//! // ├─ Spacer (N/A, Begin)
//! // ├─ Minimum (N/A, Center)
//! // │  └─ Spacer (Center, Center)
//! // └─ Spacer (N/A, End) = spacer3
//! ```
//!
//! ## Provided nodes
//!
//! - [`HStack`] - Horizontal arrangement
//! - [`VStack`] - Vertical arrangement
//! - [`Overlap`] - Independent arrangement of children
//! - [`Margin`] - Adds padding to a child
//! - [`Minimum`] - Creates a minimum size constraint for precise control
//! - [`Spacer`] - A leaf node with configurable empty space
//! - [`Clip`] - Allows a child to outgrow the node with the assumption that the renderer will
//!   clip it, and enables a scroll offset to be applied if the child is larger.
//! - [`Hider`] - Allows a child's visibility to be controlled. An invisible node has no minimum
//!   size and should not be attempted to be rendered.
//! - [`Selector`] - Selects a single child node to be visible at a time.
//! - [`AspectRatio`] - Maintains a horizontal:vertical ratio.
//! - [`HSplit`] - Horizontal split between two children.
//! - [`VSplit`] - Vertical split between two children.
//! - [`Percent`] - Maintains a percentage of space for a child.
//! - [`HEqual`] - Horizontal arrangement with each child getting equal space.
//! - [`VEqual`] - Vertical arrangement with each child getting equal space.
//! - [`Grid`] - 2D grid of children.
//! - [`Clamp`] - Constrains a child to a maximum size.
//! - [`HSplit3`] - Horizontal split between two children, with a third child separating the two.
//! - [`VSplit3`] - Vertical split between two children, with a third child separating the two.
//!
//! [`calculate_min_size`]: UiNode::calculate_min_size
//! [`calculate_rects`]: UiNode::calculate_rects
//! [`HStack`]: stack::HStack
//! [`VStack`]: stack::VStack
//! [`Overlap`]: overlap::Overlap
//! [`Margin`]: padding::Margin
//! [`Minimum`]: padding::Minimum
//! [`Spacer`]: padding::Spacer
//! [`Clip`]: clip::Clip
//! [`Hider`]: visibility::Hider
//! [`Selector`]: visibility::Selector
//! [`AspectRatio`]: proportion::AspectRatio
//! [`HSplit`]: proportion::HSplit
//! [`VSplit`]: proportion::VSplit
//! [`Percent`]: proportion::Percent
//! [`HEqual`]: grid::HEqual
//! [`VEqual`]: grid::VEqual
//! [`Grid`]: grid::Grid
//! [`Clamp`]: limit::Clamp
//! [`HSplit3`]: split3::HSplit3
//! [`VSplit3`]: split3::VSplit3
//!
//! [`mpsc`]: https://doc.rust-lang.org/nightly/std/sync/mpsc/index.html

#![warn(clippy::all, missing_docs)]
#![deny(clippy::unwrap_used)]

use std::collections::{HashMap, VecDeque};

use thunderdome::Arena;

pub mod clip;
pub mod grid;
pub mod limit;
pub mod overlap;
pub mod padding;
pub mod prelude;
pub mod proportion;
pub mod split3;
pub mod stack;
pub mod visibility;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
/// An index to a node in a tree.
pub struct NodeIndex(thunderdome::Index);

#[derive(Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
/// An index to a node in a tree, with the caveat that it represents a node not already in the
/// hierarchy.
///
/// To prevent tree malformation, this index is produced by [`UiTree::add_node`] and cannot be
/// duplicated.
pub struct OwnedIndex(thunderdome::Index);

impl OwnedIndex {
    /// Clones the index as a [`NodeIndex`].
    pub fn shareable(&self) -> NodeIndex {
        NodeIndex(self.0)
    }
}

impl From<OwnedIndex> for NodeIndex {
    fn from(value: OwnedIndex) -> Self {
        NodeIndex(value.0)
    }
}

impl From<&OwnedIndex> for NodeIndex {
    fn from(value: &OwnedIndex) -> Self {
        NodeIndex(value.0)
    }
}

impl std::borrow::Borrow<NodeIndex> for OwnedIndex {
    fn borrow(&self) -> &NodeIndex {
        // # Safety
        // repr(transparent) used, and same definition as NodeIndex
        unsafe { std::mem::transmute(self) }
    }
}

impl From<thunderdome::Index> for NodeIndex {
    fn from(value: thunderdome::Index) -> Self {
        NodeIndex(value)
    }
}

impl From<NodeIndex> for thunderdome::Index {
    fn from(value: NodeIndex) -> thunderdome::Index {
        value.0
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
/// An alignment of a node within its parent.
///
/// `Begin`, `Center`, and `End` cause a node to shrink to its minimum size in to that position.
/// `Full` causes a node to occupy all space it is given. For example, to shrink to the left-middle,
/// use (`Begin`, `Center`). To fill horizontally and shrink down, use (`Full`, `End`).
///
/// The default is `Full`.
pub enum Alignment {
    /// Shrink to the left or top.
    Begin,
    /// Shrink to the center.
    Center,
    /// Shrink to the right or bottom.
    End,
    #[default]
    /// Grow to the entire available space.
    Full,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
/// Shrinking direction of a constrained rectangle.
///
/// While similar to [`Alignment`], `Anchor` does not support [`Full`], since that would be growing
/// instead of shrinking.
///
/// It is also noteworthy that the default is [`Center`], not [`Alignment::Begin`].
///
/// [`Alignment`]: Alignment
/// [`Center`]: Anchor::Center
/// [`Full`]: Alignment::Full
pub enum Anchor {
    /// Top left
    Begin,
    #[default]
    /// Center
    Center,
    /// Bottom right
    End,
}

#[derive(Copy, Clone, Debug, PartialEq, Default)]
/// A rectangle in space, represented with `f32` coordinates, representing unitless position and
/// size.
///
/// A rectangle is "valid" if *both* its width and height are non-negative. 0 is valid. A rectangle
/// is "empty" if *either* its width or height is 0. An invalid rectangle may not be empty. A
/// rectangle is "zero" if *both* its width and height are 0.
pub struct Rect {
    /// The x position of the top left corner.
    pub x: f32,
    /// The y position of the top left corner.
    pub y: f32,
    /// The width of the rectangle.
    pub w: f32,
    /// The height of the rectangle.
    pub h: f32,
}

impl Rect {
    /// Create a new rectangle with the given dimensions and position.
    pub fn new(x: f32, y: f32, w: f32, h: f32) -> Self {
        Self { x, y, w, h }
    }

    /// Identical to [`new`], but returns `None` if either the width or height is negative.
    ///
    /// [`new`]: Self::new
    pub fn new_checked(x: f32, y: f32, w: f32, h: f32) -> Option<Self> {
        if w >= 0.0 && h >= 0.0 {
            Some(Self::new(x, y, w, h))
        } else {
            None
        }
    }

    /// Returns `false` if *either* the width or height is negative. Otherwise, returns `true`.
    pub fn is_valid(&self) -> bool {
        self.w >= 0.0 && self.h >= 0.0
    }

    /// Returns `true` if the rectangle has zero width *or* zero height.
    pub fn is_empty(&self) -> bool {
        self.w == 0.0 || self.h == 0.0
    }

    /// Returns `true` if the rectangle has zero width *and* zero height.
    pub fn is_zero(&self) -> bool {
        self.w == 0.0 && self.h == 0.0
    }

    /// Returns the width and height of the rectangle.
    pub fn get_size(&self) -> (f32, f32) {
        (self.w, self.h)
    }

    /// Set the width and height of the rectangle.
    pub fn with_size(mut self, w: f32, h: f32) -> Self {
        self.w = w;
        self.h = h;
        self
    }

    /// Shrink the width of the rectangle by the given amount toward the **left**.
    pub fn shrink_begin_x(mut self, amount: f32) -> Self {
        self.w -= amount;
        self
    }

    /// Shrink the width of the rectangle by the given amount toward the **right**.
    pub fn shrink_end_x(mut self, amount: f32) -> Self {
        self.w -= amount;
        self.x += amount;
        self
    }

    /// Shrink the **width** of the rectangle by the given amount toward the **center**.
    pub fn shrink_center_x(mut self, amount: f32) -> Self {
        self.w -= amount;
        self.x += amount * 0.5;
        self
    }

    /// Shrink the height of the rectangle by the given amount toward the **top**.
    pub fn shrink_begin_y(mut self, amount: f32) -> Self {
        self.h -= amount;
        self
    }

    /// Shrink the height of the rectangle by the given amount toward the **bottom**.
    pub fn shrink_end_y(mut self, amount: f32) -> Self {
        self.h -= amount;
        self.y += amount;
        self
    }

    /// Shrink the **height** of the rectangle by the given amount toward the **middle**.
    pub fn shrink_center_y(mut self, amount: f32) -> Self {
        self.h -= amount;
        self.y += amount * 0.5;
        self
    }

    /// Create a contained rectangle aligned within `self`.
    ///
    /// See [`Alignment`] for what each alignment does.
    ///
    /// If self` rectangle is smaller than `min` and a shrinking mode is used, the contained
    /// rectangle will *grow* in the opposite direction it would have (eg. End would grow to the
    /// top-left).
    ///
    /// Example:
    ///
    /// ```rust
    /// use layuit::{Rect, Alignment};
    ///
    /// let rect = Rect::new(0.0, 0.0, 100.0, 100.0);
    ///
    /// let contained_center = rect.align((Alignment::Center, Alignment::Center), (50.0, 50.0));
    /// assert_eq!(contained_center, Rect::new(25.0, 25.0, 50.0, 50.0));
    ///
    /// let contained_top_right = rect.align((Alignment::End, Alignment::Begin), (50.0, 50.0));
    /// assert_eq!(contained_top_right, Rect::new(50.0, 0.0, 50.0, 50.0));
    ///
    /// let contained_equal = rect.align((Alignment::Full, Alignment::Full), (50.0, 50.0));
    /// assert_eq!(contained_equal, Rect::new(0.0, 0.0, 100.0, 100.0));
    /// ```
    pub fn align(mut self, align: (Alignment, Alignment), min: (f32, f32)) -> Self {
        self = match align.0 {
            Alignment::Begin => self.shrink_begin_x(self.w - min.0),
            Alignment::Center => self.shrink_center_x(self.w - min.0),
            Alignment::End => self.shrink_end_x(self.w - min.0),
            Alignment::Full => self,
        };

        match align.1 {
            Alignment::Begin => self.shrink_begin_y(self.h - min.1),
            Alignment::Center => self.shrink_center_y(self.h - min.1),
            Alignment::End => self.shrink_end_y(self.h - min.1),
            Alignment::Full => self,
        }
    }

    /// Create a contained rectangle aligned within `self`, always shrinking to the given size.
    ///
    /// Nearly identical behavior to [`align`], but uses [`Anchor`] instead of [`Alignment`],
    /// preventing use of [`Alignment::Full`].
    ///
    /// If self` rectangle is smaller than `min`, the contained rectangle will *grow* in the
    /// opposite direction it would have (eg. End would grow to the top-left).
    ///
    /// [`align`]: Self::align
    pub fn anchor(mut self, anchor: (Anchor, Anchor), size: (f32, f32)) -> Self {
        self = match anchor.0 {
            Anchor::Begin => self.shrink_begin_x(self.w - size.0),
            Anchor::Center => self.shrink_center_x(self.w - size.0),
            Anchor::End => self.shrink_end_x(self.w - size.0),
        };

        match anchor.1 {
            Anchor::Begin => self.shrink_begin_y(self.h - size.1),
            Anchor::Center => self.shrink_center_y(self.h - size.1),
            Anchor::End => self.shrink_end_y(self.h - size.1),
        }
    }

    /// Returns `true` if `point` is contained within `self`. It is inclusive of the begin edges,
    /// but exclusive of the end edges.
    pub fn contains(&self, point: (f32, f32)) -> bool {
        point.0 >= self.x
            && point.0 < self.x + self.w
            && point.1 >= self.y
            && point.1 < self.y + self.h
    }

    /// Returns `true` if `rect` is contained entirely within `self`.
    pub fn contains_rect(&self, rect: Rect) -> bool {
        rect.x >= self.x
            && rect.x + rect.w <= self.x + self.w
            && rect.y >= self.y
            && rect.y + rect.h <= self.y + self.h
    }

    /// Returns the area included by both `self` and `other`.
    ///
    /// Always returns a valid rectangle (both >=0), but it may be empty (either =0) if the two
    /// rectangles do not overlap.
    pub fn intersect(self, other: Self) -> Self {
        let x1 = self.x.max(other.x);
        let y1 = self.y.max(other.y);

        let x2 = (self.x + self.w).min(other.x + other.w);
        let y2 = (self.y + self.h).min(other.y + other.h);

        Self::new(x1, y1, (x2 - x1).max(0.0), (y2 - y1).max(0.0))
    }
}

#[derive(Copy, Clone, Debug, Default)]
/// Cached layout information for a node.
pub struct NodeCache {
    /// The stored minimum size of the node.
    pub min_size: (f32, f32),
    /// The stored rect of the node.
    pub rect: Rect,
}

/// Basic functionality for a UI node.
pub trait UiNode: std::any::Any {
    /// Get the alignment of the node.
    fn get_align(&self) -> (Alignment, Alignment);
    /// Get a mutable reference to the alignment of the node.
    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment);

    /// Calculate the minimum size of the node.
    ///
    /// It is the parent's responsibility to ensure the minimum size of all children is met.
    fn calculate_min_size(&self, tree: &UiTree) -> (f32, f32);

    /// Recalculate the position and size of child nodes, in the same order and count as
    /// [`get_visible_children`].
    ///
    /// Parents control the space allocated to their children. It is not the child's responsibility
    /// to manage its own rects. The parent **must** apply the correct alignment to all children,
    /// no matter what kind of container they are.
    ///
    /// A good mental model is to draw boxes on a piece of paper. Each node is a box. A box must be
    /// entirely contained within another. With exceptions, no line can cross through any other.
    /// They may touch and run parallel, but not cross.
    ///
    /// This is optional, and should only be implemented if the node has a child/children.
    ///
    /// [`get_visible_children`]: Self::get_visible_children
    fn calculate_rects(&self, cache: &NodeCache, tree: &UiTree) -> Vec<Rect> {
        let _ = (cache, tree);
        vec![]
    }

    /// Get all children of the node, if applicable.
    fn get_children(&self) -> Vec<NodeIndex> {
        vec![]
    }

    /// Get all **visible** children of the node, if applicable.
    ///
    /// If a node has children, but does not control visibility, this defaults to the result of
    /// [`get_children`].
    ///
    /// This does not have to have the same order as [`get_children`], but it must be a subset.
    ///
    /// [`get_children`]: Self::get_children
    fn get_visible_children(&self) -> Vec<NodeIndex> {
        self.get_children()
    }
}

impl dyn UiNode {
    /// Downcast a reference to a specific type. See [`Any::downcast_ref`].
    ///
    /// [`Any::downcast_ref`]: https://doc.rust-lang.org/std/any/trait.Any.html#method.downcast_ref
    pub fn downcast_ref<T: UiNode>(&self) -> Option<&T> {
        (self as &dyn std::any::Any).downcast_ref()
    }

    /// Downcast a mutable reference to a specific type. See [`Any::downcast_mut`].
    ///
    /// [`Any::downcast_mut`]: https://doc.rust-lang.org/std/any/trait.Any.html#method.downcast_mut
    pub fn downcast_mut<T: UiNode>(&mut self) -> Option<&mut T> {
        (self as &mut dyn std::any::Any).downcast_mut()
    }
}

/// A walker for a UI tree.
pub trait UiWalker {
    /// Called when a node is visited, before its children.
    fn enter(&mut self, node: &mut dyn UiNode, rect: Rect, index: NodeIndex);

    /// Called after all children of a node have been visited, including if it has no
    /// children.
    fn leave(&mut self, node: &mut dyn UiNode, rect: Rect, index: NodeIndex);
}

/// Walks a UI tree, but only in cases where a point is within the node's rect.
pub trait PointTester {
    /// Called on every visited node. Should return `true` to cancel the walk.
    fn accept(&self, p: (f32, f32), node: &dyn UiNode, rect: Rect, index: NodeIndex) -> bool;
}

#[derive(Default)]
/// Settings for [`calculate_layout_ex`]
///
/// [`calculate_layout_ex`]: UiTree::calculate_layout_ex
pub struct LayoutConfig {
    /// If `true`, applies the root node's alignment instead of ignoring it.
    pub align_root: bool,
    /// If `true`, ensures that the minimum size of the root node is met, even if that would exceed
    /// the provided rect.
    pub force_good: bool,
}

/// A tree of UI nodes, stored as an arena.
pub struct UiTree {
    root: thunderdome::Index,
    arena: Arena<Box<dyn UiNode>>,
    cache: HashMap<thunderdome::Index, NodeCache>,
}

impl UiTree {
    /// Create a new UI tree with the given root node.
    pub fn new(root: impl UiNode) -> Self {
        let mut arena = Arena::new();
        let index = arena.insert(Box::new(root) as Box<dyn UiNode>);
        Self {
            root: index,
            arena,
            cache: HashMap::new(),
        }
    }

    /// Add a node to the arena.
    ///
    /// This produces an [`OwnedIndex`] that is used to ensure exactly one node holds parental
    /// "ownership" of the node in the hierarchy.
    pub fn add_node(&mut self, node: impl UiNode) -> OwnedIndex {
        let index = self.arena.insert(Box::new(node) as Box<dyn UiNode>);
        self.cache.insert(index, Default::default());
        OwnedIndex(index)
    }

    /// Remove a node and all of its children from the arena.
    ///
    /// # Panics
    /// If the root node is removed.
    pub fn remove_node(&mut self, index: OwnedIndex) {
        assert_ne!(index.0, self.root, "Root node cannot be removed");

        let mut queue: VecDeque<_> = self.arena[index.0].get_children().into();
        while let Some(child) = queue.pop_front() {
            queue.extend(self.arena[child.0].get_children());
            self.arena.remove(child.0);
            self.cache.remove(&child.0);
        }
        self.arena.remove(index.0);
        self.cache.remove(&index.0);
    }

    /// Get a reference to the cached layout information for a node.
    pub fn get_cache(&self, index: NodeIndex) -> Option<&NodeCache> {
        self.cache.get(&index.0)
    }

    /// Get a reference to a node.
    pub fn get_node(&self, index: NodeIndex) -> Option<&dyn UiNode> {
        self.arena.get(index.0).map(|node| &**node)
    }

    /// Get a mutable reference to a node.
    pub fn get_node_mut(&mut self, index: NodeIndex) -> Option<&mut dyn UiNode> {
        self.arena.get_mut(index.0).map(|node| &mut **node)
    }

    /// Get a reference to a node, performing a downcast at the same time.
    pub fn get_cast<T: UiNode>(&self, index: NodeIndex) -> Option<&T> {
        self.get_node(index).and_then(|node| node.downcast_ref())
    }

    /// Get a mutable reference to a node, performing a downcast at the same time.
    pub fn get_cast_mut<T: UiNode>(&mut self, index: NodeIndex) -> Option<&mut T> {
        self.get_node_mut(index).and_then(|node| node.downcast_mut())
    }

    /// Return the index of the root node.
    pub fn root_index(&self) -> NodeIndex {
        NodeIndex(self.root)
    }

    /// Calculate the layout information for all nodes in the tree. This is equivalent to calling
    /// [`calculate_layout_ex`] with the default configuration.
    ///
    /// Returns `true` if `root_rect` preserves minimum size requirements. If the given
    /// space is too small, `false` is returned, but the cache will still be updated. The root node
    /// will be treated as having ([`Full`], [`Full`]) alignment.
    ///
    /// Nodes that are not visible will be given a minimum size of `(0, 0)`.
    ///
    /// [`calculate_layout_ex`]: Self::calculate_layout_ex
    /// [`Full`]: Alignment::Full
    pub fn calculate_layout(&mut self, root_rect: Rect) -> bool {
        self.calculate_layout_ex(root_rect, Default::default())
    }

    /// Calculate the layout information for all nodes in the tree, with additional control of the
    /// process.
    ///
    /// If `force_good` is `true`, ensures that the minimum size of the root node is met, even if
    /// that would exceed the provided rect. If `false`, returns whether the minimum size of the
    /// root node is met.
    ///
    /// If `align_root` is `true`, applies the root node's alignment instead of ignoring it.
    pub fn calculate_layout_ex(&mut self, root_rect: Rect, config: LayoutConfig) -> bool {
        // Clear cache
        for v in self.cache.values_mut() {
            *v = Default::default();
        }

        // Queue to visit now
        let mut visit_stack = vec![self.root];
        // Queue to visit later
        let mut min_stack = Vec::new();
        while let Some(v) = visit_stack.pop() {
            min_stack.push(v);
            visit_stack.extend(
                self.arena[v]
                    .get_visible_children()
                    .into_iter()
                    .map(|v| v.0),
            );
        }

        for v in min_stack.iter().rev() {
            let min = self.arena[*v].calculate_min_size(self);
            self.cache.entry(*v).or_default().min_size = min;
        }

        let (is_good, root_rect) = if config.force_good {
            let root_min = self.cache[&self.root].min_size;
            (
                true,
                Rect::new(
                    0.0,
                    0.0,
                    root_min.0.max(root_rect.w),
                    root_min.1.max(root_rect.h),
                ),
            )
        } else {
            let is_good = root_rect.w >= self.cache[&self.root].min_size.0
                && root_rect.h >= self.cache[&self.root].min_size.1;

            (is_good, root_rect)
        };

        if config.align_root {
            let align = self.arena[self.root].get_align();
            let min = self.cache[&self.root].min_size;
            self.cache
                .entry(self.root)
                .and_modify(|e| e.rect = root_rect.align(align, min));
        } else {
            self.cache
                .entry(self.root)
                .and_modify(|e| e.rect = root_rect);
        }

        for v in min_stack {
            let rects = self.arena[v].calculate_rects(&self.cache[&v], self);
            for (child, rect) in self.arena[v].get_children().iter().zip(rects) {
                self.cache.entry(child.0).or_default().rect = rect;
            }
        }

        is_good
    }

    /// Walks the entire tree, starting from the root, with the given walker. See
    /// [`walk_node`].
    ///
    /// If `use_visible` is `true`, only nodes that are visible will be visited.
    ///
    /// [`walk_node`]: Self::walk_node
    pub fn walk_tree(&mut self, walker: &mut impl UiWalker, use_visible: bool) {
        self.walk_node(NodeIndex(self.root), walker, use_visible);
    }

    /// Walks a single node and its children, with the given walker.
    ///
    /// First, the walker receives [`enter`] with the node and its cached rect and index.
    /// Then, any and all children are walked in the order returned by
    #[doc = concat!("[`", stringify!(UiNode), "::get_visible_children`]")]
    /// . Finally, the walker receives [`leave`].
    ///
    /// Parents are always visited before their children.
    ///
    /// *Every* call to [`enter`] **will** be matched with a call to [`leave`].
    ///
    /// If `use_visible` is `true`, only nodes that are visible will be visited.
    ///
    /// [`enter`]: UiWalker::enter
    /// [`leave`]: UiWalker::leave
    pub fn walk_node(&mut self, index: NodeIndex, walker: &mut impl UiWalker, use_visible: bool) {
        let rect = self.cache[&index.0].rect;
        walker.enter(self.arena[index.0].as_mut(), rect, index);

        let children = if use_visible {
            self.arena[index.0].get_visible_children()
        } else {
            self.arena[index.0].get_children()
        };
        for child in children {
            self.walk_node(child, walker, use_visible);
        }

        walker.leave(self.arena[index.0].as_mut(), rect, index);
    }

    /// Walks the tree from the root, whitelisting nodes where `point` is contained within the
    /// node's rect.
    ///
    /// Only visible nodes will be visited. Nodes that do not contain `point` will not be visited.
    pub fn point_test(&self, point: (f32, f32), tester: &mut impl PointTester) {
        self.point_test_int(self.root, point, tester);
    }

    /// Walks a single node and its children, whitelisting nodes where `point` is contained within
    /// the node's rect.
    ///
    /// Only visible nodes will be visited. Nodes that do not contain `point` will not be visited.
    pub fn point_test_node(
        &self,
        index: NodeIndex,
        point: (f32, f32),
        tester: &mut impl PointTester,
    ) {
        self.point_test_int(index.0, point, tester);
    }

    fn point_test_int(
        &self,
        index: thunderdome::Index,
        point: (f32, f32),
        tester: &mut impl PointTester,
    ) -> bool {
        let rect = self.cache[&index].rect;
        if !rect.contains(point) {
            return false;
        }

        if tester.accept(point, self.arena[index].as_ref(), rect, NodeIndex(index)) {
            return true;
        }

        let children = self.arena[index].get_children();

        for child in children.into_iter() {
            if self.point_test_int(child.0, point, tester) {
                return true;
            }
        }

        false
    }
}

/// A [`UiTree`] that does not have a full tree structure assigned.
///
/// The API for this struct only contains a method to add a node to the tree and a method to
/// complete the tree. This comes at the benefit of not requiring a root node at
/// construction.
///
/// It is used internally by the [`ui!`] macro, but can also be used manually.
pub struct PartialTree {
    arena: Arena<Box<dyn UiNode>>,
}

impl PartialTree {
    /// Constructs a new empty tree.
    pub fn new() -> Self {
        Self {
            arena: Arena::new(),
        }
    }

    /// Adds a node to the tree and returns its index.
    pub fn add_node(&mut self, node: impl UiNode) -> OwnedIndex {
        OwnedIndex(self.arena.insert(Box::new(node) as Box<dyn UiNode>))
    }

    /// Converts a partial tree into a [`UiTree`], using the given root node.
    ///
    /// The root node is taken as an [`OwnedIndex`], since it cannot have a parent.
    ///
    /// Existing [`NodeIndex`]es are not invalidated.
    pub fn complete(self, root: OwnedIndex) -> UiTree {
        let cache = self
            .arena
            .iter()
            .map(|(id, _)| (id, Default::default()))
            .collect();
        UiTree {
            arena: self.arena,
            root: root.0,
            cache,
        }
    }
}

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

#[macro_export]
/// A macro for making the process of creating a UI subtree easier.
///
/// # Arguments
///
/// The macro takes a tree and a node, recursively appending nodes to the tree. **The returned value
/// is dependent on what type of tree argument is passed.**
///
/// ### Tree
///
/// The tree can be either:
///
/// - `%%`, which creates a new [`UiTree`] and returns it.
///
/// - `%%!`, which creates a new [`PartialTree`] and returns a tuple containing it and the owned
///   index of the new node.
///
/// - Or a mutable reference to a tree, which will return the owned index of the new node. The tree
///   can be either a [`UiTree`] or a [`PartialTree`].
///
/// ### Node
///
/// Nodes are recursively defined by constructing them, modifying them, and adding them to the tree.
///
/// Each node is represented by an optional assignment; two alignment symbols, separated by a `|`; a
/// constructor for the node; and an optional list of children, contained in square brackets after
/// `=>`.
///
/// Ex `binding = +|+ HStack::new() => [ ]`
///
/// The assignment is a *name* followed by `=`, not an expression. The variable with the
/// corresponding name will be assigned the [`NodeIndex`] of the created node.
///
/// The alignment characters are orderer horizontal then vertical, with the following allowed:
///
/// `+` - [`Full`]
///
/// `-` - [`Center`]
///
/// `<` - [`Begin`]
///
/// `>` - [`End`]
///
/// You can also replace a node with `#expr` to use a node that is already present in the tree. It
/// expects a [`OwnedIndex`], not a [`NodeIndex`].
///
/// # Example usage
/// ```rust
/// use layuit::padding::{Spacer, Minimum};
/// use layuit::stack::HStack;
///
/// let minimum;
/// let tree = layuit::ui!(
///     %%,
///     +|+ HStack::new() => [
///         -|< Spacer::sized((10.0, 10.0)),
///         minimum = -|- Minimum::new().with_min((20.0, 20.0)) => [
///             -|- Spacer::sized((10.0, 10.0))
///         ],
///         -|> Spacer::sized((10.0, 10.0))
///     ]
/// );
///
/// // Resulting tree:
/// // HStack (Full, Full)
/// // ├─ Spacer (Center, Begin)
/// // ├─ Minimum (Center, Center) = minimum
/// // │  └─ Spacer (Center, Center)
/// // └─ Spacer (Center, End)
/// ```
///
/// [`Begin`]: crate::Alignment::Begin
/// [`Center`]: crate::Alignment::Center
/// [`End`]: crate::Alignment::End
/// [`Full`]: crate::Alignment::Full
macro_rules! ui {
    ($tree:expr, $($node:tt)*) => {
        $crate::ui!(@@_node $tree;; $($node)*)
    };

    (%%, $($node:tt)*) => {
        {
            let mut tree = $crate::PartialTree::new();
            let root = $crate::ui!(@@_node &mut tree;; $($node)*);
            tree.complete(root)
        }
    };

    (%%!, $($node:tt)*) => {
        {
            let mut tree = $crate::PartialTree::new();
            let root = $crate::ui!(@@_node &mut tree;; $($node)*);
            (tree, root)
        }
    };

    (@@_node $tree:expr;; #$expr:expr) => {
        $expr
    };

    // With binding, no children
    (@@_node $tree:expr;; $binding:ident = $ha:tt | $va:tt $node:expr) => {
        {
            let __node_idx = $tree.add_node($crate::ui!(@@_align $ha | $va $node));
            $binding = __node_idx.shareable();
            __node_idx
        }
    };

    // With binding, with children
    (@@_node $tree:expr;; $binding:ident = $ha:tt | $va:tt $node:expr => [ $($child:tt)* ]) => {
        {
            let __ui_node = $crate::ui!(
                @@_child
                $tree;;
                $crate::ui!(@@_align $ha | $va $node)
                => [ $($child)* ]
            );
            let __node_idx = $tree.add_node(__ui_node);
            $binding = __node_idx.shareable();
            __node_idx
        }
    };

    // Without binding, without children
    (@@_node $tree:expr;; $ha:tt | $va:tt $node:expr) => {
        {
            let __node_idx = $tree.add_node($crate::ui!(@@_align $ha | $va $node));
            __node_idx
        }
    };

    // Without binding, with children
    (@@_node $tree:expr;; $ha:tt | $va:tt $node:expr => [ $($child:tt)* ]) => {
        {
            let __ui_node = $crate::ui!(
                @@_child
                $tree;;
                $crate::ui!(@@_align $ha | $va $node)
                => [ $($child)* ]
            );
            let __node_idx = $tree.add_node(__ui_node);
            __node_idx
        }
    };

    // No children
    (@@_child $tree:expr;; $node:expr => [ ]) => {
        $node
    };

    // Children, with binding
    (
        @@_child
        $tree:expr;;
        $node:expr
        => [
            $binding:ident =
            $ha:tt |
            $va:tt
            $child:expr
            $(=> [ $($grand:tt)* ])?
            $(, $($rest:tt)*)?
        ]
    ) => {
        {
            let __node_idx = $crate::ui!(
                @@_node
                $tree;;
                $binding =
                $ha |
                $va
                $child
                $(=> [ $($grand)* ])?
            );

            $crate::ui!(
                @@_child
                $tree;;
                $node.with_child(__node_idx)
                => [ $($($rest)*)? ]
            )
        }
    };

    // Children, no binding
    (
        @@_child
        $tree:expr;;
        $node:expr
        => [
            $ha:tt |
            $va:tt
            $child:expr
            $(=> [ $($grand:tt)* ])?
            $(, $($rest:tt)*)?
        ]
    ) => {
        {
            let __node_idx = $crate::ui!(
                @@_node
                $tree;;
                $ha |
                $va
                $child
                $(=> [ $($grand)* ])?
            );

            $crate::ui!(
                @@_child
                $tree;;
                $node.with_child(__node_idx)
                => [ $($($rest)*)? ]
            )
        }
    };

    (@@_align $ha:tt | $va: tt $node:expr) => {
        $node.with_align(($crate::ui!(@@_align $ha), $crate::ui!(@@_align $va)))
    };

    (@@_align <) => {
        $crate::Alignment::Begin
    };

    (@@_align -) => {
        $crate::Alignment::Center
    };

    (@@_align >) => {
        $crate::Alignment::End
    };

    (@@_align +) => {
        $crate::Alignment::Full
    };

    (@@_align $a:tt) => {
        compile_error!("Invalid alignment. Only `<`, `-`, `>`, and `+` are allowed.")
    };

    (@@_node $tree:expr;; $node:expr $(=> [ $($child:tt)* ])?) => {
        compile_error!(
            "Nodes must have an alignment, represented by two of `<`, `-`, `>`, and `+`, \
            separated by a `|`"
        )
    };

    (@@_node $tree:expr;; $($tt:tt)*) => {
        compile_error!(concat!(
            "Invalid node syntax. A node should have an optional assignment, two alignment \
            characters, separated by a pipe, and a node expression, optionally followed by \
            a fat arrow and children in square brackets.\n\nExample:\n\
            `target = -|< Node::new() => [ ]`\n\nFound: `",
            $(stringify!($tt),)*
            "`."
        ))
    };

    ($($random:tt)*, $($node:tt)*) => {
        compile_error!(concat!(
            "Expected either a mutable reference to a tree, `%%`, or `%%!`, found `",
            stringify!($($random)*),
            "` where the tree should have been."
        ));
    };

    ($($tt:tt)*) => {
        compile_error!(concat!(
            "Expected a tree followed by a `,`, followed by a node, found `",
            stringify!($($tt)*),
            "`."
        ))
    };
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;

    #[test]
    fn rect_intersect1() {
        let r1 = Rect::new(0.0, 0.0, 10.0, 10.0);
        let r2 = Rect::new(5.0, 0.0, 10.0, 10.0);

        assert_eq!(
            r1.intersect(r2),
            Rect::new(5.0, 0.0, 5.0, 10.0)
        );
    }

    #[test]
    fn valid_tree() {
        let root;
        let sep;
        let mut tree = ui!(
            %%,
            root = +|+ stack::HStack::new() => [
                -|> split3::HSplit3::new() => [
                    <|+ padding::Spacer::sized((10.0, 5.0)),
                    sep = -|+ padding::Spacer::sized((1.0, 10.0)),
                    >|+ padding::Spacer::sized((10.0, 5.0)),
                ],
                -|< padding::Spacer::sized((10.0, 10.0)),
            ]
        );

        tree
            .get_cast_mut::<stack::HStack>(root)
            .unwrap()
            .spacing = 10.0;

        assert_eq!(
            tree
                .get_cast::<padding::Spacer>(sep)
                .unwrap()
                .get_size(),
            (1.0, 10.0)
        )
    }


}