Skip to content

Commit d886dae

Browse files
committed
Refactor
1 parent 4d93a69 commit d886dae

File tree

5 files changed

+44
-74
lines changed

5 files changed

+44
-74
lines changed

editor/src/document/document_file.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ impl From<DocumentOperation> for Message {
104104
}
105105

106106
impl DocumentMessageHandler {
107-
pub fn active_document(&self) -> &DocumentMessageHandler {
108-
self
109-
}
110-
pub fn active_document_mut(&mut self) -> &mut DocumentMessageHandler {
111-
self
112-
}
113107
fn filter_document_responses(&self, document_responses: &mut Vec<DocumentResponse>) -> bool {
114108
let len = document_responses.len();
115109
document_responses.retain(|response| !matches!(response, DocumentResponse::DocumentChanged));
@@ -285,9 +279,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
285279
)
286280
}
287281
SetBlendModeForSelectedLayers(blend_mode) => {
288-
let active_document = self;
289-
290-
for path in active_document.layer_data.iter().filter_map(|(path, data)| data.selected.then(|| path.clone())) {
282+
for path in self.layer_data.iter().filter_map(|(path, data)| data.selected.then(|| path.clone())) {
291283
responses.push_back(DocumentOperation::SetLayerBlendMode { path, blend_mode }.into());
292284
}
293285
}
@@ -372,28 +364,30 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
372364
Err(e) => log::error!("DocumentError: {:?}", e),
373365
Ok(_) => (),
374366
},
375-
RenderDocument => responses.extend([
376-
FrontendMessage::UpdateCanvas {
377-
document: self.document.render_root(),
378-
}
379-
.into(),
380-
FrontendMessage::UpdateScrollbars {
381-
bounds: {
382-
let bounds = self.active_document_mut().document.visible_layers_bounding_box();
383-
let bounds = bounds.unwrap_or([glam::DVec2::ZERO, glam::DVec2::ZERO]);
384-
let viewport = ipp.viewport_size.as_f64();
385-
[
386-
bounds[0].x.min(0.) - viewport.x * 0.5,
387-
bounds[0].y.min(0.) - viewport.y * 0.5,
388-
bounds[1].x.max(viewport.x) + viewport.x * 0.5,
389-
bounds[1].y.max(viewport.y) + viewport.y * 0.5,
390-
]
391-
},
392-
position: self.document.root.transform.translation.into(),
393-
viewport_size: ipp.viewport_size.as_f64().into(),
394-
}
395-
.into(),
396-
]),
367+
RenderDocument => {
368+
responses.push_back(
369+
FrontendMessage::UpdateCanvas {
370+
document: self.document.render_root(),
371+
}
372+
.into(),
373+
);
374+
let viewport = ipp.viewport_size.as_f64();
375+
let [bounds1, bounds2] = self.document.visible_layers_bounding_box().unwrap_or_default();
376+
let bounds1 = bounds1.min(glam::DVec2::ZERO) - viewport * 0.5;
377+
let bounds2 = bounds2.max(viewport) + viewport * 0.5;
378+
let bounds_length = (bounds2 - bounds1).abs();
379+
let scrollbar_multiplier = bounds_length - viewport;
380+
let scrollbar_position = bounds1.abs() / scrollbar_multiplier;
381+
let scrollbar_size = viewport / bounds_length;
382+
responses.push_back(
383+
FrontendMessage::UpdateScrollbars {
384+
position: scrollbar_position.into(),
385+
size: scrollbar_size.into(),
386+
multiplier: scrollbar_multiplier.into(),
387+
}
388+
.into(),
389+
);
390+
}
397391

