@@ -130,6 +130,9 @@ impl DocumentMessageHandler {
130
130
// TODO: Add deduplication
131
131
( !path. is_empty ( ) ) . then ( || self . handle_folder_changed ( path[ ..path. len ( ) - 1 ] . to_vec ( ) ) ) . flatten ( )
132
132
}
133
+ fn selected_layers ( & self ) -> impl Iterator < Item = & Vec < LayerId > > {
134
+ self . active_document ( ) . layer_data . iter ( ) . filter_map ( |( path, data) | data. selected . then ( || path) )
135
+ }
133
136
fn layerdata ( & self , path : & [ LayerId ] ) -> & LayerData {
134
137
self . active_document ( ) . layer_data . get ( path) . expect ( "Layerdata does not exist" )
135
138
}
@@ -149,8 +152,7 @@ impl DocumentMessageHandler {
149
152
) ;
150
153
}
151
154
152
- /// Returns the paths to all layers in order, optionally including only selected or non
153
- /// selected layers.
155
+ /// Returns the paths to all layers in order, optionally including only selected or non-selected layers.
154
156
fn layers_sorted ( & self , selected : Option < bool > ) -> Vec < Vec < LayerId > > {
155
157
// Compute the indices for each layer to be able to sort them
156
158
let mut layers_with_indices: Vec < ( Vec < LayerId > , Vec < usize > ) > = self
@@ -360,17 +362,14 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
360
362
. into ( ) ,
361
363
) ,
362
364
SetBlendModeForSelectedLayers ( blend_mode) => {
363
- let active_document = self . active_document ( ) ;
364
-
365
- for path in active_document. layer_data . iter ( ) . filter_map ( |( path, data) | data. selected . then ( || path. clone ( ) ) ) {
365
+ for path in self . selected_layers ( ) . cloned ( ) {
366
366
responses. push_back ( DocumentOperation :: SetLayerBlendMode { path, blend_mode } . into ( ) ) ;
367
367
}
368
368
}
369
369
SetOpacityForSelectedLayers ( opacity) => {
370
370
let opacity = opacity. clamp ( 0. , 1. ) ;
371
- let active_document = self . active_document ( ) ;
372
371
373
- for path in active_document . layer_data . iter ( ) . filter_map ( | ( path , data ) | data . selected . then ( || path . clone ( ) ) ) {
372
+ for path in self . selected_layers ( ) . cloned ( ) {
374
373
responses. push_back ( DocumentOperation :: SetLayerOpacity { path, opacity } . into ( ) ) ;
375
374
}
376
375
}
@@ -382,8 +381,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
382
381
responses. extend ( self . handle_folder_changed ( path) ) ;
383
382
}
384
383
DeleteSelectedLayers => {
385
- let paths = self . selected_layers_sorted ( ) ;
386
- for path in paths {
384
+ for path in self . selected_layers ( ) . cloned ( ) {
387
385
responses. push_back ( DocumentOperation :: DeleteLayer { path } . into ( ) )
388
386
}
389
387
}
@@ -602,14 +600,12 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
602
600
responses. push_back ( FrontendMessage :: SetCanvasRotation { new_radians : new } . into ( ) ) ;
603
601
}
604
602
NudgeSelectedLayers ( x, y) => {
605
- let paths = self . selected_layers_sorted ( ) ;
606
-
607
603
let delta = {
608
604
let root_layer_rotation = self . layerdata_mut ( & [ ] ) . rotation ;
609
605
let rotate_to_viewport_space = DAffine2 :: from_angle ( root_layer_rotation) . inverse ( ) ;
610
606
rotate_to_viewport_space. transform_point2 ( ( x, y) . into ( ) )
611
607
} ;
612
- for path in paths {
608
+ for path in self . selected_layers ( ) . cloned ( ) {
613
609
let operation = DocumentOperation :: TransformLayer {
614
610
path,
615
611
transform : DAffine2 :: from_translation ( delta) . to_cols_array ( ) ,
@@ -666,13 +662,13 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
666
662
}
667
663
AlignSelectedLayers ( axis, aggregate) => {
668
664
// TODO: Handle folder nested transforms with the transforms API
669
- let selected_paths = self . selected_layers_sorted ( ) ;
670
- if selected_paths . is_empty ( ) {
665
+ let selected_layers = self . selected_layers ( ) . cloned ( ) . peekable ( ) ;
666
+ if selected_layers . peek ( ) . is_none ( ) {
671
667
return ;
672
668
}
673
669
674
- let selected_layers = selected_paths . iter ( ) . filter_map ( |path| {
675
- let layer = self . active_document ( ) . document . layer ( path) . unwrap ( ) ;
670
+ let selected_layers = selected_layers . filter_map ( |path| {
671
+ let layer = self . active_document ( ) . document . layer ( & path) . unwrap ( ) ;
676
672
let point = {
677
673
let bounding_box = layer. bounding_box ( layer. transform , layer. style ) ?;
678
674
match aggregate {
@@ -689,13 +685,13 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
689
685
Some ( ( path. clone ( ) , bounding_box_coord, translation_coord) )
690
686
} ) ;
691
687
692
- let bounding_box_coords = selected_layers. clone ( ) . map ( |( _, bounding_box_coord, _) | bounding_box_coord) ;
688
+ let bounding_box_coords = selected_layers. map ( |( _, bounding_box_coord, _) | bounding_box_coord) ;
693
689
let aggregated_coord = match aggregate {
694
690
AlignAggregate :: Min => bounding_box_coords. reduce ( |a, b| a. min ( b) ) . unwrap ( ) ,
695
691
AlignAggregate :: Max => bounding_box_coords. reduce ( |a, b| a. max ( b) ) . unwrap ( ) ,
696
692
AlignAggregate :: Center => {
697
693
// TODO: Refactor with `reduce` and `merge_bounding_boxes` once the latter is added
698
- let bounding_boxes = selected_paths . iter ( ) . filter_map ( |path| {
694
+ let bounding_boxes = selected_layers . iter ( ) . filter_map ( |path| {
699
695
let layer = self . active_document ( ) . document . layer ( path) . unwrap ( ) ;
700
696
layer. bounding_box ( layer. transform , layer. style )
701
697
} ) ;
@@ -717,7 +713,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
717
713
. unwrap ( ) ;
718
714
( min + max) / 2.
719
715
}
720
- AlignAggregate :: Average => bounding_box_coords. sum :: < f64 > ( ) / selected_paths . len ( ) as f64 ,
716
+ AlignAggregate :: Average => bounding_box_coords. sum :: < f64 > ( ) / selected_layers . len ( ) as f64 ,
721
717
} ;
722
718
for ( path, bounding_box_coord, translation_coord) in selected_layers {
723
719
let new_coord = aggregated_coord - ( bounding_box_coord - translation_coord) ;
0 commit comments