diff --git a/crates/engine/src/assets/components/sync.rs b/crates/engine/src/assets/components/sync.rs index d7781db7e..1cda4aaa3 100644 --- a/crates/engine/src/assets/components/sync.rs +++ b/crates/engine/src/assets/components/sync.rs @@ -521,8 +521,14 @@ pub fn keep_text_overrides(doc: &mut Document, inst: NodeId) { /// Where auto layout places and sizes a rebuilt copy `n` of main layer `orig`, keep the old /// copy's `o` values (an instance's auto layout re-flows from its own content, so they usually /// differ from the main's): unchanged layers then stay unchanged, and auto layout re-flows wherever -/// something that sizes or places layers did change. +/// something that sizes or places layers did change. Only when `o` mirrored `orig` itself: a layer +/// matched to another variant's layer (or a new one) takes its geometry from it, so the re-flow +/// starts from the new main's consistent state instead of re-applying the resize to children that +/// already hold new-main sizes (which stretched them twice). fn keep_laid_out(doc: &Document, orig: &Node, o: &Node, n: &mut Node) { + if o.source != Some(orig.id) { + return; + } if o.parent != n.parent || o.overrides.contains("size") || o.overrides.contains("position") { return; } diff --git a/crates/engine/src/components_behaviour_tests.rs b/crates/engine/src/components_behaviour_tests.rs index e8dda9e95..29100be64 100644 --- a/crates/engine/src/components_behaviour_tests.rs +++ b/crates/engine/src/components_behaviour_tests.rs @@ -1417,3 +1417,50 @@ fn a_failing_create_set_leaves_no_components_behind() { assert_eq!(snapshot(&s), before); assert_eq!(s.selection(), selection); } + +/// One variant of a tab bar: an auto layout root holding an "Inner" fill frame with an absolute +/// stretched "BG" and `tabs` flow children. Returns the component. +fn tabs_variant(s: &mut Session, name: &str, x: f64, root_w: f64, tabs: usize) -> NodeId { + let root = idv(&ex(s, "frame.create", json!({"x": x, "y": 0, "width": root_w, "height": 100, "name": name}))); + ex(s, "node.set", json!({"id": root, "props": {"layout": "horizontal", "padding": 10, "mainAlign": "center"}})); + let inner_w = root_w - 20.0; + let inner = idv(&ex(s, "frame.create", json!({"x": 0, "y": 0, "width": inner_w, "height": 80, "parent": root, "name": "Inner"}))); + ex(s, "node.set", json!({"id": inner, "props": {"layout": "horizontal", "sizingH": "fill", "sizingV": "fixed", "gap": 0, "padding": 0}})); + let bg = idv(&ex(s, "shape.rectangle", json!({"x": 0, "y": 0, "width": inner_w, "height": 80, "parent": inner, "props": {"name": "BG"}}))); + ex(s, "node.set", json!({"id": bg, "props": {"absolute": true, "constraints": {"horizontal": "leftRight", "vertical": "topBottom"}}})); + for t in 0..tabs { + ex(s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 60, "height": 80, "parent": inner, "props": {"name": format!("Tab{t}")}})); + } + idv(&ex(s, "component.create", json!({"ids": [root]}))) +} + +/// (root, inner, bg) widths and the bg's left edge of an instance of a [`tabs_variant`]. +fn tabs_geometry(s: &Session, inst: NodeId) -> (f64, f64, f64, f64) { + let inner = kids(s, inst).into_iter().find(|c| node(s, *c).name == "Inner").unwrap(); + let bg = kids(s, inner).into_iter().find(|c| node(s, *c).name == "BG").unwrap(); + let [.., x, _] = node(s, bg).transform.as_coeffs(); + (node(s, inst).width, node(s, inner).width, node(s, bg).width, x) +} + +#[test] +fn switching_variants_re_lays_out_stretched_children_like_a_fresh_instance() { + let mut s = Session::new(); + let narrow = tabs_variant(&mut s, "Tabs=2", 0.0, 200.0, 2); + let wide = tabs_variant(&mut s, "Tabs=4", 300.0, 400.0, 4); + let set = idv(&ex(&mut s, "component.combineVariants", json!({"ids": [narrow, wide]}))); + let i = idv(&ex(&mut s, "instance.create", json!({"component": set, "x": 0, "y": 300}))); + assert_eq!(component_of(&s, i), narrow, "the top-left variant is the default"); + // Growing: switching to the wider variant lays out like an instance made from it directly. + ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Tabs": "4"}})); + let direct = idv(&ex(&mut s, "instance.create", json!({"component": wide, "x": 500, "y": 300}))); + assert_eq!(tabs_geometry(&s, i), tabs_geometry(&s, direct), "switched matches a fresh wide instance"); + let (_, inner, bg, x) = tabs_geometry(&s, i); + assert_eq!((bg, x), (inner, 0.0), "the stretched background exactly fits its frame"); + // Shrinking back: same, the other way. + ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Tabs": "2"}})); + let direct = idv(&ex(&mut s, "instance.create", json!({"component": narrow, "x": 500, "y": 500}))); + assert_eq!(tabs_geometry(&s, i), tabs_geometry(&s, direct), "switched matches a fresh narrow instance"); + let (_, inner, bg, x) = tabs_geometry(&s, i); + assert_eq!((bg, x), (inner, 0.0), "the stretched background exactly fits its frame"); + healthy(&s); +}