398392
NudgeSelectedLayers(x, y) => {
399393
for path in self.selected_layers().cloned() {

editor/src/document/document_message_handler.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,12 @@ impl MessageHandler<DocumentsMessage, &InputPreprocessor> for DocumentsMessageHa
119119
}
120120
.into(),
121121
);
122-
responses.extend([
122+
responses.push_back(
123123
FrontendMessage::UpdateCanvas {
124124
document: self.active_document_mut().document.render_root(),
125125
}
126126
.into(),
127-
FrontendMessage::UpdateScrollbars {
128-
bounds: {
129-
let bounds = self.active_document_mut().document.visible_layers_bounding_box();
130-
let bounds = bounds.unwrap_or([glam::DVec2::ZERO, glam::DVec2::ZERO]);
131-
[bounds[0].x, bounds[0].y, bounds[1].x, bounds[1].y]
132-
},
133-
position: self.active_document_mut().document.root.transform.translation.into(),
134-
viewport_size: ipp.viewport_size.as_f64().into(),
135-
}
136-
.into(),
137-
]);
127+
);
138128
}
139129
// Active doc will move one space to the left
140130
else if id < self.active_document_index {

editor/src/frontend/frontend_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum FrontendMessage {
1717
DisplayConfirmationToCloseDocument { document_index: usize },
1818
DisplayConfirmationToCloseAllDocuments,
1919
UpdateCanvas { document: String },
20-
UpdateScrollbars { bounds: [f64; 4], position: (f64, f64), viewport_size: (f64, f64) },
20+
UpdateScrollbars { position: (f64, f64), size: (f64, f64), multiplier: (f64, f64) },
2121
UpdateLayer { path: Vec<LayerId>, data: LayerPanelEntry },
2222
ExportDocument { document: String },
2323
EnableTextInput,

frontend/src/components/panels/Document.vue

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,19 @@
124124
<LayoutCol :class="'bar-area'">
125125
<PersistentScrollbar
126126
:direction="ScrollbarDirection.Vertical"
127-
:handlePosition="scrollbarYPos"
127+
:handlePosition="scrollbarPos.y"
128128
@update:handlePosition="translateCanvasY"
129-
v-model:handleLength="scrollbarYSize"
129+
v-model:handleLength="scrollbarSize.y"
130130
:class="'right-scrollbar'"
131131
/>
132132
</LayoutCol>
133133
</LayoutRow>
134134
<LayoutRow :class="'bar-area'">
135135
<PersistentScrollbar
136136
:direction="ScrollbarDirection.Horizontal"
137-
:handlePosition="scrollbarXPos"
137+
:handlePosition="scrollbarPos.x"
138138
@update:handlePosition="translateCanvasX"
139-
v-model:handleLength="scrollbarXSize"
139+
v-model:handleLength="scrollbarSize.x"
140140
:class="'bottom-scrollbar'"
141141
/>
142142
</LayoutRow>
@@ -305,11 +305,11 @@ export default defineComponent({
305305
},
306306
async translateCanvasX(newValue: number) {
307307
const { translate_canvas } = await wasm;
308-
translate_canvas(-(newValue - this.scrollbarXPos) * (Math.abs(this.bounds.topX) + Math.abs(this.bounds.bottomX)), 0);
308+
translate_canvas(-(newValue - this.scrollbarPos.x) * this.scrollbarMultiplier.x, 0);
309309
},
310310
async translateCanvasY(newValue: number) {
311311
const { translate_canvas } = await wasm;
312-
translate_canvas(0, -(newValue - this.scrollbarYPos) * (Math.abs(this.bounds.topY) + Math.abs(this.bounds.bottomY)));
312+
translate_canvas(0, -(newValue - this.scrollbarPos.y) * this.scrollbarMultiplier.y);
313313
},
314314
async selectTool(toolName: string) {
315315
const { select_tool } = await wasm;
@@ -343,21 +343,9 @@ export default defineComponent({
343343
registerResponseHandler(ResponseType.UpdateScrollbars, (responseData: Response) => {
344344
const updateData = responseData as UpdateScrollbars;
345345
if (updateData) {
346-
this.bounds = updateData.bounds;
347-
const span = { x: Math.abs(updateData.bounds.bottomX - updateData.bounds.topX), y: Math.abs(updateData.bounds.bottomY - updateData.bounds.topY) };
348-
const min = { x: Math.min(updateData.bounds.bottomX, updateData.bounds.topX), y: Math.min(updateData.bounds.bottomY, updateData.bounds.topY) };
349-
if (span.x < updateData.viewport_size.x) {
350-
this.scrollbarXSize = 1;
351-
} else {
352-
this.scrollbarXPos = -min.x / (span.x - updateData.viewport_size.x);
353-
this.scrollbarXSize = updateData.viewport_size.x / span.x;
354-
}
355-
if (span.y < updateData.viewport_size.y) {
356-
this.scrollbarYSize = 1;
357-
} else {
358-
this.scrollbarYPos = -min.y / (span.y - updateData.viewport_size.y);
359-
this.scrollbarYSize = updateData.viewport_size.y / span.y;
360-
}
346+
this.scrollbarPos = updateData.position;
347+
this.scrollbarSize = updateData.size;
348+
this.scrollbarMultiplier = updateData.multiplier;
361349
}
362350
});
363351
registerResponseHandler(ResponseType.ExportDocument, (responseData: Response) => {
@@ -404,11 +392,9 @@ export default defineComponent({
404392
overlaysEnabled: true,
405393
documentRotation: 0,
406394
documentZoom: 100,
407-
scrollbarXPos: 0.5,
408-
scrollbarXSize: 0.5,
409-
scrollbarYPos: 0.5,
410-
scrollbarYSize: 0.5,
411-
bounds: { topX: 0, topY: 0, bottomX: 1, bottomY: 1 },
395+
scrollbarPos: { x: 0.5, y: 0.5 },
396+
scrollbarSize: { x: 0.5, y: 0.5 },
397+
scrollbarMultiplier: { x: 0, y: 0 },
412398
IncrementDirection,
413399
MenuDirection,
414400
SeparatorDirection,

frontend/src/utilities/response-handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ function newUpdateCanvas(input: any): UpdateCanvas {
168168
}
169169

170170
export interface UpdateScrollbars {
171-
bounds: { topX: number; topY: number; bottomX: number; bottomY: number };
172171
position: { x: number; y: number };
173-
viewport_size: { x: number; y: number };
172+
size: { x: number; y: number };
173+
multiplier: { x: number; y: number };
174174
}
175175
function newUpdateScrollbars(input: any): UpdateScrollbars {
176176
return {
177-
bounds: { topX: input.bounds[0], topY: input.bounds[1], bottomX: input.bounds[2], bottomY: input.bounds[3] },
178177
position: { x: input.position[0], y: input.position[1] },
179-
viewport_size: { x: input.viewport_size[0], y: input.viewport_size[1] },
178+
size: { x: input.size[0], y: input.size[1] },
179+
multiplier: { x: input.multiplier[0], y: input.multiplier[1] },
180180
};
181181
}
182182

0 commit comments

Comments
 (0)