Swatches: a one-row drag checks an earlier swatch list without indexing it

When a drag of rows from the Swatches list starts, or is checked after a
command, the rows are compared with the states of the document before the
newest undo steps. When such a state holds other swatch lists than the
current document, the check built a name index of that whole state even
for a single dragged row. A single row is now looked up directly in that
state, and an index is built only when more than one row is checked.

A new test starts a one-row drag after a swatch edit and checks that no
index is built and that the drag holds its row.
This commit is contained in:
dotCooCoo committed 2026-12-04 10:50:07 -08:00
1 parent 44964cb019
commit 0eaa1adba3
2 files changed
+51 -22

No files matched your search

+38 -22
View File
@@ -86,22 +86,37 @@ enum HeldRow {
}
impl HeldRow {
/// Row `name` of `d`: its swatch, else the colour group of that name. `index` holds the names of `d`.
fn of(d: &Document, index: &SwatchIndex, name: &str) -> Option<Self> {
match index.swatch(d, name) {
/// Row `name` of `d`: its swatch, else the color group of that name. With no index, looks directly in `d`.
fn of(d: &Document, index: Option<&SwatchIndex>, name: &str) -> Option<Self> {
match index.map_or_else(|| d.swatch(name), |index| index.swatch(d, name)) {
Some(w) => Some(HeldRow::Swatch(w.clone())),
None => index.groups(d, name).next().cloned().map(HeldRow::Group),
None => index
.map_or_else(|| d.swatch_groups.iter().find(|g| g.name == name), |index| index.groups(d, name).next())
.cloned()
.map(HeldRow::Group),
}
}
/// Whether `d` holds this row unchanged. `index` holds the names of `d`.
fn unchanged_in(&self, d: &Document, index: &SwatchIndex) -> bool {
/// Whether `d` holds this row unchanged. With no index, looks directly in `d`.
fn unchanged_in(&self, d: &Document, index: Option<&SwatchIndex>) -> bool {
match self {
HeldRow::Swatch(w) => index.swatch(d, &w.name) == Some(w),
HeldRow::Group(g) => index.groups(d, &g.name).any(|x| x == g),
HeldRow::Swatch(w) => index.map_or_else(|| d.swatch(&w.name), |index| index.swatch(d, &w.name)) == Some(w),
HeldRow::Group(g) => index.map_or_else(|| d.swatch_groups.iter().any(|x| x == g), |index| index.groups(d, &g.name).any(|x| x == g)),
}
}
}
/// The index that checks `rows` held rows against `d`: `index` when it holds the names of `d`, else an index of `d`
/// from `cache` for more than one row, else none, so that a single row is looked up directly in `d`.
fn checking_index(d: &Document, index: &Arc<SwatchIndex>, cache: &IndexCache, rows: usize) -> Option<Arc<SwatchIndex>> {
if index.of(d) {
Some(Arc::clone(index))
} else if rows > 1 {
Some(cache.get(d))
} else {
None
}
}
/// The newest undo step of `st` and the one under it, and the next redo step and the one under it
/// ([`HistoryEntry::serial`]), 0 for none.
fn steps(st: &DocState) -> [u64; 4] {
@@ -142,12 +157,12 @@ impl DragStart {
/// Rows `names` as this document holds them.
fn rows(&self, names: &[String]) -> HeldRows {
let rows: Vec<_> = names.iter().map(|n| (n.clone(), HeldRow::of(&self.doc, &self.index, n))).collect();
let back = self.back.as_deref().map(|d| {
let index = if self.index.of(d) { Arc::clone(&self.index) } else { Arc::new(SwatchIndex::new(d)) };
(d, index)
});
let back = rows.iter().map(|(_, r)| r.as_ref().zip(back.as_ref()).is_some_and(|(r, (d, index))| r.unchanged_in(d, index))).collect();
let rows: Vec<_> = names.iter().map(|n| (n.clone(), HeldRow::of(&self.doc, Some(&self.index), n))).collect();
let count = rows.iter().filter(|(_, row)| row.is_some()).count();
let indexes = IndexCache::default();
let back = self.back.as_deref().map(|d| (d, checking_index(d, &self.index, &indexes, count)));
let back =
rows.iter().map(|(_, r)| r.as_ref().zip(back.as_ref()).is_some_and(|(r, (d, index))| r.unchanged_in(d, index.as_deref()))).collect();
HeldRows { uid: self.uid, reverts: self.reverts, doc: Arc::downgrade(&self.doc), steps: self.steps, rows, back }
}
}
@@ -232,27 +247,28 @@ fn checked(held: HeldRows, st: &DocState, index: &Arc<SwatchIndex>) -> HeldRows
// Each state is looked at once for every row still held, and states with the same swatch lists share an index.
let mut kept: Vec<bool> = held.rows.iter().zip(&held.back).map(|((_, row), back)| row.is_some() && (*back || !undone)).collect();
let indexes = IndexCache::default();
let index_of = |d: &Document| if index.of(d) { Arc::clone(index) } else { indexes.get(d) };
match between {
Some(b) => {
for e in b.iter().chain(left) {
if !kept.contains(&true) {
let count = kept.iter().filter(|&&keep| keep).count();
if count == 0 {
break;
}
let at = index_of(&e.doc);
let at = checking_index(&e.doc, index, &indexes, count);
for ((_, row), keep) in held.rows.iter().zip(&mut kept) {
*keep = *keep && row.as_ref().is_some_and(|r| r.unchanged_in(&e.doc, &at));
*keep = *keep && row.as_ref().is_some_and(|r| r.unchanged_in(&e.doc, at.as_deref()));
}
}
for ((_, row), keep) in held.rows.iter().zip(&mut kept) {
*keep = *keep && row.as_ref().is_some_and(|r| r.unchanged_in(&st.doc, index));
*keep = *keep && row.as_ref().is_some_and(|r| r.unchanged_in(&st.doc, Some(index)));
}
}
None => kept.fill(false),
}
let rows: Vec<_> = held.rows.into_iter().zip(kept).map(|((name, row), keep)| (name, row.filter(|_| keep))).collect();
let last = undo.last().filter(|_| rows.iter().any(|(_, row)| row.is_some())).map(|e| (&*e.doc, index_of(&e.doc)));
let back = rows.iter().map(|(_, r)| r.as_ref().zip(last.as_ref()).is_some_and(|(r, (d, at))| r.unchanged_in(d, at))).collect();
let count = rows.iter().filter(|(_, row)| row.is_some()).count();
let last = undo.last().filter(|_| count > 0).map(|e| (&*e.doc, checking_index(&e.doc, index, &indexes, count)));
let back = rows.iter().map(|(_, r)| r.as_ref().zip(last.as_ref()).is_some_and(|(r, (d, at))| r.unchanged_in(d, at.as_deref()))).collect();
HeldRows { uid: held.uid, reverts: held.reverts, doc: Arc::downgrade(&st.doc), steps: now, rows, back }
}
@@ -276,7 +292,7 @@ pub(super) fn follow(app: &mut VectorcraftApp, r: &SwatchRows) -> Vec<String> {
r.names
.iter()
.zip(&now.rows)
.filter(|(n, (h, row))| *n == h && row.as_ref().is_some_and(|row| row.unchanged_in(&st.doc, &index)))
.filter(|(n, (h, row))| *n == h && row.as_ref().is_some_and(|row| row.unchanged_in(&st.doc, Some(&index))))
.map(|(n, _)| n.clone())
.collect()
} else {
@@ -96,6 +96,19 @@ fn a_drag_check_looks_up_names_in_proportion_to_the_swatch_count() {
assert!(large <= 2 * small, "name lookup work in one drag check: {small} with 200 swatches, {large} with 400");
}
/// Starting a one-row drag after a swatch edit uses the panel's index and checks the undo state without building an index.
#[test]
fn a_one_row_drag_after_a_swatch_edit_builds_no_name_index() {
let mut app = crowded(200);
app.run("swatch.new", json!({"name": "Fresh", "color": "#336699"})).unwrap();
index_of(&app);
index::counts::take();
let drag = rows_drag(&app, "Ink 0", &["Ink 0"]);
assert_eq!(index::counts::take().1, 0, "starting a one-row drag builds no index after a swatch edit");
let PanelDrag::Paint { rows: Some(held), .. } = &drag else { panic!("a rows drag") };
assert_eq!(dnd::follow(&mut app, held), ["Ink 0"], "the drag holds its row");
}
/// Highlighting a selection looks each selected name up once in the index, however many color groups the document
/// holds, and lights every selected name.
#[test]