From e7047e2aeae6e493075b6bf23fc7687681dfedce Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:37:20 +0100 Subject: [PATCH 1/5] M3: Swap library re-points the styles and variables of the copies it keeps (#2568) library.swap re-pointed style links and variable bindings only on layers outside the read-only library copies. A copy the swap keeps (no match in the other library, or another library's component using the swapped style) kept the old style and variable, so instances placed after the swap showed the old colours. The copies' own layers are re-pointed too. --- crates/engine/src/assets/library_swap.rs | 6 +- .../engine/src/assets/library_swap/tests.rs | 70 ++++++++++++++++++- docs/components-parity.md | 5 +- 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/assets/library_swap.rs b/crates/engine/src/assets/library_swap.rs index e3b312cf8..e6071e0de 100644 --- a/crates/engine/src/assets/library_swap.rs +++ b/crates/engine/src/assets/library_swap.rs @@ -210,8 +210,10 @@ pub fn swap_library(doc: &mut Document, from: &str, to: &PublishedLibrary) -> Re out.unmatched.push(format!("extended collection {}", c.name)); } - // Re-point layers outside the library copies, and local variables aliasing A's. - let ids: Vec = doc.nodes().filter(|n| outside(doc, n.id)).map(|n| n.id).collect(); + // Re-point the layers, and local variables aliasing A's. The library copies' own layers too: + // a copy B has no match for stays (A's, or another library's), and instances placed from it + // later must link what the ones re-pointed here link (#2568). + let ids: Vec = doc.nodes().map(|n| n.id).collect(); for id in ids { let Ok(n) = doc.get_mut(id) else { continue }; for r in [&mut n.styles.fill, &mut n.styles.stroke, &mut n.styles.effect, &mut n.styles.grid, &mut n.styles.text] { diff --git a/crates/engine/src/assets/library_swap/tests.rs b/crates/engine/src/assets/library_swap/tests.rs index d90996576..a132e4610 100644 --- a/crates/engine/src/assets/library_swap/tests.rs +++ b/crates/engine/src/assets/library_swap/tests.rs @@ -31,6 +31,12 @@ impl Drop for Scratch { /// A library file at `path` with "Button" (filled `color`, a "Label" text), a "Brand" paint style, /// a "Theme" collection with an `accent` colour, and `extra` components; returns its id. fn library(path: &str, color: &str, extra: &[&str]) -> String { + library_with(path, color, extra, false) +} + +/// [`library`], and with `linked` also "Badge", filled by the Brand style, and "Dot", its fill +/// bound to `accent`: components whose own layers link the library's style and variable. +fn library_with(path: &str, color: &str, extra: &[&str], linked: bool) -> String { let mut s = Session::new(); let f = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 100, "height": 40, "name": "Button"}))); ex(&mut s, "text.create", json!({"text": "OK", "x": 10, "y": 10, "parent": f, "props": {"name": "Label"}})); @@ -41,6 +47,14 @@ fn library(path: &str, color: &str, extra: &[&str]) -> String { ex(&mut s, "style.create", json!({"type": "paint", "name": "Brand", "from": r})); ex(&mut s, "variable.createCollection", json!({"name": "Theme", "modes": ["Light", "Dark"]})); ex(&mut s, "variable.create", json!({"collection": "Theme", "name": "accent", "type": "color", "value": color})); + if linked { + let badge = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 300, "width": 30, "height": 30, "name": "Badge"}))); + ex(&mut s, "style.apply", json!({"ids": [badge], "style": "Brand"})); + let dot = idv(&ex(&mut s, "frame.create", json!({"x": 50, "y": 300, "width": 30, "height": 30, "name": "Dot"}))); + ex(&mut s, "variable.bind", json!({"ids": [dot], "field": "fill", "variable": "accent"})); + ex(&mut s, "component.create", json!({"ids": [badge]})); + ex(&mut s, "component.create", json!({"ids": [dot]})); + } for (i, name) in extra.iter().enumerate() { let e = idv(&ex(&mut s, "frame.create", json!({"x": 200 + 50 * i, "y": 0, "width": 20, "height": 20, "name": name}))); ex(&mut s, "component.create", json!({"ids": [e]})); @@ -60,10 +74,15 @@ fn main_of(s: &Session, inst: NodeId) -> NodeId { /// A session with libraries A (blue, plus `extra_a` components) and B (green) enabled, files in a /// scratch directory named by `tag`; returns the directory guard, the session and both library ids. fn two_libraries(tag: &str, extra_a: &[&str]) -> (Scratch, Session, String, String) { + two_libraries_with(tag, extra_a, false) +} + +/// [`two_libraries`], with A made by [`library_with`] and `linked_a`. +fn two_libraries_with(tag: &str, extra_a: &[&str], linked_a: bool) -> (Scratch, Session, String, String) { let dir = Scratch(std::env::temp_dir().join(format!("figcraft-libswap-{tag}-{}", std::process::id()))); std::fs::create_dir_all(&dir.0).unwrap(); let (pa, pb) = (dir.0.join("a.figcraft").to_string_lossy().into_owned(), dir.0.join("b.figcraft").to_string_lossy().into_owned()); - let a = library(&pa, "#0000ff", extra_a); + let a = library_with(&pa, "#0000ff", extra_a, linked_a); let b = library(&pb, "#00ff00", &[]); let mut s = Session::new(); ex(&mut s, "library.add", json!({"path": pa})); @@ -180,3 +199,52 @@ fn swap_repoints_colour_calculations() { let reads: Vec<&VariableId> = ["veil", "wash"].iter().flat_map(|n| vars.get(n).unwrap().values.values()).flat_map(|v| v.references()).collect(); assert!(reads.iter().all(|r| doc.libraries.variable_import(r).is_some_and(|i| i.library == b)), "{reads:?}"); } + +/// A component B has no match for stays on A, but the Brand style and `accent` variable its own +/// layers link do have matches (#2568). The swap re-points them in A's read-only copy as on the +/// placed instances, so an instance placed after the swap looks like the ones placed before it, +/// and A's style and variable copies nothing links any more go. +#[test] +fn swap_repoints_the_styles_and_variables_of_component_copies_it_keeps() { + let (_dir, mut s, a, b) = two_libraries_with("copies", &[], true); + let badge = idv(&ex(&mut s, "library.place", json!({"library": a, "component": "Badge", "x": 0, "y": 0}))); + let dot = idv(&ex(&mut s, "library.place", json!({"library": a, "component": "Dot", "x": 100, "y": 0}))); + let fill = |s: &Session, id: NodeId| s.doc().unwrap().node(id).unwrap().fills[0].solid_color().unwrap().to_hex(); + assert_eq!((fill(&s, badge), fill(&s, dot)), ("0000FF".to_string(), "0000FF".to_string())); + + let r = ex(&mut s, "library.swap", json!({"from": a, "to": b})); + assert_eq!((r["components"].as_u64(), r["styles"].as_u64(), r["variables"].as_u64()), (Some(0), Some(1), Some(1)), "{r}"); + assert_eq!(r["unmatched"], json!(["component Badge", "component Dot"]), "{r}"); + let (badge_main, dot_main) = (main_of(&s, badge), main_of(&s, dot)); + let doc = s.doc().unwrap(); + assert_eq!((library_of(doc, badge_main), library_of(doc, dot_main)), (Some(a.as_str()), Some(a.as_str())), "no match: still A's"); + // The copies link B's style and variable, as the instances do. + for (id, what) in [(badge_main, "the Badge copy"), (badge, "the Badge instance")] { + let style = doc.node(id).unwrap().styles.fill.clone().unwrap_or_default(); + assert!(doc.libraries.style_import(&style).is_some_and(|i| i.library == b), "{what} links {style}"); + } + for (id, what) in [(dot_main, "the Dot copy"), (dot, "the Dot instance")] { + let bound = doc.node(id).unwrap().bound.get("fill").cloned().unwrap_or_default(); + assert!(doc.libraries.variable_import(&bound).is_some_and(|i| i.library == b), "{what} binds {bound}"); + } + assert!(doc.libraries.styles.iter().all(|i| i.library == b), "A's Brand copy is linked by nothing: {:?}", doc.libraries.styles); + assert!(doc.libraries.variables.iter().all(|i| i.library == b), "A's accent copy is bound by nothing: {:?}", doc.libraries.variables); + assert!(doc.validate().is_empty(), "{:?}", doc.validate()); + + // Instances placed after the swap show B's colours, as those placed before it. + let late_badge = idv(&ex(&mut s, "instance.create", json!({"component": badge_main, "x": 0, "y": 100}))); + let late_dot = idv(&ex(&mut s, "instance.create", json!({"component": dot_main, "x": 100, "y": 100}))); + let shown: Vec = [badge, dot, late_badge, late_dot].iter().map(|i| fill(&s, *i)).collect(); + assert_eq!(shown, ["00FF00"; 4]); + + // One undo of each instance and the swap puts the copies back on A's style and variable. + s.undo().unwrap(); + s.undo().unwrap(); + s.undo().unwrap(); + let doc = s.doc().unwrap(); + let style = doc.node(badge_main).unwrap().styles.fill.clone().unwrap_or_default(); + assert!(doc.libraries.style_import(&style).is_some_and(|i| i.library == a), "{style}"); + let bound = doc.node(dot_main).unwrap().bound.get("fill").cloned().unwrap_or_default(); + assert!(doc.libraries.variable_import(&bound).is_some_and(|i| i.library == a), "{bound}"); + assert_eq!((fill(&s, badge), fill(&s, dot)), ("0000FF".to_string(), "0000FF".to_string())); +} diff --git a/docs/components-parity.md b/docs/components-parity.md index fa4d2494e..2ca1df0e7 100644 --- a/docs/components-parity.md +++ b/docs/components-parity.md @@ -1,6 +1,6 @@ # Components, variants, styles and variables parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-19 · **Change:** minor (a style of one library that reaches a file through another library's component is, in a file linking both, the first library's own bound copy, so its modes colour it (#2414); with a variant selected, its property dropdowns show its own values and choosing one renames only that variant, instead of setting the set's default and moving the variants (#2349); library analytics count insertions and detachments of styles and variables; combining components gives every variant the properties any of them had; renaming a variable group to a path with `/` nests it under a parent group instead of turning the slash into a hyphen (#2353); a group's context menu in the Local variables window has Duplicate group, a copy named after it plus " 2" holding a copy of every variable, in one undo step (#2084); pages take a variable mode: with nothing selected the Design panel's Page section shows a Variable modes section, Auto or a mode of each collection that has more than one, as the help center says ("Apply variable mode" in the Page section of the right sidebar) (#2215); a text style binds a number variable to its weight (#2174); an instance lists only the properties its variant's layers use and long property names are cut to one line (#2022); with several layers selected the Design panel header's component button offers Create multiple components (#2088); the Local variables window selects several variables (click, Shift+click for a range, Cmd/Ctrl+click to toggle, Escape to clear) and delete, move to a group and scope act on the selected local variables in one undo step (#2002); text styles carry paragraph spacing and indent, the list style and the OpenType features, as the Supported in text styles table of Create and apply text styles says (#1829); a layer's effect binding takes only a field its effect has; Go to main component on an instance of a library component opens the library file in a tab and selects the main there; glass effects take variables; Delete on a layer inside an instance hides it as an override instead of being refused, as the help center says: "Figma will only toggle the layer's visibility instead of removing it"; library analytics list how many layers and files use each style and variable; SwiftUI and Compose code names a bound variable by its iOS or Android code syntax; library analytics count insertions and show insertions and detachments over the past 30 days, 90 days or year; a collection of this file can be extended (Extend collection on its row, `variable.extendCollection`), its extension following its edits at once, and extended collections extend again in the file; a typed or stored variable, collection or mode id names only the object with that id (#1613); colour variables can be calculated from other colours per mode — `{alpha}` replaces a colour's alpha, `{mix}` blends two — and follow them (#1280); stored variable and collection references keep their identity when another object is named like their id (follow-up to #1542); a component set's property rows fit the 240 px right panel: variant values wrap two to a line there and every + and − stays in view; opening a file contacts no team library server it names until you check it (#1097); an explicit variables file path (`variable.import`, the `variables` of `file.open` and `file.import`) is refused past 256 MiB or when it isn't a regular file, as the sibling `variables.json` already was (part of #1114); component properties keep the order they were made in when one is renamed, and Make slot asks for the slot's name first (#1536, #1537); components can't contain their own instances, directly or through others (#1084); the instance-swap menus have a search field (#1338); an instance-swap property's default menu scrolls (#1338); variables on a text style's fields from the style editor; instance-swap properties in the component playground; duplicating or Alt-dragging a main component makes an instance of it (#892); a bound colour variable's alpha applies once (#802); variables import reads `variables` outside `meta` and warns when it imports none (#790); variant properties reorder by drag; library files saved right after a read are read again; Hide when publishing note corrected; size and resizing overrides of layers inside instances (#731); variables export in the browser as a download; core editor leftovers: component playground, variables inside styles; layers and assets panels behaviour suite and fixes; extended collections tail; styles and variables behaviour suite and fixes; components behaviour suite and fixes; team libraries on the server, library usage and swapping a library; extended collections, variables import/export, gradient stops and variant properties bound to variables) · **Target:** Figma Design (desktop app 126.10.7) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-23 · **Change:** minor (Swap library re-points the styles and variables of the library copies that stay, so instances placed after a swap follow it too (#2568); a style of one library that reaches a file through another library's component is, in a file linking both, the first library's own bound copy, so its modes colour it (#2414); with a variant selected, its property dropdowns show its own values and choosing one renames only that variant, instead of setting the set's default and moving the variants (#2349); library analytics count insertions and detachments of styles and variables; combining components gives every variant the properties any of them had; renaming a variable group to a path with `/` nests it under a parent group instead of turning the slash into a hyphen (#2353); a group's context menu in the Local variables window has Duplicate group, a copy named after it plus " 2" holding a copy of every variable, in one undo step (#2084); pages take a variable mode: with nothing selected the Design panel's Page section shows a Variable modes section, Auto or a mode of each collection that has more than one, as the help center says ("Apply variable mode" in the Page section of the right sidebar) (#2215); a text style binds a number variable to its weight (#2174); an instance lists only the properties its variant's layers use and long property names are cut to one line (#2022); with several layers selected the Design panel header's component button offers Create multiple components (#2088); the Local variables window selects several variables (click, Shift+click for a range, Cmd/Ctrl+click to toggle, Escape to clear) and delete, move to a group and scope act on the selected local variables in one undo step (#2002); text styles carry paragraph spacing and indent, the list style and the OpenType features, as the Supported in text styles table of Create and apply text styles says (#1829); a layer's effect binding takes only a field its effect has; Go to main component on an instance of a library component opens the library file in a tab and selects the main there; glass effects take variables; Delete on a layer inside an instance hides it as an override instead of being refused, as the help center says: "Figma will only toggle the layer's visibility instead of removing it"; library analytics list how many layers and files use each style and variable; SwiftUI and Compose code names a bound variable by its iOS or Android code syntax; library analytics count insertions and show insertions and detachments over the past 30 days, 90 days or year; a collection of this file can be extended (Extend collection on its row, `variable.extendCollection`), its extension following its edits at once, and extended collections extend again in the file; a typed or stored variable, collection or mode id names only the object with that id (#1613); colour variables can be calculated from other colours per mode — `{alpha}` replaces a colour's alpha, `{mix}` blends two — and follow them (#1280); stored variable and collection references keep their identity when another object is named like their id (follow-up to #1542); a component set's property rows fit the 240 px right panel: variant values wrap two to a line there and every + and − stays in view; opening a file contacts no team library server it names until you check it (#1097); an explicit variables file path (`variable.import`, the `variables` of `file.open` and `file.import`) is refused past 256 MiB or when it isn't a regular file, as the sibling `variables.json` already was (part of #1114); component properties keep the order they were made in when one is renamed, and Make slot asks for the slot's name first (#1536, #1537); components can't contain their own instances, directly or through others (#1084); the instance-swap menus have a search field (#1338); an instance-swap property's default menu scrolls (#1338); variables on a text style's fields from the style editor; instance-swap properties in the component playground; duplicating or Alt-dragging a main component makes an instance of it (#892); a bound colour variable's alpha applies once (#802); variables import reads `variables` outside `meta` and warns when it imports none (#790); variant properties reorder by drag; library files saved right after a read are read again; Hide when publishing note corrected; size and resizing overrides of layers inside instances (#731); variables export in the browser as a download; core editor leftovers: component playground, variables inside styles; layers and assets panels behaviour suite and fixes; extended collections tail; styles and variables behaviour suite and fixes; components behaviour suite and fixes; team libraries on the server, library usage and swapping a library; extended collections, variables import/export, gradient stops and variant properties bound to variables) · **Target:** Figma Design (desktop app 126.10.7) Design systems are why teams adopt Figma. **Components: breadth ~96%, ready ~72%. Styles and variables: breadth ~95%, ready ~76% (estimated).** QA'd with a synthetic design system of ~300 @@ -55,7 +55,7 @@ updates with overrides kept: `library_tests.rs`, `components_scale_tests.rs`. | Library updates available; overrides kept | done | a library file saved again within two seconds of being read, at the same length, is read again (its time stamp can't prove it unchanged; #652) | | Team libraries: publish to a `figcraft-server` under the file, enable by a share link (a view link reads it), background update checks | done | [server.md](server.md#team-libraries); no team-wide library list or accounts ([gaps](gaps.md) #10) | | Library analytics | partial | instances per component and the files using them, and the instances each file detached (per component and per file), as apps report them; insertions and detachments week by week for a year (Show usage: the past 30 days, 90 days or year), for components; the layers and files using each style and variable, with their insertions and detachments over the same periods, no modes; no chart | -| Swap library (instances, styles and variables to another library's of the same names, overrides kept) | done | what has no match stays and is listed; one undo step | +| Swap library (instances, styles and variables to another library's of the same names, overrides kept) | done | what has no match stays and is listed; one undo step; a library copy that stays (no match, or another library's) links the swapped styles and variables too, so instances placed after the swap look like those placed before it (`library_swap/tests.rs`, #2568) | ## Styles and variables @@ -80,6 +80,7 @@ updates with overrides kept: `library_tests.rs`, `components_scale_tests.rs`. | Date | Change | Summary | |---|---|---| +| 2026-11-23 | patch | M3, #2568: `library.swap {from, to}` re-pointed style links and variable bindings only on layers outside the read-only library copies. A copy the swap keeps (a component of `from` that `to` has no match for, or another library's component using `from`'s style) kept `from`'s style and variable, so instances placed after the swap showed the old colours while those placed before showed the new ones. The swap now re-points the copies' own layers too, with the same maps, and `from`'s style and variable copies that nothing links any more go. Test: `library_swap/tests.rs` (`swap_repoints_the_styles_and_variables_of_component_copies_it_keeps`) | | 2026-11-19 | patch | M3, #2414: a kit's style used by a component of a second library reached a file linking both as a second, unbound copy (`::S:1`) frozen in the kit's default mode, so a mode set on the kit's collection didn't colour it (from two hops on, every such style). Publishing now keeps the variables the published styles bind and where the file's copies of other libraries' styles, variables and collections came from; importing files such a copy under that library when the file links it, so it is the file's own bound copy. Test: `library_style_hops_tests::a_kit_style_through_another_library_is_the_kits_bound_copy`; the issue's four-file chain renders Brand and Alt everywhere. Grade unchanged. | | 2026-11-19 | patch | M3, #2349: with one variant of a set selected, the Design panel's variant property dropdowns showed the set's default values, and choosing a value set the default, so the variants swapped places. They now show the selected variant's own values (hover: "This variant's value"), and choosing one renames that variant only; with the set selected they still set its default. Test: `variant_own_value_tests::a_selected_variants_dropdown_shows_and_sets_its_own_value`. Grade unchanged. | | 2026-11-18 | minor | M3, gap 10: library analytics count insertions and detachments of styles and variables. Applying one, or copying or duplicating content that uses it, inserts it; the Detach button detaches it; an instance counts as its component alone. Show usage and `library.usage` give them per style and variable over the chosen period. Modes aren't counted. Numbers unchanged. Tests: `library_asset_counts_tests`, `library_assets_tests`. | From fe156078d00d4ed6157279dd08b10387e7a0f67a Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:35:00 +0100 Subject: [PATCH 2/5] Swap library keeps a collection's old mode while a variable of it stays on the old library A layer choosing a mode of the old library's collection had that choice replaced by the new library's, so a variable of that collection with no match (still the old library's) fell back to the default mode. The old choice now stays next to the new one while such a variable is left. --- crates/engine/src/assets/library_swap.rs | 16 ++++++-- .../engine/src/assets/library_swap/tests.rs | 37 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/assets/library_swap.rs b/crates/engine/src/assets/library_swap.rs index e6071e0de..a06015d63 100644 --- a/crates/engine/src/assets/library_swap.rs +++ b/crates/engine/src/assets/library_swap.rs @@ -213,6 +213,15 @@ pub fn swap_library(doc: &mut Document, from: &str, to: &PublishedLibrary) -> Re // Re-point the layers, and local variables aliasing A's. The library copies' own layers too: // a copy B has no match for stays (A's, or another library's), and instances placed from it // later must link what the ones re-pointed here link (#2568). + // A's collections that keep a variable on A (one B has no match for): a layer choosing a mode + // of one keeps that choice next to B's, so what still reads the A variable keeps its mode. + let still_a: BTreeSet = doc + .libraries + .variables + .iter() + .filter(|r| r.library == from && !vars.contains_key(&r.local)) + .filter_map(|r| doc.variables.get_by_id(&r.local).map(|v| v.collection.clone())) + .collect(); let ids: Vec = doc.nodes().map(|n| n.id).collect(); for id in ids { let Ok(n) = doc.get_mut(id) else { continue }; @@ -231,9 +240,10 @@ pub fn swap_library(doc: &mut Document, from: &str, to: &PublishedLibrary) -> Re } n.var_modes = std::mem::take(&mut n.var_modes) .into_iter() - .map(|(c, mode)| match colls.get(&c).and_then(|(b, modes)| modes.get(&mode).map(|bm| (b.clone(), bm.clone()))) { - Some(moved) => moved, - None => (c, mode), + .flat_map(|(c, mode)| match colls.get(&c).and_then(|(b, modes)| modes.get(&mode).map(|bm| (b.clone(), bm.clone()))) { + Some(moved) if still_a.contains(&c) => vec![moved, (c, mode)], + Some(moved) => vec![moved], + None => vec![(c, mode)], }) .collect(); } diff --git a/crates/engine/src/assets/library_swap/tests.rs b/crates/engine/src/assets/library_swap/tests.rs index a132e4610..1131c0363 100644 --- a/crates/engine/src/assets/library_swap/tests.rs +++ b/crates/engine/src/assets/library_swap/tests.rs @@ -54,6 +54,9 @@ fn library_with(path: &str, color: &str, extra: &[&str], linked: bool) -> String ex(&mut s, "variable.bind", json!({"ids": [dot], "field": "fill", "variable": "accent"})); ex(&mut s, "component.create", json!({"ids": [badge]})); ex(&mut s, "component.create", json!({"ids": [dot]})); + // A Theme variable no other library has, with a value of its own in Dark. + ex(&mut s, "variable.create", json!({"collection": "Theme", "name": "glow", "type": "color", "value": "#111111"})); + ex(&mut s, "variable.set", json!({"variable": "glow", "mode": "Dark", "value": "#222222"})); } for (i, name) in extra.iter().enumerate() { let e = idv(&ex(&mut s, "frame.create", json!({"x": 200 + 50 * i, "y": 0, "width": 20, "height": 20, "name": name}))); @@ -248,3 +251,37 @@ fn swap_repoints_the_styles_and_variables_of_component_copies_it_keeps() { assert!(doc.libraries.variable_import(&bound).is_some_and(|i| i.library == a), "{bound}"); assert_eq!((fill(&s, badge), fill(&s, dot)), ("0000FF".to_string(), "0000FF".to_string())); } + +/// A frame choosing A's Theme mode Dark holds a layer bound to `accent` (B has it) and one bound +/// to `glow` (only A has it). The swap gives the frame B's Dark for `accent` and keeps A's Dark for +/// `glow`, which stays on A, so it doesn't fall back to Light. +#[test] +fn swap_keeps_the_mode_of_a_collection_that_still_has_a_variable_on_the_old_library() { + let (_dir, mut s, a, b) = two_libraries_with("modes", &[], true); + let frame = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 100, "height": 100}))); + let rect = |s: &mut Session, x: f64| idv(&ex(s, "shape.rectangle", json!({"x": x, "y": 0, "width": 10, "height": 10, "parent": frame}))); + let (accent, glow) = (rect(&mut s, 0.0), rect(&mut s, 20.0)); + ex(&mut s, "library.bindVariable", json!({"library": a, "variable": "accent", "field": "fill", "ids": [accent]})); + ex(&mut s, "library.bindVariable", json!({"library": a, "variable": "glow", "field": "fill", "ids": [glow]})); + ex(&mut s, "library.setVariableMode", json!({"library": a, "collection": "Theme", "mode": "Dark", "ids": [frame]})); + let fill = |s: &Session, id: NodeId| s.doc().unwrap().node(id).unwrap().fills[0].solid_color().unwrap().to_hex(); + assert_eq!(fill(&s, glow), "222222", "Dark before the swap"); + + let r = ex(&mut s, "library.swap", json!({"from": a, "to": b})); + assert_eq!((r["variables"].as_u64(), &r["unmatched"]), (Some(1), &json!(["variable Theme/glow"])), "{r}"); + let doc = s.doc().unwrap(); + let modes = &doc.node(frame).unwrap().var_modes; + let lib = |c: &str| doc.libraries.collection_import(c).map(|r| r.library.clone()); + let mut owners: Vec> = modes.keys().map(|c| lib(c)).collect(); + owners.sort(); + let mut expected = vec![Some(a.clone()), Some(b.clone())]; + expected.sort(); + assert_eq!(owners, expected, "B's Theme mode, and A's kept: {modes:?}"); + assert_eq!((fill(&s, accent), fill(&s, glow)), ("00FF00".to_string(), "222222".to_string()), "glow stays on A's Dark"); + + // With nothing left on A's Theme, its mode goes with it. + ex(&mut s, "edit.delete", json!({"ids": [glow]})); + ex(&mut s, "library.swap", json!({"from": a, "to": b})); + let doc = s.doc().unwrap(); + assert!(doc.node(frame).unwrap().var_modes.keys().all(|c| doc.libraries.collection_import(c).is_some_and(|r| r.library == b))); +} From e07770073ef7a6b1967c518ceb16e5d389323aaf Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:44:06 +0100 Subject: [PATCH 3/5] docs: the swap's kept mode in the components parity row --- docs/components-parity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/components-parity.md b/docs/components-parity.md index 2ca1df0e7..cc3677a1f 100644 --- a/docs/components-parity.md +++ b/docs/components-parity.md @@ -80,7 +80,7 @@ updates with overrides kept: `library_tests.rs`, `components_scale_tests.rs`. | Date | Change | Summary | |---|---|---| -| 2026-11-23 | patch | M3, #2568: `library.swap {from, to}` re-pointed style links and variable bindings only on layers outside the read-only library copies. A copy the swap keeps (a component of `from` that `to` has no match for, or another library's component using `from`'s style) kept `from`'s style and variable, so instances placed after the swap showed the old colours while those placed before showed the new ones. The swap now re-points the copies' own layers too, with the same maps, and `from`'s style and variable copies that nothing links any more go. Test: `library_swap/tests.rs` (`swap_repoints_the_styles_and_variables_of_component_copies_it_keeps`) | +| 2026-11-23 | patch | M3, #2568: `library.swap {from, to}` re-pointed style links and variable bindings only on layers outside the read-only library copies. A copy the swap keeps (a component of `from` that `to` has no match for, or another library's component using `from`'s style) kept `from`'s style and variable, so instances placed after the swap showed the old colours while those placed before showed the new ones. The swap now re-points the copies' own layers too, with the same maps, and `from`'s style and variable copies that nothing links any more go. A layer's explicit mode for one of `from`'s collections stays next to `to`'s while a variable of that collection is left on `from` (no match), so that variable keeps its mode instead of falling back to the default. Tests: `library_swap/tests.rs` (`swap_repoints_the_styles_and_variables_of_component_copies_it_keeps`, `swap_keeps_the_mode_of_a_collection_that_still_has_a_variable_on_the_old_library`) | | 2026-11-19 | patch | M3, #2414: a kit's style used by a component of a second library reached a file linking both as a second, unbound copy (`::S:1`) frozen in the kit's default mode, so a mode set on the kit's collection didn't colour it (from two hops on, every such style). Publishing now keeps the variables the published styles bind and where the file's copies of other libraries' styles, variables and collections came from; importing files such a copy under that library when the file links it, so it is the file's own bound copy. Test: `library_style_hops_tests::a_kit_style_through_another_library_is_the_kits_bound_copy`; the issue's four-file chain renders Brand and Alt everywhere. Grade unchanged. | | 2026-11-19 | patch | M3, #2349: with one variant of a set selected, the Design panel's variant property dropdowns showed the set's default values, and choosing a value set the default, so the variants swapped places. They now show the selected variant's own values (hover: "This variant's value"), and choosing one renames that variant only; with the set selected they still set its default. Test: `variant_own_value_tests::a_selected_variants_dropdown_shows_and_sets_its_own_value`. Grade unchanged. | | 2026-11-18 | minor | M3, gap 10: library analytics count insertions and detachments of styles and variables. Applying one, or copying or duplicating content that uses it, inserts it; the Detach button detaches it; an instance counts as its component alone. Show usage and `library.usage` give them per style and variable over the chosen period. Modes aren't counted. Numbers unchanged. Tests: `library_asset_counts_tests`, `library_assets_tests`. | From 860bb542a6adc96155ad2a1254821ebeac4bac84 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:01:40 +0100 Subject: [PATCH 4/5] Swap library: a mode the layer chose of the new library's collection wins over a moved one When a collection keeps its old-library mode, a second swap maps that mode onto the new library's collection, which the layer may already have a mode of. The layer's own choice now wins, whatever the order of the collection ids. --- crates/engine/src/assets/library_swap.rs | 27 +++++++++++++------ .../engine/src/assets/library_swap/tests.rs | 15 +++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/assets/library_swap.rs b/crates/engine/src/assets/library_swap.rs index a06015d63..2d0733b3c 100644 --- a/crates/engine/src/assets/library_swap.rs +++ b/crates/engine/src/assets/library_swap.rs @@ -238,14 +238,25 @@ pub fn swap_library(doc: &mut Document, from: &str, to: &PublishedLibrary) -> Re *v = b.clone(); } } - n.var_modes = std::mem::take(&mut n.var_modes) - .into_iter() - .flat_map(|(c, mode)| match colls.get(&c).and_then(|(b, modes)| modes.get(&mode).map(|bm| (b.clone(), bm.clone()))) { - Some(moved) if still_a.contains(&c) => vec![moved, (c, mode)], - Some(moved) => vec![moved], - None => vec![(c, mode)], - }) - .collect(); + // A mode the layer already chose of B's collection wins over one moved from A's. + let old = std::mem::take(&mut n.var_modes); + let mut moved = Vec::new(); + for (c, mode) in old { + match colls.get(&c).and_then(|(b, modes)| modes.get(&mode).map(|bm| (b.clone(), bm.clone()))) { + Some(to) => { + moved.push(to); + if still_a.contains(&c) { + n.var_modes.insert(c, mode); + } + } + None => { + n.var_modes.insert(c, mode); + } + } + } + for (c, mode) in moved { + n.var_modes.entry(c).or_insert(mode); + } } for v in doc.variables.variables.iter_mut().filter(|v| !remote.contains(&v.id)) { for r in v.values.values_mut().flat_map(VariableValue::references_mut) { diff --git a/crates/engine/src/assets/library_swap/tests.rs b/crates/engine/src/assets/library_swap/tests.rs index 1131c0363..16ee59d27 100644 --- a/crates/engine/src/assets/library_swap/tests.rs +++ b/crates/engine/src/assets/library_swap/tests.rs @@ -279,6 +279,21 @@ fn swap_keeps_the_mode_of_a_collection_that_still_has_a_variable_on_the_old_libr assert_eq!(owners, expected, "B's Theme mode, and A's kept: {modes:?}"); assert_eq!((fill(&s, accent), fill(&s, glow)), ("00FF00".to_string(), "222222".to_string()), "glow stays on A's Dark"); + // A mode chosen of B's Theme since wins over A's Dark moved onto it by a second swap. + ex(&mut s, "library.setVariableMode", json!({"library": b, "collection": "Theme", "mode": "Light", "ids": [frame]})); + ex(&mut s, "library.swap", json!({"from": a, "to": b})); + assert_eq!(fill(&s, accent), "00FF00", "B's Light kept"); + let doc = s.doc().unwrap(); + let b_mode = doc + .node(frame) + .unwrap() + .var_modes + .iter() + .find(|(c, _)| doc.libraries.collection_import(c).is_some_and(|r| r.library == b)) + .map(|(c, m)| (c.clone(), m.clone())); + let (b_coll, mode) = b_mode.unwrap(); + assert_eq!(doc.variables.collection_by_id(&b_coll).and_then(|c| c.mode_by_id(&mode)).map(|m| m.name.as_str()), Some("Light")); + // With nothing left on A's Theme, its mode goes with it. ex(&mut s, "edit.delete", json!({"ids": [glow]})); ex(&mut s, "library.swap", json!({"from": a, "to": b})); From f8f955681dd2362e72ec40cb0d0ccdb79e85bf94 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:10:40 +0100 Subject: [PATCH 5/5] Swap library: the mode remapping in a function of its own --- crates/engine/src/assets/library_swap.rs | 50 +++++++++++++++--------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/assets/library_swap.rs b/crates/engine/src/assets/library_swap.rs index 2d0733b3c..f202a6e67 100644 --- a/crates/engine/src/assets/library_swap.rs +++ b/crates/engine/src/assets/library_swap.rs @@ -70,6 +70,36 @@ fn match_components(doc: &Document, records: &[ImportedComponent], m: &LibraryMa pairs } +/// A layer's explicit modes after a swap: a mode of A's collection becomes B's mode of the same +/// name (`colls`: A's collection → B's, and A's mode ids → B's), and stays as well while that +/// collection still has a variable on A (`still_a`). A mode the layer already chose of B's +/// collection wins over one moved from A's. +fn move_modes( + old: BTreeMap, + colls: &BTreeMap)>, + still_a: &BTreeSet, +) -> BTreeMap { + let mut out = BTreeMap::new(); + let mut moved = Vec::new(); + for (c, mode) in old { + match colls.get(&c).and_then(|(b, modes)| modes.get(&mode).map(|bm| (b.clone(), bm.clone()))) { + Some(to) => { + moved.push(to); + if still_a.contains(&c) { + out.insert(c, mode); + } + } + None => { + out.insert(c, mode); + } + } + } + for (c, mode) in moved { + out.entry(c).or_insert(mode); + } + out +} + /// Swap the copies of library `from` in `doc` to library `to` by names (see the module docs). pub fn swap_library(doc: &mut Document, from: &str, to: &PublishedLibrary) -> Result { let m = &to.manifest; @@ -238,25 +268,7 @@ pub fn swap_library(doc: &mut Document, from: &str, to: &PublishedLibrary) -> Re *v = b.clone(); } } - // A mode the layer already chose of B's collection wins over one moved from A's. - let old = std::mem::take(&mut n.var_modes); - let mut moved = Vec::new(); - for (c, mode) in old { - match colls.get(&c).and_then(|(b, modes)| modes.get(&mode).map(|bm| (b.clone(), bm.clone()))) { - Some(to) => { - moved.push(to); - if still_a.contains(&c) { - n.var_modes.insert(c, mode); - } - } - None => { - n.var_modes.insert(c, mode); - } - } - } - for (c, mode) in moved { - n.var_modes.entry(c).or_insert(mode); - } + n.var_modes = move_modes(std::mem::take(&mut n.var_modes), &colls, &still_a); } for v in doc.variables.variables.iter_mut().filter(|v| !remote.contains(&v.id)) { for r in v.values.values_mut().flat_map(VariableValue::references_mut) {