From d5368d7979eefe0c2f7ae3f81fc2913d6ebbab68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 25 Jun 2019 18:39:41 +0200 Subject: [PATCH] Implement `Canvas::request_redraw` --- src/platform_impl/web/web_sys/canvas.rs | 12 +++++++++++- src/platform_impl/web/web_sys/mod.rs | 6 ------ src/platform_impl/web/window.rs | 24 +++++++++--------------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index 845c3ee3..3108e1c2 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -9,6 +9,7 @@ use web_sys::{HtmlCanvasElement, PointerEvent, WheelEvent}; pub struct Canvas { raw: HtmlCanvasElement, + on_redraw: Closure, on_mouse_out: Option>, on_mouse_over: Option>, on_mouse_up: Option>, @@ -24,7 +25,10 @@ impl Drop for Canvas { } impl Canvas { - pub fn create() -> Result { + pub fn create(on_redraw: F) -> Result + where + F: 'static + Fn(), + { let window = web_sys::window().expect("Failed to obtain window"); let document = window.document().expect("Failed to obtain document"); @@ -41,6 +45,7 @@ impl Canvas { Ok(Canvas { raw: canvas, + on_redraw: Closure::wrap(Box::new(on_redraw) as Box), on_mouse_out: None, on_mouse_over: None, on_mouse_up: None, @@ -79,6 +84,11 @@ impl Canvas { &self.raw } + pub fn request_redraw(&self) { + let window = web_sys::window().expect("Failed to obtain window"); + window.request_animation_frame(&self.on_redraw.as_ref().unchecked_ref()); + } + pub fn on_mouse_out(&mut self, mut handler: F) where F: 'static + FnMut(i32), diff --git a/src/platform_impl/web/web_sys/mod.rs b/src/platform_impl/web/web_sys/mod.rs index 0e3dba74..40d8ff20 100644 --- a/src/platform_impl/web/web_sys/mod.rs +++ b/src/platform_impl/web/web_sys/mod.rs @@ -11,12 +11,6 @@ use crate::platform::web::WindowExtWebSys; use crate::window::Window; use web_sys::HtmlCanvasElement; -pub fn request_animation_frame(f: F) -where - F: Fn(), -{ -} - pub fn throw(msg: &str) { wasm_bindgen::throw_str(msg); } diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index dc3230c0..4dc06f9a 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -13,7 +13,6 @@ use std::collections::VecDeque; pub struct Window { canvas: backend::Canvas, - redraw: Box, previous_pointer: RefCell<&'static str>, position: RefCell, } @@ -24,24 +23,19 @@ impl Window { attr: WindowAttributes, _: PlatformSpecificBuilderAttributes, ) -> Result { - let mut canvas = backend::Canvas::create()?; + let runner = target.runner.clone(); + + let mut canvas = backend::Canvas::create(move || { + runner.send_event(Event::WindowEvent { + window_id: RootWI(Id), + event: WindowEvent::RedrawRequested, + }) + })?; target.register(&mut canvas); - let runner = target.runner.clone(); - let redraw = Box::new(move || { - let runner = runner.clone(); - backend::request_animation_frame(move || { - runner.send_event(Event::WindowEvent { - window_id: RootWI(Id), - event: WindowEvent::RedrawRequested, - }) - }); - }); - let window = Window { canvas, - redraw, previous_pointer: RefCell::new("auto"), position: RefCell::new(LogicalPosition { x: 0.0, y: 0.0 }), }; @@ -71,7 +65,7 @@ impl Window { } pub fn request_redraw(&self) { - (self.redraw)(); + self.canvas.request_redraw(); } pub fn outer_position(&self) -> Result {