1use crate::geometry::{Point, Size};
4use crate::style::{AvailableSpace, Overflow, Position};
5use crate::tree::{CollapsibleMarginSet, RunMode};
6use crate::tree::{LayoutInput, LayoutOutput, SizingMode};
7use crate::util::debug::debug_log;
8use crate::util::sys::f32_max;
9use crate::util::MaybeMath;
10use crate::util::{MaybeResolve, ResolveOrZero};
11use crate::{BoxSizing, CoreStyle};
12use core::unreachable;
13
14pub fn compute_leaf_layout<MeasureFunction>(
16 inputs: LayoutInput,
17 style: &impl CoreStyle,
18 measure_function: MeasureFunction,
19) -> LayoutOutput
20where
21 MeasureFunction: FnOnce(Size<Option<f32>>, Size<AvailableSpace>) -> Size<f32>,
22{
23 let LayoutInput { known_dimensions, parent_size, available_space, sizing_mode, run_mode, .. } = inputs;
24
25 let margin = style.margin().resolve_or_zero(parent_size.width);
28 let padding = style.padding().resolve_or_zero(parent_size.width);
29 let border = style.border().resolve_or_zero(parent_size.width);
30 let padding_border = padding + border;
31 let pb_sum = padding_border.sum_axes();
32 let box_sizing_adjustment = if style.box_sizing() == BoxSizing::ContentBox { pb_sum } else { Size::ZERO };
33
34 let (node_size, node_min_size, node_max_size, aspect_ratio) = match sizing_mode {
37 SizingMode::ContentSize => {
38 let node_size = known_dimensions;
39 let node_min_size = Size::NONE;
40 let node_max_size = Size::NONE;
41 (node_size, node_min_size, node_max_size, None)
42 }
43 SizingMode::InherentSize => {
44 let aspect_ratio = style.aspect_ratio();
45 let style_size = style
46 .size()
47 .maybe_resolve(parent_size)
48 .maybe_apply_aspect_ratio(aspect_ratio)
49 .maybe_add(box_sizing_adjustment);
50 let style_min_size = style
51 .min_size()
52 .maybe_resolve(parent_size)
53 .maybe_apply_aspect_ratio(aspect_ratio)
54 .maybe_add(box_sizing_adjustment);
55 let style_max_size = style.max_size().maybe_resolve(parent_size).maybe_add(box_sizing_adjustment);
56
57 let node_size = known_dimensions.or(style_size);
58 (node_size, style_min_size, style_max_size, aspect_ratio)
59 }
60 };
61
62 let scrollbar_gutter = style.overflow().transpose().map(|overflow| match overflow {
66 Overflow::Scroll => style.scrollbar_width(),
67 _ => 0.0,
68 });
69 let mut content_box_inset = padding_border;
71 content_box_inset.right += scrollbar_gutter.x;
72 content_box_inset.bottom += scrollbar_gutter.y;
73
74 let has_styles_preventing_being_collapsed_through = !style.is_block()
75 || style.overflow().x.is_scroll_container()
76 || style.overflow().y.is_scroll_container()
77 || style.position() == Position::Absolute
78 || padding.top > 0.0
79 || padding.bottom > 0.0
80 || border.top > 0.0
81 || border.bottom > 0.0
82 || matches!(node_size.height, Some(h) if h > 0.0)
83 || matches!(node_min_size.height, Some(h) if h > 0.0);
84
85 debug_log!("LEAF");
86 debug_log!("node_size", dbg:node_size);
87 debug_log!("min_size ", dbg:node_min_size);
88 debug_log!("max_size ", dbg:node_max_size);
89
90 if run_mode == RunMode::ComputeSize && has_styles_preventing_being_collapsed_through {
92 if let Size { width: Some(width), height: Some(height) } = node_size {
93 let size = Size { width, height }
94 .maybe_clamp(node_min_size, node_max_size)
95 .maybe_max(padding_border.sum_axes().map(Some));
96 return LayoutOutput {
97 size,
98 #[cfg(feature = "content_size")]
99 content_size: Size::ZERO,
100 first_baselines: Point::NONE,
101 top_margin: CollapsibleMarginSet::ZERO,
102 bottom_margin: CollapsibleMarginSet::ZERO,
103 margins_can_collapse_through: false,
104 };
105 };
106 }
107
108 let available_space = Size {
110 width: known_dimensions
111 .width
112 .map(AvailableSpace::from)
113 .unwrap_or(available_space.width)
114 .maybe_sub(margin.horizontal_axis_sum())
115 .maybe_set(known_dimensions.width)
116 .maybe_set(node_size.width)
117 .maybe_set(node_max_size.width)
118 .map_definite_value(|size| {
119 size.maybe_clamp(node_min_size.width, node_max_size.width) - content_box_inset.horizontal_axis_sum()
120 }),
121 height: known_dimensions
122 .height
123 .map(AvailableSpace::from)
124 .unwrap_or(available_space.height)
125 .maybe_sub(margin.vertical_axis_sum())
126 .maybe_set(known_dimensions.height)
127 .maybe_set(node_size.height)
128 .maybe_set(node_max_size.height)
129 .map_definite_value(|size| {
130 size.maybe_clamp(node_min_size.height, node_max_size.height) - content_box_inset.vertical_axis_sum()
131 }),
132 };
133
134 let measured_size = measure_function(
136 match run_mode {
137 RunMode::ComputeSize => known_dimensions,
138 RunMode::PerformLayout => Size::NONE,
139 RunMode::PerformHiddenLayout => unreachable!(),
140 },
141 available_space,
142 );
143 let clamped_size = known_dimensions
144 .or(node_size)
145 .unwrap_or(measured_size + content_box_inset.sum_axes())
146 .maybe_clamp(node_min_size, node_max_size);
147 let size = Size {
148 width: clamped_size.width,
149 height: f32_max(clamped_size.height, aspect_ratio.map(|ratio| clamped_size.width / ratio).unwrap_or(0.0)),
150 };
151 let size = size.maybe_max(padding_border.sum_axes().map(Some));
152
153 LayoutOutput {
154 size,
155 #[cfg(feature = "content_size")]
156 content_size: measured_size + padding.sum_axes(),
157 first_baselines: Point::NONE,
158 top_margin: CollapsibleMarginSet::ZERO,
159 bottom_margin: CollapsibleMarginSet::ZERO,
160 margins_can_collapse_through: !has_styles_preventing_being_collapsed_through
161 && size.height == 0.0
162 && measured_size.height == 0.0,
163 }
164}