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
open Geometry
open Style
open Tree
let align_tracks ~grid_container_content_box_size ~padding ~border ~tracks
~track_alignment_style =
let used_size =
Array.fold_left
(fun acc track -> acc +. track.Grid_track.base_size)
0.0 tracks
in
let free_space = grid_container_content_box_size -. used_size in
let origin = padding.Line.start +. border.Line.start in
let num_tracks =
let count = ref 0 in
for i = 1 to Array.length tracks - 1 do
if i mod 2 = 1 && not tracks.(i).Grid_track.is_collapsed then incr count
done;
!count
in
let gap = 0.0 in
let layout_is_reversed = false in
let is_safe = false in
let track_alignment =
Compute_helpers.apply_alignment_fallback ~free_space ~num_items:num_tracks
~alignment_mode:track_alignment_style ~is_safe
in
let total_offset = ref origin in
Array.iteri
(fun i track ->
let is_gutter = i mod 2 = 0 in
let is_first = i = 1 in
let offset =
if is_gutter then 0.0
else
Compute_helpers.compute_alignment_offset ~free_space
~num_items:num_tracks ~gap ~alignment_mode:track_alignment
~layout_is_flex_reversed:layout_is_reversed ~is_first
in
track.Grid_track.offset <- !total_offset +. offset;
total_offset := !total_offset +. offset +. track.Grid_track.base_size)
tracks
let align_item_within_area ~grid_area ~(alignment_style : Align_items.t)
~resolved_size ~position ~inset ~margin ~baseline_shim =
let non_auto_margin =
Line.
{
start = Option.value margin.Line.start ~default:0.0 +. baseline_shim;
end_ = Option.value margin.Line.end_ ~default:0.0;
}
in
let grid_area_size =
Float.max (grid_area.Line.end_ -. grid_area.Line.start) 0.0
in
let free_space =
Float.max (grid_area_size -. resolved_size -. Line.sum non_auto_margin) 0.0
in
let auto_margin_count =
(if Option.is_none margin.Line.start then 1 else 0)
+ if Option.is_none margin.Line.end_ then 1 else 0
in
let auto_margin_size =
if auto_margin_count > 0 then free_space /. float_of_int auto_margin_count
else 0.0
in
let resolved_margin =
Line.
{
start =
Option.value margin.Line.start ~default:auto_margin_size
+. baseline_shim;
end_ = Option.value margin.Line.end_ ~default:auto_margin_size;
}
in
let alignment_based_offset =
match alignment_style with
| Align_items.Start | Align_items.Flex_start -> resolved_margin.Line.start
| Align_items.End | Align_items.Flex_end ->
grid_area_size -. resolved_size -. resolved_margin.Line.end_
| Align_items.Center ->
(grid_area_size -. resolved_size +. resolved_margin.Line.start
-. resolved_margin.Line.end_)
/. 2.0
| Align_items.Baseline -> resolved_margin.Line.start
| Align_items.Stretch -> resolved_margin.Line.start
in
let offset_within_area =
if position = Position.Absolute then
match inset.Line.start with
| Some start -> start +. non_auto_margin.Line.start
| None -> (
match inset.Line.end_ with
| Some end_ ->
grid_area_size -. end_ -. resolved_size
-. non_auto_margin.Line.end_
| None -> alignment_based_offset)
else alignment_based_offset
in
let start = ref (grid_area.Line.start +. offset_within_area) in
(if position = Position.Relative then
let inset_adjustment =
match (inset.Line.start, inset.Line.end_) with
| Some s, _ -> s
| None, Some e -> -.e
| None, None -> 0.0
in
start := !start +. inset_adjustment);
(!start, resolved_margin)
let align_and_position_item (type t)
(module Tree : LAYOUT_PARTIAL_TREE with type t = t) ~tree ~node ~order
~grid_area
~(container_alignment_styles : align_items option in_both_abs_axis)
~baseline_shim =
let grid_area_size =
Size.
{
width = grid_area.Rect.right -. grid_area.Rect.left;
height = grid_area.Rect.bottom -. grid_area.Rect.top;
}
in
let style = Tree.get_core_container_style tree node in
let overflow = Style.overflow style in
let scrollbar_width = Style.scrollbar_width style in
let aspect_ratio = Style.aspect_ratio style in
let justify_self = Style.justify_self style in
let align_self = Style.align_self style in
let position = Style.position style in
let inset_horizontal =
let inset = Style.inset style in
let line = Rect.horizontal_components inset in
Line.map
(fun size ->
Length_percentage_auto.resolve_to_option_with_calc size
grid_area_size.width
(Tree.resolve_calc_value tree))
line
in
let inset_vertical =
let inset = Style.inset style in
let line = Rect.vertical_components inset in
Line.map
(fun size ->
Length_percentage_auto.resolve_to_option_with_calc size
grid_area_size.height
(Tree.resolve_calc_value tree))
line
in
let padding =
Style.padding style
|> Rect.map (fun p ->
Length_percentage.resolve_or_zero p (Some grid_area_size.width)
(Tree.resolve_calc_value tree))
in
let border =
Style.border style
|> Rect.map (fun p ->
Length_percentage.resolve_or_zero p (Some grid_area_size.width)
(Tree.resolve_calc_value tree))
in
let padding_border_size = Rect.sum_axes (Rect.add padding border) in
let box_sizing_adjustment =
if Style.box_sizing style = Box_sizing.Content_box then padding_border_size
else Size.zero
in
let inherent_size =
let style_size = Style.size style in
let resolved_size =
Size.
{
width =
Dimension.maybe_resolve style_size.width (Some grid_area_size.width)
(Tree.resolve_calc_value tree);
height =
Dimension.maybe_resolve style_size.height
(Some grid_area_size.height)
(Tree.resolve_calc_value tree);
}
in
let with_aspect = Size.apply_aspect_ratio aspect_ratio resolved_size in
Size.maybe_add box_sizing_adjustment with_aspect
in
let min_size =
let style_min_size = Style.min_size style in
let resolved_min_size =
Size.
{
width =
Dimension.maybe_resolve style_min_size.width
(Some grid_area_size.width)
(Tree.resolve_calc_value tree);
height =
Dimension.maybe_resolve style_min_size.height
(Some grid_area_size.height)
(Tree.resolve_calc_value tree);
}
in
let with_box_sizing =
Size.maybe_add box_sizing_adjustment resolved_min_size
in
let with_padding_border =
Size.choose_first with_box_sizing
(Size.map Option.some padding_border_size)
in
let max_with_pb = Size.maybe_max padding_border_size with_padding_border in
Size.apply_aspect_ratio aspect_ratio max_with_pb
in
let max_size =
let style_max_size = Style.max_size style in
let resolved_max_size =
Size.
{
width =
Dimension.maybe_resolve style_max_size.width
(Some grid_area_size.width)
(Tree.resolve_calc_value tree);
height =
Dimension.maybe_resolve style_max_size.height
(Some grid_area_size.height)
(Tree.resolve_calc_value tree);
}
in
let with_aspect = Size.apply_aspect_ratio aspect_ratio resolved_max_size in
Size.maybe_add box_sizing_adjustment with_aspect
in
let alignment_styles =
In_both_abs_axis.
{
horizontal =
(match (justify_self, container_alignment_styles.horizontal) with
| Some js, _ -> js
| None, Some ca -> ca
| None, None ->
if Option.is_some inherent_size.width then Align_items.Start
else Align_items.Stretch);
vertical =
(match (align_self, container_alignment_styles.vertical) with
| Some as_, _ -> as_
| None, Some ca -> ca
| None, None ->
if
Option.is_some inherent_size.height
|| Option.is_some aspect_ratio
then Align_items.Start
else Align_items.Stretch);
}
in
let margin =
Style.margin style
|> Rect.map (fun margin ->
Length_percentage_auto.resolve_to_option_with_calc margin
grid_area_size.width
(Tree.resolve_calc_value tree))
in
let subtract_option base opt_val =
match opt_val with Some v -> base -. v | None -> base
in
let grid_area_minus_item_margins_size =
Size.
{
width =
( (grid_area_size.width |> fun w -> subtract_option w margin.left)
|> fun w -> subtract_option w margin.right );
height =
( ( (grid_area_size.height |> fun h -> subtract_option h margin.top)
|> fun h -> subtract_option h margin.bottom )
|> fun h -> h -. baseline_shim );
}
in
let width =
match inherent_size.width with
| Some w -> Some w
| None ->
if position = Position.Absolute then
match (inset_horizontal.start, inset_horizontal.end_) with
| Some left, Some right ->
Some
(Float.max
(grid_area_minus_item_margins_size.width -. left -. right)
0.0)
| _ -> None
else if
Option.is_some margin.left
&& Option.is_some margin.right
&& alignment_styles.horizontal = Align_items.Stretch
&& position <> Position.Absolute
then Some grid_area_minus_item_margins_size.width
else None
in
let size1 =
Size.apply_aspect_ratio aspect_ratio
Size.{ width; height = inherent_size.height }
in
let height =
match size1.height with
| Some h -> Some h
| None ->
if position = Position.Absolute then
match (inset_vertical.start, inset_vertical.end_) with
| Some top, Some bottom ->
Some
(Float.max
(grid_area_minus_item_margins_size.height -. top -. bottom)
0.0)
| _ -> None
else if
Option.is_some margin.top
&& Option.is_some margin.bottom
&& alignment_styles.vertical = Align_items.Stretch
&& position <> Position.Absolute
then Some grid_area_minus_item_margins_size.height
else None
in
let size2 =
Size.apply_aspect_ratio aspect_ratio Size.{ width = size1.width; height }
in
let clamped_size = Size.clamp_option min_size max_size size2 in
let size =
if
position = Position.Absolute
&& (Option.is_none width || Option.is_none height)
then
let measured_size =
Tree.compute_child_layout tree node
(Layout_input.make ~run_mode:Run_mode.Compute_size
~sizing_mode:Sizing_mode.Inherent_size ~axis:Requested_axis.Both
~known_dimensions:clamped_size
~parent_size:(Size.map Option.some grid_area_size)
~available_space:
(Size.map Available_space.of_float
grid_area_minus_item_margins_size)
~vertical_margins_are_collapsible:Line.both_false)
|> Layout_output.size
in
Size.map Option.some measured_size
else clamped_size
in
let layout_output =
Tree.compute_child_layout tree node
(Layout_input.make ~run_mode:Run_mode.Perform_layout
~sizing_mode:Sizing_mode.Inherent_size ~axis:Requested_axis.Both
~known_dimensions:size
~parent_size:(Size.map Option.some grid_area_size)
~available_space:
(Size.map Available_space.of_float grid_area_minus_item_margins_size)
~vertical_margins_are_collapsible:Line.both_false)
in
let final_size =
let measured_size = Layout_output.size layout_output in
let chosen_size = Size.unwrap_or measured_size size in
Size.clamp min_size max_size chosen_size
in
let x, x_margin =
align_item_within_area
~grid_area:Line.{ start = grid_area.left; end_ = grid_area.right }
~alignment_style:
(Option.value justify_self ~default:alignment_styles.horizontal)
~resolved_size:final_size.width ~position ~inset:inset_horizontal
~margin:(Rect.horizontal_components margin)
~baseline_shim:0.0
in
let y, y_margin =
align_item_within_area
~grid_area:Line.{ start = grid_area.top; end_ = grid_area.bottom }
~alignment_style:
(Option.value align_self ~default:alignment_styles.vertical)
~resolved_size:final_size.height ~position ~inset:inset_vertical
~margin:(Rect.vertical_components margin)
~baseline_shim
in
let scrollbar_size =
Size.
{
width =
(if overflow.Point.y = Overflow.Scroll then scrollbar_width else 0.0);
height =
(if overflow.Point.x = Overflow.Scroll then scrollbar_width else 0.0);
}
in
let resolved_margin =
Rect.
{
left = x_margin.start;
right = x_margin.end_;
top = y_margin.start;
bottom = y_margin.end_;
}
in
Tree.set_unrounded_layout tree node
(Layout.make ~order
~location:Point.{ x; y }
~size:final_size
~content_size:(Layout_output.content_size layout_output)
~scrollbar_size ~padding ~border ~margin:resolved_margin);
let contribution =
Compute_helpers.compute_content_size_contribution
~location:Point.{ x; y }
~size:final_size
~content_size:(Layout_output.content_size layout_output)
~overflow
in
(contribution, y, final_size.height)