@@ -273,31 +273,14 @@ impl DocumentMessageHandler {
273273 self . layer_metadata . iter ( ) . filter_map ( |( path, data) | data. selected . then ( || path. as_slice ( ) ) )
274274 }
275275
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) ) ;
297281
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 ( ) )
301284 }
302285
303286 pub fn selected_layers_contains ( & self , path : & [ LayerId ] ) -> bool {
@@ -365,23 +348,19 @@ impl DocumentMessageHandler {
365348 }
366349
367350 /// 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 ( ) ) )
370353 }
371354
372355 /// 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 ] > {
374357 // 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
379359 // '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) )
381361 . 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
383362 // 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) {
385364 Err ( err) => {
386365 warn ! ( "layers_sorted: Could not get indices for the layer {:?}: {:?}" , path, err) ;
387366 None
@@ -396,19 +375,19 @@ impl DocumentMessageHandler {
396375 }
397376
398377 /// 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 ( ) )
401380 }
402381
403382 /// 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 ( ) )
406385 }
407386
408387 /// Returns the paths to all non_selected layers in order
409388 #[ 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 ) ) )
412391 }
413392
414393 pub fn layer_metadata ( & self , path : & [ LayerId ] ) -> & LayerMetadata {
@@ -652,7 +631,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
652631 self . backup ( responses) ;
653632
654633 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 ( ) ) ;
656635 }
657636
658637 responses. push_front ( ToolMessage :: DocumentIsDirty . into ( ) ) ;
@@ -664,7 +643,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
664643 DuplicateSelectedLayers => {
665644 self . backup ( responses) ;
666645 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 ( ) ) ;
668647 }
669648 }
670649 SelectLayer ( selected, ctrl, shift) => {
@@ -730,7 +709,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
730709 }
731710 SelectAllLayers => {
732711 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 ( ) ) ;
734713 }
735714 DeselectAllLayers => {
736715 responses. push_front ( SetSelectedLayers ( vec ! [ ] ) . into ( ) ) ;
0 commit comments