Task bar: the Recolor check stops at the first color

The Contextual Task Bar and the Control bar check, each time they are
drawn, whether the selection holds a color that Recolor Artwork can
change. The check walked the whole subtree of each selected object even
after the first color, so with one large group selected, the check read
every paint in the group.

- Node::try_walk visits in walk's order and ends at a ControlFlow::Break.
- swatches::node_has_colors stops at the first color node_colors would
  visit; node_colors now runs the same visitor without stopping, so the
  two read the same nodes and paints in the same order.
- has_colors in the task bar uses node_has_colors.

The result is the same for every selection. On one group of 100,000
paths, the check takes 16 ns instead of 14.5 ms when the first path is
filled; when only the last path is filled, or none is, it costs about
what the full walk costs.
This commit is contained in:
dotCooCoo
2026-11-16 08:14:55 -08:00
parent a6e94de555
commit dc6f1d2c69
5 changed files with 302 additions and 10 deletions
+12
View File
@@ -608,6 +608,18 @@ impl Node {
}
}
/// [`Self::walk`] that ends at the first node for which `f` returns [`std::ops::ControlFlow::Break`]: no
/// node after it is visited, and the break is returned.
pub fn try_walk<'a, B>(&'a self, f: &mut impl FnMut(&'a Node) -> std::ops::ControlFlow<B>) -> std::ops::ControlFlow<B> {
f(self)?;
if let Some(ch) = self.children() {
for c in ch {
c.try_walk(f)?;
}
}
std::ops::ControlFlow::Continue(())
}
/// Count of nodes in this subtree, ruler guides left out.
pub fn count(&self) -> usize {
let mut n = 0;
+28
View File
@@ -805,3 +805,31 @@ fn mapping_ruler_guide_positions_looks_at_each_child_of_nested_layers_once() {
assert!(Arc::ptr_eq(&group.children().unwrap()[0], &twice));
assert_eq!(calls, 0);
}
/// `try_walk` visits in `walk`'s order and visits nothing after the node that breaks it: not its
/// children, its later siblings or its ancestors' later siblings.
#[test]
fn try_walk_ends_at_the_break() {
use std::ops::ControlFlow;
let leaf = |id| Arc::new(Node::group(NodeId(id), vec![]));
let inner = Node::group(NodeId(2), vec![leaf(3), leaf(4), leaf(5)]);
let root = Node::group(NodeId(1), vec![Arc::new(inner), leaf(6), leaf(7)]);
let mut all = vec![];
root.walk(&mut |n| all.push(n.id));
let mut seen = vec![];
assert_eq!(
root.try_walk(&mut |n| {
seen.push(n.id);
ControlFlow::<()>::Continue(())
}),
ControlFlow::Continue(())
);
assert_eq!(seen, all, "without a break it visits what walk visits, in the same order");
seen.clear();
let flow = root.try_walk(&mut |n| {
seen.push(n.id);
if n.id == NodeId(3) { ControlFlow::Break(n.id) } else { ControlFlow::Continue(()) }
});
assert_eq!(flow, ControlFlow::Break(NodeId(3)));
assert_eq!(seen, [NodeId(1), NodeId(2), NodeId(3)]);
}