@@ -273,31 +273,14 @@ impl DocumentMessageHandler {
273
273
self . layer_metadata . iter ( ) . filter_map ( |( path, data) | data. selected . then ( || path. as_slice ( ) ) )
274
274
}
275
275
276
- pub fn selected_layers_without_children ( & self ) -> Vec < Vec < LayerId > > {
277
- // TODO optimize this after MVP. Look at commit 1aaf5c0d7dbe67cd9f4ba8b536a4771a2cef6439, optimization was started
278
- // Traversing the layer tree recursively was chosen for readability, but should be replaced with an optimized approach later
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
- }
295
- }
296
- }
276
+ pub fn selected_layers_without_children ( & self ) -> Vec < & [ LayerId ] > {
277
+ let mut sorted_layers = self . selected_layers ( ) . collect :: < Vec < _ > > ( ) ;
278
+ // Sorting here creates groups of similar UUID paths
279
+ sorted_layers. sort ( ) ;
280
+ sorted_layers. dedup_by ( |a, b| a. starts_with ( b) ) ;
297
281
298
- let mut without_children: Vec < Vec < LayerId > > = vec ! [ ] ;
299
- recurse_layer_tree ( self , vec ! [ ] , & mut without_children, false ) ;
300
- without_children
282
+ // We need to maintain layer ordering
283
+ self . sort_layers ( sorted_layers. iter ( ) . copied ( ) )
301
284
}
302
285
303
286
pub fn selected_layers_contains ( & self , path : & [ LayerId ] ) -> bool {
@@ -365,23 +348,19 @@ impl DocumentMessageHandler {
365
348
}
366
349
367
350
/// Returns an unsorted list of all layer paths including folders at all levels, except the document's top-level root folder itself
368
- pub fn all_layers ( & self ) -> Vec < Vec < LayerId > > {
369
- self . layer_metadata . keys ( ) . filter ( |path| !path. is_empty ( ) ) . cloned ( ) . collect ( )
351
+ pub fn all_layers ( & self ) -> impl Iterator < Item = & [ LayerId ] > {
352
+ self . layer_metadata . keys ( ) . filter_map ( |path| ( !path. is_empty ( ) ) . then ( || path . as_slice ( ) ) )
370
353
}
371
354
372
355
/// Returns the paths to all layers in order, optionally including only selected or non-selected layers.
373
- fn layers_sorted ( & self , selected : Option < bool > ) -> Vec < Vec < LayerId > > {
356
+ fn sort_layers < ' a > ( & self , paths : impl Iterator < Item = & ' a [ LayerId ] > ) -> Vec < & ' a [ LayerId ] > {
374
357
// Compute the indices for each layer to be able to sort them
375
- let mut layers_with_indices: Vec < ( Vec < LayerId > , Vec < usize > ) > = self
376
-
377
- . layer_metadata
378
- . iter ( )
358
+ let mut layers_with_indices: Vec < ( & [ LayerId ] , Vec < usize > ) > = paths
379
359
// 'path.len() > 0' filters out root layer since it has no indices
380
- . filter_map ( |( path, data ) | ( !path. is_empty ( ) && ( data . selected == selected . unwrap_or ( data . selected ) ) ) . then ( || path. clone ( ) ) )
360
+ . filter_map ( |path| ( !path. is_empty ( ) ) . then ( || path) )
381
361
. filter_map ( |path| {
382
- // TODO: Currently it is possible that `layer_metadata` contains layers that are don't actually exist (has been partially fixed in #281) and thus
383
362
// TODO: `indices_for_path` can return an error. We currently skip these layers and log a warning. Once this problem is solved this code can be simplified.
384
- match self . graphene_document . indices_for_path ( & path) {
363
+ match self . graphene_document . indices_for_path ( path) {
385
364
Err ( err) => {
386
365
warn ! ( "layers_sorted: Could not get indices for the layer {:?}: {:?}" , path, err) ;
387
366
None
@@ -396,19 +375,19 @@ impl DocumentMessageHandler {
396
375
}
397
376
398
377
/// Returns the paths to all layers in order
399
- pub fn all_layers_sorted ( & self ) -> Vec < Vec < LayerId > > {
400
- self . layers_sorted ( None )
378
+ pub fn all_layers_sorted ( & self ) -> Vec < & [ LayerId ] > {
379
+ self . sort_layers ( self . all_layers ( ) )
401
380
}
402
381
403
382
/// Returns the paths to all selected layers in order
404
- pub fn selected_layers_sorted ( & self ) -> Vec < Vec < LayerId > > {
405
- self . layers_sorted ( Some ( true ) )
383
+ pub fn selected_layers_sorted ( & self ) -> Vec < & [ LayerId ] > {
384
+ self . sort_layers ( self . selected_layers ( ) )
406
385
}
407
386
408
387
/// Returns the paths to all non_selected layers in order
409
388
#[ allow( dead_code) ] // used for test cases
410
- pub fn non_selected_layers_sorted ( & self ) -> Vec < Vec < LayerId > > {
411
- self . layers_sorted ( Some ( false ) )
389
+ pub fn non_selected_layers_sorted ( & self ) -> Vec < & [ LayerId ] > {
390
+ self . sort_layers ( self . all_layers ( ) . filter ( |layer| ! self . selected_layers ( ) . any ( |path| & path == layer ) ) )
412
391
}
413
392
414
393
pub fn layer_metadata ( & self , path : & [ LayerId ] ) -> & LayerMetadata {
@@ -652,7 +631,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
652
631
self . backup ( responses) ;
653
632
654
633
for path in self . selected_layers_without_children ( ) {
655
- responses. push_front ( DocumentOperation :: DeleteLayer { path } . into ( ) ) ;
634
+ responses. push_front ( DocumentOperation :: DeleteLayer { path : path . to_vec ( ) } . into ( ) ) ;
656
635
}
657
636
658
637
responses. push_front ( ToolMessage :: DocumentIsDirty . into ( ) ) ;
@@ -664,7 +643,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
664
643
DuplicateSelectedLayers => {
665
644
self . backup ( responses) ;
666
645
for path in self . selected_layers_sorted ( ) {
667
- responses. push_back ( DocumentOperation :: DuplicateLayer { path } . into ( ) ) ;
646
+ responses. push_back ( DocumentOperation :: DuplicateLayer { path : path . to_vec ( ) } . into ( ) ) ;
668
647
}
669
648
}
670
649
SelectLayer ( selected, ctrl, shift) => {
@@ -730,7 +709,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
730
709
}
731
710
SelectAllLayers => {
732
711
let all_layer_paths = self . all_layers ( ) ;
733
- responses. push_front ( SetSelectedLayers ( all_layer_paths) . into ( ) ) ;
712
+ responses. push_front ( SetSelectedLayers ( all_layer_paths. map ( |path| path . to_vec ( ) ) . collect ( ) ) . into ( ) ) ;
734
713
}
735
714
DeselectAllLayers => {
736
715
responses. push_front ( SetSelectedLayers ( vec ! [ ] ) . into ( ) ) ;
0 commit comments