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
pub use euclid::Rect;
use rustc_hash::FxHashMap;
use crate::{
custom_measurer::LayoutMeasurer,
geometry::{
Area,
Size2D,
},
node::Node,
prelude::{
AlignAxis,
Alignment,
AlignmentDirection,
AreaConverter,
AreaModel,
AreaOf,
Available,
AvailableAreaModel,
Direction,
Inner,
LayoutMetadata,
Length,
Parent,
Position,
Torin,
},
size::Size,
torin::DirtyReason,
tree_adapter::{
LayoutNode,
NodeKey,
TreeAdapter,
},
};
/// Some layout strategies require two-phase measurements
/// Example: Alignments or content-fit.
#[derive(Clone, Copy, PartialEq)]
pub enum Phase {
Initial,
Final,
}
pub struct MeasureContext<'a, Key, L, D>
where
Key: NodeKey,
L: LayoutMeasurer<Key>,
D: TreeAdapter<Key>,
{
pub layout: &'a mut Torin<Key>,
pub measurer: &'a mut Option<L>,
pub tree_adapter: &'a mut D,
pub layout_metadata: LayoutMetadata,
}
impl<Key, L, D> MeasureContext<'_, Key, L, D>
where
Key: NodeKey,
L: LayoutMeasurer<Key>,
D: TreeAdapter<Key>,
{
/// Translate all the children of the given Node by the specified X and Y offsets.
fn recursive_translate(&mut self, node_id: Key, offset_x: Length, offset_y: Length) {
let mut buffer = self.tree_adapter.children_of(&node_id);
while let Some(child) = buffer.pop() {
let node = self
.tree_adapter
.get_node(&child)
.expect("Node does not exist");
let translate = match node.position {
Position::Global(_) => false,
Position::Stacked(_) | Position::Absolute(_) => true,
};
if translate {
let layout_node = self
.layout
.get_mut(&child)
.expect("Cached node does not exist");
layout_node.area.origin.x += offset_x.get();
layout_node.area.origin.y += offset_y.get();
layout_node.inner_area.origin.x += offset_x.get();
layout_node.inner_area.origin.y += offset_y.get();
if let Some(measurer) = self.measurer {
measurer.notify_layout_references(
child,
layout_node.area,
layout_node.visible_area(),
layout_node.inner_sizes,
);
}
buffer.extend(self.tree_adapter.children_of(&child));
}
}
}
/// Measure a Node and all its children.
#[allow(clippy::too_many_arguments, clippy::missing_panics_doc)]
pub fn measure_node(
&mut self,
node_id: Key,
node: &Node,
// Area occupied by it's parent
parent_area: AreaOf<Parent>,
// Initial area occupied by it's parent
initial_parent_area: AreaOf<Parent>,
// Area that is available to use by the children of the parent
available_parent_area: AreaOf<Available>,
// Whether to cache the measurements of this Node's children
must_cache_children: bool,
// Parent Node is dirty.
parent_is_dirty: bool,
// Current phase of measurement
phase: Phase,
) -> (bool, LayoutNode) {
let reason = self.layout.dirty.get(&node_id).copied();
// If possible translate all this Node's descendants to avoid relayout
if let Some(layout_node) = self.layout.get_mut(&node_id)
&& reason == Some(DirtyReason::InnerLayout)
&& must_cache_children
{
// Get the offset difference since the last layout
let offset_x = node.offset_x - layout_node.offset_x;
let offset_y = node.offset_y - layout_node.offset_y;
layout_node.offset_x = node.offset_x;
layout_node.offset_y = node.offset_y;
let layout_node = layout_node.clone();
self.recursive_translate(node_id, offset_x, offset_y);
return (must_cache_children, layout_node);
}
// 1. If parent is dirty
// 2. If this Node has been marked as dirty
// 3. If there is no know cached data about this Node.
let must_revalidate =
parent_is_dirty || reason.is_some() || !self.layout.results.contains_key(&node_id);
if must_revalidate {
// Create the initial Node area size
let mut area_size = Size2D::new(node.padding.horizontal(), node.padding.vertical());
// Compute the width and height given the size, the minimum size, the maximum size and margins
area_size.width = node.width.min_max(
area_size.width,
initial_parent_area.size.width,
available_parent_area.size.width,
node.margin.left(),
node.margin.horizontal(),
&node.minimum_width,
&node.maximum_width,
self.layout_metadata.root_area.width(),
phase,
);
area_size.height = node.height.min_max(
area_size.height,
initial_parent_area.size.height,
available_parent_area.size.height,
node.margin.top(),
node.margin.vertical(),
&node.minimum_height,
&node.maximum_height,
self.layout_metadata.root_area.height(),
phase,
);
// If available, run a custom layout measure function
// This is useful when you use third-party libraries (e.g. rust-skia, cosmic-text) to measure text layouts
let node_data = if let Some(measurer) = self.measurer {
if measurer.should_hook_measurement(node_id) {
let available_width =
Size::Pixels(Length::new(available_parent_area.size.width)).min_max(
area_size.width,
initial_parent_area.size.width,
available_parent_area.size.width,
node.margin.left(),
node.margin.horizontal(),
&node.minimum_width,
&node.maximum_width,
self.layout_metadata.root_area.width(),
phase,
);
let available_height =
Size::Pixels(Length::new(available_parent_area.size.height)).min_max(
area_size.height,
initial_parent_area.size.height,
available_parent_area.size.height,
node.margin.top(),
node.margin.vertical(),
&node.minimum_height,
&node.maximum_height,
self.layout_metadata.root_area.height(),
phase,
);
let most_fitting_width = *node
.width
.most_fitting_size(&area_size.width, &available_width);
let most_fitting_height = *node
.height
.most_fitting_size(&area_size.height, &available_height);
let most_fitting_area_size =
Size2D::new(most_fitting_width, most_fitting_height);
let res = measurer.measure(node_id, node, &most_fitting_area_size);
// Compute the width and height again using the new custom area sizes
#[allow(clippy::float_cmp)]
if let Some((custom_size, node_data)) = res {
if node.width.inner_sized() {
area_size.width = node.width.min_max(
custom_size.width,
initial_parent_area.size.width,
available_parent_area.size.width,
node.margin.left(),
node.margin.horizontal(),
&node.minimum_width,
&node.maximum_width,
self.layout_metadata.root_area.width(),
phase,
);
}
if node.height.inner_sized() {
area_size.height = node.height.min_max(
custom_size.height,
initial_parent_area.size.height,
available_parent_area.size.height,
node.margin.top(),
node.margin.vertical(),
&node.minimum_height,
&node.maximum_height,
self.layout_metadata.root_area.height(),
phase,
);
}
// Do not measure inner children
Some(node_data)
} else {
None
}
} else {
None
}
} else {
None
};
let measure_inner_children = if let Some(measurer) = self.measurer {
measurer.should_measure_inner_children(node_id)
} else {
true
};
// There is no need to measure inner children in the initial phase if this Node size
// isn't decided by his children
let phase_measure_inner_children = if phase == Phase::Initial {
node.width.inner_sized() || node.height.inner_sized()
} else {
true
};
// Compute the inner size of the Node, which is basically the size inside the margins and paddings
let inner_size = {
let mut inner_size = area_size;
// When having an unsized bound we set it to whatever is still available in the parent's area
if node.width.inner_sized() {
inner_size.width = node.width.min_max(
available_parent_area.width(),
initial_parent_area.size.width,
available_parent_area.width(),
node.margin.left(),
node.margin.horizontal(),
&node.minimum_width,
&node.maximum_width,
self.layout_metadata.root_area.width(),
phase,
);
}
if node.height.inner_sized() {
inner_size.height = node.height.min_max(
available_parent_area.height(),
initial_parent_area.size.height,
available_parent_area.height(),
node.margin.top(),
node.margin.vertical(),
&node.minimum_height,
&node.maximum_height,
self.layout_metadata.root_area.height(),
phase,
);
}
inner_size
};
// Create the areas
let area_origin = node.position.get_origin(
&available_parent_area,
&parent_area,
area_size,
&self.layout_metadata.root_area,
);
let mut area = Area::new(area_origin, area_size);
let mut inner_area = Rect::new(area_origin, inner_size)
.without_gaps(&node.padding)
.without_gaps(&node.margin)
.as_inner();
let mut inner_sizes = Size2D::default();
if measure_inner_children && phase_measure_inner_children {
// Create an area containing the available space inside the inner area
let mut available_area = inner_area.as_available();
available_area.move_with_offsets(&node.offset_x, &node.offset_y);
let mut parent_area = area.as_parent();
// Measure the layout of this Node's children
self.measure_children(
&node_id,
node,
&mut parent_area,
&mut inner_area,
&mut available_area,
&mut inner_sizes,
must_cache_children,
true,
);
// Re apply min max values after measuring with inner sized
// Margins are set to 0 because area.size already contains the margins
if node.width.inner_sized() {
parent_area.size.width = node.width.min_max(
parent_area.size.width,
parent_area.size.width,
available_parent_area.size.width,
0.,
0.,
&node.minimum_width,
&node.maximum_width,
self.layout_metadata.root_area.width(),
phase,
);
}
if node.height.inner_sized() {
parent_area.size.height = node.height.min_max(
parent_area.size.height,
parent_area.size.height,
available_parent_area.size.height,
0.,
0.,
&node.minimum_height,
&node.maximum_height,
self.layout_metadata.root_area.height(),
phase,
);
}
area = parent_area.cast_unit();
}
let layout_node = LayoutNode {
area,
margin: node.margin,
offset_x: node.offset_x,
offset_y: node.offset_y,
inner_area,
data: node_data,
inner_sizes,
};
// In case of any layout listener, notify it with the new areas.
if must_cache_children
&& phase == Phase::Final
&& node.has_layout_references
&& let Some(measurer) = self.measurer
{
inner_sizes.width += node.padding.horizontal();
inner_sizes.height += node.padding.vertical();
measurer.notify_layout_references(
node_id,
layout_node.area,
layout_node.visible_area(),
inner_sizes,
);
}
(must_cache_children, layout_node)
} else {
let layout_node = self
.layout
.get(&node_id)
.expect("Cached node does not exist")
.clone();
let mut inner_sizes = Size2D::default();
let mut available_area = layout_node.inner_area.as_available();
let mut area = layout_node.area.as_parent();
let mut inner_area = layout_node.inner_area.as_inner();
available_area.move_with_offsets(&node.offset_x, &node.offset_y);
let measure_inner_children = if let Some(measurer) = self.measurer {
measurer.should_measure_inner_children(node_id)
} else {
true
};
if measure_inner_children {
self.measure_children(
&node_id,
node,
&mut area,
&mut inner_area,
&mut available_area,
&mut inner_sizes,
must_cache_children,
false,
);
}
(false, layout_node)
}
}
/// Measure the children layouts of a Node.
#[allow(clippy::too_many_arguments)]
pub fn measure_children(
&mut self,
parent_node_id: &Key,
parent_node: &Node,
parent_area: &mut AreaOf<Parent>,
inner_area: &mut AreaOf<Inner>,
available_area: &mut AreaOf<Available>,
// Accumulated sizes in both axis in the Node
inner_sizes: &mut Size2D,
// Whether to cache the measurements of this Node's children
must_cache_children: bool,
// Parent Node is dirty.
parent_is_dirty: bool,
) {
let children = self.tree_adapter.children_of(parent_node_id);
let initial_area = *inner_area;
let mut initial_phase_flex_grows = FxHashMap::default();
let mut initial_phase_sizes = FxHashMap::default();
let mut initial_phase_inner_sizes = Size2D::default();
// Used to calculate the spacing and some alignments
let (non_absolute_children_len, first_child, last_child) = if parent_node.spacing.get() > 0.
{
let mut last_child = None;
let mut first_child = None;
let len = children
.iter()
.filter(|child_id| {
let Some(child_data) = self.tree_adapter.get_node(child_id) else {
return false;
};
let is_stacked = child_data.position.is_stacked();
if is_stacked {
last_child = Some(**child_id);
if first_child.is_none() {
first_child = Some(**child_id);
}
}
is_stacked
})
.count();
(len, first_child, last_child)
} else {
(
children.len(),
children.first().copied(),
children.last().copied(),
)
};
let needs_initial_phase = parent_node.cross_alignment.is_not_start()
|| parent_node.main_alignment.is_not_start()
|| parent_node.content.is_fit()
|| parent_node.content.is_flex()
|| parent_node.content.is_wrap();
let mut initial_phase_parent_area = *parent_area;
let mut initial_phase_inner_area = *inner_area;
let mut initial_phase_available_area = *available_area;
// Initial phase: Measure the size and position of the children if the parent has a
// non-start cross alignment, non-start main alignment of a fit-content.
if needs_initial_phase {
// Measure the children
for child_id in &children {
let Some(child_data) = self.tree_adapter.get_node(child_id) else {
continue;
};
// No need to consider this Node for a two-phasing
// measurements as it will float on its own.
if !child_data.position.is_stacked() {
continue;
}
let is_last_child = last_child == Some(*child_id);
let inner_area = initial_phase_inner_area;
let (_, mut child_areas) = self.measure_node(
*child_id,
&child_data,
inner_area.as_parent(),
initial_area.as_parent(),
initial_phase_available_area,
false,
parent_is_dirty,
Phase::Initial,
);
child_areas.area.adjust_size(&child_data);
// Stack this child into the parent
Self::stack_child(
&mut initial_phase_available_area,
parent_node,
&child_data,
&mut initial_phase_parent_area,
&mut initial_phase_inner_area,
&mut initial_phase_inner_sizes,
&child_areas.area,
is_last_child,
Phase::Initial,
);
if parent_node.cross_alignment.is_not_start()
|| parent_node.main_alignment.is_spaced()
|| parent_node.content.is_wrap()
{
initial_phase_sizes.insert(*child_id, child_areas.area.size);
}
if parent_node.content.is_flex() {
match parent_node.direction {
Direction::Vertical => {
if let Some(ff) = child_data.height.flex_grow() {
initial_phase_flex_grows.insert(*child_id, ff);
}
}
Direction::Horizontal => {
if let Some(ff) = child_data.width.flex_grow() {
initial_phase_flex_grows.insert(*child_id, ff);
}
}
}
}
}
}
let flex_grows = initial_phase_flex_grows
.values()
.copied()
.reduce(|acc, v| acc + v)
.unwrap_or_default()
.max(Length::new(1.0));
let flex_axis = AlignAxis::new(&parent_node.direction, AlignmentDirection::Main);
let flex_available_width = available_area.width() - initial_phase_inner_sizes.width;
let flex_available_height = available_area.height() - initial_phase_inner_sizes.height;
if parent_node.content.is_flex() {
initial_phase_inner_sizes =
initial_phase_flex_grows
.values()
.fold(initial_phase_inner_sizes, |mut acc, f| {
let flex_grow_per = f.get() / flex_grows.get() * 100.;
match flex_axis {
AlignAxis::Height => {
let size = flex_available_height / 100. * flex_grow_per;
acc.height += size;
}
AlignAxis::Width => {
let size = flex_available_width / 100. * flex_grow_per;
acc.width += size;
}
}
acc
});
}
if needs_initial_phase {
if parent_node.content.is_wrap() {
Self::wrap_adjust_initial(
&children,
parent_node,
&initial_phase_sizes,
&mut initial_phase_inner_sizes,
&mut initial_phase_available_area,
&mut initial_phase_parent_area,
&mut initial_phase_inner_area,
);
}
if parent_node.main_alignment.is_not_start() && parent_node.content.allows_alignments()
{
// Adjust the available and inner areas of the Main axis
Self::shrink_area_to_fit_when_unbounded(
available_area,
&initial_phase_parent_area,
&mut initial_phase_inner_area,
parent_node,
AlignmentDirection::Main,
);
// Align the Main axis
Self::align_content(
available_area,
&initial_phase_inner_area,
initial_phase_inner_sizes,
&parent_node.main_alignment,
parent_node.direction,
AlignmentDirection::Main,
);
}
if (parent_node.cross_alignment.is_not_start() || parent_node.content.is_fit())
&& parent_node.content.allows_alignments()
{
// Adjust the available and inner areas of the Cross axis
Self::shrink_area_to_fit_when_unbounded(
available_area,
&initial_phase_parent_area,
&mut initial_phase_inner_area,
parent_node,
AlignmentDirection::Cross,
);
}
}
let initial_available_area = *available_area;
// Final phase: measure the children with all the axis and sizes adjusted
for child_id in children {
let Some(child_data) = self.tree_adapter.get_node(&child_id) else {
continue;
};
let is_first_child = first_child == Some(child_id);
let is_last_child = last_child == Some(child_id);
let mut adapted_available_area = *available_area;
if parent_node.content.is_flex() {
let flex_grow = initial_phase_flex_grows.get(&child_id);
if let Some(flex_grow) = flex_grow {
let flex_grow_per = flex_grow.get() / flex_grows.get() * 100.;
match flex_axis {
AlignAxis::Height => {
let size = flex_available_height / 100. * flex_grow_per;
adapted_available_area.size.height = size;
}
AlignAxis::Width => {
let size = flex_available_width / 100. * flex_grow_per;
adapted_available_area.size.width = size;
}
}
}
}
// Only the stacked children will be aligned
if parent_node.main_alignment.is_spaced()
&& child_data.position.is_stacked()
&& parent_node.content.allows_alignments()
{
// Align the Main axis if necessary
Self::align_position(
AlignmentDirection::Main,
&mut adapted_available_area,
&initial_available_area,
initial_phase_inner_sizes,
&parent_node.main_alignment,
parent_node.direction,
non_absolute_children_len,
is_first_child,
);
}
if parent_node.cross_alignment.is_not_start() && parent_node.content.allows_alignments()
{
let initial_phase_size = initial_phase_sizes.get(&child_id);
if let Some(initial_phase_size) = initial_phase_size {
// Align the Cross axis if necessary
Self::align_content(
&mut adapted_available_area,
&available_area.as_inner(),
*initial_phase_size,
&parent_node.cross_alignment,
parent_node.direction,
AlignmentDirection::Cross,
);
}
}
if parent_node.content.is_wrap() {
let initial_phase_size = initial_phase_sizes.get(&child_id);
Self::wrap_handle_final(
parent_node,
initial_phase_size,
&initial_available_area,
available_area,
&mut adapted_available_area,
*inner_sizes,
);
}
// Final measurement
let (child_revalidated, mut child_areas) = self.measure_node(
child_id,
&child_data,
inner_area.as_parent(),
initial_area.as_parent(),
adapted_available_area,
must_cache_children,
parent_is_dirty,
Phase::Final,
);
// Adjust the size of the area if needed
child_areas.area.adjust_size(&child_data);
// Stack this child into the parent
if child_data.position.is_stacked() {
Self::stack_child(
available_area,
parent_node,
&child_data,
parent_area,
inner_area,
inner_sizes,
&child_areas.area,
is_last_child,
Phase::Final,
);
}
// Cache the child layout if it was mutated and children must be cached
if child_revalidated && must_cache_children {
// Finally cache this node areas into Torin
self.layout.cache_node(child_id, child_areas);
}
}
}
// In order to make the elements wrap in the second phase we need to
// update the available area to have the proper width/height as if nodes had been wrapped already
fn wrap_adjust_initial(
children: &[Key],
parent_node: &Node,
initial_phase_sizes: &FxHashMap<Key, Size2D>,
initial_phase_inner_sizes: &mut Size2D,
available_area: &mut AreaOf<Available>,
parent_area: &mut AreaOf<Parent>,
_initial_phase_inner_area: &mut AreaOf<Inner>,
) {
let mut used_area = Area::zero();
let break_size = *initial_phase_inner_sizes;
for child_id in children {
let initial_phase_size = initial_phase_sizes.get(child_id);
if let Some(initial_phase_size) = initial_phase_size {
match parent_node.direction {
Direction::Vertical => {
if available_area.height() - used_area.height() - initial_phase_size.height
< 0.
{
// Break the line if there is no enough space
used_area.size.height = initial_phase_size.height;
available_area.size.width += break_size.width;
// Keep increasing for every break
initial_phase_inner_sizes.width += break_size.width;
if parent_node.width.inner_sized() {
parent_area.size.width += break_size.width;
}
} else {
// Otherwise just keep adding each child size
used_area.size.height += initial_phase_size.height;
}
}
Direction::Horizontal => {
if available_area.width() - used_area.width() - initial_phase_size.width
< 0.
{
// Break the line if there is no enough space
used_area.size.width = initial_phase_size.width;
available_area.size.height += break_size.height;
// Keep increasing for every break
initial_phase_inner_sizes.height += break_size.height;
if parent_node.height.inner_sized() {
parent_area.size.height += break_size.height;
}
} else {
// Otherwise just keep adding each child size
used_area.size.width += initial_phase_size.width;
}
}
}
}
}
}
/// Handle wrapping adjustments for a single child during the final phase.
fn wrap_handle_final(
parent_node: &Node,
initial_phase_size: Option<&Size2D>,
initial_available_area: &AreaOf<Available>,
available_area: &mut AreaOf<Available>,
adapted_available_area: &mut AreaOf<Available>,
inner_sizes: Size2D,
) {
if let Some(initial_phase_size) = initial_phase_size {
match parent_node.direction {
Direction::Vertical => {
if adapted_available_area.height() - initial_phase_size.height < 0. {
available_area.origin.y = initial_available_area.origin.y;
available_area.size.height = initial_available_area.size.height;
available_area.origin.x += inner_sizes.width;
adapted_available_area.origin.y = initial_available_area.origin.y;
adapted_available_area.size.height = initial_available_area.size.height;
adapted_available_area.origin.x += inner_sizes.width;
}
}
Direction::Horizontal => {
if adapted_available_area.width() - initial_phase_size.width < 0. {
available_area.origin.x = initial_available_area.origin.x;
available_area.size.width = initial_available_area.size.width;
available_area.origin.y += inner_sizes.height;
adapted_available_area.origin.x = initial_available_area.origin.x;
adapted_available_area.size.width = initial_available_area.size.width;
adapted_available_area.origin.y += inner_sizes.height;
}
}
}
}
}
/// Align the content of this node.
fn align_content(
available_area: &mut AreaOf<Available>,
inner_area: &AreaOf<Inner>,
contents_size: Size2D,
alignment: &Alignment,
direction: Direction,
alignment_direction: AlignmentDirection,
) {
let axis = AlignAxis::new(&direction, alignment_direction);
match axis {
AlignAxis::Height => match alignment {
Alignment::Center => {
let new_origin_y = (inner_area.height() / 2.0) - (contents_size.height / 2.0);
available_area.origin.y = inner_area.min_y() + new_origin_y;
}
Alignment::End => {
available_area.origin.y = inner_area.max_y() - contents_size.height;
}
_ => {}
},
AlignAxis::Width => match alignment {
Alignment::Center => {
let new_origin_x = (inner_area.width() / 2.0) - (contents_size.width / 2.0);
available_area.origin.x = inner_area.min_x() + new_origin_x;
}
Alignment::End => {
available_area.origin.x = inner_area.max_x() - contents_size.width;
}
_ => {}
},
}
}
/// Align the position of this node.
#[allow(clippy::too_many_arguments)]
fn align_position(
alignment_direction: AlignmentDirection,
available_area: &mut AreaOf<Available>,
initial_available_area: &AreaOf<Available>,
inner_sizes: Size2D,
alignment: &Alignment,
direction: Direction,
siblings_len: usize,
is_first_sibling: bool,
) {
let axis = AlignAxis::new(&direction, alignment_direction);
match axis {
AlignAxis::Height => match alignment {
Alignment::SpaceBetween if !is_first_sibling => {
let all_gaps_sizes = initial_available_area.height() - inner_sizes.height;
let gap_size = all_gaps_sizes / (siblings_len - 1) as f32;
available_area.origin.y += gap_size;
}
Alignment::SpaceEvenly => {
let all_gaps_sizes = initial_available_area.height() - inner_sizes.height;
let gap_size = all_gaps_sizes / (siblings_len + 1) as f32;
available_area.origin.y += gap_size;
}
Alignment::SpaceAround => {
let all_gaps_sizes = initial_available_area.height() - inner_sizes.height;
let one_gap_size = all_gaps_sizes / siblings_len as f32;
let gap_size = if is_first_sibling {
one_gap_size / 2.
} else {
one_gap_size
};
available_area.origin.y += gap_size;
}
_ => {}
},
AlignAxis::Width => match alignment {
Alignment::SpaceBetween if !is_first_sibling => {
let all_gaps_sizes = initial_available_area.width() - inner_sizes.width;
let gap_size = all_gaps_sizes / (siblings_len - 1) as f32;
available_area.origin.x += gap_size;
}
Alignment::SpaceEvenly => {
let all_gaps_sizes = initial_available_area.width() - inner_sizes.width;
let gap_size = all_gaps_sizes / (siblings_len + 1) as f32;
available_area.origin.x += gap_size;
}
Alignment::SpaceAround => {
let all_gaps_sizes = initial_available_area.width() - inner_sizes.width;
let one_gap_size = all_gaps_sizes / siblings_len as f32;
let gap_size = if is_first_sibling {
one_gap_size / 2.
} else {
one_gap_size
};
available_area.origin.x += gap_size;
}
_ => {}
},
}
}
/// Stack a child Node into its parent
#[allow(clippy::too_many_arguments)]
fn stack_child(
available_area: &mut AreaOf<Available>,
parent_node: &Node,
child_node: &Node,
parent_area: &mut AreaOf<Parent>,
inner_area: &mut AreaOf<Inner>,
inner_sizes: &mut Size2D,
child_area: &Area,
is_last_sibilin: bool,
phase: Phase,
) {
// Only apply the spacing to elements after `i > 0` and `i < len - 1`
let spacing = if is_last_sibilin {
Length::default()
} else {
parent_node.spacing
};
match parent_node.direction {
Direction::Horizontal => {
// Move the available area
available_area.origin.x = child_area.max_x() + spacing.get();
available_area.size.width -= child_area.size.width + spacing.get();
inner_sizes.height = child_area.height().max(inner_sizes.height);
inner_sizes.width += spacing.get();
if !child_node.width.is_flex() || phase == Phase::Final {
inner_sizes.width += child_area.width();
}
// Keep the biggest height
if parent_node.height.inner_sized() {
parent_area.size.height = parent_area.size.height.max(
child_area.size.height
+ parent_node.padding.vertical()
+ parent_node.margin.vertical(),
);
// Keep the inner area in sync
inner_area.size.height = parent_area.size.height
- parent_node.padding.vertical()
- parent_node.margin.vertical();
}
// Accumulate width
if parent_node.width.inner_sized() {
parent_area.size.width += child_area.size.width + spacing.get();
}
}
Direction::Vertical => {
// Move the available area
available_area.origin.y = child_area.max_y() + spacing.get();
available_area.size.height -= child_area.size.height + spacing.get();
inner_sizes.width = child_area.width().max(inner_sizes.width);
inner_sizes.height += spacing.get();
if !child_node.height.is_flex() || phase == Phase::Final {
inner_sizes.height += child_area.height();
}
// Keep the biggest width
if parent_node.width.inner_sized() {
parent_area.size.width = parent_area.size.width.max(
child_area.size.width
+ parent_node.padding.horizontal()
+ parent_node.margin.horizontal(),
);
// Keep the inner area in sync
inner_area.size.width = parent_area.size.width
- parent_node.padding.horizontal()
- parent_node.margin.horizontal();
}
// Accumulate height
if parent_node.height.inner_sized() {
parent_area.size.height += child_area.size.height + spacing.get();
}
}
}
}
/// Shrink the available area and inner area of a parent node when for example height is set to "auto",
/// direction is vertical and main_alignment is set to "center" or "end" or the content is set to "fit".
/// The intended usage is to call this after the first measurement and before the second,
/// this way the second measurement will align the content relatively to the parent element instead
/// of overflowing due to being aligned relatively to the upper parent element
fn shrink_area_to_fit_when_unbounded(
available_area: &mut AreaOf<Available>,
parent_area: &AreaOf<Parent>,
inner_area: &mut AreaOf<Inner>,
parent_node: &Node,
alignment_direction: AlignmentDirection,
) {
struct NodeData<'a> {
pub inner_origin: &'a mut f32,
pub inner_size: &'a mut f32,
pub area_origin: f32,
pub area_size: f32,
pub one_side_padding: f32,
pub two_sides_padding: f32,
pub one_side_margin: f32,
pub two_sides_margin: f32,
pub available_size: &'a mut f32,
}
let axis = AlignAxis::new(&parent_node.direction, alignment_direction);
let (is_vertical_not_start, is_horizontal_not_start) = match parent_node.direction {
Direction::Vertical => (
parent_node.main_alignment.is_not_start(),
parent_node.cross_alignment.is_not_start() || parent_node.content.is_fit(),
),
Direction::Horizontal => (
parent_node.cross_alignment.is_not_start() || parent_node.content.is_fit(),
parent_node.main_alignment.is_not_start(),
),
};
let NodeData {
inner_origin,
inner_size,
area_origin,
area_size,
one_side_padding,
two_sides_padding,
one_side_margin,
two_sides_margin,
available_size,
} = match axis {
AlignAxis::Height if parent_node.height.inner_sized() && is_vertical_not_start => {
NodeData {
inner_origin: &mut inner_area.origin.y,
inner_size: &mut inner_area.size.height,
area_origin: parent_area.origin.y,
area_size: parent_area.size.height,
one_side_padding: parent_node.padding.top(),
two_sides_padding: parent_node.padding.vertical(),
one_side_margin: parent_node.margin.top(),
two_sides_margin: parent_node.margin.vertical(),
available_size: &mut available_area.size.height,
}
}
AlignAxis::Width if parent_node.width.inner_sized() && is_horizontal_not_start => {
NodeData {
inner_origin: &mut inner_area.origin.x,
inner_size: &mut inner_area.size.width,
area_origin: parent_area.origin.x,
area_size: parent_area.size.width,
one_side_padding: parent_node.padding.left(),
two_sides_padding: parent_node.padding.horizontal(),
one_side_margin: parent_node.margin.left(),
two_sides_margin: parent_node.margin.horizontal(),
available_size: &mut available_area.size.width,
}
}
_ => return,
};
// Set the origin of the inner area to the origin of the area plus the padding and margin for the given axis
*inner_origin = area_origin + one_side_padding + one_side_margin;
// Set the size of the inner area to the size of the area minus the padding and margin for the given axis
*inner_size = area_size - two_sides_padding - two_sides_margin;
// Set the same available size as the inner area for the given axis
*available_size = *inner_size;
}
}