Fix Empty sometimes returning an infinite size in layout

This commit also ensures that elements never report an infinite size by
adding a debug assertion in the `Lifecycle` element.
This commit is contained in:
Antonio Scandurra 2021-03-26 09:46:53 +01:00
parent 1cb987d489
commit 5439213199
2 changed files with 19 additions and 2 deletions

View file

@ -1,7 +1,10 @@
use crate::geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
};
use crate::{
AfterLayoutContext, Element, Event, EventContext, LayoutContext, PaintContext, SizeConstraint,
};
use pathfinder_geometry::{rect::RectF, vector::Vector2F};
pub struct Empty;
@ -20,7 +23,18 @@ impl Element for Empty {
constraint: SizeConstraint,
_: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) {
(constraint.max, ())
let x = if constraint.max.x().is_finite() {
constraint.max.x()
} else {
constraint.min.x()
};
let y = if constraint.max.y().is_finite() {
constraint.max.y()
} else {
constraint.min.y()
};
(vec2f(x, y), ())
}
fn after_layout(&mut self, _: Vector2F, _: &mut Self::LayoutState, _: &mut AfterLayoutContext) {

View file

@ -87,6 +87,9 @@ impl<T: Element> AnyElement for Lifecycle<T> {
| Lifecycle::PostLayout { mut element, .. }
| Lifecycle::PostPaint { mut element, .. } => {
let (size, layout) = element.layout(constraint, ctx);
debug_assert!(size.x().is_finite());
debug_assert!(size.y().is_finite());
result = Some(size);
Lifecycle::PostLayout {
element,