Merge pull request #9317 from dotCooCoo/fix/selection-anchors-by-path

Canvas: the anchor overlay groups selected anchors by path with one lookup per path
This commit is contained in:
BFlat
2026-11-26 01:42:48 +07:00
committed by GitHub
2 changed files with 55 additions and 4 deletions
+24 -4
View File
@@ -2,7 +2,8 @@
//! centre point, point type's origin, the key anchor). Each rule is a function of the app that returns what to draw;
//! `show` hands the results to the framework's controls, which keep what they stroke between passes.
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::collections::hash_map::{DefaultHasher, Entry};
use std::hash::{Hash, Hasher};
use craft::behavior::Input;
@@ -316,13 +317,32 @@ pub(super) fn anchors(app: &VectorcraftApp, st: &DocState, tool: &str, tool_poin
primary.and_then(|key| chosen.iter().find(|(k, _)| *k == key)).map(|(_, knots)| knots.clone()).unwrap_or_default(),
segment.map(|(_, at)| at),
);
for (key, knots) in chosen.into_iter().filter(|(key, _)| Some(*key) != primary) {
selection = selection.also(key, knots);
}
selection.others = by_key(chosen.into_iter().filter(|(key, _)| Some(*key) != primary));
out.selection = selection;
out
}
/// `listed` with the lists of a repeated key joined, each key where it first appears: what
/// [`PathSelection::also`] makes of the entries added one at a time, found with one lookup per entry.
fn by_key<K: Copy + Eq + Hash, V>(listed: impl IntoIterator<Item = (K, Vec<V>)>) -> Vec<(K, Vec<V>)> {
let mut at: HashMap<K, usize> = HashMap::new();
let mut out: Vec<(K, Vec<V>)> = Vec::new();
for (key, items) in listed {
match at.entry(key) {
Entry::Occupied(seen) => {
if let Some((_, held)) = out.get_mut(*seen.get()) {
held.extend(items);
}
}
Entry::Vacant(new) => {
new.insert(out.len());
out.push((key, items));
}
}
}
out
}
/// The outline of the object under the pointer at document point `at`, at view `zoom`, for Smart Guides › Object
/// Highlighting: the object a click of the selection tools would pick, unless the click keeps the selection (the
/// object, or with the Selection tool selected art inside it, is selected) or the object has too many points.
@@ -558,3 +558,34 @@ fn an_empty_selection_has_no_marks() {
let st = app.session.active().unwrap();
assert_eq!(dots(&app, st).centers.len() + dots(&app, st).origins.len(), 0);
}
/// A path key that counts the comparisons made with it.
#[derive(Clone, Copy, Debug)]
struct Counted<'a>(u64, &'a std::cell::Cell<usize>);
impl PartialEq for Counted<'_> {
fn eq(&self, other: &Self) -> bool {
self.1.set(self.1.get() + 1);
self.0 == other.0
}
}
impl Eq for Counted<'_> {}
impl Hash for Counted<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
// Grouping the selected anchors of 2,000 paths compares each key a bounded number of times, not once per path listed
// before it, and joins a repeated path's anchors where the path first appears, as `PathSelection::also` does.
#[test]
fn grouping_selected_anchors_by_path_compares_each_path_a_bounded_number_of_times() {
let compared = std::cell::Cell::new(0);
let listed = (0..2000u64).map(|key| (Counted(key, &compared), vec![key])).chain([(Counted(5, &compared), vec![9])]);
let grouped = by_key(listed);
assert_eq!(grouped.iter().map(|(key, _)| key.0).collect::<Vec<_>>(), (0..2000).collect::<Vec<_>>());
assert_eq!(grouped[5].1, vec![5, 9]);
assert!(compared.get() < 2 * 2001, "{} comparisons for 2,001 entries", compared.get());
}