Skip to content

Commit c5d2153

Browse files
committed
Refactor AlignSelectedLayers
1 parent af2ae20 commit c5d2153

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

core/editor/src/document/document_message_handler.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,12 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
662662
}
663663
AlignSelectedLayers(axis, aggregate) => {
664664
// 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() {
667666
return;
668667
}
669668

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()?;
672671
let point = {
673672
let bounding_box = layer.bounding_box(layer.transform, layer.style)?;
674673
match aggregate {
@@ -682,44 +681,33 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
682681
AlignAxis::X => (point.x, layer.transform.translation.x),
683682
AlignAxis::Y => (point.y, layer.transform.translation.y),
684683
};
685-
Some((path.clone(), bounding_box_coord, translation_coord))
684+
Some((path, bounding_box_coord, translation_coord))
686685
});
686+
let selected_layers: Vec<_> = selected_layers.collect();
687687

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)),
692692
AlignAggregate::Center => {
693693
// 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())
700696
.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),
703699
})
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.)
715702
}
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+
}
723711
}
724712
}
725713
}

0 commit comments

Comments
 (0)