Implement Canvas::request_redraw

This commit is contained in:
Héctor Ramón Jiménez 2019-06-25 18:39:41 +02:00
parent 9c5657b86c
commit d5368d7979
3 changed files with 20 additions and 22 deletions

View file

@ -9,6 +9,7 @@ use web_sys::{HtmlCanvasElement, PointerEvent, WheelEvent};
pub struct Canvas { pub struct Canvas {
raw: HtmlCanvasElement, raw: HtmlCanvasElement,
on_redraw: Closure<dyn Fn()>,
on_mouse_out: Option<Closure<dyn FnMut(PointerEvent)>>, on_mouse_out: Option<Closure<dyn FnMut(PointerEvent)>>,
on_mouse_over: Option<Closure<dyn FnMut(PointerEvent)>>, on_mouse_over: Option<Closure<dyn FnMut(PointerEvent)>>,
on_mouse_up: Option<Closure<dyn FnMut(PointerEvent)>>, on_mouse_up: Option<Closure<dyn FnMut(PointerEvent)>>,
@ -24,7 +25,10 @@ impl Drop for Canvas {
} }
impl Canvas { impl Canvas {
pub fn create() -> Result<Self, RootOE> { pub fn create<F>(on_redraw: F) -> Result<Self, RootOE>
where
F: 'static + Fn(),
{
let window = web_sys::window().expect("Failed to obtain window"); let window = web_sys::window().expect("Failed to obtain window");
let document = window.document().expect("Failed to obtain document"); let document = window.document().expect("Failed to obtain document");
@ -41,6 +45,7 @@ impl Canvas {
Ok(Canvas { Ok(Canvas {
raw: canvas, raw: canvas,
on_redraw: Closure::wrap(Box::new(on_redraw) as Box<dyn Fn()>),
on_mouse_out: None, on_mouse_out: None,
on_mouse_over: None, on_mouse_over: None,
on_mouse_up: None, on_mouse_up: None,
@ -79,6 +84,11 @@ impl Canvas {
&self.raw &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<F>(&mut self, mut handler: F) pub fn on_mouse_out<F>(&mut self, mut handler: F)
where where
F: 'static + FnMut(i32), F: 'static + FnMut(i32),

View file

@ -11,12 +11,6 @@ use crate::platform::web::WindowExtWebSys;
use crate::window::Window; use crate::window::Window;
use web_sys::HtmlCanvasElement; use web_sys::HtmlCanvasElement;
pub fn request_animation_frame<F>(f: F)
where
F: Fn(),
{
}
pub fn throw(msg: &str) { pub fn throw(msg: &str) {
wasm_bindgen::throw_str(msg); wasm_bindgen::throw_str(msg);
} }

View file

@ -13,7 +13,6 @@ use std::collections::VecDeque;
pub struct Window { pub struct Window {
canvas: backend::Canvas, canvas: backend::Canvas,
redraw: Box<dyn Fn()>,
previous_pointer: RefCell<&'static str>, previous_pointer: RefCell<&'static str>,
position: RefCell<LogicalPosition>, position: RefCell<LogicalPosition>,
} }
@ -24,24 +23,19 @@ impl Window {
attr: WindowAttributes, attr: WindowAttributes,
_: PlatformSpecificBuilderAttributes, _: PlatformSpecificBuilderAttributes,
) -> Result<Self, RootOE> { ) -> Result<Self, RootOE> {
let mut canvas = backend::Canvas::create()?;
target.register(&mut canvas);
let runner = target.runner.clone(); let runner = target.runner.clone();
let redraw = Box::new(move || {
let runner = runner.clone(); let mut canvas = backend::Canvas::create(move || {
backend::request_animation_frame(move || {
runner.send_event(Event::WindowEvent { runner.send_event(Event::WindowEvent {
window_id: RootWI(Id), window_id: RootWI(Id),
event: WindowEvent::RedrawRequested, event: WindowEvent::RedrawRequested,
}) })
}); })?;
});
target.register(&mut canvas);
let window = Window { let window = Window {
canvas, canvas,
redraw,
previous_pointer: RefCell::new("auto"), previous_pointer: RefCell::new("auto"),
position: RefCell::new(LogicalPosition { x: 0.0, y: 0.0 }), position: RefCell::new(LogicalPosition { x: 0.0, y: 0.0 }),
}; };
@ -71,7 +65,7 @@ impl Window {
} }
pub fn request_redraw(&self) { pub fn request_redraw(&self) {
(self.redraw)(); self.canvas.request_redraw();
} }
pub fn outer_position(&self) -> Result<LogicalPosition, NotSupportedError> { pub fn outer_position(&self) -> Result<LogicalPosition, NotSupportedError> {