diff --git a/crates/engine/src/cmd/brushsym.rs b/crates/engine/src/cmd/brushsym.rs index 57194d3d45..9dab808563 100644 --- a/crates/engine/src/cmd/brushsym.rs +++ b/crates/engine/src/cmd/brushsym.rs @@ -265,7 +265,7 @@ pub fn specs() -> Vec { "Redefine Symbol", [], None, - "{name, ids?|id?} replace the symbol's art with the selection (instances update, keeping their appearance changes to the objects at paths the new art still has; the selection becomes an instance; a frame of threaded text in the selection leaves its thread with the text it shows, which the symbol's art holds) → {name, id}", + "{name, ids?|id?} replace the symbol's art with the selection (instances update, keeping their appearance changes to the objects at paths the new art still has; the selection becomes an instance; a frame of threaded text in the selection leaves its thread with the text it shows, which the symbol's art holds; art that holds an instance of the symbol (opacity masks included), directly or through the symbols it nests, is refused) → {name, id}", has_selection, symbol_update ), diff --git a/crates/engine/src/cmd/brushsym/symbols.rs b/crates/engine/src/cmd/brushsym/symbols.rs index cecd314858..776dbbfc6a 100644 --- a/crates/engine/src/cmd/brushsym/symbols.rs +++ b/crates/engine/src/cmd/brushsym/symbols.rs @@ -625,6 +625,10 @@ pub(super) fn symbol_update(s: &mut Session, p: &Value) -> Result { } } let mut art = selection_art(s, p)?.ok_or_else(|| EngineError::Other("nothing selected".into()))?; + // Nor can art that holds an instance of it, directly or through the symbols it nests. + if crate::cmd::symboledit::holds_symbol(&s.doc()?.doc, &art, &name, &mut Default::default()) { + return Err(EngineError::Other("a symbol can't hold an instance of itself".into())); + } crate::cmd::threads::keep_shown_text(&s.doc()?.doc, &mut art); let (art, size, c) = normalize(art)?; let old = natural_size(&s.doc()?.doc, &name); diff --git a/crates/engine/src/cmd/symboledit.rs b/crates/engine/src/cmd/symboledit.rs index 77266b5aaa..a413631bb3 100644 --- a/crates/engine/src/cmd/symboledit.rs +++ b/crates/engine/src/cmd/symboledit.rs @@ -314,12 +314,12 @@ pub(crate) fn follow_history(st: &mut crate::DocState, was: Option<&SymbolEdit>) } } -/// Does `n` hold an instance of symbol `name`, directly or through the art of the symbols it holds -/// instances of? -fn holds_symbol(d: &Document, n: &Node, name: &str, seen: &mut HashSet) -> bool { +/// Does `n` hold an instance of symbol `name` (opacity masks included), directly or through the art +/// of the symbols it holds instances of? +pub(crate) fn holds_symbol(d: &Document, n: &Node, name: &str, seen: &mut HashSet) -> bool { let mut found = false; let mut nested = vec![]; - n.walk(&mut |c| { + vectorcraft_doc::swatches::walk_with_masks(n, &mut |c| { if let NodeKind::SymbolInstance { symbol, .. } = &c.kind { if symbol == name { found = true; diff --git a/crates/engine/src/tests/brushsym.rs b/crates/engine/src/tests/brushsym.rs index d8bfed74d8..167709cdf1 100644 --- a/crates/engine/src/tests/brushsym.rs +++ b/crates/engine/src/tests/brushsym.rs @@ -2159,6 +2159,50 @@ fn break_link_then_update_redefines_all_instances() { assert!((bounds(&s, other).center().x - 300.0).abs() < 1e-6); } +/// Redefine Symbol refuses art that holds an instance of the symbol it redefines, directly, in a +/// group, in an opacity mask, or through the art of another symbol it holds an instance of, and +/// then changes nothing and adds no undo step. An instance of an unrelated symbol still redefines +/// it. +#[test] +fn redefine_refuses_art_holding_an_instance_of_the_symbol() { + let mut s = session(); + let (inst, name, _) = make_symbol(&mut s); + // Ring's art is an instance of Box. + let ring = NodeId(s.execute("symbol.new", &json!({"name": "Ring", "ids": [inst.0]})).unwrap()["id"].as_u64().unwrap()); + let place = + |s: &mut Session, x: f64| NodeId(s.execute("symbol.place", &json!({"name": name, "x": x, "y": 200})).unwrap()["id"].as_u64().unwrap()); + let direct = place(&mut s, 300.0); + let plain = rect(&mut s, 20.0, 220.0, 30.0, 30.0); + let grouped = place(&mut s, 100.0); + // A rectangle whose opacity mask is a Box instance. + let masked = rect(&mut s, 60.0, 260.0, 30.0, 30.0); + let mut mask_art = node(&s, direct); + let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); + mask_art.id = doc.alloc_id(); + doc.node_mut(masked).unwrap().extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(mask_art, true))); + let cases = [ + ("a Box instance", vec![direct.0]), + ("Ring, whose art holds Box", vec![ring.0]), + ("art with a Box instance", vec![plain.0, grouped.0]), + ("art masked by a Box instance", vec![masked.0]), + ]; + for (case, ids) in cases { + let before = s.doc().unwrap().doc.clone(); + let undo = s.doc().unwrap().history.undo.len(); + let e = s.execute("symbol.update", &json!({"name": name, "ids": ids})).unwrap_err(); + assert!(e.to_string().contains("can't hold an instance of itself"), "{case}: {e}"); + assert!(std::sync::Arc::ptr_eq(&before, &s.doc().unwrap().doc), "{case}: the document is unchanged"); + assert_eq!(s.doc().unwrap().history.undo.len(), undo, "{case}: no undo step"); + } + // An instance of a symbol that does not hold Box redefines it. + let dot = rect(&mut s, 200.0, 20.0, 10.0, 10.0); + let dot = NodeId(s.execute("symbol.new", &json!({"name": "Dot", "ids": [dot.0]})).unwrap()["id"].as_u64().unwrap()); + let r = s.execute("symbol.update", &json!({"name": name, "ids": [dot.0]})).unwrap(); + assert_eq!(r["name"], json!(name)); + let art = s.doc().unwrap().doc.symbols.iter().find(|x| x.name == name).map(|x| x.art.clone()).unwrap(); + assert!(matches!(&art.kind, NodeKind::SymbolInstance { symbol, .. } if symbol == "Dot"), "{}", art.kind_label()); +} + /// A linked opacity mask of a 10 × 10 square at (100, 50). fn square_mask(s: &mut Session) -> Box { let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc); diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index f605e5e947..bdda812306 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -317,7 +317,7 @@ pub(super) fn drag_art_out(app: &VectorcraftApp, ui: &Ui, resp: &egui::Response, // The art the drag started with: the release rolls the move back to it, and an Alt-copy's // copies, selected while it previews, go with that. let ids = it.selection.objects.clone(); - egui::DragAndDrop::set_payload(ui.ctx(), widgets::PanelDrag::Art(ids)); + widgets::drag_art(ui.ctx(), st.uid, ids); true } diff --git a/crates/ui-egui/src/panels/symbols.rs b/crates/ui-egui/src/panels/symbols.rs index e48a290bae..67fac53a15 100644 --- a/crates/ui-egui/src/panels/symbols.rs +++ b/crates/ui-egui/src/panels/symbols.rs @@ -326,6 +326,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { // The symbols drawn selected: the ones Select All Unused picked, else the current one. let on = |n: &str| if marked.is_empty() { sel.as_deref() == Some(n) } else { marked.iter().any(|m| m == n) }; let mut clicked: Option<(String, bool)> = None; + // Art released on a symbol with Alt (Option) held redefines that symbol; while Alt art is over + // a symbol, the list does not take it as a new symbol. + let mut redefine: Option<(String, Vec)> = None; + let mut over_symbol = false; let list_rect = widgets::list_box(ui, |ui| { ui.set_min_height(110.0); ui.set_width(ui.available_width()); @@ -345,6 +349,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } chip(ui, &doc, &keys, egui::Rect::from_center_size(r.left_center() + vec2(3.0 + side / 2.0, 0.0), vec2(side, side)), n); ui.painter().text(r.left_center() + vec2(side + 10.0, 0.0), egui::Align2::LEFT_CENTER, n, egui::FontId::proportional(text), t.text); + if let Some(ids) = art_redefine_drop(ui, &resp, &doc, uid, &mut over_symbol) { + redefine = Some((n.clone(), ids)); + } let resp = resp.on_hover_text(n); if resp.double_clicked() { clicked = Some((n.clone(), true)); @@ -372,6 +379,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { Stroke::new(if on { 2.0 } else { 1.0 }, if on { t.accent } else { t.border }), StrokeKind::Inside, ); + if let Some(ids) = art_redefine_drop(ui, &resp, &doc, uid, &mut over_symbol) { + redefine = Some((n.clone(), ids)); + } let resp = resp.on_hover_text(n); if resp.double_clicked() { clicked = Some((n.clone(), true)); @@ -391,11 +401,18 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { ui.min_rect() }); let zone = ui.interact(list_rect, ui.scope_id().with("symbols-drop"), Sense::hover()); - if let Some(ids) = widgets::art_drop(ui, &zone) + if !over_symbol + && let Some(ids) = widgets::art_drop(ui, &zone) && let Err(e) = app.run("symbol.new", json!({ "ids": ids, "type": "dynamic" })) { app.status(e); } + // Redefine Symbol with the dropped art, which becomes an instance of it, in one undo step. + if let Some((name, ids)) = redefine + && let Err(e) = app.run("symbol.update", json!({ "name": name, "ids": ids })) + { + app.status(e); + } // A double-click edits the symbol in symbol editing mode. if let Some((n, edit)) = clicked { // A click picks one symbol again. @@ -537,6 +554,43 @@ fn symbol_dropped(ui: &Ui, button: &egui::Response, names: &[String], uid: Optio button.dnd_release_payload::().map(|_| name.clone()) } +/// The ids of the art dragged off the canvas of the active document `doc` (`uid`) and released +/// with Alt (Option) held on the symbol tile or row `item` this frame, if any. While such art is +/// held over `item` with Alt, `over` is set and `item` is outlined. Art of another document, or +/// whose objects `doc` no longer has, is not taken. Only the release of the primary button drops +/// the art; the release of another button ends the drag. +fn art_redefine_drop(ui: &Ui, item: &egui::Response, doc: &Document, uid: Option, over: &mut bool) -> Option> { + if !alt_at_release(ui) { + return None; + } + let held = item.dnd_hover_payload::()?; + let PanelDrag::Art(ids) = &*held else { return None }; + *over = true; + let ours = uid.is_some() && widgets::art_drag_doc(ui.ctx()) == uid; + if !ours || ids.is_empty() || !ids.iter().all(|id| doc.node(*id).is_some_and(|n| !n.is_layer())) { + return None; + } + widgets::drop_target_outline(ui, item.rect); + if !ui.input(|i| i.pointer.button_released(egui::PointerButton::Primary)) { + return None; + } + item.dnd_release_payload::().map(|_| ids.iter().map(|id| id.0).collect()) +} + +/// Whether Alt (Option) is held for a drop: when the primary button went up in this frame, as it +/// went up, else now. +fn alt_at_release(ui: &Ui) -> bool { + ui.input(|i| { + i.events + .iter() + .find_map(|e| match e { + egui::Event::PointerButton { button: egui::PointerButton::Primary, pressed: false, modifiers, .. } => Some(modifiers.alt), + _ => None, + }) + .unwrap_or(i.modifiers.alt) + }) +} + /// The panel state key of the symbols Select All Unused picked. const MARKED: &str = "sym-unused"; @@ -769,6 +823,11 @@ mod tests { egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() } } + /// The primary button going up at `at` with Alt (Option) held. + fn alt_release(at: Pos2) -> egui::Event { + egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::ALT } + } + fn symbols(app: &mut VectorcraftApp) -> Vec { let list = app.run("symbol.list", json!({})).unwrap(); list["symbols"].as_array().unwrap().iter().map(|s| s["name"].as_str().unwrap_or_default().to_string()).collect() @@ -2418,6 +2477,177 @@ mod tests { assert_eq!(symbols(&mut app), ["Dot"], "the first document keeps its Dot"); } + /// The rects `out` outlines as drop targets (1.5 pt strokes). + fn drop_outlines(out: &egui::FullOutput) -> Vec { + out.shapes + .iter() + .filter_map(|c| match &c.shape { + egui::Shape::Rect(r) if (r.stroke.width - 1.5).abs() < 0.01 => Some(r.rect), + _ => None, + }) + .collect() + } + + /// Where symbol `name`'s tile or row is in the panel: its name in the list views, its tile (the + /// 44 pt squares, in panel order) in Thumbnail View. + fn item_at(ctx: &egui::Context, app: &mut VectorcraftApp, name: &str) -> Pos2 { + if app.ui.symbols_view != View::Thumbnail { + return panel_labels(ctx, app, &[name]).first().map(|l| l.1).unwrap_or_else(|| panic!("a {name} row")); + } + let out = frame_held(ctx, app, vec![], egui::Modifiers::NONE); + let tiles: Vec = out + .shapes + .iter() + .filter_map(|c| match &c.shape { + egui::Shape::Rect(r) if (r.rect.width() - 44.0).abs() < 0.01 && (r.rect.height() - 44.0).abs() < 0.01 => Some(r.rect), + _ => None, + }) + .collect(); + let i = symbols(app).iter().position(|n| n == name).unwrap_or_else(|| panic!("a {name} symbol")); + tiles.get(i).map(|r| r.center()).unwrap_or_else(|| panic!("a {name} tile in {tiles:?}")) + } + + /// The art of symbol `name` in the active document. + fn symbol_art(app: &VectorcraftApp, name: &str) -> Arc { + let doc = &app.session.active().unwrap().doc; + doc.symbols.iter().find(|s| s.name == name).map(|s| s.art.clone()).unwrap_or_else(|| panic!("a {name} symbol")) + } + + fn undo_len(app: &VectorcraftApp) -> usize { + app.session.active().unwrap().history.undo.len() + } + + /// Art dragged off the canvas and released with Alt (Option on macOS) held on a symbol's tile + /// or row, in each view, redefines that symbol and not the current one: its instances take the + /// art, the art becomes an instance of it, and no symbol is added, in one undo step. Only that + /// tile or row is outlined while the art is held over it with Alt. + #[test] + fn alt_art_drop_on_a_symbol_redefines_that_symbol_in_one_step() { + for view in ["thumbnail", "smallList", "largeList"] { + let mut app = leaf_and_dot(); + app.run("ui.panelView", json!({"panel": "symbols", "view": view})).unwrap(); + let shape = app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let (leaf, dot) = (symbol_art(&app, "Leaf"), symbol_art(&app, "Dot")); + let placed = instance_of(&app, "Leaf").expect("a Leaf instance"); + let undo = undo_len(&app); + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + let (at, other) = (item_at(&ctx, &mut app, "Leaf"), item_at(&ctx, &mut app, "Dot")); + widgets::drag_art(&ctx, uid(&app).unwrap(), vec![NodeId(shape)]); + let out = frame_held(&ctx, &mut app, vec![egui::Event::PointerMoved(at)], egui::Modifiers::ALT); + let outlines = drop_outlines(&out); + assert_eq!(outlines.len(), 1, "{view}: only the Leaf item is outlined: {outlines:?}"); + assert!(outlines[0].contains(at) && !outlines[0].contains(other), "{view}: {outlines:?}"); + frame_held(&ctx, &mut app, vec![alt_release(at)], egui::Modifiers::ALT); + assert_eq!(symbols(&mut app), ["Leaf", "Dot"], "{view}: no symbol added"); + assert_eq!(undo_len(&app), undo + 1, "{view}: one undo step"); + // The Leaf instance there was takes the 40 × 20 rectangle's shape, where the old art was square. + let doc = app.session.active().unwrap().doc.clone(); + let shape = doc.node(placed).and_then(|n| n.geometric_bounds()).map(|b| b.width() / b.height()); + assert!(shape.is_some_and(|r| (r - 2.0).abs() < 1e-6), "{view}: Leaf is the dropped art: {shape:?}"); + assert!(Arc::ptr_eq(&dot, &symbol_art(&app, "Dot")), "{view}: the current symbol Dot is unchanged"); + assert_eq!(art(&app, "Leaf"), (2, 0), "{view}: the art became a Leaf instance"); + app.run("edit.undo", json!({})).unwrap(); + assert!(Arc::ptr_eq(&leaf, &symbol_art(&app, "Leaf")), "{view}: undo restores Leaf"); + assert_eq!(art(&app, "Leaf"), (1, 1), "{view}: undo restores the art"); + app.run("edit.redo", json!({})).unwrap(); + assert_eq!(art(&app, "Leaf"), (2, 0), "{view}: redo"); + } + } + + /// Alt held as the primary button goes up redefines the symbol, though Alt is let go later in + /// the same frame. + #[test] + fn alt_let_go_after_the_release_in_the_same_frame_still_redefines() { + let mut app = leaf_and_dot(); + let shape = app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let leaf = symbol_art(&app, "Leaf"); + let undo = undo_len(&app); + let ctx = egui::Context::default(); + let at = item_at(&ctx, &mut app, "Leaf"); + widgets::drag_art(&ctx, uid(&app).unwrap(), vec![NodeId(shape)]); + frame_held(&ctx, &mut app, vec![egui::Event::PointerMoved(at)], egui::Modifiers::ALT); + frame_held(&ctx, &mut app, vec![alt_release(at), egui::Event::ModifiersChanged(egui::Modifiers::NONE)], egui::Modifiers::ALT); + assert_eq!(symbols(&mut app), ["Leaf", "Dot"], "no symbol added"); + assert!(!Arc::ptr_eq(&leaf, &symbol_art(&app, "Leaf")), "Leaf is redefined"); + assert_eq!(undo_len(&app), undo + 1, "one undo step"); + } + + /// Art released on a symbol without Alt makes a new symbol, as a drop elsewhere on the list + /// does, and leaves the symbol under the pointer as it was; nothing is outlined but the list. + #[test] + fn a_plain_art_drop_on_a_symbol_makes_a_new_symbol() { + let mut app = leaf_and_dot(); + let shape = app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let leaf = symbol_art(&app, "Leaf"); + let ctx = egui::Context::default(); + let at = item_at(&ctx, &mut app, "Leaf"); + widgets::drag_art(&ctx, uid(&app).unwrap(), vec![NodeId(shape)]); + let out = frame_held(&ctx, &mut app, vec![egui::Event::PointerMoved(at)], egui::Modifiers::NONE); + let outlines = drop_outlines(&out); + assert!(outlines.len() == 1 && outlines[0].width() > 100.0, "the list is outlined, not the tile: {outlines:?}"); + frame(&ctx, &mut app, vec![button(at, false)]); + assert_eq!(symbols(&mut app).len(), 3, "a new symbol"); + assert!(Arc::ptr_eq(&leaf, &symbol_art(&app, "Leaf")), "Leaf is unchanged"); + } + + /// An Alt drop that cannot redefine the symbol under the pointer changes nothing, makes no new + /// symbol and adds no undo step: art of another document, objects the document no longer has, + /// no objects, and a release away from the panel are not outlined, and art holding an instance + /// of the symbol is refused when it is released. + #[test] + fn an_alt_art_drop_that_cannot_redefine_changes_nothing() { + let mut app = leaf_and_dot(); + let shape = app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let instance = instance_of(&app, "Leaf").expect("a Leaf instance"); + let leaf = symbol_art(&app, "Leaf"); + let undo = undo_len(&app); + let ctx = egui::Context::default(); + let at = item_at(&ctx, &mut app, "Leaf"); + let doc = uid(&app).unwrap(); + let away = pos2(230.0, 395.0); + let cases = [ + ("another document's art", doc + 1, vec![NodeId(shape)], at, false), + ("objects the document lacks", doc, vec![NodeId(u64::MAX)], at, false), + ("no objects", doc, vec![], at, false), + ("a release away from the panel", doc, vec![NodeId(shape)], away, false), + ("a Leaf instance", doc, vec![instance], at, true), + ]; + for (case, from, ids, to, outline) in cases { + widgets::drag_art(&ctx, from, ids); + let out = frame_held(&ctx, &mut app, vec![egui::Event::PointerMoved(to)], egui::Modifiers::ALT); + assert_eq!(drop_outlines(&out).len(), usize::from(outline), "{case}: {:?}", drop_outlines(&out)); + frame_held(&ctx, &mut app, vec![alt_release(to)], egui::Modifiers::ALT); + egui::DragAndDrop::clear_payload(&ctx); + assert_eq!(symbols(&mut app), ["Leaf", "Dot"], "{case}: no symbol added"); + assert!(Arc::ptr_eq(&leaf, &symbol_art(&app, "Leaf")), "{case}: Leaf is unchanged"); + assert_eq!(undo_len(&app), undo, "{case}: no undo step"); + } + } + + /// Art held over a symbol, with Alt or without, and released with the secondary button is not + /// dropped: no symbol is redefined or added and no undo step is added. + #[test] + fn a_secondary_button_release_drops_no_art() { + let mut app = leaf_and_dot(); + let shape = app.run("shape.rectangle", json!({"x": 200, "y": 120, "width": 40, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let leaf = symbol_art(&app, "Leaf"); + let undo = undo_len(&app); + let ctx = egui::Context::default(); + let at = item_at(&ctx, &mut app, "Leaf"); + let secondary = egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() }; + for (case, modifiers) in [("with Alt", egui::Modifiers::ALT), ("without Alt", egui::Modifiers::NONE)] { + widgets::drag_art(&ctx, uid(&app).unwrap(), vec![NodeId(shape)]); + let out = frame_held(&ctx, &mut app, vec![egui::Event::PointerMoved(at)], modifiers); + assert_eq!(drop_outlines(&out).len(), 1, "{case}: the art is held over a target"); + frame_held(&ctx, &mut app, vec![secondary.clone()], modifiers); + egui::DragAndDrop::clear_payload(&ctx); + assert_eq!(symbols(&mut app), ["Leaf", "Dot"], "{case}: no symbol added"); + assert!(Arc::ptr_eq(&leaf, &symbol_art(&app, "Leaf")), "{case}: Leaf is unchanged"); + assert_eq!(undo_len(&app), undo, "{case}: no undo step"); + } + } + /// CPU time of this thread in nanoseconds where the system reports it (Linux), else `None`. fn thread_cpu_ns() -> Option { std::fs::read_to_string("/proc/thread-self/schedstat").ok()?.split_whitespace().next()?.parse().ok() diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 18ea33fa1c..12c9c37dd0 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -44,7 +44,8 @@ pub use color::{ }; pub(crate) use color::{HEX_FIELD_CHARS, focus_by_pointer, hex_text, pointer_phase}; pub use drag::{ - PanelDrag, RowClick, SwatchRows, apply_row_click, art_drop, doc_preview, drag_ghost, drag_source, drop_target_outline, ghost_row, row_click, + PanelDrag, RowClick, SwatchRows, apply_row_click, art_drag_doc, art_drop, doc_preview, drag_art, drag_ghost, drag_source, drop_target_outline, + ghost_row, row_click, }; #[cfg(test)] use dropdowns::combo_button_id; diff --git a/crates/ui-egui/src/widgets/drag.rs b/crates/ui-egui/src/widgets/drag.rs index eddadd4703..315bdf0366 100644 --- a/crates/ui-egui/src/widgets/drag.rs +++ b/crates/ui-egui/src/widgets/drag.rs @@ -51,14 +51,34 @@ pub enum PanelDrag { } /// A panel list `zone` that takes art dragged off the canvas: outlined while art is held over it; -/// → the dragged ids when it is released there. +/// → the dragged ids when the primary button is released there (the release of another button +/// ends the drag). pub fn art_drop(ui: &Ui, zone: &Response) -> Option> { let drag = zone.dnd_hover_payload::()?; let PanelDrag::Art(ids) = &*drag else { return None }; ui.painter().rect_stroke(zone.rect, 0.0, Stroke::new(1.5, Tokens::get(ui.ctx()).accent), StrokeKind::Inside); + if !ui.input(|i| i.pointer.button_released(egui::PointerButton::Primary)) { + return None; + } zone.dnd_release_payload::().map(|_| ids.iter().map(|id| id.0).collect()) } +/// Key of the document whose art a [`PanelDrag::Art`] carries. +fn art_doc_id() -> egui::Id { + egui::Id::unique("panel-drag-art-doc") +} + +/// Start (or keep up) a panel drag of the art `ids` of document `uid`, dragged off its canvas. +pub fn drag_art(ctx: &egui::Context, uid: u64, ids: Vec) { + ctx.data_mut(|d| d.insert_temp(art_doc_id(), uid)); + egui::DragAndDrop::set_payload(ctx, PanelDrag::Art(ids)); +} + +/// The document (`uid`) whose art the last [`drag_art`] carried. +pub fn art_drag_doc(ctx: &egui::Context) -> Option { + ctx.data(|d| d.get_temp::(art_doc_id())) +} + /// The Swatches panel rows a drag from that panel moves: the swatch, None, Registration or colour /// group under the pointer when the drag started (`grabbed`) and `names`, the panel selection when /// the grabbed one is part of it (only colour groups, or only swatches, like the grabbed one), in diff --git a/docs/mcp.md b/docs/mcp.md index ea72051f9f..b7ebc03f81 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3647,6 +3647,10 @@ already walks Image Trace. Short agent entry points: when that document is no longer open; plug-ins cannot call it), `symbol.breakLink {ids?}` (the art keeps the instance's opacity mask and its bounding box, square after `object.resetBoundingBox`; a hidden or locked instance's art stays hidden or locked and is not selected), `symbol.replace`, + `symbol.update {name, ids?|id?}` (Redefine Symbol: the selection, or `ids`, becomes the symbol's art and an instance + of it in one undo step; art that holds an instance of the symbol (opacity masks included), directly or through the art + of the symbols it holds instances of, is refused and nothing changes; the Symbols panel sends it with the dragged art's `ids` when art dragged + off the canvas is released on a symbol's tile or row with Alt (Option on macOS) held), `symbol.edit {id?, ids?, name?, x?, y?, artboard?, viewCenter?}` (symbol editing mode: the symbol's art on a temporary isolated layer, where the instance `id` shows it or centered on (x, y), by default on the center of artboard `artboard`, the active one the app passes, or in a document without artboards on `viewCenter`, the view's center the diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 3dc1f845b3..1e51476b16 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1133,6 +1133,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **Symbols panel context menu** ([Symbols panel](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/symbols-panel-options.html)): right-clicking a symbol in thumbnail view or list view displays a context menu with Place Symbol Instance (passes view center or artboard center), Edit Symbol, Duplicate Symbol, Delete Symbol, Break Link to Symbol (enabled when canvas selection includes instances of this symbol), and Select All Instances. Right-clicking an unselected symbol selects it first (`symbol.setCurrent`). Tests: `panels::symbols::tests::right_click_context_menu_in_symbols_panel_runs_actions`. - **Drag a symbol onto New Symbol to duplicate it** ([the help page](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=197), Duplicate a symbol in the panel section): a symbol dragged from the Symbols panel, in thumbnail view or a list view, and released on the New Symbol button is copied as Duplicate Symbol copies it (`symbol.duplicate`), with no Symbol Options dialog, even while no art is selected and the button is disabled for clicks. VectorCraft's own rules: the copy is made of the dragged symbol, not the current one, under Duplicate Symbol's default name, in one undo step; the current symbol and the selection stay as they were and no instance is placed; the button is outlined while a symbol of the document is held over it; a library symbol, art, a name the document no longer has or a symbol dragged from another document is ignored. Tests: `panels::symbols::tests::symbol_dropped_on_new_button_duplicates_the_dragged_symbol_in_both_views`, `new_symbol_button_ignores_other_drops_and_releases_outside`, `a_symbol_dragged_from_another_document_is_not_duplicated`. - **Drag a symbol onto Delete Symbol to delete it** ([the tutorial](https://shop.smashingmagazine.com/2011/09/an-in-depth-study-of-symbols-in-illustrator-cs5/), [Symbols panel](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/symbols-panel-options.html)): a symbol dragged from the Symbols panel, in thumbnail view or a list view, and released on the Delete Symbol button is deleted as a click on the button deletes it (`symbol.delete`): an unused symbol asks "Delete the selected symbol?", a symbol with placed instances asks with Expand Instances, Delete Instances and Cancel, and Alt held at the release deletes an unused symbol without asking. VectorCraft's own rules: the dragged symbol is deleted, not the current one, in one undo step; a symbol that Select All Unused picked deletes every picked symbol still unused, after one question, and a symbol outside the pick is deleted on its own; the button is outlined while a symbol of the document is held over it; a library symbol, art, a name the document no longer has or a symbol dragged from another document is ignored. Tests: `panels::symbols::tests::symbol_dropped_on_delete_button_deletes_the_dragged_symbol_in_both_views`, `a_picked_symbol_dropped_on_delete_deletes_the_picked_symbols`, `delete_symbol_button_ignores_other_drops_and_releases_outside`. + - **Alt-drag art onto a symbol to redefine it** ([the help page](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=194), Modify a symbol instance section): art dragged off the canvas and released on a symbol in the Symbols panel with Alt (Option on macOS) held replaces that symbol's art, and every instance of the symbol updates, as Redefine Symbol does (`symbol.update` with the dragged art's `ids`). Released without Alt, the art still becomes a new symbol. VectorCraft's own rules: the symbol under the pointer is redefined, not the current one, in thumbnail view and both list views; the art becomes an instance of it, in one undo step; only that tile or row is outlined while the art is held over it with Alt; art of another document, objects the document no longer has and art that holds an instance of the symbol (directly, in an opacity mask or through the symbols it nests, which Redefine Symbol from the menu also refuses) change nothing. Tests: `panels::symbols::tests::alt_art_drop_on_a_symbol_redefines_that_symbol_in_one_step`, `alt_let_go_after_the_release_in_the_same_frame_still_redefines`, `a_plain_art_drop_on_a_symbol_makes_a_new_symbol`, `an_alt_art_drop_that_cannot_redefine_changes_nothing`, `a_secondary_button_release_drops_no_art`, `tests::brushsym::redefine_refuses_art_holding_an_instance_of_the_symbol`. - **Symbols › Select All Unused** ([Symbols panel](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/symbols-panel-options.html), #4918): the Symbols panel menu's Select All Unused shows the symbols nothing places selected (`symbol.unused`): instances in the art (opacity masks included), graph designs, pattern tiles and brush art count, and so do instances in a used symbol's art. Delete Symbol, the button or the menu item, then asks "Delete the selected symbols?" once and deletes those still unused in one undo step (`symbol.delete {names}`); Alt-click on the button deletes them without asking, and a click on a symbol picks just it again. Tests: `tests::brushsym::symbol_unused_lists_the_symbols_nothing_places_and_delete_takes_several`, `panels::symbols::tests::select_all_unused_picks_the_unused_symbols_and_delete_deletes_them`, `dialogs::symbol_delete::tests::several_symbols_get_the_plural_question`. - **Symbols › Sort by Name and the list views** ([the help page](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/symbols-panel-options.html)): the Symbols panel menu's Sort by Name puts the symbols in alphabetical order (`symbol.sortByName`), and its Thumbnail View, Small List View and Large List View show them as thumbnails, in a compact list or in a list with larger thumbnails. VectorCraft's own rules: the sort ignores case and keeps names that differ only in case in their order; it changes the document's order, which `symbol.list` reports, as one undo step, keeps the current symbol and adds no undo step when the symbols are already in order; Thumbnail View is the default, Small List View has 18-point rows with 14-point thumbnails and Large List View 26-point rows with 22-point thumbnails, and a click or double-click on a symbol does the same in every view. `ui.panelView` sets the view of the Symbols panel, and of the Swatches and Graphic Styles panels, as their panel menus do. Tests: `tests::brushsym::symbol_sort_by_name_orders_the_symbols_in_one_undo_step`, `panels::symbols::tests::sort_by_name_and_the_small_and_large_list_views`, `panels::symbols::tests::panel_view_sets_the_view_of_each_panel`. - **The Symbols panel reads its current symbol from the document:** the Symbols panel and its panel menu read the current symbol on each frame without running `symbol.list`, which counts every symbol's instances by walking the whole document. The current symbol they show and act on is the one `symbol.list` reports, and an idle frame of the panel or its menu runs no command, so on macOS the native menu is not brought up to date on every frame while the panel is open. In a document with 200 symbols and 50,000 objects, the read takes 0.035 µs instead of 48.5 ms in a release build (thread CPU time on Linux, median of 7 runs); in a small document the difference is too small to notice. Tests: `tests::brushsym::current_symbol_is_the_current_symbol_list_answers`, `panels::symbols::tests::current_is_the_current_symbol_list_answers`, `panels::symbols::tests::idle_frames_of_the_panel_and_its_menu_run_no_command`; the timing probe is `panels::symbols::tests::current_symbol_read_timing`.