1use crate::geometry::{Line, Point, Rect, Size};
3use crate::style::{AvailableSpace, CoreStyle, LengthPercentageAuto, Overflow, Position};
4use crate::style_helpers::TaffyMaxContent;
5use crate::tree::{CollapsibleMarginSet, Layout, LayoutInput, LayoutOutput, RunMode, SizingMode};
6use crate::tree::{LayoutPartialTree, LayoutPartialTreeExt, NodeId};
7use crate::util::debug::debug_log;
8use crate::util::sys::f32_max;
9use crate::util::sys::Vec;
10use crate::util::MaybeMath;
11use crate::util::{MaybeResolve, ResolveOrZero};
12use crate::{BlockContainerStyle, BlockItemStyle, BoxGenerationMode, BoxSizing, LayoutBlockContainer, TextAlign};
13
14#[cfg(feature = "content_size")]
15use super::common::content_size::compute_content_size_contribution;
16
17struct BlockItem {
19 node_id: NodeId,
21
22 order: u32,
25
26 is_table: bool,
28
29 size: Size<Option<f32>>,
31 min_size: Size<Option<f32>>,
33 max_size: Size<Option<f32>>,
35
36 overflow: Point<Overflow>,
38 scrollbar_width: f32,
40
41 position: Position,
43 inset: Rect<LengthPercentageAuto>,
45 margin: Rect<LengthPercentageAuto>,
47 padding: Rect<f32>,
49 border: Rect<f32>,
51 padding_border_sum: Size<f32>,
53
54 computed_size: Size<f32>,
56 static_position: Point<f32>,
59 can_be_collapsed_through: bool,
61}
62
63pub fn compute_block_layout(
65 tree: &mut impl LayoutBlockContainer,
66 node_id: NodeId,
67 inputs: LayoutInput,
68) -> LayoutOutput {
69 let LayoutInput { known_dimensions, parent_size, run_mode, .. } = inputs;
70 let style = tree.get_block_container_style(node_id);
71
72 let aspect_ratio = style.aspect_ratio();
74 let padding = style.padding().resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
75 let border = style.border().resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
76 let padding_border_size = (padding + border).sum_axes();
77 let box_sizing_adjustment =
78 if style.box_sizing() == BoxSizing::ContentBox { padding_border_size } else { Size::ZERO };
79
80 let min_size = style
81 .min_size()
82 .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
83 .maybe_apply_aspect_ratio(aspect_ratio)
84 .maybe_add(box_sizing_adjustment);
85 let max_size = style
86 .max_size()
87 .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
88 .maybe_apply_aspect_ratio(aspect_ratio)
89 .maybe_add(box_sizing_adjustment);
90 let clamped_style_size = if inputs.sizing_mode == SizingMode::InherentSize {
91 style
92 .size()
93 .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
94 .maybe_apply_aspect_ratio(aspect_ratio)
95 .maybe_add(box_sizing_adjustment)
96 .maybe_clamp(min_size, max_size)
97 } else {
98 Size::NONE
99 };
100
101 drop(style);
102
103 let min_max_definite_size = min_size.zip_map(max_size, |min, max| match (min, max) {
105 (Some(min), Some(max)) if max <= min => Some(min),
106 _ => None,
107 });
108
109 let styled_based_known_dimensions =
110 known_dimensions.or(min_max_definite_size).or(clamped_style_size).maybe_max(padding_border_size);
111
112 if run_mode == RunMode::ComputeSize {
115 if let Size { width: Some(width), height: Some(height) } = styled_based_known_dimensions {
116 return LayoutOutput::from_outer_size(Size { width, height });
117 }
118 }
119
120 debug_log!("BLOCK");
121 compute_inner(tree, node_id, LayoutInput { known_dimensions: styled_based_known_dimensions, ..inputs })
122}
123
124fn compute_inner(tree: &mut impl LayoutBlockContainer, node_id: NodeId, inputs: LayoutInput) -> LayoutOutput {
126 let LayoutInput {
127 known_dimensions, parent_size, available_space, run_mode, vertical_margins_are_collapsible, ..
128 } = inputs;
129
130 let style = tree.get_block_container_style(node_id);
131 let raw_padding = style.padding();
132 let raw_border = style.border();
133 let raw_margin = style.margin();
134 let aspect_ratio = style.aspect_ratio();
135 let padding = raw_padding.resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
136 let border = raw_border.resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
137
138 let scrollbar_gutter = {
142 let offsets = style.overflow().transpose().map(|overflow| match overflow {
143 Overflow::Scroll => style.scrollbar_width(),
144 _ => 0.0,
145 });
146 Rect { top: 0.0, left: 0.0, right: offsets.x, bottom: offsets.y }
148 };
149 let padding_border = padding + border;
150 let padding_border_size = padding_border.sum_axes();
151 let content_box_inset = padding_border + scrollbar_gutter;
152 let container_content_box_size = known_dimensions.maybe_sub(content_box_inset.sum_axes());
153
154 let box_sizing_adjustment =
155 if style.box_sizing() == BoxSizing::ContentBox { padding_border_size } else { Size::ZERO };
156 let size = style
157 .size()
158 .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
159 .maybe_apply_aspect_ratio(aspect_ratio)
160 .maybe_add(box_sizing_adjustment);
161 let min_size = style
162 .min_size()
163 .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
164 .maybe_apply_aspect_ratio(aspect_ratio)
165 .maybe_add(box_sizing_adjustment);
166 let max_size = style
167 .max_size()
168 .maybe_resolve(parent_size, |val, basis| tree.calc(val, basis))
169 .maybe_apply_aspect_ratio(aspect_ratio)
170 .maybe_add(box_sizing_adjustment);
171
172 let own_margins_collapse_with_children = Line {
174 start: vertical_margins_are_collapsible.start
175 && !style.overflow().x.is_scroll_container()
176 && !style.overflow().y.is_scroll_container()
177 && style.position() == Position::Relative
178 && padding.top == 0.0
179 && border.top == 0.0,
180 end: vertical_margins_are_collapsible.end
181 && !style.overflow().x.is_scroll_container()
182 && !style.overflow().y.is_scroll_container()
183 && style.position() == Position::Relative
184 && padding.bottom == 0.0
185 && border.bottom == 0.0
186 && size.height.is_none(),
187 };
188 let has_styles_preventing_being_collapsed_through = !style.is_block()
189 || style.overflow().x.is_scroll_container()
190 || style.overflow().y.is_scroll_container()
191 || style.position() == Position::Absolute
192 || padding.top > 0.0
193 || padding.bottom > 0.0
194 || border.top > 0.0
195 || border.bottom > 0.0
196 || matches!(size.height, Some(h) if h > 0.0)
197 || matches!(min_size.height, Some(h) if h > 0.0);
198
199 let text_align = style.text_align();
200
201 drop(style);
202
203 let mut items = generate_item_list(tree, node_id, container_content_box_size);
205
206 let container_outer_width = known_dimensions.width.unwrap_or_else(|| {
208 let available_width = available_space.width.maybe_sub(content_box_inset.horizontal_axis_sum());
209 let intrinsic_width = determine_content_based_container_width(tree, &items, available_width)
210 + content_box_inset.horizontal_axis_sum();
211 intrinsic_width.maybe_clamp(min_size.width, max_size.width).maybe_max(Some(padding_border_size.width))
212 });
213
214 if let (RunMode::ComputeSize, Some(container_outer_height)) = (run_mode, known_dimensions.height) {
216 return LayoutOutput::from_outer_size(Size { width: container_outer_width, height: container_outer_height });
217 }
218
219 let resolved_padding = raw_padding.resolve_or_zero(Some(container_outer_width), |val, basis| tree.calc(val, basis));
221 let resolved_border = raw_border.resolve_or_zero(Some(container_outer_width), |val, basis| tree.calc(val, basis));
222 let resolved_content_box_inset = resolved_padding + resolved_border + scrollbar_gutter;
223 let (inflow_content_size, intrinsic_outer_height, first_child_top_margin_set, last_child_bottom_margin_set) =
224 perform_final_layout_on_in_flow_children(
225 tree,
226 &mut items,
227 container_outer_width,
228 content_box_inset,
229 resolved_content_box_inset,
230 text_align,
231 own_margins_collapse_with_children,
232 );
233 let container_outer_height = known_dimensions
234 .height
235 .unwrap_or(intrinsic_outer_height.maybe_clamp(min_size.height, max_size.height))
236 .maybe_max(Some(padding_border_size.height));
237 let final_outer_size = Size { width: container_outer_width, height: container_outer_height };
238
239 if run_mode == RunMode::ComputeSize {
241 return LayoutOutput::from_outer_size(final_outer_size);
242 }
243
244 let absolute_position_inset = resolved_border + scrollbar_gutter;
246 let absolute_position_area = final_outer_size - absolute_position_inset.sum_axes();
247 let absolute_position_offset = Point { x: absolute_position_inset.left, y: absolute_position_inset.top };
248 let absolute_content_size =
249 perform_absolute_layout_on_absolute_children(tree, &items, absolute_position_area, absolute_position_offset);
250
251 let len = tree.child_count(node_id);
253 for order in 0..len {
254 let child = tree.get_child_id(node_id, order);
255 if tree.get_block_child_style(child).box_generation_mode() == BoxGenerationMode::None {
256 tree.set_unrounded_layout(child, &Layout::with_order(order as u32));
257 tree.perform_child_layout(
258 child,
259 Size::NONE,
260 Size::NONE,
261 Size::MAX_CONTENT,
262 SizingMode::InherentSize,
263 Line::FALSE,
264 );
265 }
266 }
267
268 let all_in_flow_children_can_be_collapsed_through =
270 items.iter().all(|item| item.position == Position::Absolute || item.can_be_collapsed_through);
271 let can_be_collapsed_through =
272 !has_styles_preventing_being_collapsed_through && all_in_flow_children_can_be_collapsed_through;
273
274 #[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
275 let content_size = inflow_content_size.f32_max(absolute_content_size);
276
277 LayoutOutput {
278 size: final_outer_size,
279 #[cfg(feature = "content_size")]
280 content_size,
281 first_baselines: Point::NONE,
282 top_margin: if own_margins_collapse_with_children.start {
283 first_child_top_margin_set
284 } else {
285 let margin_top = raw_margin.top.resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
286 CollapsibleMarginSet::from_margin(margin_top)
287 },
288 bottom_margin: if own_margins_collapse_with_children.end {
289 last_child_bottom_margin_set
290 } else {
291 let margin_bottom =
292 raw_margin.bottom.resolve_or_zero(parent_size.width, |val, basis| tree.calc(val, basis));
293 CollapsibleMarginSet::from_margin(margin_bottom)
294 },
295 margins_can_collapse_through: can_be_collapsed_through,
296 }
297}
298
299#[inline]
301fn generate_item_list(
302 tree: &impl LayoutBlockContainer,
303 node: NodeId,
304 node_inner_size: Size<Option<f32>>,
305) -> Vec<BlockItem> {
306 tree.child_ids(node)
307 .map(|child_node_id| (child_node_id, tree.get_block_child_style(child_node_id)))
308 .filter(|(_, style)| style.box_generation_mode() != BoxGenerationMode::None)
309 .enumerate()
310 .map(|(order, (child_node_id, child_style))| {
311 let aspect_ratio = child_style.aspect_ratio();
312 let padding = child_style.padding().resolve_or_zero(node_inner_size, |val, basis| tree.calc(val, basis));
313 let border = child_style.border().resolve_or_zero(node_inner_size, |val, basis| tree.calc(val, basis));
314 let pb_sum = (padding + border).sum_axes();
315 let box_sizing_adjustment =
316 if child_style.box_sizing() == BoxSizing::ContentBox { pb_sum } else { Size::ZERO };
317 BlockItem {
318 node_id: child_node_id,
319 order: order as u32,
320 is_table: child_style.is_table(),
321 size: child_style
322 .size()
323 .maybe_resolve(node_inner_size, |val, basis| tree.calc(val, basis))
324 .maybe_apply_aspect_ratio(aspect_ratio)
325 .maybe_add(box_sizing_adjustment),
326 min_size: child_style
327 .min_size()
328 .maybe_resolve(node_inner_size, |val, basis| tree.calc(val, basis))
329 .maybe_apply_aspect_ratio(aspect_ratio)
330 .maybe_add(box_sizing_adjustment),
331 max_size: child_style
332 .max_size()
333 .maybe_resolve(node_inner_size, |val, basis| tree.calc(val, basis))
334 .maybe_apply_aspect_ratio(aspect_ratio)
335 .maybe_add(box_sizing_adjustment),
336 overflow: child_style.overflow(),
337 scrollbar_width: child_style.scrollbar_width(),
338 position: child_style.position(),
339 inset: child_style.inset(),
340 margin: child_style.margin(),
341 padding,
342 border,
343 padding_border_sum: pb_sum,
344
345 computed_size: Size::zero(),
347 static_position: Point::zero(),
348 can_be_collapsed_through: false,
349 }
350 })
351 .collect()
352}
353
354#[inline]
356fn determine_content_based_container_width(
357 tree: &mut impl LayoutPartialTree,
358 items: &[BlockItem],
359 available_width: AvailableSpace,
360) -> f32 {
361 let available_space = Size { width: available_width, height: AvailableSpace::MinContent };
362
363 let mut max_child_width = 0.0;
364 for item in items.iter().filter(|item| item.position != Position::Absolute) {
365 let known_dimensions = item.size.maybe_clamp(item.min_size, item.max_size);
366
367 let width = known_dimensions.width.unwrap_or_else(|| {
368 let item_x_margin_sum = item
369 .margin
370 .resolve_or_zero(available_space.width.into_option(), |val, basis| tree.calc(val, basis))
371 .horizontal_axis_sum();
372 let size_and_baselines = tree.perform_child_layout(
373 item.node_id,
374 known_dimensions,
375 Size::NONE,
376 available_space.map_width(|w| w.maybe_sub(item_x_margin_sum)),
377 SizingMode::InherentSize,
378 Line::TRUE,
379 );
380
381 size_and_baselines.size.width + item_x_margin_sum
382 });
383 let width = f32_max(width, item.padding_border_sum.width);
384
385 max_child_width = f32_max(max_child_width, width);
386 }
387
388 max_child_width
389}
390
391#[inline]
393fn perform_final_layout_on_in_flow_children(
394 tree: &mut impl LayoutPartialTree,
395 items: &mut [BlockItem],
396 container_outer_width: f32,
397 content_box_inset: Rect<f32>,
398 resolved_content_box_inset: Rect<f32>,
399 text_align: TextAlign,
400 own_margins_collapse_with_children: Line<bool>,
401) -> (Size<f32>, f32, CollapsibleMarginSet, CollapsibleMarginSet) {
402 let container_inner_width = container_outer_width - content_box_inset.horizontal_axis_sum();
404 let parent_size = Size { width: Some(container_outer_width), height: None };
405 let available_space =
406 Size { width: AvailableSpace::Definite(container_inner_width), height: AvailableSpace::MinContent };
407
408 #[cfg_attr(not(feature = "content_size"), allow(unused_mut))]
409 let mut inflow_content_size = Size::ZERO;
410 let mut committed_y_offset = resolved_content_box_inset.top;
411 let mut y_offset_for_absolute = resolved_content_box_inset.top;
412 let mut first_child_top_margin_set = CollapsibleMarginSet::ZERO;
413 let mut active_collapsible_margin_set = CollapsibleMarginSet::ZERO;
414 let mut is_collapsing_with_first_margin_set = true;
415 for item in items.iter_mut() {
416 if item.position == Position::Absolute {
417 item.static_position = Point { x: resolved_content_box_inset.left, y: y_offset_for_absolute }
418 } else {
419 let item_margin = item
420 .margin
421 .map(|margin| margin.resolve_to_option(container_outer_width, |val, basis| tree.calc(val, basis)));
422 let item_non_auto_margin = item_margin.map(|m| m.unwrap_or(0.0));
423 let item_non_auto_x_margin_sum = item_non_auto_margin.horizontal_axis_sum();
424 let known_dimensions = if item.is_table {
425 Size::NONE
426 } else {
427 item.size
428 .map_width(|width| {
429 Some(
432 width
433 .unwrap_or(container_inner_width - item_non_auto_x_margin_sum)
434 .maybe_clamp(item.min_size.width, item.max_size.width),
435 )
436 })
437 .maybe_clamp(item.min_size, item.max_size)
438 };
439
440 let item_layout = tree.perform_child_layout(
441 item.node_id,
442 known_dimensions,
443 parent_size,
444 available_space.map_width(|w| w.maybe_sub(item_non_auto_x_margin_sum)),
445 SizingMode::InherentSize,
446 Line::TRUE,
447 );
448 let final_size = item_layout.size;
449
450 let top_margin_set = item_layout.top_margin.collapse_with_margin(item_margin.top.unwrap_or(0.0));
451 let bottom_margin_set = item_layout.bottom_margin.collapse_with_margin(item_margin.bottom.unwrap_or(0.0));
452
453 let free_x_space = f32_max(0.0, container_inner_width - final_size.width - item_non_auto_x_margin_sum);
457 let x_axis_auto_margin_size = {
458 let auto_margin_count = item_margin.left.is_none() as u8 + item_margin.right.is_none() as u8;
459 if auto_margin_count > 0 {
460 free_x_space / auto_margin_count as f32
461 } else {
462 0.0
463 }
464 };
465 let resolved_margin = Rect {
466 left: item_margin.left.unwrap_or(x_axis_auto_margin_size),
467 right: item_margin.right.unwrap_or(x_axis_auto_margin_size),
468 top: top_margin_set.resolve(),
469 bottom: bottom_margin_set.resolve(),
470 };
471
472 let inset = item.inset.zip_size(Size { width: container_inner_width, height: 0.0 }, |p, s| {
474 p.maybe_resolve(s, |val, basis| tree.calc(val, basis))
475 });
476 let inset_offset = Point {
477 x: inset.left.or(inset.right.map(|x| -x)).unwrap_or(0.0),
478 y: inset.top.or(inset.bottom.map(|x| -x)).unwrap_or(0.0),
479 };
480
481 let y_margin_offset = if is_collapsing_with_first_margin_set && own_margins_collapse_with_children.start {
482 0.0
483 } else {
484 active_collapsible_margin_set.collapse_with_margin(resolved_margin.top).resolve()
485 };
486
487 item.computed_size = item_layout.size;
488 item.can_be_collapsed_through = item_layout.margins_can_collapse_through;
489 item.static_position = Point {
490 x: resolved_content_box_inset.left,
491 y: committed_y_offset + active_collapsible_margin_set.resolve(),
492 };
493 let mut location = Point {
494 x: resolved_content_box_inset.left + inset_offset.x + resolved_margin.left,
495 y: committed_y_offset + inset_offset.y + y_margin_offset,
496 };
497
498 let item_outer_width = item_layout.size.width + resolved_margin.horizontal_axis_sum();
500 if item_outer_width < container_inner_width {
501 match text_align {
502 TextAlign::Auto => {
503 }
505 TextAlign::LegacyLeft => {
506 }
508 TextAlign::LegacyRight => location.x += container_inner_width - item_outer_width,
509 TextAlign::LegacyCenter => location.x += (container_inner_width - item_outer_width) / 2.0,
510 }
511 }
512
513 let scrollbar_size = Size {
514 width: if item.overflow.y == Overflow::Scroll { item.scrollbar_width } else { 0.0 },
515 height: if item.overflow.x == Overflow::Scroll { item.scrollbar_width } else { 0.0 },
516 };
517
518 tree.set_unrounded_layout(
519 item.node_id,
520 &Layout {
521 order: item.order,
522 size: item_layout.size,
523 #[cfg(feature = "content_size")]
524 content_size: item_layout.content_size,
525 scrollbar_size,
526 location,
527 padding: item.padding,
528 border: item.border,
529 margin: resolved_margin,
530 },
531 );
532
533 #[cfg(feature = "content_size")]
534 {
535 inflow_content_size = inflow_content_size.f32_max(compute_content_size_contribution(
536 location,
537 final_size,
538 item_layout.content_size,
539 item.overflow,
540 ));
541 }
542
543 if is_collapsing_with_first_margin_set {
545 if item.can_be_collapsed_through {
546 first_child_top_margin_set = first_child_top_margin_set
547 .collapse_with_set(top_margin_set)
548 .collapse_with_set(bottom_margin_set);
549 } else {
550 first_child_top_margin_set = first_child_top_margin_set.collapse_with_set(top_margin_set);
551 is_collapsing_with_first_margin_set = false;
552 }
553 }
554
555 if item.can_be_collapsed_through {
557 active_collapsible_margin_set = active_collapsible_margin_set
558 .collapse_with_set(top_margin_set)
559 .collapse_with_set(bottom_margin_set);
560 y_offset_for_absolute = committed_y_offset + item_layout.size.height + y_margin_offset;
561 } else {
562 committed_y_offset += item_layout.size.height + y_margin_offset;
563 active_collapsible_margin_set = bottom_margin_set;
564 y_offset_for_absolute = committed_y_offset + active_collapsible_margin_set.resolve();
565 }
566 }
567 }
568
569 let last_child_bottom_margin_set = active_collapsible_margin_set;
570 let bottom_y_margin_offset =
571 if own_margins_collapse_with_children.end { 0.0 } else { last_child_bottom_margin_set.resolve() };
572
573 committed_y_offset += resolved_content_box_inset.bottom + bottom_y_margin_offset;
574 let content_height = f32_max(0.0, committed_y_offset);
575 (inflow_content_size, content_height, first_child_top_margin_set, last_child_bottom_margin_set)
576}
577
578#[inline]
580fn perform_absolute_layout_on_absolute_children(
581 tree: &mut impl LayoutBlockContainer,
582 items: &[BlockItem],
583 area_size: Size<f32>,
584 area_offset: Point<f32>,
585) -> Size<f32> {
586 let area_width = area_size.width;
587 let area_height = area_size.height;
588
589 #[cfg_attr(not(feature = "content_size"), allow(unused_mut))]
590 let mut absolute_content_size = Size::ZERO;
591
592 for item in items.iter().filter(|item| item.position == Position::Absolute) {
593 let child_style = tree.get_block_child_style(item.node_id);
594
595 if child_style.box_generation_mode() == BoxGenerationMode::None || child_style.position() != Position::Absolute
597 {
598 continue;
599 }
600
601 let aspect_ratio = child_style.aspect_ratio();
602 let margin =
603 child_style.margin().map(|margin| margin.resolve_to_option(area_width, |val, basis| tree.calc(val, basis)));
604 let padding = child_style.padding().resolve_or_zero(Some(area_width), |val, basis| tree.calc(val, basis));
605 let border = child_style.border().resolve_or_zero(Some(area_width), |val, basis| tree.calc(val, basis));
606 let padding_border_sum = (padding + border).sum_axes();
607 let box_sizing_adjustment =
608 if child_style.box_sizing() == BoxSizing::ContentBox { padding_border_sum } else { Size::ZERO };
609
610 let left = child_style.inset().left.maybe_resolve(area_width, |val, basis| tree.calc(val, basis));
612 let right = child_style.inset().right.maybe_resolve(area_width, |val, basis| tree.calc(val, basis));
613 let top = child_style.inset().top.maybe_resolve(area_height, |val, basis| tree.calc(val, basis));
614 let bottom = child_style.inset().bottom.maybe_resolve(area_height, |val, basis| tree.calc(val, basis));
615
616 let style_size = child_style
618 .size()
619 .maybe_resolve(area_size, |val, basis| tree.calc(val, basis))
620 .maybe_apply_aspect_ratio(aspect_ratio)
621 .maybe_add(box_sizing_adjustment);
622 let min_size = child_style
623 .min_size()
624 .maybe_resolve(area_size, |val, basis| tree.calc(val, basis))
625 .maybe_apply_aspect_ratio(aspect_ratio)
626 .maybe_add(box_sizing_adjustment)
627 .or(padding_border_sum.map(Some))
628 .maybe_max(padding_border_sum);
629 let max_size = child_style
630 .max_size()
631 .maybe_resolve(area_size, |val, basis| tree.calc(val, basis))
632 .maybe_apply_aspect_ratio(aspect_ratio)
633 .maybe_add(box_sizing_adjustment);
634 let mut known_dimensions = style_size.maybe_clamp(min_size, max_size);
635
636 drop(child_style);
637
638 if let (None, Some(left), Some(right)) = (known_dimensions.width, left, right) {
642 let new_width_raw = area_width.maybe_sub(margin.left).maybe_sub(margin.right) - left - right;
643 known_dimensions.width = Some(f32_max(new_width_raw, 0.0));
644 known_dimensions = known_dimensions.maybe_apply_aspect_ratio(aspect_ratio).maybe_clamp(min_size, max_size);
645 }
646
647 if let (None, Some(top), Some(bottom)) = (known_dimensions.height, top, bottom) {
651 let new_height_raw = area_height.maybe_sub(margin.top).maybe_sub(margin.bottom) - top - bottom;
652 known_dimensions.height = Some(f32_max(new_height_raw, 0.0));
653 known_dimensions = known_dimensions.maybe_apply_aspect_ratio(aspect_ratio).maybe_clamp(min_size, max_size);
654 }
655
656 let layout_output = tree.perform_child_layout(
657 item.node_id,
658 known_dimensions,
659 area_size.map(Some),
660 Size {
661 width: AvailableSpace::Definite(area_width.maybe_clamp(min_size.width, max_size.width)),
662 height: AvailableSpace::Definite(area_height.maybe_clamp(min_size.height, max_size.height)),
663 },
664 SizingMode::ContentSize,
665 Line::FALSE,
666 );
667 let measured_size = layout_output.size;
668 let final_size = known_dimensions.unwrap_or(measured_size).maybe_clamp(min_size, max_size);
669
670 let non_auto_margin = Rect {
671 left: if left.is_some() { margin.left.unwrap_or(0.0) } else { 0.0 },
672 right: if right.is_some() { margin.right.unwrap_or(0.0) } else { 0.0 },
673 top: if top.is_some() { margin.top.unwrap_or(0.0) } else { 0.0 },
674 bottom: if bottom.is_some() { margin.left.unwrap_or(0.0) } else { 0.0 },
675 };
676
677 let auto_margin = {
680 let absolute_auto_margin_space = Point {
683 x: right.map(|right| area_size.width - right - left.unwrap_or(0.0)).unwrap_or(final_size.width),
684 y: bottom.map(|bottom| area_size.height - bottom - top.unwrap_or(0.0)).unwrap_or(final_size.height),
685 };
686 let free_space = Size {
687 width: absolute_auto_margin_space.x - final_size.width - non_auto_margin.horizontal_axis_sum(),
688 height: absolute_auto_margin_space.y - final_size.height - non_auto_margin.vertical_axis_sum(),
689 };
690
691 let auto_margin_size = Size {
692 width: {
702 let auto_margin_count = margin.left.is_none() as u8 + margin.right.is_none() as u8;
703 if auto_margin_count == 2
704 && (style_size.width.is_none() || style_size.width.unwrap() >= free_space.width)
705 {
706 0.0
707 } else if auto_margin_count > 0 {
708 free_space.width / auto_margin_count as f32
709 } else {
710 0.0
711 }
712 },
713 height: {
714 let auto_margin_count = margin.top.is_none() as u8 + margin.bottom.is_none() as u8;
715 if auto_margin_count == 2
716 && (style_size.height.is_none() || style_size.height.unwrap() >= free_space.height)
717 {
718 0.0
719 } else if auto_margin_count > 0 {
720 free_space.height / auto_margin_count as f32
721 } else {
722 0.0
723 }
724 },
725 };
726
727 Rect {
728 left: margin.left.map(|_| 0.0).unwrap_or(auto_margin_size.width),
729 right: margin.right.map(|_| 0.0).unwrap_or(auto_margin_size.width),
730 top: margin.top.map(|_| 0.0).unwrap_or(auto_margin_size.height),
731 bottom: margin.bottom.map(|_| 0.0).unwrap_or(auto_margin_size.height),
732 }
733 };
734
735 let resolved_margin = Rect {
736 left: margin.left.unwrap_or(auto_margin.left),
737 right: margin.right.unwrap_or(auto_margin.right),
738 top: margin.top.unwrap_or(auto_margin.top),
739 bottom: margin.bottom.unwrap_or(auto_margin.bottom),
740 };
741
742 let location = Point {
743 x: left
744 .map(|left| left + resolved_margin.left)
745 .or(right.map(|right| area_size.width - final_size.width - right - resolved_margin.right))
746 .maybe_add(area_offset.x)
747 .unwrap_or(item.static_position.x + resolved_margin.left),
748 y: top
749 .map(|top| top + resolved_margin.top)
750 .or(bottom.map(|bottom| area_size.height - final_size.height - bottom - resolved_margin.bottom))
751 .maybe_add(area_offset.y)
752 .unwrap_or(item.static_position.y + resolved_margin.top),
753 };
754 let scrollbar_size = Size {
757 width: if item.overflow.y == Overflow::Scroll { item.scrollbar_width } else { 0.0 },
758 height: if item.overflow.x == Overflow::Scroll { item.scrollbar_width } else { 0.0 },
759 };
760
761 tree.set_unrounded_layout(
762 item.node_id,
763 &Layout {
764 order: item.order,
765 size: final_size,
766 #[cfg(feature = "content_size")]
767 content_size: layout_output.content_size,
768 scrollbar_size,
769 location,
770 padding,
771 border,
772 margin: resolved_margin,
773 },
774 );
775
776 #[cfg(feature = "content_size")]
777 {
778 absolute_content_size = absolute_content_size.f32_max(compute_content_size_contribution(
779 location,
780 final_size,
781 layout_output.content_size,
782 item.overflow,
783 ));
784 }
785 }
786
787 absolute_content_size
788}