Optimizing Stack layout in Known dimensions

This commit is contained in:
Jonathan Johnson 2023-11-14 07:59:13 -08:00
parent a04619a279
commit b72e4b0caf
No known key found for this signature in database
GPG key ID: A66D6A34D6620579

View file

@ -400,6 +400,11 @@ impl Layout {
let allocated_space = let allocated_space =
self.allocated_space.0 + self.allocated_space.1.into_px(scale).into_unsigned(); self.allocated_space.0 + self.allocated_space.1.into_px(scale).into_unsigned();
let mut remaining = available_space.saturating_sub(allocated_space); let mut remaining = available_space.saturating_sub(allocated_space);
// If our `other_constraint` is not known, we will need to give child
// widgets an opportunity to lay themselves out in the full area. This
// requires one extra layout call, so we avoid persisting layouts during
// the first loop if this is the case.
let needs_final_layout = !matches!(other_constraint, ConstraintLimit::Known(_));
// Measure the children that fit their content // Measure the children that fit their content
self.other = UPx(0); self.other = UPx(0);
@ -409,7 +414,7 @@ impl Layout {
index, index,
self.orientation self.orientation
.make_size(ConstraintLimit::ClippedAfter(remaining), other_constraint), .make_size(ConstraintLimit::ClippedAfter(remaining), other_constraint),
false, !needs_final_layout,
)); ));
self.layouts[index].size = measured; self.layouts[index].size = measured;
self.other = self.other.max(other); self.other = self.other.max(other);
@ -450,7 +455,7 @@ impl Layout {
), ),
other_constraint, other_constraint,
), ),
true, !needs_final_layout,
)); ));
self.other = self.other.max(measured); self.other = self.other.max(measured);
} }
@ -461,19 +466,23 @@ impl Layout {
ConstraintLimit::ClippedAfter(clip_limit) => self.other.min(clip_limit), ConstraintLimit::ClippedAfter(clip_limit) => self.other.min(clip_limit),
}; };
// Finally layout the widgets with the final constraints // Finally, compute the offsets of all of the widgets.
let mut offset = UPx(0); let mut offset = UPx(0);
for index in 0..self.children.len() { for index in 0..self.children.len() {
self.layouts[index].offset = offset; self.layouts[index].offset = offset;
offset += self.layouts[index].size; offset += self.layouts[index].size;
self.orientation.split_size(measure( if needs_final_layout {
index, self.orientation.split_size(measure(
self.orientation.make_size( index,
ConstraintLimit::Known(self.layouts[index].size.into_px(scale).into_unsigned()), self.orientation.make_size(
ConstraintLimit::Known(self.other), ConstraintLimit::Known(
), self.layouts[index].size.into_px(scale).into_unsigned(),
true, ),
)); ConstraintLimit::Known(self.other),
),
true,
));
}
} }
self.orientation.make_size(offset, self.other) self.orientation.make_size(offset, self.other)