From 9e25561edf868dd077926f2b108407b917560c5c Mon Sep 17 00:00:00 2001 From: Ryan Goldstein Date: Tue, 2 Apr 2019 22:31:30 -0400 Subject: [PATCH] Fix compile failures and add canvas positioning --- src/platform_impl/stdweb/event_loop.rs | 15 +++++++++------ src/platform_impl/stdweb/window.rs | 22 ++++++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/platform_impl/stdweb/event_loop.rs b/src/platform_impl/stdweb/event_loop.rs index 2827138c..f6bdcab4 100644 --- a/src/platform_impl/stdweb/event_loop.rs +++ b/src/platform_impl/stdweb/event_loop.rs @@ -232,8 +232,9 @@ fn add_event(elrs: &EventLoopRunnerShared, target: &impl IE target.add_event_listener(move |event: E| { // Don't capture the event if the events loop has been destroyed - if elrs.runner.borrow().control == ControlFlow::Exit { - return; + match &*elrs.runner.borrow() { + Some(ref runner) if runner.control == ControlFlow::Exit => return, + _ => () } event.prevent_default(); @@ -248,7 +249,7 @@ impl ELRShared { fn set_listener(&self, event_handler: Box, &mut ControlFlow)>) { *self.runner.borrow_mut() = Some(EventLoopRunner { control: ControlFlow::Poll, - handling: false + handling: false, event_handler }); } @@ -257,18 +258,20 @@ impl ELRShared { // TODO: handle event buffer pub fn send_event(&self, event: Event) { let start_cause = StartCause::Poll; // TODO: this is obviously not right - self.handle_start(StartCause::Poll); + self.handle_start(start_cause); self.handle_event(event); self.handle_event(Event::EventsCleared); } fn handle_start(&self, start: StartCause) { - let is_handling = if Some(ref runner) = *self.runner.borrow() { + let is_handling = if let Some(ref runner) = *self.runner.borrow() { runner.handling } else { false }; - self.handle_event(Event::StartCause(is_handling)); + if is_handling { + self.handle_event(Event::NewEvents(start)); + } } fn handle_event(&self, event: Event) { diff --git a/src/platform_impl/stdweb/window.rs b/src/platform_impl/stdweb/window.rs index df875f03..2a712f12 100644 --- a/src/platform_impl/stdweb/window.rs +++ b/src/platform_impl/stdweb/window.rs @@ -53,7 +53,8 @@ impl WindowId { pub struct Window { pub(crate) canvas: CanvasElement, pub(crate) redraw: Box, - previous_pointer: RefCell<&'static str> + previous_pointer: RefCell<&'static str>, + position: RefCell, } impl Window { @@ -79,7 +80,11 @@ impl Window { let window = Window { canvas, redraw, - previous_pointer: RefCell::new("auto") + previous_pointer: RefCell::new("auto"), + position: RefCell::new(LogicalPosition { + x: 0.0, + y: 0.0 + }) }; if let Some(dimensions) = attr.dimensions { @@ -134,12 +139,17 @@ impl Window { } pub fn get_inner_position(&self) -> Option { - // TODO - None + Some(*self.position.borrow()) } - pub fn set_position(&self, _position: LogicalPosition) { - // TODO: use CSS? + pub fn set_position(&self, position: LogicalPosition) { + *self.position.borrow_mut() = position; + self.canvas.set_attribute("position", "fixed") + .expect("Setting the position for the canvas"); + self.canvas.set_attribute("left", &position.x.to_string()) + .expect("Setting the position for the canvas"); + self.canvas.set_attribute("top", &position.y.to_string()) + .expect("Setting the position for the canvas"); } #[inline]