diff --git a/src/platform_impl/web/event_loop/runner.rs b/src/platform_impl/web/event_loop/runner.rs index cadd1dcd..f704346e 100644 --- a/src/platform_impl/web/event_loop/runner.rs +++ b/src/platform_impl/web/event_loop/runner.rs @@ -16,6 +16,7 @@ impl Clone for Shared { pub struct Execution { runner: RefCell>>, events: RefCell>>, + id: RefCell, } struct Runner { @@ -39,6 +40,7 @@ impl Shared { Shared(Rc::new(Execution { runner: RefCell::new(None), events: RefCell::new(VecDeque::new()), + id: RefCell::new(0), })) } @@ -53,6 +55,15 @@ impl Shared { backend::on_unload(move || close_instance.handle_unload()); } + // Generate a strictly increasing ID + // This is used to differentiate windows when handling events + pub fn generate_id(&self) -> u32 { + let mut id = self.0.id.borrow_mut(); + *id += 1; + + *id + } + // Add an event to the event loop runner // // It will determine if the event should be immediately sent to the user or buffered for later diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 17ba26ae..e7bab388 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -31,11 +31,16 @@ impl WindowTarget { self.runner.set_listener(event_handler); } - pub fn register(&self, canvas: &mut backend::Canvas) { + pub fn generate_id(&self) -> window::Id { + window::Id(self.runner.generate_id()) + } + + pub fn register(&self, canvas: &mut backend::Canvas, id: window::Id) { let runner = self.runner.clone(); + canvas.on_blur(move || { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::Focused(false), }); }); @@ -43,7 +48,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_focus(move || { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::Focused(true), }); }); @@ -51,7 +56,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_keyboard_press(move |scancode, virtual_keycode, modifiers| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::KeyboardInput { device_id: DeviceId(unsafe { device::Id::dummy() }), input: KeyboardInput { @@ -67,7 +72,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_keyboard_release(move |scancode, virtual_keycode, modifiers| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::KeyboardInput { device_id: DeviceId(unsafe { device::Id::dummy() }), input: KeyboardInput { @@ -83,7 +88,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_received_character(move |char_code| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::ReceivedCharacter(char_code), }); }); @@ -91,7 +96,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_cursor_leave(move |pointer_id| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::CursorLeft { device_id: DeviceId(device::Id(pointer_id)), }, @@ -101,7 +106,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_cursor_enter(move |pointer_id| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::CursorEntered { device_id: DeviceId(device::Id(pointer_id)), }, @@ -111,7 +116,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_cursor_move(move |pointer_id, position, modifiers| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::CursorMoved { device_id: DeviceId(device::Id(pointer_id)), position, @@ -123,7 +128,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_mouse_press(move |pointer_id, button, modifiers| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::MouseInput { device_id: DeviceId(device::Id(pointer_id)), state: ElementState::Pressed, @@ -136,7 +141,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_mouse_release(move |pointer_id, button, modifiers| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::MouseInput { device_id: DeviceId(device::Id(pointer_id)), state: ElementState::Released, @@ -149,7 +154,7 @@ impl WindowTarget { let runner = self.runner.clone(); canvas.on_mouse_wheel(move |pointer_id, delta, modifiers| { runner.send_event(Event::WindowEvent { - window_id: WindowId(window::Id), + window_id: WindowId(id), event: WindowEvent::MouseWheel { device_id: DeviceId(device::Id(pointer_id)), delta, diff --git a/src/platform_impl/web/stdweb/canvas.rs b/src/platform_impl/web/stdweb/canvas.rs index 1cd99e7e..cf670471 100644 --- a/src/platform_impl/web/stdweb/canvas.rs +++ b/src/platform_impl/web/stdweb/canvas.rs @@ -13,7 +13,7 @@ use stdweb::web::event::{ }; use stdweb::web::html_element::CanvasElement; use stdweb::web::{ - document, window, EventListenerHandle, IChildNode, IElement, IEventTarget, IHtmlElement, INode, + document, window, EventListenerHandle, IChildNode, IElement, IEventTarget, IHtmlElement, }; pub struct Canvas { diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index 7a8f740a..be9a9082 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -15,6 +15,7 @@ pub struct Window { canvas: backend::Canvas, previous_pointer: RefCell<&'static str>, position: RefCell, + id: Id, } impl Window { @@ -25,19 +26,22 @@ impl Window { ) -> Result { let runner = target.runner.clone(); + let id = target.generate_id(); + let mut canvas = backend::Canvas::create(move || { runner.send_event(Event::WindowEvent { - window_id: RootWI(Id), + window_id: RootWI(id), event: WindowEvent::RedrawRequested, }) })?; - target.register(&mut canvas); + target.register(&mut canvas, id); let window = Window { canvas, previous_pointer: RefCell::new("auto"), position: RefCell::new(LogicalPosition { x: 0.0, y: 0.0 }), + id, }; window.set_inner_size(attr.inner_size.unwrap_or(LogicalSize { @@ -250,17 +254,16 @@ impl Window { #[inline] pub fn id(&self) -> Id { - // TODO ? - unsafe { Id::dummy() } + return self.id; } } #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Id; +pub struct Id(pub(crate) u32); impl Id { pub unsafe fn dummy() -> Id { - Id + Id(0) } }