Skip to content

Commit af2ae20

Browse files
committed
Add a selected_layers() function and refactor selected_layers_sorted()
1 parent edfbc27 commit af2ae20

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

core/editor/src/document/document_message_handler.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ impl DocumentMessageHandler {
130130
// TODO: Add deduplication
131131
(!path.is_empty()).then(|| self.handle_folder_changed(path[..path.len() - 1].to_vec())).flatten()
132132
}
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+
}
133136
fn layerdata(&self, path: &[LayerId]) -> &LayerData {
134137
self.active_document().layer_data.get(path).expect("Layerdata does not exist")
135138
}
@@ -149,8 +152,7 @@ impl DocumentMessageHandler {
149152
);
150153
}
151154

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.
154156
fn layers_sorted(&self, selected: Option<bool>) -> Vec<Vec<LayerId>> {
155157
// Compute the indices for each layer to be able to sort them
156158
let mut layers_with_indices: Vec<(Vec<LayerId>, Vec<usize>)> = self
@@ -360,17 +362,14 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
360362
.into(),
361363
),
362364
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() {
366366
responses.push_back(DocumentOperation::SetLayerBlendMode { path, blend_mode }.into());
367367
}
368368
}
369369
SetOpacityForSelectedLayers(opacity) => {
370370
let opacity = opacity.clamp(0., 1.);
371-
let active_document = self.active_document();
372371

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() {
374373
responses.push_back(DocumentOperation::SetLayerOpacity { path, opacity }.into());
375374
}
376375
}
@@ -382,8 +381,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
382381
responses.extend(self.handle_folder_changed(path));
383382
}
384383
DeleteSelectedLayers => {
385-
let paths = self.selected_layers_sorted();
386-
for path in paths {
384+
for path in self.selected_layers().cloned() {
387385
responses.push_back(DocumentOperation::DeleteLayer { path }.into())
388386
}
389387
}
@@ -602,14 +600,12 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
602600
responses.push_back(FrontendMessage::SetCanvasRotation { new_radians: new }.into());
603601
}
604602
NudgeSelectedLayers(x, y) => {
605-
let paths = self.selected_layers_sorted();
606-
607603
let delta = {
608604
let root_layer_rotation = self.layerdata_mut(&[]).rotation;
609605
let rotate_to_viewport_space = DAffine2::from_angle(root_layer_rotation).inverse();
610606
rotate_to_viewport_space.transform_point2((x, y).into())
611607
};
612-
for path in paths {
608+
for path in self.selected_layers().cloned() {
613609
let operation = DocumentOperation::TransformLayer {
614610
path,
615611
transform: DAffine2::from_translation(delta).to_cols_array(),
@@ -666,13 +662,13 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
666662
}
667663
AlignSelectedLayers(axis, aggregate) => {
668664
// 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() {
671667
return;
672668
}
673669

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();
676672
let point = {
677673
let bounding_box = layer.bounding_box(layer.transform, layer.style)?;
678674
match aggregate {
@@ -689,13 +685,13 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
689685
Some((path.clone(), bounding_box_coord, translation_coord))
690686
});
691687

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);
693689
let aggregated_coord = match aggregate {
694690
AlignAggregate::Min => bounding_box_coords.reduce(|a, b| a.min(b)).unwrap(),
695691
AlignAggregate::Max => bounding_box_coords.reduce(|a, b| a.max(b)).unwrap(),
696692
AlignAggregate::Center => {
697693
// 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| {
699695
let layer = self.active_document().document.layer(path).unwrap();
700696
layer.bounding_box(layer.transform, layer.style)
701697
});
@@ -717,7 +713,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
717713
.unwrap();
718714
(min + max) / 2.
719715
}
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,
721717
};
722718
for (path, bounding_box_coord, translation_coord) in selected_layers {
723719
let new_coord = aggregated_coord - (bounding_box_coord - translation_coord);

0 commit comments

Comments
 (0)