Fix compile failures and add canvas positioning

This commit is contained in:
Ryan Goldstein 2019-04-02 22:31:30 -04:00
parent 7c6bdcc459
commit 9e25561edf
2 changed files with 25 additions and 12 deletions

View file

@ -232,8 +232,9 @@ fn add_event<T: 'static, E, F>(elrs: &EventLoopRunnerShared<T>, target: &impl IE
target.add_event_listener(move |event: E| { target.add_event_listener(move |event: E| {
// Don't capture the event if the events loop has been destroyed // Don't capture the event if the events loop has been destroyed
if elrs.runner.borrow().control == ControlFlow::Exit { match &*elrs.runner.borrow() {
return; Some(ref runner) if runner.control == ControlFlow::Exit => return,
_ => ()
} }
event.prevent_default(); event.prevent_default();
@ -248,7 +249,7 @@ impl<T> ELRShared<T> {
fn set_listener(&self, event_handler: Box<dyn FnMut(Event<T>, &mut ControlFlow)>) { fn set_listener(&self, event_handler: Box<dyn FnMut(Event<T>, &mut ControlFlow)>) {
*self.runner.borrow_mut() = Some(EventLoopRunner { *self.runner.borrow_mut() = Some(EventLoopRunner {
control: ControlFlow::Poll, control: ControlFlow::Poll,
handling: false handling: false,
event_handler event_handler
}); });
} }
@ -257,18 +258,20 @@ impl<T> ELRShared<T> {
// TODO: handle event buffer // TODO: handle event buffer
pub fn send_event(&self, event: Event<T>) { pub fn send_event(&self, event: Event<T>) {
let start_cause = StartCause::Poll; // TODO: this is obviously not right 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);
self.handle_event(Event::EventsCleared); self.handle_event(Event::EventsCleared);
} }
fn handle_start(&self, start: StartCause) { 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 runner.handling
} else { } else {
false false
}; };
self.handle_event(Event::StartCause(is_handling)); if is_handling {
self.handle_event(Event::NewEvents(start));
}
} }
fn handle_event(&self, event: Event<T>) { fn handle_event(&self, event: Event<T>) {

View file

@ -53,7 +53,8 @@ impl WindowId {
pub struct Window { pub struct Window {
pub(crate) canvas: CanvasElement, pub(crate) canvas: CanvasElement,
pub(crate) redraw: Box<dyn Fn()>, pub(crate) redraw: Box<dyn Fn()>,
previous_pointer: RefCell<&'static str> previous_pointer: RefCell<&'static str>,
position: RefCell<LogicalPosition>,
} }
impl Window { impl Window {
@ -79,7 +80,11 @@ impl Window {
let window = Window { let window = Window {
canvas, canvas,
redraw, 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 { if let Some(dimensions) = attr.dimensions {
@ -134,12 +139,17 @@ impl Window {
} }
pub fn get_inner_position(&self) -> Option<LogicalPosition> { pub fn get_inner_position(&self) -> Option<LogicalPosition> {
// TODO Some(*self.position.borrow())
None
} }
pub fn set_position(&self, _position: LogicalPosition) { pub fn set_position(&self, position: LogicalPosition) {
// TODO: use CSS? *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] #[inline]