Merge pull request #9995 from dotCooCoo/fix/speed-i13656-selection-marks

Canvas: the selection's marks are built once per change
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 16:43:01 -08:00
commit 15aba428ce
5 files changed
+87 -12

No files matched your search

+1
View File
@@ -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,
+2
View File
@@ -82,6 +82,8 @@ pub struct CanvasCache {
pub selection_box: Option<((u64, u64, vectorcraft_effects::preview::Measure), Option<vectorcraft_doc::OrientedBox>)>,
/// [`VectorcraftApp::selection_bounds`] for (document uid, revision).
pub selection_bounds: Option<((u64, u64), Option<vectorcraft_geom::Rect>)>,
/// The selection's outlines, anchors and dots the canvas drew last ([`canvas::selection::SelectionMarks`]).
pub(crate) selection_marks: Option<canvas::selection::SelectionMarks>,
/// [`VectorcraftApp::content_bounds_of`] for (document uid, revision), one entry for each open document.
pub content_bounds: Vec<((u64, u64), Option<vectorcraft_geom::Rect>)>,
/// Whether the 3D and Materials panel's Export 3D Object button is available (a selected
+1 -1
View File
@@ -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;
+46 -11
View File
@@ -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<Outlined>,
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)]
@@ -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<usize> = 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<Vec<vectorcraft_geom::Rect>>, 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::<Vec<_>>();
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");
}