mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 21:58:36 +00:00
Merge pull request #10134 from dotCooCoo/fix/properties-selection-summary
Performance: Properties rows read a large selection once per edit
This commit is contained in:
@@ -874,6 +874,9 @@ pub(crate) use vectorcraft_doc::range::parse_range;
|
||||
/// What the fill and stroke proxies read from the selected objects, kept with the selection
|
||||
/// ([`crate::doc_state::SelectionRecord`]).
|
||||
pub(crate) use paint::SelectionPaints;
|
||||
/// The faces and edges of Live Paint groups among objects that count with their group
|
||||
/// ([`crate::DocState::live_paint_covered_kept`] keeps an empty answer).
|
||||
pub(crate) use paint::live_paint_covered;
|
||||
#[cfg(test)]
|
||||
pub(crate) use paint::{PAINTS_READ, WEIGHTS_READ};
|
||||
|
||||
|
||||
@@ -1470,12 +1470,13 @@ fn in_live_paint(d: &Document, id: NodeId) -> bool {
|
||||
|
||||
impl DocState {
|
||||
/// [`live_paint_covered`] for `ids`, the selected objects or the selection's subjects: none
|
||||
/// when the [`SelectionPaints`] of the selection say no selected object is in a Live Paint group.
|
||||
/// when the [`SelectionPaints`] of the selection say no selected object is in a Live Paint group,
|
||||
/// else as [`DocState::live_paint_covered_kept`] keeps it for this version of the document.
|
||||
pub(crate) fn selection_live_paint_covered(&self, ids: &[NodeId]) -> std::collections::HashSet<NodeId> {
|
||||
if self.selection_paints().is_some_and(|p| !p.in_live_paint()) {
|
||||
return std::collections::HashSet::new();
|
||||
}
|
||||
live_paint_covered(&self.doc, ids)
|
||||
self.live_paint_covered_kept(ids)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -242,6 +242,58 @@ fn the_question_marks_read_only_the_selected_objects() {
|
||||
assert!(vectorcraft_doc::tree_walks() - walks <= 1, "one walk for a large selection");
|
||||
}
|
||||
|
||||
/// How many times this thread read the groups around selected objects for their Live Paint faces
|
||||
/// and edges ([`DocState::live_paint_covered_kept`]).
|
||||
fn cover_reads() -> usize {
|
||||
crate::doc_state::LIVE_PAINT_COVER_READS.with(Cell::get)
|
||||
}
|
||||
|
||||
/// After an edit the selection keeps no record of what it holds, and the proxies, the Stroke panel
|
||||
/// and the "?" ask on every frame whether its objects lie in a Live Paint group. They read the
|
||||
/// objects' groups once for each version of the document and selection, not once for each question.
|
||||
/// A Live Paint group selected with one of its faces still counts the face with its group.
|
||||
#[test]
|
||||
fn live_paint_faces_are_looked_for_once_per_document_version() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
let ids: Vec<NodeId> = (0..20).map(|i| rect(&mut s, 10.0 + 30.0 * f64::from(i), "#ff0000", "#000000")).collect();
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 5, "dy": 0})).unwrap();
|
||||
assert!(s.active().unwrap().selection_paints().is_none(), "the edit leaves the selection without a record");
|
||||
let ask = |s: &Session| {
|
||||
for _ in 0..3 {
|
||||
s.proxy_paints();
|
||||
s.shown_stroke();
|
||||
s.proxy_mixed();
|
||||
s.active().unwrap().stroke_mixed();
|
||||
}
|
||||
};
|
||||
let before = cover_reads();
|
||||
ask(&s);
|
||||
assert_eq!(cover_reads() - before, 1, "one read for this version of the document");
|
||||
s.execute("object.move", &json!({"dx": 5, "dy": 0})).unwrap();
|
||||
let before = cover_reads();
|
||||
ask(&s);
|
||||
assert_eq!(cover_reads() - before, 1, "an edit reads them again, once");
|
||||
s.execute("select.set", &json!({"ids": [ids[0].0, ids[1].0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 5, "dy": 0})).unwrap();
|
||||
let before = cover_reads();
|
||||
ask(&s);
|
||||
assert_eq!(cover_reads() - before, 1, "another selection reads them again, once");
|
||||
|
||||
// A Live Paint group with one of its edges selected too: the edge counts with its group on every ask.
|
||||
s.execute("livePaint.make", &json!({"ids": [ids[2].0, ids[3].0]})).unwrap();
|
||||
let group = s.active().unwrap().selection.objects[0];
|
||||
let edge = vectorcraft_tools::builder::edges(s.active().unwrap().doc.node(group).unwrap())[0].id;
|
||||
s.execute("select.set", &json!({"ids": [group.0, edge.0]})).unwrap();
|
||||
let st = s.active().unwrap();
|
||||
let covered = live_paint_covered(&st.doc, &st.selection.objects);
|
||||
assert!(covered.contains(&edge));
|
||||
for _ in 0..2 {
|
||||
assert_eq!(st.selection_live_paint_covered(&st.selection.objects), covered);
|
||||
}
|
||||
}
|
||||
|
||||
/// Random selections of a document with nested groups answer as a walk does.
|
||||
#[test]
|
||||
fn random_selections_match_a_walk_of_the_document() {
|
||||
|
||||
@@ -1027,6 +1027,84 @@ fn used_colors(d: &Document, ids: &[NodeId], only: Option<&BTreeSet<NodeId>>) ->
|
||||
used.into_iter().map(|(_, u)| u).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// Tests: how many objects [`several_colors`] read on this thread.
|
||||
static SEVERAL_COLORS_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Whether [`used_colors`] lists more than one color for the objects `ids`. The objects are read
|
||||
/// until a second color turns up.
|
||||
fn several_colors(d: &Document, ids: &[NodeId], only: Option<&BTreeSet<NodeId>>) -> bool {
|
||||
struct Seen<'a> {
|
||||
first: Option<ColorKey>,
|
||||
several: bool,
|
||||
patterns: Vec<&'a str>,
|
||||
}
|
||||
impl<'a> Seen<'a> {
|
||||
fn read(&mut self, m: &'a Node) {
|
||||
#[cfg(test)]
|
||||
SEVERAL_COLORS_READS.with(|c| c.set(c.get() + 1));
|
||||
let (first, several) = (&mut self.first, &mut self.several);
|
||||
paints(
|
||||
m,
|
||||
&mut |c, _, _, _| {
|
||||
let k = ColorKey::of(c);
|
||||
match first {
|
||||
None => *first = Some(k),
|
||||
Some(f) => *several |= *f != k,
|
||||
}
|
||||
},
|
||||
&mut self.patterns,
|
||||
);
|
||||
}
|
||||
fn read_tree(&mut self, n: &'a Node, only: Option<&BTreeSet<NodeId>>) {
|
||||
if self.several || only.is_some_and(|o| !o.contains(&n.id)) {
|
||||
return;
|
||||
}
|
||||
self.read(n);
|
||||
for c in n.children().into_iter().flatten() {
|
||||
self.read_tree(c, only);
|
||||
if self.several {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut seen = Seen { first: None, several: false, patterns: vec![] };
|
||||
for n in ids.iter().filter_map(|id| d.node(*id)) {
|
||||
seen.read_tree(n, only);
|
||||
if seen.several {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
let mut next = 0;
|
||||
while !seen.several
|
||||
&& let Some(&name) = seen.patterns.get(next)
|
||||
{
|
||||
next += 1;
|
||||
for a in d.pattern(name).map(|def| def.art.as_slice()).unwrap_or_default() {
|
||||
let _ = a.try_walk(&mut |m| {
|
||||
seen.read(m);
|
||||
if seen.several { std::ops::ControlFlow::Break(()) } else { std::ops::ControlFlow::Continue(()) }
|
||||
});
|
||||
if seen.several {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
seen.several
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Whether the objects `recolor.colors` reads without `ids` use more than one color: the
|
||||
/// selection, or with nothing selected the objects Select All takes. False without a document.
|
||||
pub fn several_colors_used(&self) -> bool {
|
||||
let Ok((ids, only)) = scope_ids(self, &Value::Null, "recolor.colors") else { return false };
|
||||
self.doc().is_ok_and(|st| several_colors(&st.doc, &ids, only.as_ref()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Count the colors `m` holds itself with `count` (the color, its swatch, the tint the paint
|
||||
/// stores, and whether it is a gradient stop that mixes the swatch's ink with process inks), and
|
||||
/// add the patterns it is painted with to `patterns`.
|
||||
@@ -2050,6 +2128,69 @@ mod tests {
|
||||
assert!(has(&t, RED), "P100 read again at the top");
|
||||
}
|
||||
|
||||
/// How many objects [`several_colors`] read on this thread.
|
||||
fn several_reads() -> usize {
|
||||
SEVERAL_COLORS_READS.with(std::cell::Cell::get)
|
||||
}
|
||||
|
||||
/// Whether more than one color is used ([`Session::several_colors_used`]), checked against the
|
||||
/// length of `recolor.colors`'s list, with how many objects the answer read.
|
||||
fn several(s: &mut Session, what: &str) -> (bool, usize) {
|
||||
let before = several_reads();
|
||||
let several = s.several_colors_used();
|
||||
let reads = several_reads() - before;
|
||||
let listed = s.execute("recolor.colors", &json!({})).unwrap()["colors"].as_array().map_or(0, Vec::len);
|
||||
assert_eq!(several, listed > 1, "{what}: {listed} colors listed");
|
||||
(several, reads)
|
||||
}
|
||||
|
||||
/// The Recolor quick action asks on every change whether the selection uses more than one
|
||||
/// color. The answer matches `recolor.colors`, pattern tiles and Select All's objects included,
|
||||
/// and the objects are read only until a second color turns up.
|
||||
#[test]
|
||||
fn several_colors_stop_at_the_second_color() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
let ids: Vec<u64> = (0..30)
|
||||
.map(|i| {
|
||||
let id =
|
||||
s.execute("shape.rectangle", &json!({"x": 10 + 20 * i, "y": 10, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("paint.setFill", &json!({"ids": [id], "color": "#ff0000"})).unwrap();
|
||||
s.execute("paint.setStroke", &json!({"ids": [id], "none": true})).unwrap();
|
||||
id
|
||||
})
|
||||
.collect();
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
assert_eq!(several(&mut s, "one red"), (false, 30), "one color: every object is read");
|
||||
// A blue object in the middle: the objects are read up to it, from either end.
|
||||
s.execute("paint.setFill", &json!({"ids": [ids[15]], "color": "#0000ff"})).unwrap();
|
||||
let (found, reads) = several(&mut s, "a blue in the middle");
|
||||
assert!(found && matches!(reads, 15 | 16), "{reads} objects read");
|
||||
s.execute("paint.setFill", &json!({"ids": [ids[0], ids[29]], "color": "#00ff00"})).unwrap();
|
||||
assert_eq!(several(&mut s, "green at both ends"), (true, 2), "the second object holds the second color");
|
||||
s.execute("select.set", &json!({"ids": &ids[1..15]})).unwrap();
|
||||
assert!(!several(&mut s, "the red ones").0);
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
assert!(several(&mut s, "Select All's objects").0);
|
||||
|
||||
// A red object filled with a pattern too: its tile's color counts.
|
||||
for (tile, expect) in [(BLUE, true), (RED, false)] {
|
||||
let filled = NodeId(ids[1]);
|
||||
s.edit("t", |d, _| {
|
||||
let art = square(d, 0.0, Paint::solid(tile));
|
||||
d.patterns.retain(|p| p.name != "Tile");
|
||||
d.patterns.push(PatternDef::new("Tile", vec![art]));
|
||||
if let Some(n) = d.node_mut(filled) {
|
||||
n.appearance.set_fill(Paint::Pattern { pattern: "Tile".into(), xf: Affine::IDENTITY });
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("select.set", &json!({"ids": [ids[1], ids[2]]})).unwrap();
|
||||
assert_eq!(several(&mut s, "a pattern tile").0, expect, "a tile of {tile:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A pattern tile's symbol instances charge the same limit as the art around it.
|
||||
#[test]
|
||||
fn pattern_tiles_charge_the_symbol_limit() {
|
||||
|
||||
@@ -203,11 +203,23 @@ pub struct DocState {
|
||||
/// The selected objects as [`crate::Session::select`] last checked them or the proxies last
|
||||
/// read them, with the document they were checked in ([`SelectionRecord`]). Not saved.
|
||||
pub(crate) selection_record: Option<SelectionRecord>,
|
||||
/// The objects that [`DocState::live_paint_covered_kept`] last found no face or edge of a Live
|
||||
/// Paint group among, with the document it read. Not saved.
|
||||
outside_live_paint: std::cell::RefCell<Option<OutsideLivePaint>>,
|
||||
/// The last move of the selected objects that changed nothing else they show ([`Moved`]).
|
||||
/// Not saved.
|
||||
pub moved: Option<Moved>,
|
||||
}
|
||||
|
||||
/// Objects that hold no face or edge of a Live Paint group that counts with its group
|
||||
/// ([`cmd::live_paint_covered`] is empty for them), with the document they were read in, held as
|
||||
/// [`EmptyArtboards::doc`] holds it.
|
||||
#[derive(Clone, Debug)]
|
||||
struct OutsideLivePaint {
|
||||
doc: std::sync::Weak<Document>,
|
||||
ids: Vec<NodeId>,
|
||||
}
|
||||
|
||||
/// A move of whole objects that took document `from` to document `to`: each object the selection
|
||||
/// shows, the key object and the key anchor included, is the one in `from` moved by `by`, and
|
||||
/// nothing else they are drawn from changed (no member snapped to the pixel grid, no blend spine
|
||||
@@ -320,6 +332,8 @@ thread_local! {
|
||||
pub(crate) static EMPTY_ARTBOARD_MEASURES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
/// Tests: how many times [`DocState::any_art_to_fit`] measured a document on this thread.
|
||||
pub(crate) static ART_TO_FIT_MEASURES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
/// Tests: how many times [`DocState::live_paint_covered_kept`] read the objects' groups on this thread.
|
||||
pub(crate) static LIVE_PAINT_COVER_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
static NEXT_DOC_UID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||||
@@ -540,6 +554,7 @@ impl DocState {
|
||||
measured_in_own: None,
|
||||
inline_resolved: None,
|
||||
selection_record: None,
|
||||
outside_live_paint: Default::default(),
|
||||
moved: None,
|
||||
}
|
||||
}
|
||||
@@ -558,6 +573,25 @@ impl DocState {
|
||||
pub(crate) fn selection_paints(&self) -> Option<&cmd::SelectionPaints> {
|
||||
self.selection_record().and_then(|r| r.paints.as_ref())
|
||||
}
|
||||
/// The faces and edges of Live Paint groups among `ids` that count with their group
|
||||
/// ([`cmd::live_paint_covered`]). An empty answer is kept for this version of the document and
|
||||
/// these objects, so the proxies and the Stroke panel, which ask on every frame, read the
|
||||
/// objects' groups once after an edit.
|
||||
pub(crate) fn live_paint_covered_kept(&self, ids: &[NodeId]) -> std::collections::HashSet<NodeId> {
|
||||
let kept = |k: &OutsideLivePaint| std::ptr::eq(k.doc.as_ptr(), Arc::as_ptr(&self.doc)) && k.ids == ids;
|
||||
if self.outside_live_paint.try_borrow().is_ok_and(|k| k.as_ref().is_some_and(kept)) {
|
||||
return std::collections::HashSet::new();
|
||||
}
|
||||
#[cfg(test)]
|
||||
LIVE_PAINT_COVER_READS.with(|c| c.set(c.get() + 1));
|
||||
let covered = cmd::live_paint_covered(&self.doc, ids);
|
||||
if covered.is_empty()
|
||||
&& let Ok(mut k) = self.outside_live_paint.try_borrow_mut()
|
||||
{
|
||||
*k = Some(OutsideLivePaint { doc: Arc::downgrade(&self.doc), ids: ids.to_vec() });
|
||||
}
|
||||
covered
|
||||
}
|
||||
/// Keep the [`SelectionRecord`] of the selection that [`crate::Session::select`] left, with
|
||||
/// Lock Guides `guides_locked`. `record` is the record of the selection before, in the same
|
||||
/// document: the objects that leave the selection are taken out of its paints and the objects
|
||||
|
||||
@@ -291,8 +291,9 @@ fn can_merge_live_paint(app: &VectorcraftApp) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the selection uses more than one colour (`recolor.colors`), which offers the Recolor
|
||||
/// quick action; memoised per document revision.
|
||||
/// Whether the selection uses more than one colour (`recolor.colors` lists more than one,
|
||||
/// [`vectorcraft_engine::Session::several_colors_used`]), which offers the Recolor quick action;
|
||||
/// memoised per document revision.
|
||||
pub(crate) fn multi_color(app: &mut VectorcraftApp) -> bool {
|
||||
let Some(st) = app.session.active() else { return false };
|
||||
let key = (st.uid, st.revision);
|
||||
@@ -301,8 +302,7 @@ pub(crate) fn multi_color(app: &mut VectorcraftApp) -> bool {
|
||||
{
|
||||
return multi;
|
||||
}
|
||||
let r = app.session.execute("recolor.colors", &json!({})).unwrap_or_default();
|
||||
let multi = r["colors"].as_array().is_some_and(|c| c.len() > 1);
|
||||
let multi = app.session.several_colors_used();
|
||||
app.canvas.multi_color = Some((key, multi));
|
||||
multi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user