diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 2d2f5e9f4c..83998cb77c 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -140,6 +140,7 @@ impl VectorcraftApp { momentary: None, selection_box: None, selection_bounds: None, + selection_marks: None, content_bounds: Vec::new(), variable_rows: None, three_d_exportable: None, diff --git a/crates/ui-egui/src/caches.rs b/crates/ui-egui/src/caches.rs index dfac727639..61f572a46c 100644 --- a/crates/ui-egui/src/caches.rs +++ b/crates/ui-egui/src/caches.rs @@ -82,6 +82,8 @@ pub struct CanvasCache { pub selection_box: Option<((u64, u64, vectorcraft_effects::preview::Measure), Option)>, /// [`VectorcraftApp::selection_bounds`] for (document uid, revision). pub selection_bounds: Option<((u64, u64), Option)>, + /// The selection's outlines, anchors and dots the canvas drew last ([`canvas::selection::SelectionMarks`]). + pub(crate) selection_marks: Option, /// [`VectorcraftApp::content_bounds_of`] for (document uid, revision), one entry for each open document. pub content_bounds: Vec<((u64, u64), Option)>, /// Whether the 3D and Materials panel's Export 3D Object button is available (a selected diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 49878cb2d8..9a7734e494 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -39,7 +39,7 @@ pub(crate) mod panes; mod quick_pen_bar; mod raster; pub(crate) mod rulers; -mod selection; +pub(crate) mod selection; mod selection_widgets; pub(crate) mod task_bar; mod tool_adapter; diff --git a/crates/ui-egui/src/canvas/selection.rs b/crates/ui-egui/src/canvas/selection.rs index c8e0dd5212..e0ac7e7aee 100644 --- a/crates/ui-egui/src/canvas/selection.rs +++ b/crates/ui-egui/src/canvas/selection.rs @@ -631,29 +631,64 @@ pub(super) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, space: &CanvasSp if let Some(h) = hovered { HoverOutline::new("Object highlight", Some(HoverShape::Path(h.path))).color(DataColor::new(h.color)).show_at(ui, rect); } - for o in outlines(app, st) { + let key = marks_key(app, st, tool, tool_points); + let marks = match app.canvas.selection_marks.take() { + Some(kept) if kept.key == key => kept, + _ => SelectionMarks::build(app, st, tool, tool_points, key), + }; + for o in &marks.outlines { let mut outline = SelectionOutline::solid("Selection", &o.paths).color(DataColor::new(o.color)).revision(o.revision); if let Some(key) = &o.key { outline = outline.key(key); } outline.show_at(ui, rect); } - let marks = anchors(app, st, tool, tool_points); - if !marks.paths.is_empty() { - let mut paths = marks.paths; + let anchors = &marks.anchors; + if !anchors.paths.is_empty() { + let mut paths = anchors.paths.clone(); // The marks only show the selection: the tools make the edits. - let _ = BezierOverlay::new("Anchors", Bind::bound(&mut paths), &marks.selection) + let _ = BezierOverlay::new("Anchors", Bind::bound(&mut paths), &anchors.selection) .tool(BezierTool::Anchors) - .handles(marks.handles) - .anchors(marks.shown) - .segment_mark(marks.segment_mark) - .path_options(&marks.options) + .handles(anchors.handles) + .anchors(anchors.shown) + .segment_mark(anchors.segment_mark) + .path_options(&anchors.options) .show_at(ui, rect); } - let dots = dots(app, st); + let dots = &marks.dots; if !(dots.centers.is_empty() && dots.origins.is_empty() && dots.key_anchor.is_none()) { - SelectionDots { common: Common::new(), dots: &dots }.label("Selection dots").show_at(ui, rect); + SelectionDots { common: Common::new(), dots }.label("Selection dots").show_at(ui, rect); } + app.canvas.selection_marks = Some(marks); +} + +/// The outlines, anchors and dots `show` draws, kept between passes while their [`marks_key`] stays: a frame with +/// nothing changed walks no selected object. +pub(crate) struct SelectionMarks { + key: u64, + outlines: Vec, + anchors: Anchors, + dots: Dots, +} + +impl SelectionMarks { + fn build(app: &VectorcraftApp, st: &DocState, tool: &str, tool_points: bool, key: u64) -> Self { + #[cfg(test)] + tests::MARKS_BUILT.with(|built| built.set(built.get() + 1)); + Self { key, outlines: outlines(app, st), anchors: anchors(app, st, tool, tool_points), dots: dots(app, st) } + } +} + +/// What the selection's marks are built from: the document and its selection (its uid and revision), the parts the +/// view hides, Halftone Preview, the tool whose marks show, whether that tool draws its own points, and the +/// Selection & Anchor Display preferences. +fn marks_key(app: &VectorcraftApp, st: &DocState, tool: &str, tool_points: bool) -> u64 { + let mut h = DefaultHasher::new(); + let hides = ViewHides::of(app); + (st.uid, st.revision, hides.templates, hides.guides, hidden(app), tool, tool_points).hash(&mut h); + let prefs = &app.session.prefs; + (prefs.anchor_size, prefs.show_handles_multiple_anchors, prefs.handle_style.as_str()).hash(&mut h); + h.finish() } #[cfg(test)] diff --git a/crates/ui-egui/src/canvas/selection/tests.rs b/crates/ui-egui/src/canvas/selection/tests.rs index a2c32f5d53..672b07094a 100644 --- a/crates/ui-egui/src/canvas/selection/tests.rs +++ b/crates/ui-egui/src/canvas/selection/tests.rs @@ -881,3 +881,40 @@ fn grouping_selected_anchors_by_path_compares_each_path_a_bounded_number_of_time assert_eq!(grouped[5].1, vec![5, 9]); assert!(compared.get() < 2 * 2001, "{} comparisons for 2,001 entries", compared.get()); } + +thread_local! { + /// How many times the canvas built the selection's marks ([`SelectionMarks::build`]). + pub(super) static MARKS_BUILT: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// The marks the canvas drew last, for comparing with what the builders give now. +fn kept(app: &VectorcraftApp) -> (Vec>, usize) { + let marks = app.canvas.selection_marks.as_ref().expect("the canvas drew the selection's marks"); + let boxes = marks.outlines.iter().map(|o| o.paths.iter().map(|p| p.bounding_box()).collect()).collect(); + (boxes, all_selected(&marks.anchors.selection)) +} + +// Whole-app frames build the selection's marks once for each change: none in frames that change nothing, one after +// the selection changes and one after the tool changes, and what the canvas keeps is what the builders give. +#[test] +fn the_canvas_builds_the_selection_marks_once_per_change() { + let (mut app, curve) = scene(); + let ctx = crate::testing::ui_context(); + let frames = |app: &mut VectorcraftApp, n: usize| { + let before = MARKS_BUILT.with(std::cell::Cell::get); + for _ in 0..n { + let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 850.0)); + crate::testing::app_frame(app, &ctx, egui::RawInput { screen_rect: Some(screen), ..Default::default() }); + } + MARKS_BUILT.with(std::cell::Cell::get) - before + }; + frames(&mut app, 3); + assert_eq!(frames(&mut app, 10), 0, "frames that change nothing build no marks"); + app.run("select.set", json!({"ids": [curve.0]})).unwrap(); + assert_eq!(frames(&mut app, 5), 1, "a new selection builds its marks once"); + let st = app.session.active().unwrap(); + let boxes = outlines(&app, st).iter().map(|o| o.paths.iter().map(|p| p.bounding_box()).collect()).collect::>(); + assert_eq!(kept(&app), (boxes, all_selected(&marks(&app).selection))); + app.select_tool("directSelection"); + assert_eq!(frames(&mut app, 5), 1, "another tool builds the marks once"); +}