From 02519a139445991556333a002fdd880194a5a0f9 Mon Sep 17 00:00:00 2001 From: 0hypercube <0hypercube@gmail.com> Date: Sun, 11 Jul 2021 08:48:26 +0100 Subject: [PATCH 1/2] Allow transforming layers --- core/document/src/document.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/core/document/src/document.rs b/core/document/src/document.rs index 260a0fc86f..b7b27c79d7 100644 --- a/core/document/src/document.rs +++ b/core/document/src/document.rs @@ -140,6 +140,18 @@ impl Document { Ok(root) } + /// Returns a mutable reference to the layer or folder at the path. Does not return an error for root + pub fn document_layer_mut(&mut self, path: &[LayerId]) -> Result<&mut Layer, DocumentError> { + let mut root = &mut self.root; + for id in path { + if root.as_folder().is_err() { + return Ok(root); + } + root = root.as_folder_mut()?.layer_mut(*id).ok_or(DocumentError::LayerNotFound)?; + } + Ok(root) + } + /// Returns a reference to the layer struct at the specified `path`. pub fn layer(&self, path: &[LayerId]) -> Result<&Layer, DocumentError> { let (path, id) = split_path(path)?; @@ -291,10 +303,11 @@ impl Document { None } Operation::TransformLayer { path, transform } => { - let transform = self.root.transform * DAffine2::from_cols_array(&transform); - let layer = self.document_folder_mut(path).unwrap(); + let layer = self.document_layer_mut(path).unwrap(); + let transform = DAffine2::from_cols_array(&transform) * layer.transform; layer.transform = transform; layer.cache_dirty = true; + self.root.cache_dirty = true; Some(vec![DocumentResponse::DocumentChanged]) } Operation::DiscardWorkingFolder => { From e0fe5f124bb96e00213c87a788610ac62c52493c Mon Sep 17 00:00:00 2001 From: 0hypercube <0hypercube@gmail.com> Date: Sun, 11 Jul 2021 09:01:51 +0100 Subject: [PATCH 2/2] Add document_layer function to return non mut --- core/document/src/document.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/document/src/document.rs b/core/document/src/document.rs index b7b27c79d7..d268203d38 100644 --- a/core/document/src/document.rs +++ b/core/document/src/document.rs @@ -140,16 +140,22 @@ impl Document { Ok(root) } + /// Returns a reference to the layer or folder at the path. Does not return an error for root + pub fn document_layer(&mut self, path: &[LayerId]) -> Result<&Layer, DocumentError> { + if path.is_empty() { + return Ok(&self.root); + } + let (path, id) = split_path(path)?; + self.document_folder(path)?.as_folder()?.layer(id).ok_or(DocumentError::LayerNotFound) + } + /// Returns a mutable reference to the layer or folder at the path. Does not return an error for root pub fn document_layer_mut(&mut self, path: &[LayerId]) -> Result<&mut Layer, DocumentError> { - let mut root = &mut self.root; - for id in path { - if root.as_folder().is_err() { - return Ok(root); - } - root = root.as_folder_mut()?.layer_mut(*id).ok_or(DocumentError::LayerNotFound)?; + if path.is_empty() { + return Ok(&mut self.root); } - Ok(root) + let (path, id) = split_path(path)?; + self.document_folder_mut(path)?.as_folder_mut()?.layer_mut(id).ok_or(DocumentError::LayerNotFound) } /// Returns a reference to the layer struct at the specified `path`.