Populate layer entry cache (#437)

* Populate layer entry cache

* Serialize the DocumentMessageHandler

* Fix restoring of collapsed/expanded state, add iter impl for Layer, and clean up layer_data() functions

* Fixed bug with CreateEmptyLayer revealed by test

Co-authored-by: Keavon Chambers <keavon@keavon.com>
Co-authored-by: otdavies <oliver@psyfer.io>
This commit is contained in:
0HyperCube
2021-12-29 04:32:44 +00:00
committed by Keavon Chambers
co-authored by Keavon Chambers otdavies
parent 3eb915eaee
commit fd1ddfc41e
10 changed files with 156 additions and 83 deletions
-10
View File
@@ -34,10 +34,6 @@ impl Default for Document {
}
impl Document {
pub fn with_content(serialized_content: &str) -> Result<Self, DocumentError> {
serde_json::from_str(serialized_content).map_err(|e| DocumentError::InvalidFile(e.to_string()))
}
/// Wrapper around render, that returns the whole document as a Response.
pub fn render_root(&mut self, mode: ViewMode) -> String {
self.root.render(&mut vec![], mode);
@@ -48,12 +44,6 @@ impl Document {
self.state_identifier.finish()
}
pub fn serialize_document(&self) -> String {
let val = serde_json::to_string(self);
// We fully expect the serialization to succeed
val.unwrap()
}
/// Checks whether each layer under `path` intersects with the provided `quad` and adds all intersection layers as paths to `intersections`.
pub fn intersects_quad(&self, quad: Quad, path: &mut Vec<LayerId>, intersections: &mut Vec<Vec<LayerId>>) {
self.layer(path).unwrap().intersects_quad(quad, path, intersections);
+41
View File
@@ -105,6 +105,10 @@ impl Layer {
}
}
pub fn iter(&self) -> LayerIter<'_> {
LayerIter { stack: vec![self] }
}
pub fn render(&mut self, transforms: &mut Vec<DAffine2>, view_mode: ViewMode) -> &str {
if !self.visible {
return "";
@@ -179,3 +183,40 @@ impl Clone for Layer {
}
}
}
impl<'a> IntoIterator for &'a Layer {
type Item = &'a Layer;
type IntoIter = LayerIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[derive(Debug)]
pub struct LayerIter<'a> {
pub stack: Vec<&'a Layer>,
}
impl Default for LayerIter<'_> {
fn default() -> Self {
Self { stack: vec![] }
}
}
impl<'a> Iterator for LayerIter<'a> {
type Item = &'a Layer;
fn next(&mut self) -> Option<Self::Item> {
match self.stack.pop() {
Some(layer) => {
if let LayerDataType::Folder(folder) = &layer.data {
let layers = folder.layers();
self.stack.extend(layers);
};
Some(layer)
}
None => None,
}
}
}