diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index f252d09ff9..6ad25f82b9 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -562,8 +562,12 @@ fn shared_selection(selection: vectorcraft_doc::Selection, like: Option<&crate:: /// Selected top-level objects in paint order, dropping any whose ancestor is also selected. pub(crate) fn selected_roots(s: &Session) -> Result> { - let st = s.doc()?; - Ok(roots_of(&st.doc, st.selection.in_paint_order(&st.doc))) + Ok(selection_roots(s.doc()?)) +} + +/// [`selected_roots`] of one document's selection. +pub(crate) fn selection_roots(st: &crate::DocState) -> Vec { + roots_of(&st.doc, st.selection.in_paint_order(&st.doc)) } /// [`selected_roots`] for the commands that move or reshape the art (Move, Transform, Rotate…): a diff --git a/crates/engine/src/cmd/pathops.rs b/crates/engine/src/cmd/pathops.rs index e98e336feb..b062bc33f8 100644 --- a/crates/engine/src/cmd/pathops.rs +++ b/crates/engine/src/cmd/pathops.rs @@ -13,7 +13,7 @@ use vectorcraft_geom::{FillRule, PathData}; use vectorcraft_pathops as po; use vectorcraft_pathops::{BoolOp, PathfinderOp}; -use super::edit::selected_roots; +use super::edit::{selected_roots, selection_roots}; use super::{ CommandSpec, EngineError, Result, Session, always, bad, bool_or, checked_ids_or_selection, cmd, has_doc, has_selection, number_or, str_param, switch_or, @@ -813,7 +813,7 @@ fn outlinable(st: &StrokeLayer) -> bool { /// Does Outline Stroke have a stroke to turn into art somewhere under `n`: a visible stroke of /// some paint and weight on a path or compound path (type is not outlined by it)? -pub fn has_outlinable_stroke(n: &Node) -> bool { +fn has_outlinable_stroke(n: &Node) -> bool { let mut found = false; each_leaf(n, &mut |l| { found |= !matches!(l.kind, NodeKind::Text(_)) && l.appearance.items.iter().any(|i| matches!(i, AppearanceItem::Stroke(st) if outlinable(st))); @@ -821,6 +821,13 @@ pub fn has_outlinable_stroke(n: &Node) -> bool { found } +/// Does `object.path.outlineStroke` have a stroke to outline in `st`'s selection? It acts on the +/// selection's roots (a compound path's member stands for its compound, which holds the +/// appearance), so eligibility looks at those same roots, not at the selected nodes themselves. +pub fn can_outline_stroke(st: &crate::DocState) -> bool { + selection_roots(st).into_iter().filter_map(|id| st.doc.node(id)).any(has_outlinable_stroke) +} + /// `n` with `opacity` and `blend` applied: set on it when it has none of its own, else on a /// group around it. pub(crate) fn with_transparency(d: &mut Document, mut n: Node, opacity: f32, blend: BlendMode) -> Node { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 88d8c836ae..bd1a91b3cf 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -29,7 +29,7 @@ pub use cmd::clipboard::Clipboard; pub use cmd::create::constrain_click; pub use cmd::distortcmds::perspective_click; pub use cmd::params::{check_keys, check_params}; -pub use cmd::pathops::{auto_simplify_level, has_outlinable_stroke}; +pub use cmd::pathops::{auto_simplify_level, can_outline_stroke}; pub use cmd::rasterfx::{export_pdf, flatten_raster_effects}; pub use cmd::{CommandInfo, CommandSpec, command_specs, find_command}; pub use cmd::{EyedropperOptions, PathfinderOptions}; diff --git a/crates/ui-egui/src/menus/context.rs b/crates/ui-egui/src/menus/context.rs index cad797710b..655ea057ad 100644 --- a/crates/ui-egui/src/menus/context.rs +++ b/crates/ui-egui/src/menus/context.rs @@ -99,8 +99,8 @@ pub fn context_items(app: &VectorcraftApp) -> Vec { if paths { v.extend([c("Join", "path.join"), c("Average…", "path.average")]); } - // Outline Stroke turns strokes into filled art: offered when some object has a stroke to outline. - if roots.iter().any(|n| vectorcraft_engine::has_outlinable_stroke(n)) { + // Outline Stroke turns strokes into filled art: offered when the command has a stroke to outline in its roots. + if vectorcraft_engine::can_outline_stroke(st) { v.push(c("Outline Stroke", "object.path.outlineStroke")); } if !st.selection.anchors.is_empty() { diff --git a/crates/ui-egui/src/tests/contextmenu.rs b/crates/ui-egui/src/tests/contextmenu.rs index 9e56d86a00..cefa6f4f34 100644 --- a/crates/ui-egui/src/tests/contextmenu.rs +++ b/crates/ui-egui/src/tests/contextmenu.rs @@ -414,3 +414,46 @@ fn the_canvas_context_menu_offers_outline_stroke_for_a_stroked_selection() { assert!(has(&texts, "Arrange"), "the menu opened: {texts:?}"); assert!(!has(&texts, "Outline Stroke"), "no stroke, no Outline Stroke in {texts:?}"); } + +/// A compound path of two rectangles, filled and stroked with `weight`, isolated, with a member +/// right-clicked → (the app, the context, the compound path, the context menu's text). The member +/// stays selected and has no appearance of its own: the compound holds the paint. +fn right_click_member_of_compound(weight: f64) -> (VectorcraftApp, egui::Context, NodeId, Vec<(String, Rect)>) { + let mut app = app(); + let ctx = egui::Context::default(); + let (a, b) = (rect(&mut app, 50.0, 50.0), rect(&mut app, 200.0, 50.0)); + app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap(); + app.run("object.compoundPath.make", json!({})).unwrap(); + let compound = app.session.active().unwrap().selection.objects[0]; + // Filled, so that a click inside a member hits it whether or not the compound has a stroke. + app.run("paint.setFill", json!({"color": "#ff8010"})).unwrap(); + app.run("stroke.set", json!({"weight": weight})).unwrap(); + menus::invoke(&mut app, "object.isolate", json!({})); + frame(&mut app, &ctx, vec![]); + let member = app.session.active().unwrap().doc.node(compound).and_then(|n| n.children().and_then(|c| c.first()).map(|m| m.id)).unwrap(); + app.run("select.set", json!({"ids": [member.0]})).unwrap(); + let p = screen(&app, 100.0, 100.0); + let texts = click(&mut app, &ctx, p, PointerButton::Secondary); + let st = app.session.active().unwrap(); + assert_eq!(st.selection.objects, [member], "the right-click leaves the member selected"); + assert!(st.doc.node(member).is_some_and(|m| m.appearance.items.is_empty()), "a member has no appearance of its own"); + (app, ctx, compound, texts) +} + +/// With a member of an isolated compound path selected (a right-click inside the isolated compound +/// leaves a member selected), the member's own appearance is empty: the compound holds the paint. +/// `object.path.outlineStroke` promotes the member to its compound, so the menu offers Outline +/// Stroke exactly when the compound has a stroke to outline. +#[test] +fn outline_stroke_is_offered_for_a_member_of_a_stroked_compound_path() { + // No stroke on the compound path: nothing to outline. + let (.., texts) = right_click_member_of_compound(0.0); + assert!(has(&texts, "Arrange"), "the menu opened: {texts:?}"); + assert!(!has(&texts, "Outline Stroke"), "no stroke, no Outline Stroke in {texts:?}"); + + // Stroked: the item is offered for the member, and the command outlines the compound. + let (mut app, ctx, compound, texts) = right_click_member_of_compound(6.0); + assert!(has(&texts, "Outline Stroke"), "Outline Stroke for a member of a stroked compound in {texts:?}"); + click(&mut app, &ctx, at(&texts, "Outline Stroke"), PointerButton::Primary); + assert!(app.session.active().unwrap().doc.node(compound).is_none(), "the stroked compound path was replaced by its outline"); +}