Skip to content

Commit df89421

Browse files
clean up viewport info update
1 parent a53b0e8 commit df89421

File tree

6 files changed

+34
-71
lines changed

6 files changed

+34
-71
lines changed

editor/src/messages/input_mapper/utility_types/input_mouse.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,13 @@ use crate::messages::prelude::*;
33
use bitflags::bitflags;
44
use glam::DVec2;
55
use std::collections::VecDeque;
6+
use std::hash::{Hash, Hasher};
67

78
// Origin is top left
89
pub type DocumentPosition = DVec2;
910
pub type ViewportPosition = DVec2;
1011
pub type EditorPosition = DVec2;
1112

12-
#[derive(PartialEq, Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
13-
pub struct ViewportBounds {
14-
pub top_left: DVec2,
15-
pub bottom_right: DVec2,
16-
}
17-
18-
impl ViewportBounds {
19-
pub fn from_slice(slice: &[f64]) -> Self {
20-
Self {
21-
top_left: DVec2::from_slice(&slice[0..2]),
22-
bottom_right: DVec2::from_slice(&slice[2..4]),
23-
}
24-
}
25-
26-
pub fn size(&self) -> DVec2 {
27-
(self.bottom_right - self.top_left).ceil()
28-
}
29-
30-
pub fn center(&self) -> DVec2 {
31-
(self.bottom_right - self.top_left).ceil() / 2.
32-
}
33-
34-
pub fn in_bounds(&self, position: ViewportPosition) -> bool {
35-
position.x >= 0. && position.y >= 0. && position.x <= self.bottom_right.x && position.y <= self.bottom_right.y
36-
}
37-
}
38-
39-
use std::hash::{Hash, Hasher};
40-
4113
#[derive(Debug, Copy, Clone, Default, serde::Serialize, serde::Deserialize)]
4214
pub struct ScrollDelta {
4315
pub x: f64,

editor/src/messages/viewport/viewport_message.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::messages::prelude::*;
33
#[impl_message(Message, Viewport)]
44
#[derive(PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)]
55
pub enum ViewportMessage {
6-
UpdateScale { scale: f64 },
7-
UpdateBounds { x: f64, y: f64, width: f64, height: f64 },
6+
Update { x: f64, y: f64, width: f64, height: f64, scale: f64 },
87
Trigger,
98
}

editor/src/messages/viewport/viewport_message_handler.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,23 @@ impl Default for ViewportMessageHandler {
2525
impl MessageHandler<ViewportMessage, ()> for ViewportMessageHandler {
2626
fn process_message(&mut self, message: ViewportMessage, responses: &mut VecDeque<Message>, _: ()) {
2727
match message {
28-
ViewportMessage::UpdateScale { scale } => {
28+
ViewportMessage::Update { x, y, width, height, scale } => {
2929
assert_ne!(scale, 0.0, "Viewport scale cannot be zero");
3030
self.scale = scale;
31-
}
32-
ViewportMessage::UpdateBounds { x, y, width, height } => {
31+
3332
self.bounds = LogicalBounds { x, y, width, height };
3433
}
3534
ViewportMessage::Trigger => {}
3635
}
3736

37+
let physical_bounds = self.physical_bounds();
38+
responses.add(FrontendMessage::UpdateViewportPhysicalBounds {
39+
x: physical_bounds.x,
40+
y: physical_bounds.y,
41+
width: physical_bounds.width,
42+
height: physical_bounds.height,
43+
});
44+
3845
responses.add(NavigationMessage::CanvasPan { delta: DVec2::ZERO });
3946
responses.add(NodeGraphMessage::SetGridAlignedEdges);
4047

@@ -46,14 +53,6 @@ impl MessageHandler<ViewportMessage, ()> for ViewportMessageHandler {
4653
.into(),
4754
],
4855
});
49-
50-
let physical_bounds = self.physical_bounds();
51-
responses.add(FrontendMessage::UpdateViewportPhysicalBounds {
52-
x: physical_bounds.x,
53-
y: physical_bounds.y,
54-
width: physical_bounds.width,
55-
height: physical_bounds.height,
56-
});
5756
}
5857

5958
advertise_actions!(ViewportMessageDiscriminant;);

frontend/src/components/panels/Document.svelte

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import type { DocumentState } from "@graphite/state-providers/document";
2121
import { textInputCleanup } from "@graphite/utility-functions/keyboard-entry";
2222
import { extractPixelData, rasterizeSVGCanvas } from "@graphite/utility-functions/rasterization";
23-
import { updateBoundsOfViewports } from "@graphite/utility-functions/viewports";
23+
import { updateBoundsOfViewports as updateViewport } from "@graphite/utility-functions/viewports";
2424
2525
import EyedropperPreview, { ZOOM_WINDOW_DIMENSIONS } from "@graphite/components/floating-menus/EyedropperPreview.svelte";
2626
import LayoutCol from "@graphite/components/layout/LayoutCol.svelte";
@@ -392,10 +392,7 @@
392392
rulerVertical?.resize();
393393
394394
// Send the new bounds of the viewports to the backend
395-
if (viewport.parentElement) updateBoundsOfViewports(editor);
396-
397-
devicePixelRatio = window.devicePixelRatio;
398-
editor.handle.updateViewportScale(devicePixelRatio);
395+
if (viewport.parentElement) updateViewport(editor);
399396
}
400397
401398
onMount(() => {

frontend/src/utility-functions/viewports.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import { type Editor } from "@graphite/editor";
22

33
export function updateBoundsOfViewports(editor: Editor) {
44
const viewports = Array.from(window.document.querySelectorAll("[data-viewport-container]"));
5-
const boundsOfViewports = viewports.map((canvas) => {
6-
const bounds = canvas.getBoundingClientRect();
7-
return [bounds.left, bounds.top, bounds.right, bounds.bottom];
8-
});
95

10-
const flattened = boundsOfViewports.flat();
11-
const data = Float64Array.from(flattened);
6+
if (viewports.length <= 0) return;
127

13-
if (boundsOfViewports.length > 0) editor.handle.boundsOfViewport(data);
8+
const viewportBounds = viewports[0].getBoundingClientRect();
9+
10+
const left = viewportBounds.left;
11+
const top = viewportBounds.top;
12+
const right = viewportBounds.right;
13+
const bottom = viewportBounds.bottom;
14+
15+
const scale = window.devicePixelRatio || 1;
16+
17+
editor.handle.updateViewport(left, top, right, bottom, scale);
1418
}

frontend/wasm/src/editor_api.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,15 @@ impl EditorHandle {
516516
self.dispatch(message);
517517
}
518518

519-
/// Send new bounds when viewport get resized or moved within the editor
520-
/// [left, top, right, bottom]
521-
#[wasm_bindgen(js_name = boundsOfViewport)]
522-
pub fn bounds_of_viewport(&self, bounds: &[f64]) {
523-
let x = bounds[0];
524-
let y = bounds[1];
525-
let width = bounds[2] - bounds[0];
526-
let height = bounds[3] - bounds[1];
527-
528-
let message = ViewportMessage::UpdateBounds { x, y, width, height };
519+
/// Send new viewport info to the backend
520+
#[wasm_bindgen(js_name = updateViewport)]
521+
pub fn update_viewport(&self, left: f64, top: f64, right: f64, bottom: f64, scale: f64) {
522+
let x = left;
523+
let y = top;
524+
let width = right - left;
525+
let height = bottom - top;
526+
527+
let message = ViewportMessage::Update { x, y, width, height, scale };
529528
self.dispatch(message);
530529
}
531530

@@ -536,13 +535,6 @@ impl EditorHandle {
536535
self.dispatch(message);
537536
}
538537

539-
/// Inform the overlays system of the current device pixel ratio
540-
#[wasm_bindgen(js_name = updateViewportScale)]
541-
pub fn update_viewport_scale(&self, scale: f64) {
542-
let message = ViewportMessage::UpdateScale { scale };
543-
self.dispatch(message);
544-
}
545-
546538
/// Mouse movement within the screenspace bounds of the viewport
547539
#[wasm_bindgen(js_name = onMouseMove)]
548540
pub fn on_mouse_move(&self, x: f64, y: f64, mouse_keys: u8, modifiers: u8) {

0 commit comments

Comments
 (0)