@@ -274,30 +274,23 @@ impl DocumentMessageHandler {
274
274
}
275
275
276
276
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;
295
290
}
296
291
}
297
292
298
- let mut without_children: Vec < Vec < LayerId > > = vec ! [ ] ;
299
- recurse_layer_tree ( self , vec ! [ ] , & mut without_children, false ) ;
300
- without_children
293
+ keep
301
294
}
302
295
303
296
pub fn selected_layers_contains ( & self , path : & [ LayerId ] ) -> bool {
0 commit comments