Symbols: a symbol dragged onto New Symbol is duplicated

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, in
one undo step. The drop works while no art is selected and the button
is disabled for clicks. The copy is made of the dragged symbol, not the
current one, and the current symbol and the selection stay as they
were. The button is outlined while a symbol of the document is held
over it; a library symbol, art or a name the document no longer has is
ignored.
This commit is contained in:
dotCooCoo
2026-11-16 06:53:55 -08:00
parent 2be34a95f1
commit 359a7a2be5
2 changed files with 110 additions and 1 deletions
+109 -1
View File
@@ -400,7 +400,14 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
app.run("symbol.breakLink", json!({})).ok();
}
ui.add_space((ui.available_width() - 2.0 * 28.0).max(0.0));
if widgets::icon_button_enabled(ui, "dc-new-item", tl!("New Symbol"), false, has_sel, 24.0).clicked() {
let new = widgets::icon_button_enabled(ui, "dc-new-item", tl!("New Symbol"), false, has_sel, 24.0);
// A symbol dragged from the panel and dropped on New Symbol is duplicated, with or without
// art selected.
if let Some(name) = symbol_dropped(ui, &new, &names) {
if let Err(e) = app.run("symbol.duplicate", json!({ "name": name })) {
app.status(e);
}
} else if new.clicked() {
// Alt skips Symbol Options: the symbol is made under the default name, dynamic as the
// dialog's default.
let (cmd, p) = if ui.input(|i| i.modifiers.alt) { ("symbol.new", json!({"type": "dynamic"})) } else { ("ui.symbolOptions", json!({})) };
@@ -486,6 +493,18 @@ pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) {
}
}
/// The document symbol (one of `names`) dragged from the panel and released on `button` this
/// frame, if any; `button` is outlined while one is held over it.
fn symbol_dropped(ui: &Ui, button: &egui::Response, names: &[String]) -> Option<String> {
let held = button.dnd_hover_payload::<PanelDrag>()?;
let PanelDrag::Symbol(name) = &*held else { return None };
if !names.contains(name) {
return None;
}
widgets::drop_target_outline(ui, button.rect);
button.dnd_release_payload::<PanelDrag>().map(|_| name.clone())
}
/// The panel state key of the symbols Select All Unused picked.
const MARKED: &str = "sym-unused";
@@ -1896,6 +1915,95 @@ mod tests {
assert_eq!(current(&app).as_deref(), Some("Dot"));
}
/// The rectangle of the panel's button labeled `label`, enabled or not.
fn button_rect(ctx: &egui::Context, app: &mut VectorcraftApp, label: &str) -> Rect {
ctx.enable_accesskit();
let out = frame_held(ctx, app, vec![], egui::Modifiers::NONE);
let nodes = &out.platform_output.accesskit_update.as_ref().expect("an accessibility tree").nodes;
let b = nodes.iter().filter(|(_, n)| n.label().is_some_and(|l| l.trim() == label)).find_map(|(_, n)| n.bounds());
let b = b.unwrap_or_else(|| panic!("a {label} button"));
Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32))
}
/// Whether `out` draws the drop outline around `rect`.
fn outlined(out: &egui::FullOutput, rect: Rect) -> bool {
out.shapes.iter().any(|c| match &c.shape {
egui::Shape::Rect(r) => r.rect.center().distance(rect.center()) < 0.5 && (r.stroke.width - 1.5).abs() < 0.01,
_ => false,
})
}
/// A symbol dragged from the panel, in Thumbnail View or a list view, and dropped on New Symbol
/// is duplicated as Duplicate Symbol does it: a copy of the dragged symbol, not the current one,
/// is added under its default name in one undo step, with no dialog and no placed instance, and
/// the selection and the current symbol stay as they were, with art selected or none (when the
/// button is disabled for clicks). The button is outlined while the symbol is held over it.
#[test]
fn symbol_dropped_on_new_button_duplicates_the_dragged_symbol_in_both_views() {
for (view, select) in [("thumbnail", false), ("smallList", true)] {
let mut app = leaf_and_dot();
app.run("ui.panelView", json!({"panel": "symbols", "view": view})).unwrap();
if select {
let leaf = instance_of(&app, "Leaf").expect("a Leaf instance");
app.run("select.set", json!({"ids": [leaf.0]})).unwrap();
}
let selection = app.session.active().unwrap().selection.clone();
let undo = app.session.active().unwrap().history.undo.len();
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
let new = button_rect(&ctx, &mut app, "New Symbol");
let from = if select { panel_labels(&ctx, &mut app, &["Leaf"])[0].1 } else { pos2(26.0, 26.0) };
frame(&ctx, &mut app, vec![egui::Event::PointerMoved(from), button(from, true)]);
frame(&ctx, &mut app, vec![egui::Event::PointerMoved(from + vec2(40.0, 40.0))]);
frame(&ctx, &mut app, vec![egui::Event::PointerMoved(from + vec2(80.0, 80.0))]);
assert_eq!(egui::DragAndDrop::payload::<PanelDrag>(&ctx).as_deref(), Some(&PanelDrag::Symbol("Leaf".into())), "{view}");
let out = frame_held(&ctx, &mut app, vec![egui::Event::PointerMoved(new.center())], egui::Modifiers::NONE);
assert!(outlined(&out, new), "{view}: outlined while Leaf is held over it");
frame(&ctx, &mut app, vec![button(new.center(), false)]);
assert_eq!(symbols(&mut app), ["Leaf", "Dot", "Leaf copy"], "{view}");
let d = app.session.active().unwrap();
assert_eq!(d.history.undo.len(), undo + 1, "{view}: one undo step");
assert_eq!(d.selection, selection, "{view}: the selection is unchanged");
let (leaf, copy) = (&d.doc.symbols[0], &d.doc.symbols[2]);
assert!(!Arc::ptr_eq(&leaf.art, &copy.art) && leaf.dynamic == copy.dynamic, "{view}: an independent copy of Leaf");
assert_eq!(d.doc.symbol_registration("Leaf copy"), d.doc.symbol_registration("Leaf"), "{view}");
assert_eq!(dialog_kind(&app), None, "{view}: no Symbol Options");
assert_eq!(art(&app, "Leaf copy").0, 0, "{view}: no instance placed");
assert_eq!(current(&app).as_deref(), Some("Dot"), "{view}: Dot stays current");
app.run("edit.undo", json!({})).unwrap();
assert_eq!(symbols(&mut app), ["Leaf", "Dot"], "{view}: undo removes only the copy");
}
}
/// New Symbol takes only a symbol of the document's: a library symbol, art, a symbol name the
/// document does not have and a symbol released elsewhere make no symbol and no undo step, and
/// the button is not outlined for them.
#[test]
fn new_symbol_button_ignores_other_drops_and_releases_outside() {
let mut app = leaf_and_dot();
let leaf = instance_of(&app, "Leaf").expect("a Leaf instance");
let undo = app.session.active().unwrap().history.undo.len();
let ctx = egui::Context::default();
let new = button_rect(&ctx, &mut app, "New Symbol");
let elsewhere = new.center() - vec2(0.0, 60.0);
let cases = [
("a library symbol", PanelDrag::LibrarySymbol { library: "basic-shapes".into(), name: "Leaf".into() }, new.center()),
("art", PanelDrag::Art(vec![leaf]), new.center()),
("a stale name", PanelDrag::Symbol("Gone".into()), new.center()),
("a symbol released elsewhere", PanelDrag::Symbol("Leaf".into()), elsewhere),
];
for (case, drag, at) in cases {
egui::DragAndDrop::set_payload(&ctx, drag);
let out = frame_held(&ctx, &mut app, vec![egui::Event::PointerMoved(at)], egui::Modifiers::NONE);
assert!(!outlined(&out, new), "{case}: not outlined");
frame(&ctx, &mut app, vec![button(at, false)]);
assert_eq!(symbols(&mut app), ["Leaf", "Dot"], "{case}");
assert_eq!(app.session.active().unwrap().history.undo.len(), undo, "{case}: no undo step");
assert_eq!(dialog_kind(&app), None, "{case}");
egui::DragAndDrop::clear_payload(&ctx);
}
}
/// CPU time of this thread in nanoseconds where the system reports it (Linux), else `None`.
fn thread_cpu_ns() -> Option<u128> {
std::fs::read_to_string("/proc/thread-self/schedstat").ok()?.split_whitespace().next()?.parse().ok()
+1
View File
@@ -1052,6 +1052,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
- **9-slice guides outside the art** ([the help page](https://help.adobe.com/archive/en/illustrator/cs6/illustrator_reference.pdf), Use 9-slice scaling section): a guide may lie outside the symbol's art, set by dragging it past the art's edge in symbol editing mode, by `symbol.options`'s `nineSliceGuides` or by a Symbol Options field below 0% or above 100%, and a scaled instance still scales, by the cells its art falls in. VectorCraft's own rules: each guide lies within 16 times the art's width or height of its edge (`-16..16` as a fraction, `-1600..1600` in percent) and the two guides of an axis keep their order; a guide past an edge of the art cuts the art at that edge, so the outer cell on its side holds no art; with both guides of an axis past opposite edges the art scales as a whole along it; with both past the same edge the art lies in the outer cell at the other edge and keeps its size, from that edge of the instance, or shrinks to the instance's width or height when the instance is smaller than the art; Symbol Options shows and keeps the guides unrounded. Native files keep the guides exactly; SVG and `.ai` write such an instance as its placed art and keep no guides. Tests: `symbol_nine_slice::tests::guides_outside_the_art_are_accepted_in_order_and_within_the_limit`, `art_scales_by_the_cells_it_falls_in_when_guides_lie_outside_it`, `the_map_with_guides_outside_the_art_is_continuous_and_monotone` in `crates/doc`, `guides_outside_the_art_are_set_by_command_and_scale_by_the_cells_that_hold_art`, `a_guide_dragged_past_the_arts_edge_lies_outside_it`, `guides_outside_the_art_survive_saving_and_reopening_a_native_file` in `crates/engine`, `nineslice::tests::a_guide_outside_the_art_is_taken_and_dragged_past_the_arts_edge`, `a_nine_slice_instance_with_a_guide_outside_its_art_is_written_as_drawn` (SVG and `.ai`).
- **Delete Symbol asks first** ([Symbols panel](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/symbols-panel-options.html), [symbol libraries](https://helpx.adobe.com/illustrator/desktop/manage-objects/traces-mockups-symbols/create-or-import-symbol-libraries.html), [Alt-click Delete](https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html)): the Symbols panel's Delete Symbol button and menu item ask "Delete the selected symbol?" with Yes and No for a symbol no instance uses. For a symbol with placed instances they ask with Expand Instances (the instances become ordinary art), Delete Instances and Cancel; either Instances button deletes the symbol in one undo step. Alt-click on the button deletes an unused symbol without asking and still asks about one with instances. A question left open while another document became active deletes nothing. `symbol.delete` itself does not ask. Tests: `panels::symbols::tests::delete_symbol_asks_first_and_alt_click_deletes_an_unused_symbol_at_once`, `the_in_use_question_expands_or_deletes_the_instances`, `a_stale_delete_symbol_question_deletes_nothing`, `the_delete_symbol_questions_show_their_buttons`.
- **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 or a name the document no longer has 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`.
- **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`.