Skip to content

Commit ad79d8c

Browse files
committed
simple mouse click event fires
1 parent 34d6374 commit ad79d8c

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ features = [
8989
'HtmlElement',
9090
'HtmlCanvasElement',
9191
'CanvasRenderingContext2d',
92+
'MouseEvent',
9293
'Node',
9394
'Window',
9495
'console'

src/platform_impl/websys/event_loop.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate wasm_bindgen;
33

44
use event_loop::{ControlFlow, EventLoopClosed};
55
use event::Event;
6-
use super::window::{MonitorHandle, Window, WindowInternal};
6+
use super::window::{MonitorHandle, Window, WindowId, WindowInternal};
77
#[macro_use]
88
use platform_impl::platform::wasm_util as util;
99

@@ -69,6 +69,12 @@ impl<T: 'static> EventLoop<T> {
6969

7070
*g.borrow_mut() = Some(Closure::wrap(Box::new(move || {
7171
if control_flow == ControlFlow::Poll {
72+
73+
let mut win_events = self.window_target.p.window().events();
74+
win_events.drain(..).for_each(|e| {
75+
event_handler(::event::Event::WindowEvent{window_id: ::window::WindowId(WindowId{}), event: e}, &self.window_target, &mut control_flow);
76+
});
77+
7278
event_handler(::event::Event::NewEvents(::event::StartCause::Poll), &self.window_target, &mut control_flow);
7379
}
7480

src/platform_impl/websys/window.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ extern crate wasm_bindgen;
33
use window::{WindowAttributes, CreationError, MouseCursor};
44
use std::collections::VecDeque;
55
use std::rc::Rc;
6+
use std::cell::RefCell;
67
use dpi::{PhysicalPosition, LogicalPosition, PhysicalSize, LogicalSize};
78
use icon::Icon;
89
use super::event_loop::{EventLoopWindowTarget};
10+
911
use self::wasm_bindgen::prelude::*;
1012
use self::wasm_bindgen::JsCast;
1113

@@ -86,7 +88,13 @@ pub struct Window {
8688
}
8789

8890
pub(crate) struct WindowInternal {
89-
pending_events: Vec<::event::WindowEvent>
91+
pub pending_events: RefCell<Vec<::event::WindowEvent>>
92+
}
93+
94+
impl WindowInternal {
95+
pub fn events(&self) -> Vec<::event::WindowEvent> {
96+
self.pending_events.replace(Vec::new())
97+
}
9098
}
9199

92100
impl Window {
@@ -127,12 +135,24 @@ impl Window {
127135
};
128136

129137
let internal = Rc::new(WindowInternal {
130-
pending_events: vec![]
138+
pending_events: RefCell::new(Vec::with_capacity(3))
131139
});
132140

133141
target.set_window(internal.clone());
134142

135143
// TODO: install WindowEvent handlers
144+
let mut win = internal.clone();
145+
let click_handler = Closure::wrap(Box::new(move |event: web_sys::MouseEvent| {
146+
// TODO: process the event
147+
win.pending_events.borrow_mut().push(::event::WindowEvent::MouseInput {
148+
device_id: ::event::DeviceId(DeviceId(0)),
149+
state: ::event::ElementState::Pressed,
150+
button: ::event::MouseButton::Left,
151+
modifiers: Default::default()
152+
});
153+
}) as Box<FnMut(web_sys::MouseEvent)>);
154+
element.set_onmousedown(Some(click_handler.as_ref().unchecked_ref()));
155+
click_handler.forget();
136156

137157
Ok(Window {
138158
canvas: element,

0 commit comments

Comments
 (0)