Skip to content

Commit 6bcbb9f

Browse files
committed
Replace recursive tree traversal with prefix matching
1 parent bf41b3e commit 6bcbb9f

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

editor/src/document/document_file.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -274,30 +274,23 @@ impl DocumentMessageHandler {
274274
}
275275

276276
pub fn selected_layers_without_children(&self) -> Vec<Vec<LayerId>> {
277-
// Traversing the layer tree recursively was chosen for both readability and instead of an n^2 comparison approach.
278-
// A future optmiziation would be not needing to start at the root []
279-
fn recurse_layer_tree(ctx: &DocumentMessageHandler, mut path: Vec<u64>, without_children: &mut Vec<Vec<LayerId>>, selected: bool) {
280-
if let Ok(folder) = ctx.graphene_document.folder(&path) {
281-
for child in folder.list_layers() {
282-
path.push(*child);
283-
let selected_or_parent_selected = selected || ctx.selected_layers_contains(&path);
284-
let selected_without_any_parent_selected = !selected && ctx.selected_layers_contains(&path);
285-
if ctx.graphene_document.is_folder(&path) {
286-
if selected_without_any_parent_selected {
287-
without_children.push(path.clone());
288-
}
289-
recurse_layer_tree(ctx, path.clone(), without_children, selected_or_parent_selected);
290-
} else if selected_without_any_parent_selected {
291-
without_children.push(path.clone());
292-
}
293-
path.pop();
294-
}
277+
let mut sorted_layers = self.selected_layers().collect::<Vec<_>>();
278+
sorted_layers.sort();
279+
280+
if sorted_layers.is_empty() {
281+
return vec![];
282+
}
283+
284+
let mut current_path = sorted_layers.first().unwrap();
285+
let mut keep = vec![current_path.to_vec()];
286+
for path in &sorted_layers {
287+
if !path.starts_with(current_path) {
288+
keep.push(path.to_vec());
289+
current_path = path;
295290
}
296291
}
297292

298-
let mut without_children: Vec<Vec<LayerId>> = vec![];
299-
recurse_layer_tree(self, vec![], &mut without_children, false);
300-
without_children
293+
keep
301294
}
302295

303296
pub fn selected_layers_contains(&self, path: &[LayerId]) -> bool {

0 commit comments

Comments
 (0)