M1: only empty frames keep their size when adding auto layout

This commit is contained in:
Ahmed5Emad
2026-10-15 14:46:20 +03:00
parent 93a75a4720
commit 48b62a8b95
3 changed files with 10 additions and 8 deletions
+3 -3
View File
@@ -182,13 +182,13 @@ fn vertical_wrap_columns_take_arrows_and_drops() {
#[test]
fn adding_auto_layout_to_a_frame_keeps_its_size() {
// Like the incumbent: an existing frame keeps its size (fixed); only a frame wrapped
// around the selection hugs it.
// An existing frame with content hugs it (padding inferred, so the size is preserved);
// an empty one keeps its size instead of collapsing to its padding.
let mut s = Session::new();
let f = id(s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 300, "height": 200})).unwrap());
rect(&mut s, f, 40.0, 20.0, "#FF0000");
s.execute("layout.add", &json!({"ids": [f]})).unwrap();
assert_eq!(sizing(&s, f), (Sizing::Fixed, Sizing::Fixed));
assert_eq!(sizing(&s, f), (Sizing::Hug, Sizing::Hug));
assert_eq!(size(&s, f), (300.0, 200.0));
// An empty frame keeps its size too.
let e = id(s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 300, "height": 200})).unwrap());
+3 -3
View File
@@ -94,9 +94,9 @@ fn nest(s: &mut Session) -> Nest {
let x = rect(s, Some(inner), 0.0, 0.0, 50.0, 50.0);
let y = rect(s, Some(inner), 60.0, 0.0, 50.0, 50.0);
s.execute("layout.add", &json!({"ids": [inner], "direction": "horizontal"})).unwrap();
set(s, inner, json!({"padding": 10, "gap": 10, "sizingH": "hug", "sizingV": "hug"}));
set(s, inner, json!({"padding": 10, "gap": 10}));
s.execute("layout.add", &json!({"ids": [outer], "direction": "vertical"})).unwrap();
set(s, outer, json!({"padding": 10, "gap": 10, "sizingH": "fixed", "sizingV": "hug", "width": 300}));
set(s, outer, json!({"padding": 10, "gap": 10, "sizingH": "fixed", "width": 300}));
s.execute("select.none", &json!({})).unwrap();
Nest { outer, a, b, inner, x, y }
}
@@ -311,7 +311,7 @@ fn fill_text_in_a_hugging_card_sets_the_height_in_one_step() {
.execute("text.create", &json!({"text": "A fairly long line of text that has to wrap inside the card", "x": 0, "y": 0, "parent": card}))
.unwrap());
s.execute("layout.add", &json!({"ids": [card], "direction": "vertical"})).unwrap();
set(&mut s, card, json!({"padding": 10, "sizingH": "fixed", "sizingV": "hug", "width": 160}));
set(&mut s, card, json!({"padding": 10, "sizingH": "fixed", "width": 160}));
set(&mut s, t, json!({"sizingH": "fill"}));
let (tb, cb) = (bounds(&s, t), bounds(&s, card));
assert_eq!(tb.width(), 140.0);
+4 -2
View File
@@ -477,11 +477,13 @@ fn layout_add(s: &mut Session, p: &Value) -> Result<Value> {
let pb = doc.node(*b).map(|n| figcraft_geom::transform_rect_bbox(n.transform, n.local_rect())).unwrap_or_default();
if vertical { pa.y0.total_cmp(&pb.y0) } else { pa.x0.total_cmp(&pb.x0) }
});
let empty = doc.node(*f).is_some_and(|n| n.children.iter().filter_map(|c| doc.node(*c)).all(|c| !c.visible || c.child_layout.absolute));
let n = doc.get_mut(*f)?;
n.children = kids;
n.layout = Some(al);
if !all_frames {
// A frame wrapped around the selection hugs it; an existing frame keeps its size.
if !all_frames || !empty {
// A frame wrapped around the selection hugs it; an existing frame with no flow
// children keeps its size instead of collapsing to its padding.
n.child_layout.sizing_h = Sizing::Hug;
n.child_layout.sizing_v = Sizing::Hug;
}