diff --git a/crates/engine/src/cmd/buildcmds.rs b/crates/engine/src/cmd/buildcmds.rs index 9755218edb..a15af16360 100644 --- a/crates/engine/src/cmd/buildcmds.rs +++ b/crates/engine/src/cmd/buildcmds.rs @@ -38,6 +38,7 @@ mod shape_builder; mod trace; pub use live_paint::live_paint_gaps; +pub(crate) use live_paint::{isolated_live_paint, merge_drawn_in_isolation}; use live_paint::{lp_expand, lp_fill, lp_gap_options, lp_info, lp_make, lp_merge, lp_release, lp_stroke_edge}; use shape_builder::{sb_merge, sb_regions}; pub(crate) use trace::TracePresetChanges; diff --git a/crates/engine/src/cmd/buildcmds/live_paint.rs b/crates/engine/src/cmd/buildcmds/live_paint.rs index 7938c07879..2eea5c2c00 100644 --- a/crates/engine/src/cmd/buildcmds/live_paint.rs +++ b/crates/engine/src/cmd/buildcmds/live_paint.rs @@ -1,8 +1,8 @@ use super::{ Appearance, AppearanceItem, Arc, Beside, Document, EDGE_NAME, EngineError, FACE_NAME, GapOptions, GapSize, LIVE_PAINT_NAME, Node, NodeId, - PathData, Point, Rect, Result, SOURCES_NAME, Selection, Session, SubPath, Value, b, bad, build_children, edge_near, face_at, finite, - fit_to_old_edge, ids_json, is_live_paint, json, number_or, object_param, paint_from, po, point_param, repaint_from, replace_roots, root_ids, - source_nodes, source_shapes, switch_or, + PathData, Point, Rect, Result, Selection, Session, SubPath, Value, b, bad, build_children, edge_near, face_at, finite, fit_to_old_edge, ids_json, + is_live_paint, json, number_or, object_param, paint_from, po, point_param, repaint_from, replace_roots, root_ids, source_nodes, source_shapes, + switch_or, }; /// `options` as stored on a Live Paint group: none for the defaults. @@ -53,6 +53,27 @@ fn make_live(d: &mut Document, sel: &mut Selection, roots: &[NodeId]) -> Result< Ok((gid, nf, ne)) } +/// The children of Live Paint group `g` besides its sources group ([`b::sources`]) and its faces and +/// edges (paths named [`FACE_NAME`] or [`EDGE_NAME`]), back to front: objects drawn in it in +/// isolation mode. A group or other object named like a part is one of them. +fn others_in(g: &Node) -> impl Iterator> { + let src = b::sources(g).map(|s| s.id); + g.children() + .into_iter() + .flatten() + .filter(move |c| Some(c.id) != src && !(c.path_data().is_some() && matches!(c.name.as_deref(), Some(FACE_NAME | EDGE_NAME)))) +} + +/// Make `parts` (a sources group, faces and edges made again) the children of Live Paint group `g`, +/// with its other children ([`others_in`]) that are not among `gone` kept in front of them, in their +/// order. +fn set_parts(g: &mut Node, mut parts: Vec>, gone: &[NodeId]) { + parts.extend(others_in(g).filter(|c| !gone.contains(&c.id)).cloned()); + if let Some(ch) = g.children_mut() { + *ch = parts; + } +} + /// Select `ids` alone, without an undo step, when they are not selected that way already. fn select_only(s: &mut Session, ids: &[NodeId]) -> Result<()> { let st = s.doc()?; @@ -134,9 +155,15 @@ fn merge_live(d: &mut Document, sel: &mut Selection, roots: &[NodeId]) -> Result b::faces(&old).iter().filter(|f| draws(f)).filter_map(|f| Some((po::interior_point(f.path_data()?)?, Carried::of(f)))).collect(); let old_parts = old.clone(); let mut sources = source_nodes(&old); + // The objects drawn in the other Live Paint groups besides their parts stay in the merged group. + let mut others = vec![]; for r in roots { if *r != target { - sources.extend(source_nodes(d.node(*r).ok_or(EngineError::NoNode(*r))?)); + let n = d.node(*r).ok_or(EngineError::NoNode(*r))?; + sources.extend(source_nodes(n)); + if is_live_paint(n) { + others.extend(others_in(n).cloned()); + } } } // Merge applies the Gap Options' merge rules to the groups' options, bottom-most first. @@ -146,8 +173,9 @@ fn merge_live(d: &mut Document, sel: &mut Selection, roots: &[NodeId]) -> Result // The group keeps its name, lock, visibility, transparency, appearance, opacity mask, graphic // style link and the rest; its children are made again and its Gap Options set to the merged ones. let mut g = old; + set_parts(&mut g, children, roots); if let Some(ch) = g.children_mut() { - *ch = children; + ch.extend(others); } g.extras_mut().live_paint_gaps = stored_gap_options(options); for (pt, carried) in keep_faces { @@ -199,6 +227,38 @@ pub(super) fn lp_merge(s: &mut Session, p: &Value) -> Result { Ok(json!({ "id": id.0, "faces": faces, "edges": edges })) } +/// The Live Paint group whose drawn art is added when isolation mode leaves `isolated`: the innermost +/// Live Paint group that is `isolated` or holds it, such as a group drawn in an isolated Live Paint +/// group and then isolated itself. None when `isolated` is not in a Live Paint group. +pub(crate) fn isolated_live_paint(d: &Document, isolated: NodeId) -> Option { + d.ancestry(isolated)?.into_iter().rev().find(|id| d.node(*id).is_some_and(is_live_paint)) +} + +/// The children of Live Paint group `g` besides its parts ([`others_in`]) that Live Paint takes +/// ([`b::live_paint_takes`]) and that are shown and unlocked with everything in them, back to +/// front: the paths drawn in it in isolation mode. +fn drawn_in(g: &Node) -> Vec { + fn open(n: &Node) -> bool { + n.visible && !n.locked && n.children().is_none_or(|ch| ch.iter().all(|c| open(c))) + } + others_in(g).filter(|c| open(c) && b::live_paint_takes(c)).map(|c| c.id).collect() +} + +/// Leaving isolation mode in Live Paint group `g` adds the paths drawn in it to the group as +/// `livePaint.merge` does, in one "Merge Live Paint" undo step; → whether any were added. Type, +/// images, other objects Live Paint does not take, and hidden or locked objects stay where they +/// are. With nothing to add, or when `g` is not a Live Paint group, nothing changes and no undo +/// step is recorded. +pub(crate) fn merge_drawn_in_isolation(s: &mut Session, g: NodeId) -> Result { + let drawn = s.doc()?.doc.node(g).filter(|n| is_live_paint(n)).map(drawn_in).unwrap_or_default(); + if drawn.is_empty() { + return Ok(false); + } + let roots: Vec = std::iter::once(g).chain(drawn).collect(); + s.edit("Merge Live Paint", |d, sel| merge_live(d, sel, &roots))?; + Ok(true) +} + fn live_targets(s: &Session, p: &Value) -> Result> { let d = &s.doc()?.doc; let ids = root_ids(s, p)?; @@ -216,7 +276,8 @@ pub(super) fn lp_release(s: &mut Session, p: &Value) -> Result { let mut released = Beside::replacing(d, &targets); for g in &targets { let n = d.node(*g).cloned().ok_or(EngineError::NoNode(*g))?; - let srcs: Vec = source_nodes(&n).iter().map(|c| (**c).clone()).collect(); + // The objects drawn in the group besides its parts are released in front of its paths. + let srcs: Vec = source_nodes(&n).iter().chain(others_in(&n)).map(|c| (**c).clone()).collect(); out.extend(srcs.iter().map(|c| c.id)); released.put(d, *g, srcs)?; } @@ -245,10 +306,12 @@ pub(super) fn lp_expand(s: &mut Session, p: &Value) -> Result { e.trace = None; e.live_paint_gaps = None; } + // Objects drawn in the group ([`others_in`]) stay, whatever they are named. + let src = b::sources(n).map(|c| c.id); if let Some(ch) = n.children_mut() { ch.retain(|c| match c.name.as_deref() { - Some(SOURCES_NAME) => false, - Some(FACE_NAME | EDGE_NAME) => draws(c), + _ if Some(c.id) == src => false, + Some(FACE_NAME | EDGE_NAME) if c.path_data().is_some() => draws(c), _ => true, }); } @@ -354,9 +417,7 @@ pub(super) fn lp_gap_options(s: &mut Session, p: &Value) -> Result { // The faces change only with what paint stops at, or with new paths. if added > 0 || options.max_gap() != old.gap_options().max_gap() { let (children, _, _) = build_children(d, sources, options.max_gap())?; - if let Some(ch) = n.children_mut() { - *ch = children; - } + set_parts(&mut n, children, &[]); repaint_from(&mut n, &old); } n.extras_mut().live_paint_gaps = stored_gap_options(options); diff --git a/crates/engine/src/cmd/object/arrange.rs b/crates/engine/src/cmd/object/arrange.rs index d925ac34c9..17568d50b9 100644 --- a/crates/engine/src/cmd/object/arrange.rs +++ b/crates/engine/src/cmd/object/arrange.rs @@ -357,6 +357,7 @@ pub(super) fn isolate(s: &mut Session, p: &Value) -> Result { && prev != isolation { crate::cmd::settle_interaction(s)?; + add_drawn_live_paint(s, prev, Some(isolation))?; let st = s.doc_mut()?; crate::cmd::distortcmds::finish_edit_text(st, prev); } @@ -369,6 +370,19 @@ pub(super) fn isolate(s: &mut Session, p: &Value) -> Result { ok() } +/// Leaving isolation mode in `isolated` for `next` (None: out of isolation mode) adds the paths drawn +/// in the Live Paint group isolation put new art in to that group, as `livePaint.merge` does, in one +/// undo step ([`crate::cmd::buildcmds::merge_drawn_in_isolation`]), unless `next` is that group or is +/// inside it; → the group when paths were added. +fn add_drawn_live_paint(s: &mut Session, isolated: NodeId, next: Option) -> Result> { + let d = &s.doc()?.doc; + let Some(g) = crate::cmd::buildcmds::isolated_live_paint(d, isolated) else { return Ok(None) }; + if next.is_some_and(|n| d.ancestry(n).is_some_and(|a| a.contains(&g))) { + return Ok(None); + } + Ok(crate::cmd::buildcmds::merge_drawn_in_isolation(s, g)?.then_some(g)) +} + pub(super) fn exit_isolation(s: &mut Session, p: &Value) -> Result { // Opacity mask editing, pattern editing and symbol editing isolate their editing layer. Leaving // isolation mode then leaves that mode, as Stop Editing Opacity Mask and the isolation bar's @@ -378,10 +392,11 @@ pub(super) fn exit_isolation(s: &mut Session, p: &Value) -> Result { && let Some(inner) = st.isolation.filter(|i| *i != layer && st.doc.ancestry(*i).is_some_and(|a| a.contains(&layer))) { crate::cmd::settle_interaction(s)?; + let left = add_drawn_live_paint(s, inner, Some(layer))?.unwrap_or(inner); let st = s.doc_mut()?; crate::cmd::distortcmds::finish_edit_text(st, inner); st.isolation = Some(layer); - st.selection.set([inner]); + st.selection.set([left]); st.revision += 1; return ok(); } @@ -396,6 +411,8 @@ pub(super) fn exit_isolation(s: &mut Session, p: &Value) -> Result { }; if let Some(leave_mode) = leave_mode { if let Some(i) = st.isolation { + crate::cmd::settle_interaction(s)?; + add_drawn_live_paint(s, i, None)?; crate::cmd::distortcmds::finish_edit_text(s.doc_mut()?, i); } leave_mode(s, p)?; @@ -405,10 +422,11 @@ pub(super) fn exit_isolation(s: &mut Session, p: &Value) -> Result { let prev = s.active().and_then(|d| d.isolation); if let Some(i) = prev { crate::cmd::settle_interaction(s)?; + let left = add_drawn_live_paint(s, i, None)?.unwrap_or(i); let st = s.doc_mut()?; st.isolation = None; crate::cmd::distortcmds::finish_edit_text(st, i); - let target = st.doc.parent_of(i).filter(|p| st.doc.node(*p).is_some_and(|n| n.extras().shaper.is_some())).unwrap_or(i); + let target = st.doc.parent_of(left).filter(|p| st.doc.node(*p).is_some_and(|n| n.extras().shaper.is_some())).unwrap_or(left); st.selection.set([target]); // A guide path isolated while Lock Guides is on isn't selected (#1534). crate::deselect_hidden(&menu, &st.doc, &mut st.selection); diff --git a/crates/engine/src/cmd/object/commands.rs b/crates/engine/src/cmd/object/commands.rs index 7f620b17ff..0c638ceb5e 100644 --- a/crates/engine/src/cmd/object/commands.rs +++ b/crates/engine/src/cmd/object/commands.rs @@ -245,7 +245,15 @@ pub fn specs() -> Vec { crate::cmd::maskedit::outside_mask_editing, isolate ), - cmd!("object.exitIsolation", "Exit Isolation Mode", [], None, "{}", has_doc, exit_isolation), + cmd!( + "object.exitIsolation", + "Exit Isolation Mode", + [], + None, + "{} leave isolation mode; leaving an isolated Live Paint group adds the paths, compound paths and groups of them drawn in it (shown and unlocked with everything in them) to the group as livePaint.merge does, in one undo step, and leaves type, images and other objects in it as they are; object.isolate on a level outside the group does the same", + has_doc, + exit_isolation + ), cmd!( "object.setProps", "Object Properties", diff --git a/crates/engine/src/tests/build.rs b/crates/engine/src/tests/build.rs index 011e76752e..8a0ba40084 100644 --- a/crates/engine/src/tests/build.rs +++ b/crates/engine/src/tests/build.rs @@ -4762,6 +4762,279 @@ fn live_paint_refuses_type_and_images_and_changes_nothing() { assert!(!vectorcraft_tools::builder::live_paint_takes(&node(&s, text_group))); } +/// A horizontal open path across [`live`]'s group at `y`, drawn with the Pen's defaults. +fn line_across(s: &mut Session, y: f64) -> NodeId { + let r = s.execute("path.create", &json!({"anchors": [{"x": -10, "y": y}, {"x": 160, "y": y}]})).unwrap(); + NodeId(r["id"].as_u64().unwrap()) +} + +/// Paths drawn in an isolated Live Paint group join it when isolation mode ends: they go among its +/// sources and cut the faces they cross, which take their sources' fill, a painted face they do not +/// cross keeps its fill, and the whole addition is one undo step, which Undo takes back to the +/// drawn paths in the group and Redo makes again. +#[test] +fn live_paint_isolation_adds_drawn_paths_on_exit() { + use vectorcraft_tools::builder::{face_at, faces, sources}; + let green = Paint::solid(Color::from_hex("#00ff00").unwrap()); + let mut s = session(); + let g = live(&mut s); + s.execute("livePaint.fill", &json!({"group": g.0, "point": [125, 50], "color": "#00ff00"})).unwrap(); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + // Two lines from left of the group that end in its right face, so they cut only the other two. + let line = |s: &mut Session, y: f64| { + let r = s.execute("path.create", &json!({"anchors": [{"x": -10, "y": y}, {"x": 105, "y": y}]})).unwrap(); + NodeId(r["id"].as_u64().unwrap()) + }; + let (a, b) = (line(&mut s, 30.0), line(&mut s, 70.0)); + assert_eq!(s.doc().unwrap().doc.parent_of(a), Some(g), "a path drawn in isolation goes into the group"); + let steps = s.doc().unwrap().history.undo.len(); + s.execute("object.exitIsolation", &json!({})).unwrap(); + let d = s.doc().unwrap(); + assert_eq!(d.isolation, None); + assert_eq!(d.history.undo.len(), steps + 1, "both paths join in one undo step"); + assert_eq!(d.selection.objects, vec![g]); + let n = node(&s, g); + let src = sources(&n).unwrap(); + assert_eq!(src.children().unwrap().len(), 4); + for id in [a, b] { + assert_eq!(s.doc().unwrap().doc.parent_of(id), Some(src.id), "{id} joined the sources"); + } + assert_eq!(faces(&n).len(), 7, "two lines cut each of the two faces they cross in three"); + assert_eq!(face_at(&n, Point::new(125.0, 50.0)).unwrap().appearance.fill_paint(), green, "the face the lines do not cross keeps its fill"); + let red = Paint::solid(Color::rgb(1.0, 0.0, 0.0)); + for y in [15.0, 85.0] { + assert_eq!(face_at(&n, Point::new(25.0, y)).unwrap().appearance.fill_paint(), red, "a new face takes its source's fill"); + } + s.execute("edit.undo", &json!({})).unwrap(); + let n = node(&s, g); + assert_eq!(faces(&n).len(), 3); + assert_eq!(sources(&n).unwrap().children().unwrap().len(), 2); + for id in [a, b] { + assert_eq!(s.doc().unwrap().doc.parent_of(id), Some(g), "Undo puts {id} back in the group as drawn"); + } + s.execute("edit.redo", &json!({})).unwrap(); + assert_eq!(faces(&node(&s, g)).len(), 7); +} + +/// Leaving isolation mode in a Live Paint group with nothing drawn in it records no undo step and +/// leaves the document as it is. Type, and a path that is hidden, stay in the group as they are, +/// while a group of paths drawn beside them joins it. +#[test] +fn live_paint_isolation_adds_only_paths_it_takes() { + use vectorcraft_tools::builder::{faces, sources}; + let mut s = session(); + let g = live(&mut s); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len()); + s.execute("object.exitIsolation", &json!({})).unwrap(); + assert!(s.doc().unwrap().doc == before.0, "an exit with nothing drawn changes nothing"); + assert_eq!(s.doc().unwrap().history.undo.len(), before.1, "and records no undo step"); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let text = NodeId(s.execute("text.create", &json!({"x": 20, "y": 20, "text": "Hi"})).unwrap()["id"].as_u64().unwrap()); + let hidden = line_across(&mut s, 50.0); + s.edit("t", |d, _| { + d.node_mut(hidden).unwrap().visible = false; + Ok(()) + }) + .unwrap(); + let steps = s.doc().unwrap().history.undo.len(); + s.execute("object.exitIsolation", &json!({})).unwrap(); + assert_eq!(s.doc().unwrap().history.undo.len(), steps, "type and a hidden path add nothing"); + assert_eq!(faces(&node(&s, g)).len(), 3); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let (a, b) = (line_across(&mut s, 30.0), line_across(&mut s, 70.0)); + s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); + let paths = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + assert_eq!(s.doc().unwrap().doc.parent_of(paths), Some(g)); + s.execute("object.exitIsolation", &json!({})).unwrap(); + let n = node(&s, g); + assert_eq!(faces(&n).len(), 9, "the group of paths joined"); + let d = &s.doc().unwrap().doc; + assert_eq!(d.parent_of(paths), Some(sources(&n).unwrap().id)); + for id in [text, hidden] { + assert_eq!(d.parent_of(id), Some(g), "{id} stays in the group"); + assert!(d.node(id).is_some()); + } +} + +/// A group drawn in an isolated Live Paint group that holds a locked path stays in it as it is when +/// isolation mode ends, as a locked path drawn there does. +#[test] +fn live_paint_isolation_leaves_a_group_holding_a_locked_path() { + use vectorcraft_tools::builder::faces; + let mut s = session(); + let g = live(&mut s); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let (a, b) = (line_across(&mut s, 30.0), line_across(&mut s, 70.0)); + s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); + let paths = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.edit("t", |d, _| { + d.node_mut(b).unwrap().locked = true; + Ok(()) + }) + .unwrap(); + let steps = s.doc().unwrap().history.undo.len(); + s.execute("object.exitIsolation", &json!({})).unwrap(); + assert_eq!(s.doc().unwrap().history.undo.len(), steps); + assert_eq!(s.doc().unwrap().doc.parent_of(paths), Some(g)); + assert_eq!(faces(&node(&s, g)).len(), 3); +} + +/// Type drawn in an isolated Live Paint group stays in it when Gap Options or Merge make the group's +/// faces and edges again, including type in a Live Paint group merged into another, and Release +/// puts it beside the released paths. +#[test] +fn live_paint_keeps_type_drawn_in_it_through_gap_options_merge_and_release() { + let mut s = session(); + let g = live(&mut s); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let text = NodeId(s.execute("text.create", &json!({"x": 20, "y": 20, "text": "Hi"})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.exitIsolation", &json!({})).unwrap(); + s.execute("livePaint.gapOptions", &json!({"ids": [g.0], "detect": true})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(text), Some(g), "Gap Options keeps the type"); + let c = rect(&mut s, 120.0, 20.0, 100.0, 20.0); + s.execute("livePaint.merge", &json!({"ids": [g.0, c.0]})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(text), Some(g), "Merge keeps the type"); + let other = rect(&mut s, 300.0, 300.0, 50.0, 50.0); + let h = NodeId(s.execute("livePaint.make", &json!({"ids": [other.0]})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.isolate", &json!({"id": h.0})).unwrap(); + let text2 = NodeId(s.execute("text.create", &json!({"x": 310, "y": 320, "text": "Yo"})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.exitIsolation", &json!({})).unwrap(); + s.execute("livePaint.merge", &json!({"ids": [g.0, h.0]})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(text2), Some(g), "the type of the merged group moves with its paths"); + s.execute("livePaint.release", &json!({"ids": [g.0]})).unwrap(); + let d = &s.doc().unwrap().doc; + for id in [text, text2] { + assert_eq!(d.parent_of(id), d.default_layer(), "Release keeps {id}"); + } +} + +/// Art drawn in an isolated Live Paint group and named like one of the group's parts is not taken +/// for one: type named "Live Paint Sources" stays in the group through Gap Options, and a group of +/// paths named "Face" joins the group when isolation mode ends. +#[test] +fn live_paint_isolation_keeps_art_named_like_its_parts() { + use vectorcraft_tools::builder::{SOURCES_NAME, faces, sources}; + let mut s = session(); + let g = live(&mut s); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let text = NodeId(s.execute("text.create", &json!({"x": 20, "y": 20, "text": "Hi"})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.setProps", &json!({"id": text.0, "name": SOURCES_NAME})).unwrap(); + let (a, b) = (line_across(&mut s, 30.0), line_across(&mut s, 70.0)); + s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); + let paths = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.setProps", &json!({"id": paths.0, "name": "Face"})).unwrap(); + s.execute("object.exitIsolation", &json!({})).unwrap(); + let n = node(&s, g); + assert_eq!(faces(&n).len(), 9, "the group named Face joined"); + assert_eq!(s.doc().unwrap().doc.parent_of(paths), Some(sources(&n).unwrap().id)); + s.execute("livePaint.gapOptions", &json!({"ids": [g.0], "detect": true})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(text), Some(g), "Gap Options keeps the type named like the sources"); +} + +/// Type drawn in an isolated Live Paint group under the name of a face, an edge or the sources +/// group is not one of the group's parts: Gap Options, Info and Make count only the faces and edges +/// Live Paint made, and Expand keeps the type. +#[test] +fn live_paint_does_not_count_art_named_like_its_parts() { + use vectorcraft_tools::builder::{EDGE_NAME, FACE_NAME, SOURCES_NAME}; + let mut s = session(); + let g = live(&mut s); + let edges = s.execute("livePaint.info", &json!({"group": g.0})).unwrap()["edges"].as_array().unwrap().len(); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let mut texts = vec![]; + for (i, name) in [FACE_NAME, EDGE_NAME, SOURCES_NAME].into_iter().enumerate() { + let r = s.execute("text.create", &json!({"x": 20 + 20 * i, "y": 20, "text": "Hi"})).unwrap(); + let t = NodeId(r["id"].as_u64().unwrap()); + s.execute("object.setProps", &json!({"id": t.0, "name": name})).unwrap(); + texts.push(t); + } + s.execute("object.exitIsolation", &json!({})).unwrap(); + let r = s.execute("livePaint.gapOptions", &json!({"ids": [g.0], "detect": true})).unwrap(); + assert_eq!(r["faces"], 3, "Gap Options counts the faces Live Paint made"); + let info = s.execute("livePaint.info", &json!({"group": g.0})).unwrap(); + assert_eq!(info["faces"].as_array().unwrap().len(), 3); + assert_eq!(info["edges"].as_array().unwrap().len(), edges); + let r = s.execute("livePaint.make", &json!({"ids": [g.0]})).unwrap(); + assert_eq!((r["faces"].as_u64(), r["edges"].as_u64()), (Some(3), Some(edges as u64))); + s.execute("livePaint.expand", &json!({"ids": [g.0]})).unwrap(); + let d = &s.doc().unwrap().doc; + for t in texts { + assert_eq!(d.parent_of(t), Some(g), "Expand keeps {t}"); + } +} + +/// With Real-time Drawing and Editing off, a rectangle still being drawn in a Live Paint group +/// isolated in a pattern tile is drawn first when Exit Isolation Mode leaves pattern editing, and +/// joins the group with the tile saved. +#[test] +fn live_paint_in_a_pattern_tile_adds_a_shape_still_being_drawn_on_exit() { + use vectorcraft_tools::builder::{is_live_paint, sources}; + let mut s = session(); + two(&mut s); + s.execute("object.pattern.make", &json!({"name": "P"})).unwrap(); + let g = NodeId(s.execute("livePaint.make", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + s.execute("prefs.set", &json!({"key": "realTimeDrawing", "value": false})).unwrap(); + let v = ViewInfo::default(); + s.select_tool("rectangle", v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Down, 10.0, 10.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 40.0, 40.0), v).unwrap(); + assert!(s.interaction_unapplied()); + s.execute("object.exitIsolation", &json!({})).unwrap(); + let st = s.doc().unwrap(); + assert!(st.doc.pattern_edit.is_none()); + let art = &st.doc.pattern("P").unwrap().art; + let n = art.iter().find(|n| is_live_paint(n)).expect("the tile holds the Live Paint group"); + assert_eq!(sources(n).unwrap().children().unwrap().len(), 3, "the rectangle joined the sources"); +} + +/// Isolating a level outside an isolated Live Paint group, as the isolation bar's back arrow does, +/// leaves the group and adds the paths drawn in it; isolating a path drawn in it does not. +#[test] +fn live_paint_isolation_adds_drawn_paths_when_isolating_a_level_outside() { + use vectorcraft_tools::builder::{faces, sources}; + let mut s = session(); + let g = live(&mut s); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let a = line_across(&mut s, 50.0); + s.execute("object.isolate", &json!({"id": a.0})).unwrap(); + assert_eq!(s.doc().unwrap().doc.parent_of(a), Some(g), "the path is still being drawn in the group"); + assert_eq!(faces(&node(&s, g)).len(), 3); + let layer = s.doc().unwrap().doc.parent_of(g).unwrap(); + s.execute("object.isolate", &json!({"id": layer.0})).unwrap(); + assert_eq!(s.doc().unwrap().isolation, Some(layer)); + let n = node(&s, g); + assert_eq!(faces(&n).len(), 6); + assert_eq!(s.doc().unwrap().doc.parent_of(a), Some(sources(&n).unwrap().id)); +} + +/// A group of paths drawn in an isolated Live Paint group and then isolated itself joins the Live +/// Paint group when isolation mode ends, or when a level outside the Live Paint group is isolated. +#[test] +fn live_paint_isolation_adds_a_drawn_group_isolated_inside_it() { + use vectorcraft_tools::builder::{faces, sources}; + for to_layer in [false, true] { + let mut s = session(); + let g = live(&mut s); + s.execute("object.isolate", &json!({"id": g.0})).unwrap(); + let (a, b) = (line_across(&mut s, 30.0), line_across(&mut s, 70.0)); + s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap(); + let paths = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + s.execute("object.isolate", &json!({"id": paths.0})).unwrap(); + assert_eq!(faces(&node(&s, g)).len(), 3, "isolating the drawn group stays in the Live Paint group"); + if to_layer { + let layer = s.doc().unwrap().doc.parent_of(g).unwrap(); + s.execute("object.isolate", &json!({"id": layer.0})).unwrap(); + } else { + s.execute("object.exitIsolation", &json!({})).unwrap(); + } + let n = node(&s, g); + assert_eq!(faces(&n).len(), 9, "the drawn group joined (to the layer: {to_layer})"); + assert_eq!(s.doc().unwrap().doc.parent_of(paths), Some(sources(&n).unwrap().id)); + } +} + /// Give Live Paint group `g` a lock, hidden state, appearance, knockout, opacity mask and /// graphic style link, and return it as it then is. fn dress_live_group(s: &mut Session, g: NodeId) -> Node { diff --git a/crates/tools/src/builder/lookup.rs b/crates/tools/src/builder/lookup.rs index 0d7acd7e67..7dfa249586 100644 --- a/crates/tools/src/builder/lookup.rs +++ b/crates/tools/src/builder/lookup.rs @@ -38,14 +38,20 @@ fn named(n: &Node, name: &str) -> bool { n.name.as_deref() == Some(name) } +/// Is `n` a Live Paint group's part named `name` ([`FACE_NAME`] or [`EDGE_NAME`]): a path with that +/// name? Type, groups and other objects drawn in the group under such a name are not parts. +fn part(n: &Node, name: &str) -> bool { + named(n, name) && n.path_data().is_some() +} + /// Face children of a Live Paint group (paint order). pub fn faces(group: &Node) -> Vec<&Arc> { - group.children().map(|c| c.iter().filter(|n| named(n, FACE_NAME)).collect()).unwrap_or_default() + group.children().map(|c| c.iter().filter(|n| part(n, FACE_NAME)).collect()).unwrap_or_default() } /// Edge children of a Live Paint group. pub fn edges(group: &Node) -> Vec<&Arc> { - group.children().map(|c| c.iter().filter(|n| named(n, EDGE_NAME)).collect()).unwrap_or_default() + group.children().map(|c| c.iter().filter(|n| part(n, EDGE_NAME)).collect()).unwrap_or_default() } /// The hidden sources group of a Live Paint group. diff --git a/docs/mcp.md b/docs/mcp.md index 8235e6085c..c359534c88 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -5441,7 +5441,11 @@ and `layerRows`, the rows highlighted in the panel. runs Hide or Lock Others for the row's layer, or Show or Unlock All when the others already are), `layer.pasteRemembersLayers`, `object.isolate {id?}` (a group, a layer or a single object, by default the first selected object; not type, not a hidden or locked object or one in a hidden or locked group or layer, not a guide path while Lock Guides is on, and - while a symbol is edited only the editing layer or art on it) / `object.exitIsolation`. + while a symbol is edited only the editing layer or art on it) / `object.exitIsolation`. Leaving an isolated Live + Paint group (`object.exitIsolation`, or `object.isolate` on a level outside it) adds the paths, compound paths and + groups of them drawn in it, shown and unlocked with everything in them, to the group as `livePaint.merge` does, in + one undo step; type, images and other objects stay in it as they are, and with nothing drawn no undo step is + recorded. - **Dialogs and panel state** (control channel and the app's MCP): `ui.layerOptions {ids?}` and `ui.newLayer {sublayer?}` open Layer Options (dialog `layerOptions`: name, color, template, locked, visible, printable, preview, dimImages, dimPercent; OK is one undo step; `ui.dialog.set` of `template` also sets `locked` and `dimImages`, as the diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index c34be93692..99815def6d 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1695,6 +1695,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Create Strokes decides stroke or fill per feature, as the [help page](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/image-trace-panel-options.html) describes the Strokes width: the widest feature of the image that can become a stroke. A shape wider than Stroke somewhere keeps as its area every disc wider than Stroke that fits in it, and each piece outside those discs that reaches out of them at least one and a half times its own width (and 3 px) is traced as a stroked center line, its end at the area carried to the cut so the line's cap closes the join. Corners and edge slivers stay in the area. Which pieces count as features is VectorCraft's own rule. Each line still has one width; without Fills, the area is a 1 px outline as before. Tests: `thin_features_of_a_wide_shape_are_strokes_and_its_body_a_fill`, `wide_shapes_without_thin_features_stay_one_fill` and `distances_to_seeds_equal_brute_force` in `vectorcraft-trace`. The Image Trace score and estimates are unchanged. - The ignored centre-lines class measures that split on a mixed-width body scene: one disc with arms of 2, 3 and 5 px, at Stroke 6 px, traces as three stroked paths and one filled path in both the anti-aliased and hard input, with 100.0% of the arms' center samples found and 99.3% (anti-aliased) and 98.3% (hard) of the disc's pixels filled ([quality notes](image-trace-quality.md#centre-lines)). Test: `mixed_width_body_is_one_shape_traced_as_a_fill_and_strokes` in `vectorcraft-trace`. The Image Trace score and estimates are unchanged. Object › Live Paint › Merge adds the other selected paths and Live Paint groups to the bottom selected Live Paint group ([Live Paint groups](https://web.archive.org/web/20241230205225/https://helpx.adobe.com/illustrator/using/live-paint-groups.html)), which keeps its id, the paint of its faces and edges, and its name, lock, visibility, transparency, appearance, opacity mask and graphic style link. Object › Live Paint › Make with a Live Paint group selected, and a Live Paint Bucket click on a selected path beside a selected Live Paint group, add the paths to that group the same way. Make, Merge and the Live Paint Bucket take paths, compound paths and groups of them; with type, an image or another object among the selection they report that it has to be converted to paths first and change nothing, as type and raster images can't go into a Live Paint group directly ([Create Live Paint groups](https://helpx.adobe.com/illustrator/desktop/paint-and-fill/learn-painting-basics/create-live-paint-groups.html)); before, such an object went among the group's hidden sources and vanished from the canvas. Evidence: `tests_build::live_paint_refuses_type_and_images_and_changes_nothing`. + Paths drawn in an isolated Live Paint group join it when isolation mode ends, as the [help page](https://helpx.adobe.com/illustrator/desktop/paint-and-fill/learn-painting-basics/modify-live-paint-groups.html) describes: drawing paths in the isolated group and pressing Esc adds them. `object.exitIsolation` (Esc, the Control bar and isolation bar exits, a double-click outside, the Layers panel and the canvas context menu) adds the shown, unlocked paths, compound paths and groups of them drawn in the group as Merge does, in one undo step, and `object.isolate` on a level outside the group does the same. As VectorCraft's own rules, type, images, hidden and locked objects drawn there, and groups holding one, stay in the group as they are, and Gap Options, Merge and Release keep them; an exit with nothing drawn records no undo step. Evidence: `tests::build::{live_paint_isolation_adds_drawn_paths_on_exit, live_paint_isolation_adds_only_paths_it_takes, live_paint_isolation_leaves_a_group_holding_a_locked_path, live_paint_isolation_adds_drawn_paths_when_isolating_a_level_outside, live_paint_isolation_adds_a_drawn_group_isolated_inside_it, live_paint_isolation_keeps_art_named_like_its_parts, live_paint_keeps_type_drawn_in_it_through_gap_options_merge_and_release}`. With the Live Paint Bucket selected, Alt-click on vector art samples its fill, stroke and stroke weight into the paint defaults ([Paint with the Live Paint Bucket tool](https://helpx.adobe.com/illustrator/desktop/paint-and-fill/learn-painting-basics/paint-with-the-live-paint-bucket-tool.html)). On a placed image it samples the image pixel as a solid fill with no stroke. Alt+Shift-click samples only the color under the pointer, including an intermediate gradient color or image pixels, into the active fill or stroke ([Default keyboard shortcuts](https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html)). Neither click paints or changes the selection or selected art, and the next Bucket click paints with the sampled paint. Evidence: `tests_build::live_paint_bucket_alt_sample_paints_next_face_without_changing_selection`. Double-clicking the Live Paint Bucket opens Live Paint Bucket Options ([Paint with the Live Paint Bucket tool](https://helpx.adobe.com/illustrator/desktop/paint-and-fill/learn-painting-basics/paint-with-the-live-paint-bucket-tool.html)): Paint Fills (on) and Paint Strokes (off) set whether a click paints faces, edges or both, Highlight turns the outline of the face or edge under the pointer on or off, and Color and Width set it (red and 4 px by default). Faces are outlined at the Width and edges at half of it, as the page says faces are highlighted with a thick line and edges with a thin line. With both on, a click paints the edge within 4 px of the pointer, else the face, and Shift switches to Paint Fills only; with one on, Shift switches to the other. The options last across tool switches and are saved with the preferences (`tool.setOption {tool: "livePaintBucket"}`). Evidence: `builder::tests::{bucket_options_choose_what_a_click_paints, bucket_options_set_the_highlight, bucket_highlights_an_edge_thinner_than_a_face}`, `dialogs::live_paint_bucket::tests::double_clicking_the_tool_opens_its_options_and_ok_sets_them`. Live Paint Bucket double-click and triple-click, as [the help page](https://helpx.adobe.com/illustrator/desktop/paint-and-fill/learn-painting-basics/paint-with-the-live-paint-bucket-tool.html) describes them: a double-click on a face fills it and the faces reached across edges that draw no stroke, and on an edge it paints the edges joined to it with the same stroke; a triple-click fills every face of the group that has the face's fill, or paints every edge that draws with the edge's stroke paint, each with the bucket's paint (`livePaint.fill` and `livePaint.strokeEdge` `{scope: "connected" | "samePaint", match?}`; `pointer_gesture` and `ui.pointer` take `tripleclick`). Each click paints as it arrives, as its own undo step when it changes a paint, and the paint compared is the one the face or edge had before the first of its clicks that painted it, including a click whose drag or release crossed it; a first click that made the Live Paint group counts when the face it filled is the clicked face. With Space held for the view tools, a click, double-click or triple-click paints nothing and no face or edge is outlined, and with Cmd/Ctrl held a double-click or triple-click paints nothing. Which faces and edges join and which paints match are VectorCraft's own rules. Evidence: `tests_build::{live_paint_bucket_double_click_floods_across_unstroked_edges, live_paint_bucket_double_click_floods_curved_and_narrow_faces, live_paint_bucket_double_click_floods_across_hidden_edges_and_past_gaps, live_paint_bucket_triple_click_fills_every_face_of_the_same_fill, live_paint_bucket_triple_click_after_a_release_across_an_edge_matches_the_fill_before, live_paint_bucket_triple_click_released_from_another_face_matches_the_fill_before, live_paint_bucket_triple_click_after_making_the_group_across_an_edge_matches_the_made_fill, live_paint_bucket_triple_click_after_making_the_group_under_an_unfilled_path_matches_the_made_fill, live_paint_bucket_triple_click_after_merging_across_an_edge_matches_the_face_fill, live_paint_bucket_triple_click_after_merging_two_groups_matches_the_merged_fill, live_paint_bucket_triple_click_after_a_merge_splits_a_face_matches_the_part_fill, live_paint_bucket_triple_click_after_merging_matches_the_fixed_gradient, live_paint_bucket_triple_click_after_making_the_group_matches_the_made_fill, live_paint_bucket_double_and_triple_click_paint_joined_and_same_stroke_edges, live_paint_bucket_double_click_across_a_junction_paints_the_joined_edges, live_paint_bucket_double_click_released_back_on_the_first_edge_paints_the_joined_edges, live_paint_bucket_edge_double_click_after_filling_a_face_paints_the_joined_edges, live_paint_bucket_triple_click_with_a_cmd_press_between_matches_the_fill_before, live_paint_scope_and_match_are_checked}`, `builder::tests::{bucket_double_and_triple_click_widen_what_a_click_paints, bucket_triple_click_after_making_the_group_on_another_face_matches_its_own_face, bucket_triple_click_with_a_click_on_another_face_between_matches_the_first_fill, bucket_triple_click_with_a_view_tool_press_between_matches_the_first_fill, bucket_highlights_nothing_with_space_held}`, `canvas::tests::bucket_triple_click_with_a_view_tool_press_between_matches_the_fill_before`, `tests::build::{live_paint_scoped_fill_and_stroke_skip_parts_with_the_paint, live_paint_bucket_cmd_double_and_triple_click_paint_nothing}`, `tests::livepaint::{a_triple_click_fills_every_face_of_the_same_fill, clicks_with_space_held_paint_nothing}` (MCP).