@@ -662,13 +662,12 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
662
662
}
663
663
AlignSelectedLayers ( axis, aggregate) => {
664
664
// TODO: Handle folder nested transforms with the transforms API
665
- let selected_layers = self . selected_layers ( ) . cloned ( ) . peekable ( ) ;
666
- if selected_layers. peek ( ) . is_none ( ) {
665
+ if self . selected_layers ( ) . next ( ) . is_none ( ) {
667
666
return ;
668
667
}
669
668
670
- let selected_layers = selected_layers. filter_map ( |path| {
671
- let layer = self . active_document ( ) . document . layer ( & path) . unwrap ( ) ;
669
+ let selected_layers = self . selected_layers ( ) . cloned ( ) . filter_map ( |path| {
670
+ let layer = self . active_document ( ) . document . layer ( & path) . ok ( ) ? ;
672
671
let point = {
673
672
let bounding_box = layer. bounding_box ( layer. transform , layer. style ) ?;
674
673
match aggregate {
@@ -682,44 +681,33 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
682
681
AlignAxis :: X => ( point. x , layer. transform . translation . x ) ,
683
682
AlignAxis :: Y => ( point. y , layer. transform . translation . y ) ,
684
683
} ;
685
- Some ( ( path. clone ( ) , bounding_box_coord, translation_coord) )
684
+ Some ( ( path, bounding_box_coord, translation_coord) )
686
685
} ) ;
686
+ let selected_layers: Vec < _ > = selected_layers. collect ( ) ;
687
687
688
- let bounding_box_coords = selected_layers. map ( |( _, bounding_box_coord, _) | bounding_box_coord) ;
689
- let aggregated_coord = match aggregate {
690
- AlignAggregate :: Min => bounding_box_coords. reduce ( |a, b| a. min ( b) ) . unwrap ( ) ,
691
- AlignAggregate :: Max => bounding_box_coords. reduce ( |a, b| a. max ( b) ) . unwrap ( ) ,
688
+ let bounding_box_coords = selected_layers. iter ( ) . map ( |( _, bounding_box_coord, _) | bounding_box_coord) . cloned ( ) ;
689
+ if let Some ( aggregated_coord) = match aggregate {
690
+ AlignAggregate :: Min => bounding_box_coords. reduce ( |a, b| a. min ( b) ) ,
691
+ AlignAggregate :: Max => bounding_box_coords. reduce ( |a, b| a. max ( b) ) ,
692
692
AlignAggregate :: Center => {
693
693
// TODO: Refactor with `reduce` and `merge_bounding_boxes` once the latter is added
694
- let bounding_boxes = selected_layers. iter ( ) . filter_map ( |path| {
695
- let layer = self . active_document ( ) . document . layer ( path) . unwrap ( ) ;
696
- layer. bounding_box ( layer. transform , layer. style )
697
- } ) ;
698
- let min = bounding_boxes
699
- . clone ( )
694
+ self . selected_layers ( )
695
+ . filter_map ( |path| self . active_document ( ) . document . layer ( path) . ok ( ) . map ( |layer| layer. bounding_box ( layer. transform , layer. style ) ) . flatten ( ) )
700
696
. map ( |bbox| match axis {
701
- AlignAxis :: X => bbox[ 0 ] . x ,
702
- AlignAxis :: Y => bbox[ 0 ] . y ,
697
+ AlignAxis :: X => ( bbox[ 0 ] . x , bbox [ 1 ] . x ) ,
698
+ AlignAxis :: Y => ( bbox[ 0 ] . y , bbox [ 1 ] . y ) ,
703
699
} )
704
- . reduce ( |a, b| a. min ( b) )
705
- . unwrap ( ) ;
706
- let max = bounding_boxes
707
- . clone ( )
708
- . map ( |bbox| match axis {
709
- AlignAxis :: X => bbox[ 1 ] . x ,
710
- AlignAxis :: Y => bbox[ 1 ] . y ,
711
- } )
712
- . reduce ( |a, b| a. max ( b) )
713
- . unwrap ( ) ;
714
- ( min + max) / 2.
700
+ . reduce ( |( a, b) , ( c, d) | ( a. min ( c) , b. max ( d) ) )
701
+ . map ( |( min, max) | ( min + max) / 2. )
715
702
}
716
- AlignAggregate :: Average => bounding_box_coords. sum :: < f64 > ( ) / selected_layers. len ( ) as f64 ,
717
- } ;
718
- for ( path, bounding_box_coord, translation_coord) in selected_layers {
719
- let new_coord = aggregated_coord - ( bounding_box_coord - translation_coord) ;
720
- match axis {
721
- AlignAxis :: X => responses. push_back ( DocumentMessage :: SetLayerTranslation ( path, Some ( new_coord) , None ) . into ( ) ) ,
722
- AlignAxis :: Y => responses. push_back ( DocumentMessage :: SetLayerTranslation ( path, None , Some ( new_coord) ) . into ( ) ) ,
703
+ AlignAggregate :: Average => Some ( bounding_box_coords. sum :: < f64 > ( ) / selected_layers. len ( ) as f64 ) ,
704
+ } {
705
+ for ( path, bounding_box_coord, translation_coord) in selected_layers {
706
+ let new_coord = aggregated_coord - ( bounding_box_coord - translation_coord) ;
707
+ match axis {
708
+ AlignAxis :: X => responses. push_back ( DocumentMessage :: SetLayerTranslation ( path, Some ( new_coord) , None ) . into ( ) ) ,
709
+ AlignAxis :: Y => responses. push_back ( DocumentMessage :: SetLayerTranslation ( path, None , Some ( new_coord) ) . into ( ) ) ,
710
+ }
723
711
}
724
712
}
725
713
}
0 commit comments