@@ -8,10 +8,9 @@ use super::movement_handler::{MovementMessage, MovementMessageHandler};
88use super :: overlay_message_handler:: OverlayMessageHandler ;
99use super :: transform_layer_handler:: { TransformLayerMessage , TransformLayerMessageHandler } ;
1010use super :: vectorize_layer_metadata;
11-
12- use crate :: consts:: DEFAULT_DOCUMENT_NAME ;
13- use crate :: consts:: GRAPHITE_DOCUMENT_VERSION ;
14- use crate :: consts:: { ASYMPTOTIC_EFFECT , FILE_EXPORT_SUFFIX , FILE_SAVE_SUFFIX , SCALE_EFFECT , SCROLLBAR_SPACING } ;
11+ use crate :: consts:: {
12+ ASYMPTOTIC_EFFECT , DEFAULT_DOCUMENT_NAME , FILE_EXPORT_SUFFIX , FILE_SAVE_SUFFIX , GRAPHITE_DOCUMENT_VERSION , SCALE_EFFECT , SCROLLBAR_SPACING , VIEWPORT_ZOOM_TO_FIT_PADDING_SCALE_FACTOR ,
13+ } ;
1514use crate :: document:: Clipboard ;
1615use crate :: input:: InputPreprocessor ;
1716use crate :: message_prelude:: * ;
@@ -82,7 +81,6 @@ pub struct DocumentMessageHandler {
8281 #[ serde( with = "vectorize_layer_metadata" ) ]
8382 pub layer_metadata : HashMap < Vec < LayerId > , LayerMetadata > ,
8483 layer_range_selection_reference : Vec < LayerId > ,
85- #[ serde( skip) ]
8684 movement_handler : MovementMessageHandler ,
8785 #[ serde( skip) ]
8886 overlay_message_handler : OverlayMessageHandler ,
@@ -182,6 +180,7 @@ pub enum DocumentMessage {
182180 neighbor : Vec < LayerId > ,
183181 } ,
184182 SetSnapping ( bool ) ,
183+ ZoomCanvasToFitAll ,
185184}
186185
187186impl From < DocumentOperation > for DocumentMessage {
@@ -225,13 +224,10 @@ impl DocumentMessageHandler {
225224 document
226225 }
227226
228- pub fn with_name_and_content ( name : String , serialized_content : String , ipp : & InputPreprocessor ) -> Result < Self , EditorError > {
227+ pub fn with_name_and_content ( name : String , serialized_content : String ) -> Result < Self , EditorError > {
229228 match Self :: deserialize_document ( & serialized_content) {
230229 Ok ( mut document) => {
231230 document. name = name;
232- let starting_root_transform = document. movement_handler . calculate_offset_transform ( ipp. viewport_bounds . size ( ) / 2. ) ;
233- document. graphene_document . root . transform = starting_root_transform;
234- document. artboard_message_handler . artboards_graphene_document . root . transform = starting_root_transform;
235231 Ok ( document)
236232 }
237233 Err ( DocumentError :: InvalidFile ( msg) ) => Err ( EditorError :: Document ( msg) ) ,
@@ -540,6 +536,14 @@ impl DocumentMessageHandler {
540536
541537 Some ( layer_panel_entry ( layer_metadata, transform, layer, path. to_vec ( ) ) )
542538 }
539+
540+ pub fn document_bounds ( & self ) -> Option < [ DVec2 ; 2 ] > {
541+ if self . artboard_message_handler . is_infinite_canvas ( ) {
542+ self . graphene_document . viewport_bounding_box ( & [ ] ) . ok ( ) . flatten ( )
543+ } else {
544+ self . artboard_message_handler . artboards_graphene_document . viewport_bounding_box ( & [ ] ) . ok ( ) . flatten ( )
545+ }
546+ }
543547}
544548
545549impl MessageHandler < DocumentMessage , & InputPreprocessor > for DocumentMessageHandler {
@@ -577,7 +581,8 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
577581 ) ;
578582 }
579583 ExportDocument => {
580- let bbox = self . graphene_document . visible_layers_bounding_box ( ) . unwrap_or ( [ DVec2 :: ZERO , ipp. viewport_bounds . size ( ) ] ) ;
584+ // TODO(MFISH33): Add Dialog to select artboards
585+ let bbox = self . document_bounds ( ) . unwrap_or ( [ DVec2 :: ZERO , ipp. viewport_bounds . size ( ) ] ) ;
581586 let size = bbox[ 1 ] - bbox[ 0 ] ;
582587 let name = match self . name . ends_with ( FILE_SAVE_SUFFIX ) {
583588 true => self . name . clone ( ) . replace ( FILE_SAVE_SUFFIX , FILE_EXPORT_SUFFIX ) ,
@@ -863,7 +868,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
863868 let scale = 0.5 + ASYMPTOTIC_EFFECT + document_transform_scale * SCALE_EFFECT ;
864869 let viewport_size = ipp. viewport_bounds . size ( ) ;
865870 let viewport_mid = ipp. viewport_bounds . center ( ) ;
866- let [ bounds1, bounds2] = self . graphene_document . visible_layers_bounding_box ( ) . unwrap_or ( [ viewport_mid; 2 ] ) ;
871+ let [ bounds1, bounds2] = self . document_bounds ( ) . unwrap_or ( [ viewport_mid; 2 ] ) ;
867872 let bounds1 = bounds1. min ( viewport_mid) - viewport_size * scale;
868873 let bounds2 = bounds2. max ( viewport_mid) + viewport_size * scale;
869874 let bounds_length = ( bounds2 - bounds1) * ( 1. + SCROLLBAR_SPACING ) ;
@@ -1063,6 +1068,18 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
10631068 SetSnapping ( new_status) => {
10641069 self . snapping_enabled = new_status;
10651070 }
1071+ ZoomCanvasToFitAll => {
1072+ if let Some ( bounds) = self . document_bounds ( ) {
1073+ responses. push_back (
1074+ MovementMessage :: FitViewportToBounds {
1075+ bounds,
1076+ padding_scale_factor : Some ( VIEWPORT_ZOOM_TO_FIT_PADDING_SCALE_FACTOR ) ,
1077+ prevent_zoom_past_100 : true ,
1078+ }
1079+ . into ( ) ,
1080+ )
1081+ }
1082+ }
10661083 }
10671084 }
10681085
@@ -1078,6 +1095,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
10781095 SetSnapping ,
10791096 DebugPrintDocument ,
10801097 MoveLayerInTree ,
1098+ ZoomCanvasToFitAll ,
10811099 ) ;
10821100
10831101 if self . layer_metadata . values ( ) . any ( |data| data. selected ) {
0 commit comments