Skip to content

Commit 53ae4c7

Browse files
committed
Refactor AlignSelectedLayers
1 parent af2ae20 commit 53ae4c7

File tree

1 file changed

+39
-49
lines changed

1 file changed

+39
-49
lines changed

core/editor/src/document/document_message_handler.rs

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -662,64 +662,54 @@ 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();
672-
let point = {
673-
let bounding_box = layer.bounding_box(layer.transform, layer.style)?;
674-
match aggregate {
675-
AlignAggregate::Min => bounding_box[0],
676-
AlignAggregate::Max => bounding_box[1],
677-
AlignAggregate::Center => bounding_box[0].lerp(bounding_box[1], 0.5),
678-
AlignAggregate::Average => bounding_box[0].lerp(bounding_box[1], 0.5),
679-
}
680-
};
681-
let (bounding_box_coord, translation_coord) = match axis {
682-
AlignAxis::X => (point.x, layer.transform.translation.x),
683-
AlignAxis::Y => (point.y, layer.transform.translation.y),
684-
};
685-
Some((path.clone(), bounding_box_coord, translation_coord))
686-
});
669+
let selected_layers = self.selected_layers().cloned();
670+
let selected_layers: Vec<_> = selected_layers
671+
.filter_map(|path| {
672+
let layer = self.active_document().document.layer(&path).ok()?;
673+
let point = {
674+
let bounding_box = layer.bounding_box(layer.transform, layer.style)?;
675+
match aggregate {
676+
AlignAggregate::Min => bounding_box[0],
677+
AlignAggregate::Max => bounding_box[1],
678+
AlignAggregate::Center => bounding_box[0].lerp(bounding_box[1], 0.5),
679+
AlignAggregate::Average => bounding_box[0].lerp(bounding_box[1], 0.5),
680+
}
681+
};
682+
let (bounding_box_coord, translation_coord) = match axis {
683+
AlignAxis::X => (point.x, layer.transform.translation.x),
684+
AlignAxis::Y => (point.y, layer.transform.translation.y),
685+
};
686+
Some((path, bounding_box_coord, translation_coord))
687+
})
688+
.collect();
687689

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(),
690+
let bounding_box_coords = selected_layers.iter().map(|(_, bounding_box_coord, _)| bounding_box_coord).cloned();
691+
if let Some(aggregated_coord) = match aggregate {
692+
AlignAggregate::Min => bounding_box_coords.reduce(|a, b| a.min(b)),
693+
AlignAggregate::Max => bounding_box_coords.reduce(|a, b| a.max(b)),
692694
AlignAggregate::Center => {
693695
// 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()
696+
self.selected_layers()
697+
.filter_map(|path| self.active_document().document.layer(path).ok().map(|layer| layer.bounding_box(layer.transform, layer.style)).flatten())
700698
.map(|bbox| match axis {
701-
AlignAxis::X => bbox[0].x,
702-
AlignAxis::Y => bbox[0].y,
699+
AlignAxis::X => (bbox[0].x, bbox[1].x),
700+
AlignAxis::Y => (bbox[0].y, bbox[1].y),
703701
})
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.
702+
.reduce(|(a, b), (c, d)| (a.min(c), b.max(d)))
703+
.map(|(min, max)| (min + max) / 2.)
715704
}
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()),
705+
AlignAggregate::Average => Some(bounding_box_coords.sum::<f64>() / selected_layers.len() as f64),
706+
} {
707+
for (path, bounding_box_coord, translation_coord) in selected_layers {
708+
let new_coord = aggregated_coord - (bounding_box_coord - translation_coord);
709+
match axis {
710+
AlignAxis::X => responses.push_back(DocumentMessage::SetLayerTranslation(path, Some(new_coord), None).into()),
711+
AlignAxis::Y => responses.push_back(DocumentMessage::SetLayerTranslation(path, None, Some(new_coord)).into()),
712+
}
723713
}
724714
}
725715
}

0 commit comments

Comments
 (0)