Support rearranging layers with hotkeys (#271)

* Support moving single layers

* Fix "Move layer to top/bottom" keybinds

* Rename things named "move" to "reorder"

Fix formatting

* Combine sorted layer helper functions

* Use integer consts for moving layers to front/back

* Fix merge mistake

* Fix some clippy lints

* Fix panic

* Remove "get" prefix from functions

* Bring layer menu items out to sub-menu

* Support moving multiple layers at a time

* Add comment explaining odd keybinding

* Add reordering tests

* Add negative test

* Add new error type

* Add layer position helper, clean up tests

* Make position helper return Result

* Clean up slice iteration

* Simplify source_layer_ids computation

Co-authored-by: Dennis Kobert <dennis@kobert.dev>
This commit is contained in:
AJ Weeks
2021-07-21 10:36:51 +01:00
committed by GitHub
parent 255cdead28
commit a448b36d9e
10 changed files with 319 additions and 13 deletions

View File

@@ -226,6 +226,19 @@ impl Document {
Ok(())
}
pub fn reorder_layers(&mut self, source_paths: &[Vec<LayerId>], target_path: &[LayerId]) -> Result<(), DocumentError> {
// TODO: Detect when moving between folders and handle properly
let source_layer_ids = source_paths
.iter()
.map(|x| x.last().cloned().ok_or(DocumentError::LayerNotFound))
.collect::<Result<Vec<LayerId>, DocumentError>>()?;
self.root.as_folder_mut()?.reorder_layers(source_layer_ids, *target_path.last().ok_or(DocumentError::LayerNotFound)?)?;
Ok(())
}
pub fn layer_axis_aligned_bounding_box(&self, path: &[LayerId]) -> Result<Option<[DVec2; 2]>, DocumentError> {
// TODO: Replace with functions of the transform api
if path.is_empty() {
@@ -393,6 +406,11 @@ impl Document {
self.mark_as_dirty(path)?;
Some(vec![DocumentResponse::DocumentChanged])
}
Operation::ReorderLayers { source_paths, target_path } => {
self.reorder_layers(source_paths, target_path)?;
Some(vec![DocumentResponse::DocumentChanged])
}
};
if !matches!(
operation,