mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 23:31:18 +00:00
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:
@@ -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;
|
||||
|
||||
@@ -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)]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user