diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index aa07b13a90..543f182569 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -51,6 +51,8 @@ pub mod transform; pub mod transparency; pub mod variables; +use std::sync::Arc; + use egui::{Rect, Sense, Ui, vec2}; use serde_json::{Value, json}; use vectorcraft_color::{BlendMode, Color, Paint}; @@ -62,9 +64,11 @@ use crate::theme::Tokens; use crate::widgets::{Live, dim_label}; use crate::{VectorcraftApp, icons}; -/// The first selected node (cloned), if any. -pub fn first_selected(app: &VectorcraftApp) -> Option { - first_node(app).cloned() +/// The first selected node, as the `Arc` the document holds, if any. It does not change with later +/// edits: read it again after one. +pub fn first_selected(app: &VectorcraftApp) -> Option> { + let st = app.session.active()?; + st.selection.objects.first().and_then(|id| layers::node_arc(&st.doc, *id)) } /// The radius the Live Corners of path `n` show in the panels: that of the corners the panels set diff --git a/crates/ui-egui/src/panels/appearance.rs b/crates/ui-egui/src/panels/appearance.rs index 308b8f3602..3ea14385c1 100644 --- a/crates/ui-egui/src/panels/appearance.rs +++ b/crates/ui-egui/src/panels/appearance.rs @@ -466,7 +466,7 @@ fn edit_tip(resp: egui::Response, name: &egui::Response) -> egui::Response { pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let t = Tokens::get(ui.ctx()); let node = first_subject(app); - let sel = current_sel(app, ui.ctx(), node.as_ref()); + let sel = current_sel(app, ui.ctx(), node.as_deref()); let hide_thumb: bool = pstate(ui.ctx(), "ap-hide-thumb"); widgets::list_box(ui, |ui| { ui.spacing_mut().item_spacing.y = 0.0; @@ -522,7 +522,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { // Spacer row like Illustrator's empty tail. ui.allocate_exact_size(vec2(ui.available_width(), 10.0), Sense::hover()); }); - bottom(app, ui, node.as_ref(), sel); + bottom(app, ui, node.as_deref(), sel); delete_key(app, ui); // A row's popup, beside the panel (in the same window when the panel floats). let panel = ui.max_rect(); @@ -543,13 +543,13 @@ fn delete_key(app: &mut VectorcraftApp, ui: &mut Ui) { let keys = super::has_row_keys(&ctx, ROW_KEYS) && !pressed_outside; super::set_row_keys(&ctx, ROW_KEYS, keys); let node = first_subject(app); - let sel = current_sel(app, &ctx, node.as_ref()); + let sel = current_sel(app, &ctx, node.as_deref()); if keys && sel.deletable() && ctx.memory(|m| m.focused().is_none()) && ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Delete) || i.consume_key(egui::Modifiers::NONE, egui::Key::Backspace)) { - delete_selected(app, ui, node.as_ref(), sel); + delete_selected(app, ui, node.as_deref(), sel); } } @@ -564,10 +564,10 @@ fn thumbnail_drag(ui: &mut Ui, th: Rect, id: NodeId) { } /// The object the panel lists: the targeted layer, group or object (`layer.target`), else the -/// first selected one. -fn first_subject(app: &VectorcraftApp) -> Option { +/// first selected one, as the `Arc` the document holds. +fn first_subject(app: &VectorcraftApp) -> Option> { let st = app.session.active()?; - st.selection.subjects().first().and_then(|id| st.doc.node(*id)).cloned() + st.selection.subjects().first().and_then(|id| super::layers::node_arc(&st.doc, *id)) } /// The selection as the Control bar and the object row name it: "No Selection", the kind ("Path", @@ -1680,7 +1680,7 @@ pub(crate) fn fx_menu(app: &mut VectorcraftApp, ui: &mut Ui) { pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) { let node = first_subject(app); let has = node.is_some(); - let sel = current_sel(app, ui.ctx(), node.as_ref()); + let sel = current_sel(app, ui.ctx(), node.as_deref()); if menu_item(ui, tl!("Add New Fill"), has, false) { app.run("appearance.addFill", json!({})).ok(); } @@ -1689,10 +1689,10 @@ pub fn menu(app: &mut VectorcraftApp, ui: &mut Ui) { } ui.separator(); if menu_item(ui, tl!("Duplicate Item"), has && sel.editable(), false) { - duplicate_selected(app, ui, node.as_ref(), sel); + duplicate_selected(app, ui, node.as_deref(), sel); } if menu_item(ui, tl!("Remove Item"), has && sel.deletable(), false) { - delete_selected(app, ui, node.as_ref(), sel); + delete_selected(app, ui, node.as_deref(), sel); } if menu_item(ui, tl!("Clear Appearance"), has, false) { app.run("appearance.clear", json!({})).ok(); diff --git a/crates/ui-egui/src/panels/appearance/tests.rs b/crates/ui-egui/src/panels/appearance/tests.rs index 1b26bd3436..2d3350443b 100644 --- a/crates/ui-egui/src/panels/appearance/tests.rs +++ b/crates/ui-egui/src/panels/appearance/tests.rs @@ -87,7 +87,7 @@ fn fx_button(window: &PopupWindow) -> egui::Pos2 { fn draw_bottom_bar(app: &mut VectorcraftApp, ui: &mut Ui) { let node = first_subject(app); - bottom(app, ui, node.as_ref(), Sel::None); + bottom(app, ui, node.as_deref(), Sel::None); } /// The Add New Effect popup has one row per effect group, plug-in groups included. @@ -144,8 +144,8 @@ fn a_duplicate_highlights_the_copies_above_the_originals() { let duplicate = |app: &mut VectorcraftApp| { let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { let node = first_subject(app); - let sel = current_sel(app, ui.ctx(), node.as_ref()); - duplicate_selected(app, ui, node.as_ref(), sel); + let sel = current_sel(app, ui.ctx(), node.as_deref()); + duplicate_selected(app, ui, node.as_deref(), sel); }); out.textures_delta.clear(); }; @@ -173,7 +173,7 @@ fn a_duplicate_highlights_the_copies_above_the_originals() { set_pstate(&ctx, "ap-multi", Highlight { owner, rows: Rows::Effects(None), order: vec![0], anchor: Some(0), active: Some(0) }); let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { let n = first_subject(&app).unwrap(); - let sel = current_sel(&app, ui.ctx(), Some(&n)); + let sel = current_sel(&app, ui.ctx(), Some(&*n)); app.run("effect.duplicate", json!({"item": null, "indices": [4]})).unwrap(); highlight_copies(&mut app, ui.ctx(), &n, sel, Rows::Effects(None), &[4]); }); @@ -197,7 +197,7 @@ fn removing_items_keeps_a_surviving_item_opacity_row_selected() { let remove = |app: &mut VectorcraftApp, k: usize| { let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { let n = first_subject(app).unwrap(); - let sel = current_sel(app, ui.ctx(), Some(&n)); + let sel = current_sel(app, ui.ctx(), Some(&*n)); app.run("appearance.removeItem", json!({ "indices": [k] })).unwrap(); highlight_after_removal(app, ui.ctx(), &n, sel, Rows::Items, &[k]); }); @@ -228,9 +228,9 @@ fn deleting_an_opacity_row_sets_it_back_to_default() { let delete = |app: &mut VectorcraftApp| { let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { let node = first_subject(app); - let sel = current_sel(app, ui.ctx(), node.as_ref()); + let sel = current_sel(app, ui.ctx(), node.as_deref()); assert!(sel.deletable() && !sel.editable(), "{sel:?}"); - delete_selected(app, ui, node.as_ref(), sel); + delete_selected(app, ui, node.as_deref(), sel); }); out.textures_delta.clear(); }; @@ -1016,3 +1016,27 @@ fn a_mixed_selection_lists_what_its_objects_share() { run(&mut app, "select.set", json!({"ids": [ids[0].clone(), t]})); assert_eq!(frame(&mut app).iter().find(|t| *t != "?").map(String::as_str), Some("Mixed Objects")); } + +/// The panel's subject is the `Arc` the document holds for the selected object, a targeted group +/// or a targeted layer, not a copy of it; nothing with nothing selected. +#[test] +fn first_subject_is_the_node_the_document_holds() { + let mut app = app_with_a_selected_rectangle(); + let held = |app: &VectorcraftApp| { + let st = app.session.active().unwrap(); + super::super::layers::node_arc(&st.doc, st.selection.subjects()[0]).unwrap() + }; + assert!(std::sync::Arc::ptr_eq(&first_subject(&app).expect("the rectangle"), &held(&app)), "the selected object"); + let group = app.run("object.group", json!({})).unwrap()["id"].clone(); + app.run("layer.target", json!({ "id": group })).unwrap(); + let n = first_subject(&app).expect("the group"); + assert_eq!(n.id.0, group.as_u64().unwrap()); + assert!(std::sync::Arc::ptr_eq(&n, &held(&app)), "the targeted group"); + let layer = app.session.active().unwrap().doc.layers[0].id; + app.run("layer.target", json!({ "id": layer.0 })).unwrap(); + let n = first_subject(&app).expect("the layer"); + assert_eq!(n.id, layer); + assert!(std::sync::Arc::ptr_eq(&n, &held(&app)), "the targeted layer"); + app.run("select.none", json!({})).unwrap(); + assert!(first_subject(&app).is_none(), "nothing selected"); +} diff --git a/crates/ui-egui/src/panels/brushes.rs b/crates/ui-egui/src/panels/brushes.rs index 0b540a4ba5..48f063e4d6 100644 --- a/crates/ui-egui/src/panels/brushes.rs +++ b/crates/ui-egui/src/panels/brushes.rs @@ -23,7 +23,7 @@ use vectorcraft_engine::cmd::swatchlib::LibraryInfo; use super::library_panel::{self, LibraryKind, LibraryRef, Row}; use super::memo::Memo; -use super::{first_selected, pstate, set_pstate}; +use super::{first_node, pstate, set_pstate}; use crate::VectorcraftApp; use crate::dialogs::brush_options; use crate::menus::Item; @@ -168,7 +168,7 @@ pub(crate) fn definition_chip(ui: &Ui, r: egui::Rect, st: Option<&DocState>, def } fn selected_brush(app: &VectorcraftApp) -> Option { - first_selected(app).and_then(|n| n.appearance.stroke().and_then(|s| s.brush.clone())) + first_node(app).and_then(|n| n.appearance.stroke().and_then(|s| s.brush.clone())) } /// What the panel's last click was made with: the document, its selected objects and the current diff --git a/crates/ui-egui/src/panels/image_trace.rs b/crates/ui-egui/src/panels/image_trace.rs index bca4192b61..bae86b0c83 100644 --- a/crates/ui-egui/src/panels/image_trace.rs +++ b/crates/ui-egui/src/panels/image_trace.rs @@ -17,7 +17,7 @@ use vectorcraft_doc::{Node, NodeId, NodeKind, TraceView}; use vectorcraft_engine::cmd::TracePresetChange; use vectorcraft_engine::cmd::swatchlib::DOCUMENT_SWATCHES; -use super::{first_selected, pstate, set_pstate, swatches}; +use super::{first_node, pstate, set_pstate, swatches}; use crate::VectorcraftApp; use crate::widgets::{self, menu_item}; @@ -152,10 +152,10 @@ pub(crate) fn is_trace(n: &Node) -> bool { /// The object Trace acts on: the first selected image or Image Trace object in paint order, as /// `imageTrace.make` takes it, else the first selected object. -fn target_node(app: &VectorcraftApp) -> Option { +fn target_node(app: &VectorcraftApp) -> Option<&Node> { let st = app.session.active()?; let traceable = |n: &&Node| is_trace(n) || matches!(n.kind, NodeKind::Image(_)); - st.selection.in_paint_order(&st.doc).into_iter().find_map(|id| st.doc.node(id).filter(traceable).cloned()).or_else(|| first_selected(app)) + st.selection.in_paint_order(&st.doc).into_iter().find_map(|id| st.doc.node(id).filter(traceable)).or_else(|| first_node(app)) } /// The active document and the object Trace acts on. @@ -166,7 +166,7 @@ fn target_key(app: &VectorcraftApp) -> Option<(u64, NodeId)> { /// What Trace acts on: an Image Trace object (with its stored settings), a plain image, or neither. fn target(app: &VectorcraftApp) -> (bool, bool, Option) { let Some(n) = target_node(app) else { return (false, false, None) }; - (is_trace(&n), matches!(n.kind, NodeKind::Image(_)), n.extras().trace.as_deref().cloned()) + (is_trace(n), matches!(n.kind, NodeKind::Image(_)), n.extras().trace.as_deref().cloned()) } /// Whether any selected object is an Image Trace object, wherever it is in the selection. diff --git a/crates/ui-egui/src/panels/image_trace/tests.rs b/crates/ui-egui/src/panels/image_trace/tests.rs index 7f686db45a..db1a66242b 100644 --- a/crates/ui-egui/src/panels/image_trace/tests.rs +++ b/crates/ui-egui/src/panels/image_trace/tests.rs @@ -2,7 +2,7 @@ use super::super::tests_long_popups::{ PopupWindow, assert_last_row_scrolls_into_view, assert_rows_stay_on_one_line, assert_short_list_does_not_scroll, }; use super::*; -use crate::panels::library_panel; +use crate::panels::{first_selected, library_panel}; use egui::{Event, PointerButton, Pos2, Rect, vec2}; /// One headless frame of the panel (and its menu) with `events`: the texts drawn, with where. diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 5dede1697b..93c38ae998 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -26,7 +26,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let editing_artboards = app.session.tool_id() == "artboard"; // Ruler guides selected on their own (a click on one): their position. let guides = super::transform::selected_guides(app); - let label = heading(first.as_ref(), n_sel, editing_artboards, guides.is_some()); + let label = heading(first.as_deref(), n_sel, editing_artboards, guides.is_some()); ui.label(egui::RichText::new(label).size(11.5).color(t.text_dim)); ui.add_space(4.0); if editing_artboards { diff --git a/crates/ui-egui/src/panels/tests_appearance.rs b/crates/ui-egui/src/panels/tests_appearance.rs index 1db803b636..5ef1d8ce4a 100644 --- a/crates/ui-egui/src/panels/tests_appearance.rs +++ b/crates/ui-egui/src/panels/tests_appearance.rs @@ -2064,3 +2064,49 @@ fn a_dash_typed_in_a_stroke_popup_sticks_when_a_click_outside_closes_it() { assert!(!texts.iter().any(|(t, _)| t == "Dashed Line"), "{link}: the popup closed"); } } + +/// `first_selected` gives the `Arc` the document holds for the first selected object, nested in a +/// group too, not a copy of it; nothing with nothing selected or no document open. +#[test] +fn first_selected_is_the_node_the_document_holds() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + assert!(first_selected(&app).is_none(), "no document"); + run(&mut app, "file.new", json!({"width": 200, "height": 200})); + let a = run(&mut app, "shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50}))["id"].clone(); + let b = run(&mut app, "shape.ellipse", json!({"x": 70, "y": 10, "width": 50, "height": 50}))["id"].clone(); + let held = |app: &VectorcraftApp, id: &Value| layers::node_arc(&app.session.active().unwrap().doc, NodeId(id.as_u64().unwrap())).unwrap(); + run(&mut app, "select.set", json!({ "ids": [a, b] })); + let id = app.session.active().unwrap().selection.objects[0]; + let first = first_selected(&app).expect("a selected object"); + assert_eq!(first.id, id, "the first of the selection"); + assert!(Arc::ptr_eq(&first, &held(&app, &json!(id.0))), "the first selected object, as the document holds it"); + run(&mut app, "object.group", json!({})); + run(&mut app, "select.set", json!({ "ids": [a] })); + assert!(Arc::ptr_eq(&first_selected(&app).expect("the rectangle"), &held(&app, &a)), "a node inside a group"); + run(&mut app, "select.none", json!({})); + assert!(first_selected(&app).is_none(), "nothing selected"); +} + +/// Properties, Appearance and Transparency show the selected object's current opacity after an +/// edit and after Undo, never an earlier one. +#[test] +fn panels_show_the_selected_object_after_an_edit_and_after_undo() { + type Panel = fn(&mut VectorcraftApp, &mut egui::Ui); + let panels: [(&str, Panel); 3] = [("Properties", properties::show), ("Appearance", appearance::show), ("Transparency", transparency::show)]; + let mut app = app_with_rect(); + // One context per panel, kept across the edits, as the app keeps its own. + let ctxs: Vec = panels.iter().map(|_| egui::Context::default()).collect(); + let shows = |app: &mut VectorcraftApp, want: &str, not: &str| { + for ((name, panel), ctx) in panels.iter().zip(&ctxs) { + let texts = frame_in(ctx, app, *panel); + assert!(texts.iter().any(|t| t.contains(want)), "{name} shows {want}: {texts:?}"); + assert!(!texts.iter().any(|t| t.contains(not)), "{name} no longer shows {not}: {texts:?}"); + } + }; + run(&mut app, "transparency.set", json!({"opacity": 60})); + shows(&mut app, "60%", "35%"); + run(&mut app, "transparency.set", json!({"opacity": 35})); + shows(&mut app, "35%", "60%"); + run(&mut app, "edit.undo", json!({})); + shows(&mut app, "60%", "35%"); +} diff --git a/crates/ui-egui/src/panels/transparency.rs b/crates/ui-egui/src/panels/transparency.rs index 4279ac11e4..e0cd492a2e 100644 --- a/crates/ui-egui/src/panels/transparency.rs +++ b/crates/ui-egui/src/panels/transparency.rs @@ -4,6 +4,8 @@ //! neutral → off) and Opacity & Mask Define Knockout Shape; the menu's Page Isolated Blending and //! Page Knockout Group. +use std::sync::Arc; + use egui::{Sense, Stroke, StrokeKind, Ui, vec2}; use serde_json::json; use vectorcraft_color::{BlendMode, Paint}; @@ -20,20 +22,21 @@ pub(super) const MASK_THUMB: &str = "tr-mask-thumb"; /// The panel's values ([`vectorcraft_engine::Session::transparency_info`]) and its first target /// (the selection, or the object whose mask is being edited), recomputed only when the document -/// or the selection changes, or other gradient mesh points are picked. -fn state(app: &VectorcraftApp, ctx: &egui::Context) -> (TransparencyInfo, Option) { +/// or the selection changes, or other gradient mesh points are picked. The target is the `Arc` the +/// document holds. +fn state(app: &VectorcraftApp, ctx: &egui::Context) -> (TransparencyInfo, Option>) { type Key = (u64, u64, usize, Option, Option, Option)>>); let Some(st) = app.session.active() else { return Default::default() }; let sel = &st.selection.objects; let key: Key = (st.uid, st.revision, sel.len(), sel.first().copied(), sel.last().copied(), app.session.mesh_points_picked()); let id = egui::Id::unique("tr-state"); - if let Some((k, v)) = ctx.data(|d| d.get_temp::<(Key, (TransparencyInfo, Option))>(id)) + if let Some((k, v)) = ctx.data(|d| d.get_temp::<(Key, (TransparencyInfo, Option>))>(id)) && k == key { return v; } let info = app.session.transparency_info(); - let first = info.ids.first().and_then(|id| st.doc.node(*id)).cloned(); + let first = info.ids.first().and_then(|id| super::layers::node_arc(&st.doc, *id)); let v = (info, first); ctx.data_mut(|d| d.insert_temp(id, (key, v.clone()))); v @@ -82,18 +85,16 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { if editing.is_some() && oresp.on_hover_text(tl!("Stop editing the opacity mask")).clicked() { app.run("transparency.stopEditingOpacityMask", json!({})).ok(); } - if let Some(n) = &n { - let mut bare = n.clone(); - bare.extras_mut().mask = None; - if !node_thumb(app, ui, "tr-obj", &bare, r.shrink(3.0), None) { - let (f, s) = current_paints(app); - let inner = r.shrink2(vec2(6.0, 10.0)); - widgets::paint_chip(ui, inner, &f); - if let Some(c) = s.color() - && !matches!(s, Paint::None) - { - ui.painter().rect_stroke(inner, 0.0, Stroke::new(1.0, super::c32(&c)), StrokeKind::Middle); - } + if let Some(n) = &n + && !node_thumb(app, ui, "tr-obj", n, r.shrink(3.0), None, false) + { + let (f, s) = current_paints(app); + let inner = r.shrink2(vec2(6.0, 10.0)); + widgets::paint_chip(ui, inner, &f); + if let Some(c) = s.color() + && !matches!(s, Paint::None) + { + ui.painter().rect_stroke(inner, 0.0, Stroke::new(1.0, super::c32(&c)), StrokeKind::Middle); } } match (mask, n.as_ref().map(|n| n.id)) { @@ -113,7 +114,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let mresp = ui.interact(mr, egui::Id::unique(MASK_THUMB), Sense::click()); let bg = if m.clip != m.invert { [0, 0, 0, 255] } else { [255, 255, 255, 255] }; ui.painter().rect_filled(mr, 0.0, egui::Color32::from_rgb(bg[0], bg[1], bg[2])); - node_thumb(app, ui, "tr-mask", &m.art, mr.shrink(2.0), Some(bg)); + node_thumb(app, ui, "tr-mask", &m.art, mr.shrink(2.0), Some(bg), true); // Viewing the mask alone (Alt-click) highlights its thumbnail. let viewing = app.session.active().is_some_and(|d| d.shown_mask().is_some()); let border = match (viewing, editing.is_some()) { @@ -262,8 +263,9 @@ fn mask_items(mask: Option<&OpacityMask>, can_make: bool) -> [(&'static str, &'s /// A rendered thumbnail of `n` (which may live outside the tree, like mask art). One texture per /// slot, re-rendered only when the document revision, object or size changes, and not during a -/// drag (every pointer move is a new revision): the last one stays until it ends. -fn node_thumb(app: &VectorcraftApp, ui: &Ui, slot: &str, n: &vectorcraft_doc::Node, r: egui::Rect, bg: Option<[u8; 4]>) -> bool { +/// drag (every pointer move is a new revision): the last one stays until it ends. With `own_mask` +/// false, `n` is drawn without its opacity mask. +fn node_thumb(app: &VectorcraftApp, ui: &Ui, slot: &str, n: &vectorcraft_doc::Node, r: egui::Rect, bg: Option<[u8; 4]>, own_mask: bool) -> bool { use std::cell::RefCell; thread_local! { static RENDERER: RefCell = RefCell::new(vectorcraft_render::Renderer::new()); @@ -275,12 +277,20 @@ fn node_thumb(app: &VectorcraftApp, ui: &Ui, slot: &str, n: &vectorcraft_doc::No let cached: Option<(egui::Id, u64, egui::TextureHandle)> = ui.ctx().data(|d| d.get_temp(slot_id)); let tex = match cached { Some((k, rev, tex)) if k == key && (rev == st.revision || busy) => Some(tex), - _ => RENDERER.with(|rr| rr.borrow_mut().render_node_thumbnail_with(&st.doc, n, px, bg, &super::layers::thumb_options())).map(|img| { - let color = crate::canvas::color_image(&img); - let tex = ui.ctx().load_texture(slot, color, egui::TextureOptions::LINEAR); - ui.ctx().data_mut(|d| d.insert_temp(slot_id, (key, st.revision, tex.clone()))); - tex - }), + _ => { + let bare = (!own_mask && n.extras().mask.is_some()).then(|| { + let mut bare = n.clone(); + bare.extras_mut().mask = None; + bare + }); + let n = bare.as_ref().unwrap_or(n); + RENDERER.with(|rr| rr.borrow_mut().render_node_thumbnail_with(&st.doc, n, px, bg, &super::layers::thumb_options())).map(|img| { + let color = crate::canvas::color_image(&img); + let tex = ui.ctx().load_texture(slot, color, egui::TextureOptions::LINEAR); + ui.ctx().data_mut(|d| d.insert_temp(slot_id, (key, st.revision, tex.clone()))); + tex + }) + } }; let Some(tex) = tex else { return false }; let side = r.width().min(r.height()); @@ -448,4 +458,57 @@ mod tests { let texts = frame(&mut app); assert!(texts.iter().any(|t| t == "✓ Page Knockout Group") && texts.iter().any(|t| t == "✓ Page Isolated Blending"), "{texts:?}"); } + + /// The pixels of the thumbnail [`node_thumb`] draws of `n`, in a context of its own (so no + /// earlier texture is reused). + fn thumb_pixels(app: &VectorcraftApp, n: &Node, own_mask: bool) -> Vec { + let ctx = egui::Context::default(); + let r = egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(44.0, 44.0)); + let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { + assert!(node_thumb(app, ui, "tr-test", n, r, None, own_mask), "the thumbnail draws"); + }); + let cached: Option<(egui::Id, u64, egui::TextureHandle)> = ctx.data(|d| d.get_temp(egui::Id::unique("tr-test"))); + let id = cached.expect("the thumbnail's texture").2.id(); + let pixels = out.textures_delta.set.iter().find(|e| e.0.eq(&id)).and_then(|e| e.1.last()).map(|d| { + let egui::ImageData::Color(img) = &d.image; + img.pixels.clone() + }); + out.textures_delta.clear(); + pixels.expect("the thumbnail uploads") + } + + /// The panel's state holds the target as the document holds it, a new one after an edit and + /// after Undo; the object thumbnail of a masked object draws as the object without its mask. + #[test] + fn state_holds_the_documents_node_and_the_object_thumbnail_leaves_out_the_mask() { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + run(&mut app, "file.new", json!({"width": 100, "height": 100})); + let a = run(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50}))["id"].clone(); + let b = run(&mut app, "shape.ellipse", json!({"x": 10, "y": 10, "width": 20, "height": 20}))["id"].clone(); + run(&mut app, "select.set", json!({"ids": [a, b]})); + run(&mut app, "transparency.makeOpacityMask", json!({})); + let held = |app: &VectorcraftApp, id: NodeId| super::super::layers::node_arc(&app.session.active().unwrap().doc, id).expect("held"); + let ctx = egui::Context::default(); + let n = state(&app, &ctx).1.expect("the masked object"); + assert!(n.extras().mask.is_some(), "the object has its mask"); + assert!(Arc::ptr_eq(&n, &held(&app, n.id)), "the node the document holds"); + assert!(Arc::ptr_eq(&state(&app, &ctx).1.expect("cached"), &n), "the cached state gives the same node"); + // The object thumbnail: as a copy with the mask removed draws, not as the masked object. + let mut bare = (*n).clone(); + bare.extras_mut().mask = None; + let without = thumb_pixels(&app, &n, false); + assert!(without == thumb_pixels(&app, &bare, true), "the object without its mask"); + assert!(without != thumb_pixels(&app, &n, true), "the mask changes the drawing"); + // An edit and its Undo: the state follows the document. + run(&mut app, "transparency.set", json!({"opacity": 40})); + let (info, edited) = state(&app, &ctx); + let edited = edited.expect("the edited object"); + assert_eq!((info.opacity, edited.opacity), (Some(40.0), 0.4)); + assert!(Arc::ptr_eq(&edited, &held(&app, n.id)) && !Arc::ptr_eq(&edited, &n), "the edited node"); + run(&mut app, "edit.undo", json!({})); + let (info, undone) = state(&app, &ctx); + let undone = undone.expect("the object after Undo"); + assert_eq!((info.opacity, undone.opacity), (Some(100.0), 1.0)); + assert!(Arc::ptr_eq(&undone, &held(&app, n.id)), "the node Undo put back"); + } } diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 9fcb62e9d4..d2d341ab15 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2252,6 +2252,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **Live objects in documents with many symbols:** a live object, or an object with effects, that shows symbols or patterns is not walked again in a later frame to find them while the document's symbols and patterns stay the same. One that shows a placed file is walked in every frame. At 1000 × 1000 pixels, a page of 2,000 envelopes around instances of the last 2,000 of 10,000 symbols takes 30 ms a frame, and 83 ms when every frame walks them again, with the same pixels. - **Control bar and task bar on large paths:** the Control bar and the contextual task bar read the first selected object in place each frame instead of copying it; the task bar copies the object only when it is a basic shape, for its Shape Properties. With one selected open path of 200,000 anchors, an idle frame with both bars shown takes 3.8 ms in a release build instead of 8.0 ms (medians of five interleaved runs). Tests: `the_control_bar_label_follows_edits_undo_documents_and_selection`, `the_task_bar_follows_the_active_document`, `the_task_bar_follows_edits_and_undo`. +- **Panels on large selections:** the Properties, Transform, Brushes, Appearance, Transparency and Image Trace panels read the selected object where the document holds it each frame instead of copying it, and the Transparency panel removes the mask from a copy of the object only when it draws the object's thumbnail again. With one selected open path of 200,000 anchors, an idle repaint uses 72.9 µs of CPU time in a release build instead of 88.4 µs for the Properties panel, 27.5 µs instead of 37.2 µs for the Appearance panel and 19.6 µs instead of 29.8 µs for the Transparency panel with its thumbnails shown (medians of seven interleaved runs). Tests: `first_selected_is_the_node_the_document_holds`, `first_subject_is_the_node_the_document_holds`, `panels_show_the_selected_object_after_an_edit_and_after_undo`, `state_holds_the_documents_node_and_the_object_thumbnail_leaves_out_the_mask`. - **Paste of many objects:** Paste leaves pasted copies as they are when no resource has another name or color in the document and the objects link no graphic style. It looks up the pasted type in one walk of the tree. A paste and undo of 20,000 rectangles in 200 groups uses 49.2 ms of CPU time in a release build instead of 59.4 ms, and of 5,000 point type objects 25.3 ms instead of 29.4 ms (medians of seven interleaved rounds, each round the CPU time of a 61-paste run minus a 1-paste run, divided by 60). Tests: `crates/engine/tests/it/paste_many.rs`. - **Type drawn as glyph outlines:** under a geometry effect, in EMF and WMF export, in DXF export when it is written as outlines, flattened with Convert All Text to Outlines, and inside an envelope, type keeps its underline and strikethrough, the weight and dashes of its character strokes when it is scaled, and the placement of its gradients and patterns, as the canvas draws it. A group's or layer's own fills and strokes also paint the underline and strikethrough of the type in it.