diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 9baff4fdf..63b5002b6 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -1,9 +1,9 @@ +pub mod images; pub mod render; pub mod shapes; pub mod state; pub mod utils; pub mod view; -pub mod images; use skia_safe as skia; @@ -36,14 +36,6 @@ pub extern "C" fn init(width: i32, height: i32) { } } -/// This is called from JS when the window is resized. -/// # Safety -#[no_mangle] -pub unsafe extern "C" fn resize_surface(width: i32, height: i32) { - let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); - state.render_state.resize(width, height); -} - #[no_mangle] pub unsafe extern "C" fn render() { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); @@ -51,7 +43,7 @@ pub unsafe extern "C" fn render() { } #[no_mangle] - pub unsafe extern "C" fn navigate() { +pub unsafe extern "C" fn navigate() { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); state.navigate(); } @@ -65,11 +57,7 @@ pub extern "C" fn reset_canvas() { #[no_mangle] pub extern "C" fn set_view(zoom: f32, x: f32, y: f32, width: f32, height: f32) { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); - state.view.zoom = zoom; - state.view.x = x; - state.view.y = y; - state.view.width = width; - state.view.height = height; + state.set_view(zoom, (x, y), (width, height)); } #[no_mangle] diff --git a/render-wasm/src/state.rs b/render-wasm/src/state.rs index fdbb06807..4a853cbbc 100644 --- a/render-wasm/src/state.rs +++ b/render-wasm/src/state.rs @@ -31,7 +31,7 @@ impl<'a> State<'a> { zoom: 1., width: 0., height: 0., - } + }, } } @@ -40,13 +40,11 @@ impl<'a> State<'a> { } pub fn navigate(&mut self) { - self.render_state - .navigate(&self.view, &self.shapes); + self.render_state.navigate(&self.view, &self.shapes); } pub fn draw_all_shapes(&mut self) { - self.render_state - .draw_all_shapes(&self.view, &self.shapes); + self.render_state.draw_all_shapes(&self.view, &self.shapes); } pub fn use_shape(&'a mut self, id: Uuid) { @@ -62,4 +60,20 @@ impl<'a> State<'a> { pub fn current_shape(&'a mut self) -> Option<&'a mut Shape> { self.current_shape.as_deref_mut() } + + pub fn set_view(&mut self, zoom: f32, pan: (f32, f32), size: (f32, f32)) { + let (x, y) = pan; + self.view.x = x; + self.view.y = y; + + self.view.zoom = zoom; + + let (w, h) = size; + if self.view.width != w || self.view.height != h { + self.view.width = w; + self.view.height = h; + + self.render_state.resize(w as i32, h as i32); + } + } }