mirror of
https://github.com/penpot/penpot.git
synced 2026-11-26 21:00:32 +00:00
🐛 Fix render-wasm panic when an auto layout has only absolute children
An auto flex or grid layout whose children were all absolute shrank to zero, and a rotated shape below it got a matrix that cannot be inverted, which panicked in `_propagate_modifiers`. Skip auto-size when there is no content, as CLJS does. A matrix that still cannot be inverted is now a recoverable error, which shows a toast instead of the error page. Closes #12763 AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
35278f3917
commit
f807e749db
@ -1039,4 +1039,228 @@ mod tests {
|
||||
|
||||
assert!(is_close_to(bounds.min_x(), 0.0));
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum TestLayout {
|
||||
None,
|
||||
Flex,
|
||||
Grid,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Child {
|
||||
InFlow,
|
||||
Absolute,
|
||||
Hidden,
|
||||
}
|
||||
|
||||
/// Adds a shape with no gaps or padding: auto-sized at the root, fixed below.
|
||||
fn add_shape(
|
||||
state: &mut State,
|
||||
parent: Option<Uuid>,
|
||||
(left, top, right, bottom): (f32, f32, f32, f32),
|
||||
layout: TestLayout,
|
||||
) -> Uuid {
|
||||
let id = Uuid::new_v4();
|
||||
let sizing = if parent.is_some() {
|
||||
Sizing::Fix
|
||||
} else {
|
||||
Sizing::Auto
|
||||
};
|
||||
let shape = state.shapes.add_shape(id);
|
||||
if !matches!(layout, TestLayout::None) {
|
||||
shape.set_shape_type(Type::Frame(Frame::default()));
|
||||
}
|
||||
let (align, justify) = (AlignItems::Start, JustifyContent::Start);
|
||||
let (content, items) = (AlignContent::Start, JustifyItems::Start);
|
||||
match layout {
|
||||
TestLayout::Flex => shape.set_flex_layout_data(
|
||||
FlexDirection::Row,
|
||||
0.0,
|
||||
0.0,
|
||||
align,
|
||||
content,
|
||||
items,
|
||||
justify,
|
||||
WrapType::NoWrap,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
),
|
||||
TestLayout::Grid => shape.set_grid_layout_data(
|
||||
GridDirection::Row,
|
||||
0.0,
|
||||
0.0,
|
||||
align,
|
||||
content,
|
||||
items,
|
||||
justify,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
),
|
||||
TestLayout::None => {}
|
||||
}
|
||||
shape.set_selrect(left, top, right, bottom);
|
||||
shape.set_flex_layout_child_data(
|
||||
0.0, 0.0, 0.0, 0.0, sizing, sizing, None, None, None, None, None, false, None,
|
||||
);
|
||||
if let Some(parent) = parent {
|
||||
shape.set_parent(parent);
|
||||
state.shapes.get_mut(&parent).unwrap().add_child(id);
|
||||
}
|
||||
id
|
||||
}
|
||||
|
||||
fn shape_mut(state: &mut State, id: Uuid) -> &mut Shape {
|
||||
state.shapes.get_mut(&id).unwrap()
|
||||
}
|
||||
|
||||
fn set_absolute(state: &mut State, id: Uuid) {
|
||||
shape_mut(state, id)
|
||||
.layout_item
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.is_absolute = true;
|
||||
}
|
||||
|
||||
fn rotate(state: &mut State, id: Uuid, degrees: f32) {
|
||||
let (sin, cos) = degrees.to_radians().sin_cos();
|
||||
let shape = shape_mut(state, id);
|
||||
shape.set_rotation(degrees);
|
||||
shape.set_transform(cos, sin, -sin, cos, 0.0, 0.0);
|
||||
}
|
||||
|
||||
/// Reflows every layout above `id`, as a committed text resize does.
|
||||
fn reflow(state: &State, id: Uuid) -> Vec<TransformEntry> {
|
||||
let entry = TransformEntry::from_input(id, Matrix::default());
|
||||
propagate_modifiers(state, &[entry], PixelPrecision::Disabled).unwrap()
|
||||
}
|
||||
|
||||
fn size_after(state: &State, result: &[TransformEntry], id: Uuid) -> (f32, f32) {
|
||||
let mut bounds = state.shapes.get_raw(&id).unwrap().bounds();
|
||||
if let Some(entry) = result.iter().find(|e| e.id == id) {
|
||||
bounds = bounds.transform(&entry.transform);
|
||||
}
|
||||
(bounds.width(), bounds.height())
|
||||
}
|
||||
|
||||
fn assert_size((width, height): (f32, f32), (expected_w, expected_h): (f32, f32)) {
|
||||
assert!(
|
||||
is_close_to(width, expected_w) && is_close_to(height, expected_h),
|
||||
"got {width}x{height}, expected {expected_w}x{expected_h}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rotated_shape_under_absolute_child_of_auto_flex_does_not_collapse() {
|
||||
// Tree from #12763: auto flex > absolute frame > rotated flex row > leaf.
|
||||
for absolute_layout in [TestLayout::Flex, TestLayout::Grid] {
|
||||
let mut state = State::new();
|
||||
state.shapes.initialize(10);
|
||||
let root = (-1545.0, -72.0, 232.0, 824.0);
|
||||
let auto = add_shape(&mut state, None, root, TestLayout::Flex);
|
||||
let frame = (-1399.0, 9.0, 72.75, 763.0);
|
||||
let absolute = add_shape(&mut state, Some(auto), frame, absolute_layout);
|
||||
set_absolute(&mut state, absolute);
|
||||
let row = (-1379.0, 29.0, -1043.0, 83.0);
|
||||
let row = add_shape(&mut state, Some(absolute), row, TestLayout::Flex);
|
||||
let leaf = (-1365.0, 47.0, -1335.0, 64.0);
|
||||
let leaf = add_shape(&mut state, Some(row), leaf, TestLayout::None);
|
||||
rotate(&mut state, row, 0.0915);
|
||||
rotate(&mut state, leaf, 0.0915);
|
||||
|
||||
let result = reflow(&state, leaf);
|
||||
|
||||
for entry in &result {
|
||||
let (width, height) = size_after(&state, &result, entry.id);
|
||||
assert!(width > 1.0 && height > 1.0, "{} collapsed", entry.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_collapsing_a_rotated_frame_is_a_recoverable_error() {
|
||||
let mut state = State::new();
|
||||
state.shapes.initialize(10);
|
||||
let frame = (0.0, 0.0, 300.0, 100.0);
|
||||
let frame = add_shape(&mut state, None, frame, TestLayout::Flex);
|
||||
let child = add_shape(
|
||||
&mut state,
|
||||
Some(frame),
|
||||
(10.0, 10.0, 50.0, 50.0),
|
||||
TestLayout::None,
|
||||
);
|
||||
set_absolute(&mut state, child);
|
||||
rotate(&mut state, frame, 0.0915);
|
||||
|
||||
// Squash the frame to zero width: its rotated corners end on one line.
|
||||
let entry = TransformEntry::from_input(frame, Matrix::scale((0.0, 1.0)));
|
||||
let result = propagate_modifiers(&state, &[entry], PixelPrecision::Disabled);
|
||||
|
||||
assert!(matches!(
|
||||
result,
|
||||
Err(crate::error::Error::RecoverableError(_))
|
||||
));
|
||||
}
|
||||
|
||||
/// Size of an auto-size 500x300 frame with one 100x50 child, after reflow.
|
||||
/// Grids get two fixed 100px columns and one fixed 50px row.
|
||||
fn auto_frame_size(layout: TestLayout, child: Child) -> (f32, f32) {
|
||||
let mut state = State::new();
|
||||
state.shapes.initialize(10);
|
||||
let frame = add_shape(&mut state, None, (0.0, 0.0, 500.0, 300.0), layout);
|
||||
let rect = (10.0, 10.0, 110.0, 60.0);
|
||||
let child_id = add_shape(&mut state, Some(frame), rect, TestLayout::None);
|
||||
match child {
|
||||
Child::Absolute => set_absolute(&mut state, child_id),
|
||||
Child::Hidden => shape_mut(&mut state, child_id).set_hidden(true),
|
||||
Child::InFlow => {}
|
||||
}
|
||||
if matches!(layout, TestLayout::Grid) {
|
||||
let column = GridTrack {
|
||||
track_type: GridTrackType::Fixed,
|
||||
value: 100.0,
|
||||
};
|
||||
let row = GridTrack {
|
||||
track_type: GridTrackType::Fixed,
|
||||
value: 50.0,
|
||||
};
|
||||
let shape = shape_mut(&mut state, frame);
|
||||
shape.set_grid_columns(vec![column.clone(), column]);
|
||||
shape.set_grid_rows(vec![row]);
|
||||
if child != Child::Absolute {
|
||||
shape.set_grid_cells(vec![GridCell {
|
||||
row: 1,
|
||||
row_span: 1,
|
||||
column: 1,
|
||||
column_span: 1,
|
||||
align_self: None,
|
||||
justify_self: None,
|
||||
shape: Some(child_id),
|
||||
}]);
|
||||
}
|
||||
}
|
||||
let result = reflow(&state, frame);
|
||||
size_after(&state, &result, frame)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_auto_flex_size_by_child_kind() {
|
||||
use Child::*;
|
||||
assert_size(auto_frame_size(TestLayout::Flex, InFlow), (100.0, 50.0));
|
||||
assert_size(auto_frame_size(TestLayout::Flex, Absolute), (500.0, 300.0));
|
||||
// Known difference from CLJS, which sizes the frame to hidden children.
|
||||
assert_size(auto_frame_size(TestLayout::Flex, Hidden), (500.0, 300.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_auto_grid_size_by_child_kind() {
|
||||
use Child::*;
|
||||
assert_size(auto_frame_size(TestLayout::Grid, InFlow), (200.0, 50.0));
|
||||
assert_size(auto_frame_size(TestLayout::Grid, Absolute), (500.0, 300.0));
|
||||
assert_size(auto_frame_size(TestLayout::Grid, Hidden), (500.0, 300.0));
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use crate::math::Bounds;
|
||||
use crate::shapes::Shape;
|
||||
use crate::state::ShapesPoolRef;
|
||||
use crate::uuid::Uuid;
|
||||
|
||||
pub trait GetBounds {
|
||||
@ -13,3 +14,12 @@ impl GetBounds for HashMap<Uuid, Bounds> {
|
||||
self.get(&shape.id).copied().unwrap_or(shape.bounds())
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a layout has a child that takes part in the flow (neither absolute
|
||||
/// nor hidden). Without one there is no content to auto-size the layout to.
|
||||
pub fn has_in_flow_children(shape: &Shape, shapes: ShapesPoolRef) -> bool {
|
||||
shape
|
||||
.children_ids_iter(true)
|
||||
.filter_map(|id| shapes.get(id))
|
||||
.any(|child| !child.is_absolute() && !child.hidden())
|
||||
}
|
||||
|
||||
@ -134,7 +134,7 @@ pub fn propagate_shape_constraints(
|
||||
parent_transform.post_translate(center);
|
||||
parent_transform.pre_translate(-center);
|
||||
|
||||
let parent_transform_inv = &parent_transform.invert().ok_or(Error::CriticalError(
|
||||
let parent_transform_inv = &parent_transform.invert().ok_or(Error::RecoverableError(
|
||||
"Failed to invert parent transform".to_string(),
|
||||
))?;
|
||||
let origin = parent_transform_inv.map_point(child_bounds_after.nw);
|
||||
|
||||
@ -9,7 +9,7 @@ use crate::uuid::Uuid;
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
use super::common::GetBounds;
|
||||
use super::common::{has_in_flow_children, GetBounds};
|
||||
|
||||
const MIN_SIZE: f32 = 0.01;
|
||||
const MAX_SIZE: f32 = f32::INFINITY;
|
||||
@ -686,7 +686,11 @@ pub fn reflow_flex_layout(
|
||||
}
|
||||
}
|
||||
|
||||
if layout_axis.is_auto_across || layout_axis.is_auto_main {
|
||||
// Like the CLJS layout, keep the size when there is no content to fit:
|
||||
// auto-sizing to padding only would collapse the frame.
|
||||
if (layout_axis.is_auto_across || layout_axis.is_auto_main)
|
||||
&& has_in_flow_children(shape, shapes)
|
||||
{
|
||||
let width = layout_bounds.width();
|
||||
let height = layout_bounds.height();
|
||||
|
||||
@ -749,7 +753,7 @@ pub fn reflow_flex_layout(
|
||||
|
||||
let parent_transform = layout_bounds.transform_matrix().unwrap_or_default();
|
||||
|
||||
let parent_transform_inv = &parent_transform.invert().ok_or(Error::CriticalError(
|
||||
let parent_transform_inv = &parent_transform.invert().ok_or(Error::RecoverableError(
|
||||
"Failed to invert parent transform".to_string(),
|
||||
))?;
|
||||
let origin = parent_transform_inv.map_point(layout_bounds.nw);
|
||||
|
||||
@ -10,7 +10,7 @@ use crate::uuid::Uuid;
|
||||
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
|
||||
use super::common::GetBounds;
|
||||
use super::common::{has_in_flow_children, GetBounds};
|
||||
|
||||
const MIN_SIZE: f32 = 0.01;
|
||||
const MAX_SIZE: f32 = f32::INFINITY;
|
||||
@ -890,7 +890,11 @@ pub fn reflow_grid_layout(
|
||||
}
|
||||
}
|
||||
|
||||
if shape.is_layout_horizontal_auto() || shape.is_layout_vertical_auto() {
|
||||
// Like the CLJS layout, keep the size when there is no content to fit:
|
||||
// auto-sizing to empty tracks would collapse the frame.
|
||||
if (shape.is_layout_horizontal_auto() || shape.is_layout_vertical_auto())
|
||||
&& has_in_flow_children(shape, shapes)
|
||||
{
|
||||
let width = layout_bounds.width();
|
||||
let height = layout_bounds.height();
|
||||
|
||||
@ -915,7 +919,7 @@ pub fn reflow_grid_layout(
|
||||
|
||||
let parent_transform = layout_bounds.transform_matrix().unwrap_or_default();
|
||||
|
||||
let parent_transform_inv = &parent_transform.invert().ok_or(Error::CriticalError(
|
||||
let parent_transform_inv = &parent_transform.invert().ok_or(Error::RecoverableError(
|
||||
"Failed to invert parent transform".to_string(),
|
||||
))?;
|
||||
let origin = parent_transform_inv.map_point(layout_bounds.nw);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user