From 720a04b501d70248ec28b875257d1aa6009e4396 Mon Sep 17 00:00:00 2001 From: 0HyperCube <78500760+0HyperCube@users.noreply.github.com> Date: Thu, 23 Dec 2021 12:02:22 +0000 Subject: [PATCH] Fix new document position (#421) * Fix new document pos * All new documents are centered before deserialization * Move constructors to the top of to block * Fix merge (moving coe around is a bad idea) Co-authored-by: Keavon Chambers --- editor/src/document/document_file.rs | 58 ++++++++++--------- .../src/document/document_message_handler.rs | 4 +- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/editor/src/document/document_file.rs b/editor/src/document/document_file.rs index 860eb98a..55687254 100644 --- a/editor/src/document/document_file.rs +++ b/editor/src/document/document_file.rs @@ -150,6 +150,36 @@ impl From for Message { } impl DocumentMessageHandler { + pub fn with_name(name: String, ipp: &InputPreprocessor) -> Self { + let mut document = Self { + graphene_document: GrapheneDocument::default(), + document_undo_history: Vec::new(), + document_redo_history: Vec::new(), + saved_document_identifier: 0, + name, + layer_data: vec![(vec![], LayerData::new(true))].into_iter().collect(), + layer_range_selection_reference: Vec::new(), + movement_handler: MovementMessageHandler::default(), + transform_layer_handler: TransformLayerMessageHandler::default(), + snapping_enabled: true, + }; + document.graphene_document.root.transform = document.layerdata(&[]).calculate_offset_transform(ipp.viewport_bounds.size() / 2.); + document + } + + pub fn with_name_and_content(name: String, serialized_content: String, ipp: &InputPreprocessor) -> Result { + let mut document = Self::with_name(name, ipp); + let internal_document = GrapheneDocument::with_content(&serialized_content); + match internal_document { + Ok(handle) => { + document.graphene_document = handle; + Ok(document) + } + Err(DocumentError::InvalidFile(msg)) => Err(EditorError::Document(msg)), + _ => Err(EditorError::Document(String::from("Failed to open file"))), + } + } + fn select_layer(&mut self, path: &[LayerId]) -> Option { if self.graphene_document.layer(path).ok()?.overlay { return None; @@ -307,34 +337,6 @@ impl DocumentMessageHandler { self.layers_sorted(Some(false)) } - pub fn with_name(name: String) -> Self { - Self { - graphene_document: GrapheneDocument::default(), - document_undo_history: Vec::new(), - document_redo_history: Vec::new(), - saved_document_identifier: 0, - name, - layer_data: vec![(vec![], LayerData::new(true))].into_iter().collect(), - layer_range_selection_reference: Vec::new(), - movement_handler: MovementMessageHandler::default(), - transform_layer_handler: TransformLayerMessageHandler::default(), - snapping_enabled: true, - } - } - - pub fn with_name_and_content(name: String, serialized_content: String) -> Result { - let mut document = Self::with_name(name); - let internal_document = GrapheneDocument::with_content(&serialized_content); - match internal_document { - Ok(handle) => { - document.graphene_document = handle; - Ok(document) - } - Err(DocumentError::InvalidFile(msg)) => Err(EditorError::Document(msg)), - _ => Err(EditorError::Document(String::from("Failed to open file"))), - } - } - pub fn layer_data(&mut self, path: &[LayerId]) -> &mut LayerData { layer_data(&mut self.layer_data, path) } diff --git a/editor/src/document/document_message_handler.rs b/editor/src/document/document_message_handler.rs index 2d391886..b9156827 100644 --- a/editor/src/document/document_message_handler.rs +++ b/editor/src/document/document_message_handler.rs @@ -199,14 +199,14 @@ impl MessageHandler for DocumentsMessageHa } NewDocument => { let name = self.generate_new_document_name(); - let new_document = DocumentMessageHandler::with_name(name); + let new_document = DocumentMessageHandler::with_name(name, ipp); self.load_document(new_document, responses); } OpenDocument => { responses.push_back(FrontendMessage::OpenDocumentBrowse.into()); } OpenDocumentFile(name, serialized_contents) => { - let document = DocumentMessageHandler::with_name_and_content(name, serialized_contents); + let document = DocumentMessageHandler::with_name_and_content(name, serialized_contents, ipp); match document { Ok(document) => { self.load_document(document, responses);