From 3ca9afcd6a90c9d6a38397316c1dd16f7a679e3c Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 11:11:21 +0200 Subject: [PATCH 01/10] feat: add pointer events to web --- Cargo.toml | 3 +- src/event.rs | 15 +++ .../web/event_loop/window_target.rs | 62 +++++++++- src/platform_impl/web/web_sys/canvas.rs | 44 ++++++- .../web/web_sys/canvas/pointer_handler.rs | 110 +++++++++++++++++- 5 files changed, 230 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bb0da1eb23..b204775f46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,7 +118,8 @@ features = [ 'Node', 'PointerEvent', 'Window', - 'WheelEvent' + 'WheelEvent', + 'PointerEvent', ] [target.'cfg(target_arch = "wasm32")'.dependencies.wasm-bindgen] diff --git a/src/event.rs b/src/event.rs index f39ddaac50..4abb6db6c1 100644 --- a/src/event.rs +++ b/src/event.rs @@ -663,6 +663,21 @@ pub struct Touch { pub force: Option, /// Unique identifier of a finger. pub id: u64, + /// Type of pointer used when touching. + /// + /// ## Platform-specific + /// + /// - Currently only available on Web targets. + pub pointer_type: Option, +} + +/// Describes the types of pointers available. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum PointerType { + Mouse, + Pen, + Touch, + Unknown, } /// Describes the force of a touch event diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 367556a7a1..21d6cb8a60 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -1,7 +1,7 @@ use super::{super::monitor, backend, device, proxy::Proxy, runner, window}; use crate::dpi::{PhysicalSize, Size}; use crate::event::{ - DeviceEvent, DeviceId, ElementState, Event, KeyboardInput, TouchPhase, WindowEvent, + DeviceEvent, DeviceId, ElementState, Event, KeyboardInput, Touch, TouchPhase, WindowEvent, }; use crate::event_loop::ControlFlow; use crate::monitor::MonitorHandle as RootMH; @@ -231,6 +231,66 @@ impl WindowTarget { runner.request_redraw(WindowId(id)); }); + let runner = self.runner.clone(); + canvas.on_pointer_move(move |device_id, location, pointer_type| { + runner.send_event(Event::WindowEvent { + window_id: WindowId(id), + event: WindowEvent::Touch(Touch { + id: device_id as u64, + device_id: DeviceId(device::Id(device_id)), + phase: TouchPhase::Moved, + force: None, // Todo + pointer_type: Some(pointer_type), + location, + }), + }); + }); + + let runner = self.runner.clone(); + canvas.on_pointer_down(move |device_id, location, pointer_type| { + runner.send_event(Event::WindowEvent { + window_id: WindowId(id), + event: WindowEvent::Touch(Touch { + id: device_id as u64, + device_id: DeviceId(device::Id(device_id)), + phase: TouchPhase::Started, + force: None, // Todo + pointer_type: Some(pointer_type), + location, + }), + }); + }); + + let runner = self.runner.clone(); + canvas.on_pointer_up(move |device_id, location, pointer_type| { + runner.send_event(Event::WindowEvent { + window_id: WindowId(id), + event: WindowEvent::Touch(Touch { + id: device_id as u64, + device_id: DeviceId(device::Id(device_id)), + phase: TouchPhase::Ended, + force: None, // Todo + pointer_type: Some(pointer_type), + location, + }), + }); + }); + + let runner = self.runner.clone(); + canvas.on_pointer_cancel(move |device_id, location, pointer_type| { + runner.send_event(Event::WindowEvent { + window_id: WindowId(id), + event: WindowEvent::Touch(Touch { + id: device_id as u64, + device_id: DeviceId(device::Id(device_id)), + phase: TouchPhase::Cancelled, + force: None, // Todo + pointer_type: Some(pointer_type), + location, + }), + }); + }); + let runner = self.runner.clone(); canvas.on_dark_mode(move |is_dark_mode| { let theme = if is_dark_mode { diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index 9725e8b6b8..08d42d3435 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -3,7 +3,9 @@ use super::event_handle::EventListenerHandle; use super::media_query_handle::MediaQueryListHandle; use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize}; use crate::error::OsError as RootOE; -use crate::event::{ModifiersState, MouseButton, MouseScrollDelta, ScanCode, VirtualKeyCode}; +use crate::event::{ + ModifiersState, MouseButton, MouseScrollDelta, PointerType, ScanCode, VirtualKeyCode, +}; use crate::platform_impl::{OsError, PlatformSpecificWindowBuilderAttributes}; use std::cell::RefCell; @@ -247,6 +249,46 @@ impl Canvas { } } + pub fn on_pointer_move(&mut self, handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + match &mut self.mouse_state { + MouseState::HasPointerEvent(h) => h.on_pointer_move(&self.common, handler), + _ => {} + } + } + + pub fn on_pointer_down(&mut self, handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + match &mut self.mouse_state { + MouseState::HasPointerEvent(h) => h.on_pointer_down(&self.common, handler), + _ => {} + } + } + + pub fn on_pointer_up(&mut self, handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + match &mut self.mouse_state { + MouseState::HasPointerEvent(h) => h.on_pointer_up(&self.common, handler), + _ => {} + } + } + + pub fn on_pointer_cancel(&mut self, handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + match &mut self.mouse_state { + MouseState::HasPointerEvent(h) => h.on_pointer_cancel(&self.common, handler), + _ => {} + } + } + pub fn on_mouse_wheel(&mut self, mut handler: F) where F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState), diff --git a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs index 5f6f321016..57b3b2ba31 100644 --- a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs +++ b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs @@ -1,7 +1,7 @@ use super::event; use super::EventListenerHandle; use crate::dpi::PhysicalPosition; -use crate::event::{ModifiersState, MouseButton}; +use crate::event::{ModifiersState, MouseButton, PointerType}; use web_sys::PointerEvent; @@ -12,6 +12,10 @@ pub(super) struct PointerHandler { on_cursor_move: Option>, on_pointer_press: Option>, on_pointer_release: Option>, + on_pointer_move: Option>, + on_pointer_down: Option>, + on_pointer_up: Option>, + on_pointer_cancel: Option>, } impl PointerHandler { @@ -22,6 +26,10 @@ impl PointerHandler { on_cursor_move: None, on_pointer_press: None, on_pointer_release: None, + on_pointer_move: None, + on_pointer_down: None, + on_pointer_up: None, + on_pointer_cancel: None, } } @@ -103,11 +111,111 @@ impl PointerHandler { )); } + pub fn on_pointer_move(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + self.on_pointer_move = Some(canvas_common.add_event( + "pointermove", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + match event.pointer_type().as_str() { + "mouse" => PointerType::Mouse, + "pen" => PointerType::Pen, + "touch" => PointerType::Touch, + _ => PointerType::Unknown, + }, + ); + }, + )); + } + + pub fn on_pointer_down(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + self.on_pointer_down = Some(canvas_common.add_event( + "pointerdown", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + match event.pointer_type().as_str() { + "mouse" => PointerType::Mouse, + "pen" => PointerType::Pen, + "touch" => PointerType::Touch, + _ => PointerType::Unknown, + }, + ); + }, + )); + } + + pub fn on_pointer_up(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + self.on_pointer_up = Some(canvas_common.add_event( + "pointerup", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + match event.pointer_type().as_str() { + "mouse" => PointerType::Mouse, + "pen" => PointerType::Pen, + "touch" => PointerType::Touch, + _ => PointerType::Unknown, + }, + ); + }, + )); + } + + pub fn on_pointer_cancel(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition, PointerType), + { + self.on_pointer_cancel = Some(canvas_common.add_event( + "pointercancel", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + match event.pointer_type().as_str() { + "mouse" => PointerType::Mouse, + "pen" => PointerType::Pen, + "touch" => PointerType::Touch, + _ => PointerType::Unknown, + }, + ); + }, + )); + } + pub fn remove_listeners(&mut self) { self.on_cursor_leave = None; self.on_cursor_enter = None; self.on_cursor_move = None; self.on_pointer_press = None; self.on_pointer_release = None; + self.on_pointer_move = None; + self.on_pointer_down = None; + self.on_pointer_up = None; + self.on_pointer_cancel = None; } } From edc3c762aef9b27021f297c55a59d0d3e720ca98 Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 11:25:53 +0200 Subject: [PATCH 02/10] fix: add missing pointer types to Touch structs --- src/platform_impl/android/mod.rs | 1 + src/platform_impl/ios/view.rs | 1 + src/platform_impl/linux/wayland/seat/touch/handlers.rs | 4 ++++ src/platform_impl/linux/x11/event_processor.rs | 1 + src/platform_impl/windows/event_loop.rs | 2 ++ 5 files changed, 9 insertions(+) diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index de69bc9238..527850ff2e 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -227,6 +227,7 @@ impl EventLoop { location, id: pointer.pointer_id() as u64, force: None, + pointer_type: None, }, ), }; diff --git a/src/platform_impl/ios/view.rs b/src/platform_impl/ios/view.rs index ccdec6bfbf..88d024007d 100644 --- a/src/platform_impl/ios/view.rs +++ b/src/platform_impl/ios/view.rs @@ -271,6 +271,7 @@ unsafe fn get_view_class(root_view_class: &'static Class) -> &'static Class { device_id: RootDeviceId(DeviceId { uiscreen }), id: touch_id, location: physical_location, + pointer_type: None, force, phase, }), diff --git a/src/platform_impl/linux/wayland/seat/touch/handlers.rs b/src/platform_impl/linux/wayland/seat/touch/handlers.rs index 8a17b39372..9365d7c82a 100644 --- a/src/platform_impl/linux/wayland/seat/touch/handlers.rs +++ b/src/platform_impl/linux/wayland/seat/touch/handlers.rs @@ -40,6 +40,7 @@ pub(super) fn handle_touch( location: position.to_physical(scale_factor), force: None, // TODO id: id as u64, + pointer_type: None, }), window_id, ); @@ -67,6 +68,7 @@ pub(super) fn handle_touch( location, force: None, // TODO id: id as u64, + pointer_type: None, }), window_id, ); @@ -92,6 +94,7 @@ pub(super) fn handle_touch( location, force: None, // TODO id: id as u64, + pointer_type: None, }), window_id, ); @@ -112,6 +115,7 @@ pub(super) fn handle_touch( location, force: None, // TODO id: touch_point.id as u64, + pointer_type: None, }), window_id, ); diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index 65ed4d9ff1..1fcf35da3f 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -1006,6 +1006,7 @@ impl EventProcessor { location, force: None, // TODO id, + pointer_type: None, }), }) } diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 795d840dd9..b1c0e884a3 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -1445,6 +1445,7 @@ unsafe fn public_window_callback_inner( force: None, // WM_TOUCH doesn't support pressure information id: input.dwID as u64, device_id: DEVICE_ID, + pointer_type: None, }), }); } @@ -1584,6 +1585,7 @@ unsafe fn public_window_callback_inner( force, id: pointer_info.pointerId as u64, device_id: DEVICE_ID, + pointer_type: None, }), }); } From 994b5f72f30430f7120352ddfb4fa3763da0e516 Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 11:26:12 +0200 Subject: [PATCH 03/10] docs: update the docs to reflect the changes --- CHANGELOG.md | 31 ++++++++++++++++--------------- FEATURES.md | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49f6d59569..b953a44c37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased - Added `Window::focus_window`to bring the window to the front and set input focus. +- On Web, added support for `Event::Touch`. # 0.25.0 (2021-05-15) @@ -94,7 +95,7 @@ - On macOS, fix inverted horizontal scroll. - **Breaking:** `current_monitor` now returns `Option`. - **Breaking:** `primary_monitor` now returns `Option`. -- On macOS, updated core-* dependencies and cocoa. +- On macOS, updated core-\* dependencies and cocoa. - Bump `parking_lot` to 0.11 - On Android, bump `ndk`, `ndk-sys` and `ndk-glue` to 0.2. Checkout the new ndk-glue main proc attribute. - On iOS, fixed starting the app in landscape where the view still had portrait dimensions. @@ -136,7 +137,7 @@ - On Windows, fix `WindowBuilder::with_maximized` being ignored. - On Android, minimal platform support. - On iOS, touch positions are now properly converted to physical pixels. -- On macOS, updated core-* dependencies and cocoa +- On macOS, updated core-\* dependencies and cocoa # 0.22.1 (2020-04-16) @@ -188,13 +189,13 @@ - On X11, fix `ModifiersChanged` emitting incorrect modifier change events - **Breaking**: Overhaul how Winit handles DPI: - + Window functions and events now return `PhysicalSize` instead of `LogicalSize`. - + Functions that take `Size` or `Position` types can now take either `Logical` or `Physical` types. - + `hidpi_factor` has been renamed to `scale_factor`. - + `HiDpiFactorChanged` has been renamed to `ScaleFactorChanged`, and lets you control how the OS + - Window functions and events now return `PhysicalSize` instead of `LogicalSize`. + - Functions that take `Size` or `Position` types can now take either `Logical` or `Physical` types. + - `hidpi_factor` has been renamed to `scale_factor`. + - `HiDpiFactorChanged` has been renamed to `ScaleFactorChanged`, and lets you control how the OS resizes the window in response to the change. - + On X11, deprecate `WINIT_HIDPI_FACTOR` environment variable in favor of `WINIT_X11_SCALE_FACTOR`. - + `Size` and `Position` types are now generic over their exact pixel type. + - On X11, deprecate `WINIT_HIDPI_FACTOR` environment variable in favor of `WINIT_X11_SCALE_FACTOR`. + - `Size` and `Position` types are now generic over their exact pixel type. # 0.20.0 Alpha 6 (2020-01-03) @@ -299,7 +300,7 @@ - `Window::set_fullscreen` now takes `Option` where `Fullscreen` consists of `Fullscreen::Exclusive(VideoMode)` and `Fullscreen::Borderless(MonitorHandle)` variants. - - Adds support for exclusive fullscreen mode. + - Adds support for exclusive fullscreen mode. - On iOS, add support for hiding the home indicator. - On iOS, add support for deferring system gestures. - On iOS, fix a crash that occurred while acquiring a monitor's name. @@ -323,7 +324,7 @@ - On X11, non-resizable windows now have maximize explicitly disabled. - On Windows, support paths longer than MAX_PATH (260 characters) in `WindowEvent::DroppedFile` -and `WindowEvent::HoveredFile`. + and `WindowEvent::HoveredFile`. - On Mac, implement `DeviceEvent::Button`. - Change `Event::Suspended(true / false)` to `Event::Suspended` and `Event::Resumed`. - On X11, fix sanity check which checks that a monitor's reported width and height (in millimeters) are non-zero when calculating the DPI factor. @@ -498,7 +499,7 @@ and `WindowEvent::HoveredFile`. # Version 0.16.1 (2018-07-02) - Added logging through `log`. Logging will become more extensive over time. -- On X11 and Windows, the window's DPI factor is guessed before creating the window. This *greatly* cuts back on unsightly auto-resizing that would occur immediately after window creation. +- On X11 and Windows, the window's DPI factor is guessed before creating the window. This _greatly_ cuts back on unsightly auto-resizing that would occur immediately after window creation. - Fixed X11 backend compilation for environments where `c_char` is unsigned. # Version 0.16.0 (2018-06-25) @@ -648,7 +649,7 @@ and `WindowEvent::HoveredFile`. # Version 0.10.1 (2018-02-05) -*Yanked* +_Yanked_ # Version 0.10.0 (2017-12-27) @@ -665,20 +666,20 @@ and `WindowEvent::HoveredFile`. - Added event `WindowEvent::HiDPIFactorChanged`. - Added method `MonitorId::get_hidpi_factor`. - Deprecated `get_inner_size_pixels` and `get_inner_size_points` methods of `Window` in favor of -`get_inner_size`. + `get_inner_size`. - **Breaking:** `EventsLoop` is `!Send` and `!Sync` because of platform-dependant constraints, but `Window`, `WindowId`, `DeviceId` and `MonitorId` guaranteed to be `Send`. - `MonitorId::get_position` now returns `(i32, i32)` instead of `(u32, u32)`. - Rewrite of the wayland backend to use wayland-client-0.11 - Support for dead keys on wayland for keyboard utf8 input - Monitor enumeration on Windows is now implemented using `EnumDisplayMonitors` instead of -`EnumDisplayDevices`. This changes the value returned by `MonitorId::get_name()`. + `EnumDisplayDevices`. This changes the value returned by `MonitorId::get_name()`. - On Windows added `MonitorIdExt::hmonitor` method - Impl `Clone` for `EventsLoopProxy` - `EventsLoop::get_primary_monitor()` on X11 will fallback to any available monitor if no primary is found - Support for touch event on wayland - `WindowEvent`s `MouseMoved`, `MouseEntered`, and `MouseLeft` have been renamed to -`CursorMoved`, `CursorEntered`, and `CursorLeft`. + `CursorMoved`, `CursorEntered`, and `CursorLeft`. - New `DeviceEvent`s added, `MouseMotion` and `MouseWheel`. - Send `CursorMoved` event after `CursorEntered` and `Focused` events. - Add support for `ModifiersState`, `MouseMove`, `MouseInput`, `MouseMotion` for emscripten backend. diff --git a/FEATURES.md b/FEATURES.md index 8bea2043fb..7f13793764 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -201,7 +201,7 @@ Legend: |Mouse set location |✔️ |✔️ |✔️ |❓ |**N/A**|**N/A**|**N/A**| |Cursor grab |✔️ |▢[#165] |▢[#242] |✔️ |**N/A**|**N/A**|❓ | |Cursor icon |✔️ |✔️ |✔️ |✔️ |**N/A**|**N/A**|✔️ | -|Touch events |✔️ |❌ |✔️ |✔️ |✔️ |✔️ |❌ | +|Touch events |✔️ |❌ |✔️ |✔️ |✔️ |✔️ |✔️ | |Touch pressure |✔️ |❌ |❌ |❌ |❌ |✔️ |❌ | |Multitouch |✔️ |❌ |✔️ |✔️ |✔️ |✔️ |❌ | |Keyboard events |✔️ |✔️ |✔️ |✔️ |❓ |❌ |✔️ | From db5bfe477d966647a4aa74932b35580ca6ac6c54 Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 11:28:34 +0200 Subject: [PATCH 04/10] chore: revert unintented changes --- CHANGELOG.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b953a44c37..10793f0929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,7 +95,7 @@ - On macOS, fix inverted horizontal scroll. - **Breaking:** `current_monitor` now returns `Option`. - **Breaking:** `primary_monitor` now returns `Option`. -- On macOS, updated core-\* dependencies and cocoa. +- On macOS, updated core-* dependencies and cocoa. - Bump `parking_lot` to 0.11 - On Android, bump `ndk`, `ndk-sys` and `ndk-glue` to 0.2. Checkout the new ndk-glue main proc attribute. - On iOS, fixed starting the app in landscape where the view still had portrait dimensions. @@ -137,7 +137,7 @@ - On Windows, fix `WindowBuilder::with_maximized` being ignored. - On Android, minimal platform support. - On iOS, touch positions are now properly converted to physical pixels. -- On macOS, updated core-\* dependencies and cocoa +- On macOS, updated core-* dependencies and cocoa # 0.22.1 (2020-04-16) @@ -189,13 +189,13 @@ - On X11, fix `ModifiersChanged` emitting incorrect modifier change events - **Breaking**: Overhaul how Winit handles DPI: - - Window functions and events now return `PhysicalSize` instead of `LogicalSize`. - - Functions that take `Size` or `Position` types can now take either `Logical` or `Physical` types. - - `hidpi_factor` has been renamed to `scale_factor`. - - `HiDpiFactorChanged` has been renamed to `ScaleFactorChanged`, and lets you control how the OS + + Window functions and events now return `PhysicalSize` instead of `LogicalSize`. + + Functions that take `Size` or `Position` types can now take either `Logical` or `Physical` types. + + `hidpi_factor` has been renamed to `scale_factor`. + + `HiDpiFactorChanged` has been renamed to `ScaleFactorChanged`, and lets you control how the OS resizes the window in response to the change. - - On X11, deprecate `WINIT_HIDPI_FACTOR` environment variable in favor of `WINIT_X11_SCALE_FACTOR`. - - `Size` and `Position` types are now generic over their exact pixel type. + + On X11, deprecate `WINIT_HIDPI_FACTOR` environment variable in favor of `WINIT_X11_SCALE_FACTOR`. + + `Size` and `Position` types are now generic over their exact pixel type. # 0.20.0 Alpha 6 (2020-01-03) @@ -300,7 +300,7 @@ - `Window::set_fullscreen` now takes `Option` where `Fullscreen` consists of `Fullscreen::Exclusive(VideoMode)` and `Fullscreen::Borderless(MonitorHandle)` variants. - - Adds support for exclusive fullscreen mode. + - Adds support for exclusive fullscreen mode. - On iOS, add support for hiding the home indicator. - On iOS, add support for deferring system gestures. - On iOS, fix a crash that occurred while acquiring a monitor's name. @@ -324,7 +324,7 @@ - On X11, non-resizable windows now have maximize explicitly disabled. - On Windows, support paths longer than MAX_PATH (260 characters) in `WindowEvent::DroppedFile` - and `WindowEvent::HoveredFile`. +and `WindowEvent::HoveredFile`. - On Mac, implement `DeviceEvent::Button`. - Change `Event::Suspended(true / false)` to `Event::Suspended` and `Event::Resumed`. - On X11, fix sanity check which checks that a monitor's reported width and height (in millimeters) are non-zero when calculating the DPI factor. @@ -499,7 +499,7 @@ # Version 0.16.1 (2018-07-02) - Added logging through `log`. Logging will become more extensive over time. -- On X11 and Windows, the window's DPI factor is guessed before creating the window. This _greatly_ cuts back on unsightly auto-resizing that would occur immediately after window creation. +- On X11 and Windows, the window's DPI factor is guessed before creating the window. This *greatly* cuts back on unsightly auto-resizing that would occur immediately after window creation. - Fixed X11 backend compilation for environments where `c_char` is unsigned. # Version 0.16.0 (2018-06-25) @@ -649,7 +649,7 @@ # Version 0.10.1 (2018-02-05) -_Yanked_ +*Yanked* # Version 0.10.0 (2017-12-27) @@ -666,20 +666,20 @@ _Yanked_ - Added event `WindowEvent::HiDPIFactorChanged`. - Added method `MonitorId::get_hidpi_factor`. - Deprecated `get_inner_size_pixels` and `get_inner_size_points` methods of `Window` in favor of - `get_inner_size`. +`get_inner_size`. - **Breaking:** `EventsLoop` is `!Send` and `!Sync` because of platform-dependant constraints, but `Window`, `WindowId`, `DeviceId` and `MonitorId` guaranteed to be `Send`. - `MonitorId::get_position` now returns `(i32, i32)` instead of `(u32, u32)`. - Rewrite of the wayland backend to use wayland-client-0.11 - Support for dead keys on wayland for keyboard utf8 input - Monitor enumeration on Windows is now implemented using `EnumDisplayMonitors` instead of - `EnumDisplayDevices`. This changes the value returned by `MonitorId::get_name()`. +`EnumDisplayDevices`. This changes the value returned by `MonitorId::get_name()`. - On Windows added `MonitorIdExt::hmonitor` method - Impl `Clone` for `EventsLoopProxy` - `EventsLoop::get_primary_monitor()` on X11 will fallback to any available monitor if no primary is found - Support for touch event on wayland - `WindowEvent`s `MouseMoved`, `MouseEntered`, and `MouseLeft` have been renamed to - `CursorMoved`, `CursorEntered`, and `CursorLeft`. +`CursorMoved`, `CursorEntered`, and `CursorLeft`. - New `DeviceEvent`s added, `MouseMotion` and `MouseWheel`. - Send `CursorMoved` event after `CursorEntered` and `Focused` events. - Add support for `ModifiersState`, `MouseMove`, `MouseInput`, `MouseMotion` for emscripten backend. From 592742408c8203c9064da7a8b71754f29db9a4ab Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 15:35:44 +0200 Subject: [PATCH 05/10] docs: reorder the Changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10793f0929..8d54d04927 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Unreleased -- Added `Window::focus_window`to bring the window to the front and set input focus. - On Web, added support for `Event::Touch`. +- Added `Window::focus_window`to bring the window to the front and set input focus. # 0.25.0 (2021-05-15) From 5ea57602c411874405d0c3c247362f1b8c4bc921 Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 15:45:33 +0200 Subject: [PATCH 06/10] feat: remove PointerType for touch events --- src/event.rs | 15 ----------- src/platform_impl/android/mod.rs | 1 - src/platform_impl/ios/view.rs | 1 - .../linux/wayland/seat/touch/handlers.rs | 4 --- .../linux/x11/event_processor.rs | 1 - .../web/event_loop/window_target.rs | 12 +++------ src/platform_impl/web/web_sys/canvas.rs | 12 ++++----- .../web/web_sys/canvas/pointer_handler.rs | 26 +++---------------- src/platform_impl/windows/event_loop.rs | 2 -- 9 files changed, 13 insertions(+), 61 deletions(-) diff --git a/src/event.rs b/src/event.rs index 4abb6db6c1..f39ddaac50 100644 --- a/src/event.rs +++ b/src/event.rs @@ -663,21 +663,6 @@ pub struct Touch { pub force: Option, /// Unique identifier of a finger. pub id: u64, - /// Type of pointer used when touching. - /// - /// ## Platform-specific - /// - /// - Currently only available on Web targets. - pub pointer_type: Option, -} - -/// Describes the types of pointers available. -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum PointerType { - Mouse, - Pen, - Touch, - Unknown, } /// Describes the force of a touch event diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 527850ff2e..de69bc9238 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -227,7 +227,6 @@ impl EventLoop { location, id: pointer.pointer_id() as u64, force: None, - pointer_type: None, }, ), }; diff --git a/src/platform_impl/ios/view.rs b/src/platform_impl/ios/view.rs index 88d024007d..ccdec6bfbf 100644 --- a/src/platform_impl/ios/view.rs +++ b/src/platform_impl/ios/view.rs @@ -271,7 +271,6 @@ unsafe fn get_view_class(root_view_class: &'static Class) -> &'static Class { device_id: RootDeviceId(DeviceId { uiscreen }), id: touch_id, location: physical_location, - pointer_type: None, force, phase, }), diff --git a/src/platform_impl/linux/wayland/seat/touch/handlers.rs b/src/platform_impl/linux/wayland/seat/touch/handlers.rs index 9365d7c82a..8a17b39372 100644 --- a/src/platform_impl/linux/wayland/seat/touch/handlers.rs +++ b/src/platform_impl/linux/wayland/seat/touch/handlers.rs @@ -40,7 +40,6 @@ pub(super) fn handle_touch( location: position.to_physical(scale_factor), force: None, // TODO id: id as u64, - pointer_type: None, }), window_id, ); @@ -68,7 +67,6 @@ pub(super) fn handle_touch( location, force: None, // TODO id: id as u64, - pointer_type: None, }), window_id, ); @@ -94,7 +92,6 @@ pub(super) fn handle_touch( location, force: None, // TODO id: id as u64, - pointer_type: None, }), window_id, ); @@ -115,7 +112,6 @@ pub(super) fn handle_touch( location, force: None, // TODO id: touch_point.id as u64, - pointer_type: None, }), window_id, ); diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index 1fcf35da3f..65ed4d9ff1 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -1006,7 +1006,6 @@ impl EventProcessor { location, force: None, // TODO id, - pointer_type: None, }), }) } diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index 21d6cb8a60..d43f346300 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -232,7 +232,7 @@ impl WindowTarget { }); let runner = self.runner.clone(); - canvas.on_pointer_move(move |device_id, location, pointer_type| { + canvas.on_pointer_move(move |device_id, location| { runner.send_event(Event::WindowEvent { window_id: WindowId(id), event: WindowEvent::Touch(Touch { @@ -240,14 +240,13 @@ impl WindowTarget { device_id: DeviceId(device::Id(device_id)), phase: TouchPhase::Moved, force: None, // Todo - pointer_type: Some(pointer_type), location, }), }); }); let runner = self.runner.clone(); - canvas.on_pointer_down(move |device_id, location, pointer_type| { + canvas.on_pointer_down(move |device_id, location| { runner.send_event(Event::WindowEvent { window_id: WindowId(id), event: WindowEvent::Touch(Touch { @@ -255,14 +254,13 @@ impl WindowTarget { device_id: DeviceId(device::Id(device_id)), phase: TouchPhase::Started, force: None, // Todo - pointer_type: Some(pointer_type), location, }), }); }); let runner = self.runner.clone(); - canvas.on_pointer_up(move |device_id, location, pointer_type| { + canvas.on_pointer_up(move |device_id, location| { runner.send_event(Event::WindowEvent { window_id: WindowId(id), event: WindowEvent::Touch(Touch { @@ -270,14 +268,13 @@ impl WindowTarget { device_id: DeviceId(device::Id(device_id)), phase: TouchPhase::Ended, force: None, // Todo - pointer_type: Some(pointer_type), location, }), }); }); let runner = self.runner.clone(); - canvas.on_pointer_cancel(move |device_id, location, pointer_type| { + canvas.on_pointer_cancel(move |device_id, location| { runner.send_event(Event::WindowEvent { window_id: WindowId(id), event: WindowEvent::Touch(Touch { @@ -285,7 +282,6 @@ impl WindowTarget { device_id: DeviceId(device::Id(device_id)), phase: TouchPhase::Cancelled, force: None, // Todo - pointer_type: Some(pointer_type), location, }), }); diff --git a/src/platform_impl/web/web_sys/canvas.rs b/src/platform_impl/web/web_sys/canvas.rs index 08d42d3435..1c01d9d15e 100644 --- a/src/platform_impl/web/web_sys/canvas.rs +++ b/src/platform_impl/web/web_sys/canvas.rs @@ -3,9 +3,7 @@ use super::event_handle::EventListenerHandle; use super::media_query_handle::MediaQueryListHandle; use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize}; use crate::error::OsError as RootOE; -use crate::event::{ - ModifiersState, MouseButton, MouseScrollDelta, PointerType, ScanCode, VirtualKeyCode, -}; +use crate::event::{ModifiersState, MouseButton, MouseScrollDelta, ScanCode, VirtualKeyCode}; use crate::platform_impl::{OsError, PlatformSpecificWindowBuilderAttributes}; use std::cell::RefCell; @@ -251,7 +249,7 @@ impl Canvas { pub fn on_pointer_move(&mut self, handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { match &mut self.mouse_state { MouseState::HasPointerEvent(h) => h.on_pointer_move(&self.common, handler), @@ -261,7 +259,7 @@ impl Canvas { pub fn on_pointer_down(&mut self, handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { match &mut self.mouse_state { MouseState::HasPointerEvent(h) => h.on_pointer_down(&self.common, handler), @@ -271,7 +269,7 @@ impl Canvas { pub fn on_pointer_up(&mut self, handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { match &mut self.mouse_state { MouseState::HasPointerEvent(h) => h.on_pointer_up(&self.common, handler), @@ -281,7 +279,7 @@ impl Canvas { pub fn on_pointer_cancel(&mut self, handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { match &mut self.mouse_state { MouseState::HasPointerEvent(h) => h.on_pointer_cancel(&self.common, handler), diff --git a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs index 57b3b2ba31..3794eabbe3 100644 --- a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs +++ b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs @@ -1,7 +1,7 @@ use super::event; use super::EventListenerHandle; use crate::dpi::PhysicalPosition; -use crate::event::{ModifiersState, MouseButton, PointerType}; +use crate::event::{ModifiersState, MouseButton}; use web_sys::PointerEvent; @@ -137,7 +137,7 @@ impl PointerHandler { pub fn on_pointer_down(&mut self, canvas_common: &super::Common, mut handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { self.on_pointer_down = Some(canvas_common.add_event( "pointerdown", @@ -148,12 +148,6 @@ impl PointerHandler { x: event.offset_x() as f64, y: event.offset_y() as f64, }, - match event.pointer_type().as_str() { - "mouse" => PointerType::Mouse, - "pen" => PointerType::Pen, - "touch" => PointerType::Touch, - _ => PointerType::Unknown, - }, ); }, )); @@ -161,7 +155,7 @@ impl PointerHandler { pub fn on_pointer_up(&mut self, canvas_common: &super::Common, mut handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { self.on_pointer_up = Some(canvas_common.add_event( "pointerup", @@ -172,12 +166,6 @@ impl PointerHandler { x: event.offset_x() as f64, y: event.offset_y() as f64, }, - match event.pointer_type().as_str() { - "mouse" => PointerType::Mouse, - "pen" => PointerType::Pen, - "touch" => PointerType::Touch, - _ => PointerType::Unknown, - }, ); }, )); @@ -185,7 +173,7 @@ impl PointerHandler { pub fn on_pointer_cancel(&mut self, canvas_common: &super::Common, mut handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { self.on_pointer_cancel = Some(canvas_common.add_event( "pointercancel", @@ -196,12 +184,6 @@ impl PointerHandler { x: event.offset_x() as f64, y: event.offset_y() as f64, }, - match event.pointer_type().as_str() { - "mouse" => PointerType::Mouse, - "pen" => PointerType::Pen, - "touch" => PointerType::Touch, - _ => PointerType::Unknown, - }, ); }, )); diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index b1c0e884a3..795d840dd9 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -1445,7 +1445,6 @@ unsafe fn public_window_callback_inner( force: None, // WM_TOUCH doesn't support pressure information id: input.dwID as u64, device_id: DEVICE_ID, - pointer_type: None, }), }); } @@ -1585,7 +1584,6 @@ unsafe fn public_window_callback_inner( force, id: pointer_info.pointerId as u64, device_id: DEVICE_ID, - pointer_type: None, }), }); } From cfb7e67c9218bba3e2b2f37dd3ba12fb65286bfe Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 15:47:33 +0200 Subject: [PATCH 07/10] docs: change Event to WindowEvent --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d54d04927..ef236c5ed8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Unreleased -- On Web, added support for `Event::Touch`. +- On Web, added support for `WindowEvent::Touch`. - Added `Window::focus_window`to bring the window to the front and set input focus. # 0.25.0 (2021-05-15) From b88bd956dea04d9f4653dcc90199d797f940b904 Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Thu, 20 May 2021 17:15:15 +0200 Subject: [PATCH 08/10] fix: compilation on stdweb and web-sys --- src/platform_impl/web/stdweb/canvas.rs | 76 +++++++++++++++++++ .../web/web_sys/canvas/pointer_handler.rs | 12 ++- 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/platform_impl/web/stdweb/canvas.rs b/src/platform_impl/web/stdweb/canvas.rs index cea976e237..e9f653f361 100644 --- a/src/platform_impl/web/stdweb/canvas.rs +++ b/src/platform_impl/web/stdweb/canvas.rs @@ -248,6 +248,82 @@ impl Canvas { })); } + pub fn on_pointer_move(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition), + { + self.on_pointer_move = Some(canvas_common.add_event( + "pointermove", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + ); + }, + )); + } + + pub fn on_pointer_down(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition), + { + let canvas = canvas_common.raw.clone(); + self.on_pointer_down = Some(canvas_common.add_event( + "pointerdown", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + ); + canvas + .set_pointer_capture(event.pointer_id()) + .expect("Failed to set pointer capture"); + }, + )); + } + + pub fn on_pointer_up(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition), + { + self.on_pointer_up = Some(canvas_common.add_event( + "pointerup", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + ); + }, + )); + } + + pub fn on_pointer_cancel(&mut self, canvas_common: &super::Common, mut handler: F) + where + F: 'static + FnMut(i32, PhysicalPosition), + { + self.on_pointer_cancel = Some(canvas_common.add_event( + "pointercancel", + move |event: PointerEvent| { + handler( + event.pointer_id(), + PhysicalPosition { + x: event.offset_x() as f64, + y: event.offset_y() as f64, + }, + ); + }, + )); + } + pub fn on_fullscreen_change(&mut self, mut handler: F) where F: 'static + FnMut(), diff --git a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs index 3794eabbe3..052de12d26 100644 --- a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs +++ b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs @@ -113,7 +113,7 @@ impl PointerHandler { pub fn on_pointer_move(&mut self, canvas_common: &super::Common, mut handler: F) where - F: 'static + FnMut(i32, PhysicalPosition, PointerType), + F: 'static + FnMut(i32, PhysicalPosition), { self.on_pointer_move = Some(canvas_common.add_event( "pointermove", @@ -124,12 +124,6 @@ impl PointerHandler { x: event.offset_x() as f64, y: event.offset_y() as f64, }, - match event.pointer_type().as_str() { - "mouse" => PointerType::Mouse, - "pen" => PointerType::Pen, - "touch" => PointerType::Touch, - _ => PointerType::Unknown, - }, ); }, )); @@ -139,6 +133,7 @@ impl PointerHandler { where F: 'static + FnMut(i32, PhysicalPosition), { + let canvas = canvas_common.raw.clone(); self.on_pointer_down = Some(canvas_common.add_event( "pointerdown", move |event: PointerEvent| { @@ -149,6 +144,9 @@ impl PointerHandler { y: event.offset_y() as f64, }, ); + canvas + .set_pointer_capture(event.pointer_id()) + .expect("Failed to set pointer capture"); }, )); } From 1dff7c4508204affbc739aed4e4da30f40e1bf7d Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Sun, 23 May 2021 11:51:21 +0200 Subject: [PATCH 09/10] revert: remove added support for stdweb --- src/platform_impl/web/stdweb/canvas.rs | 76 -------------------------- 1 file changed, 76 deletions(-) diff --git a/src/platform_impl/web/stdweb/canvas.rs b/src/platform_impl/web/stdweb/canvas.rs index e9f653f361..cea976e237 100644 --- a/src/platform_impl/web/stdweb/canvas.rs +++ b/src/platform_impl/web/stdweb/canvas.rs @@ -248,82 +248,6 @@ impl Canvas { })); } - pub fn on_pointer_move(&mut self, canvas_common: &super::Common, mut handler: F) - where - F: 'static + FnMut(i32, PhysicalPosition), - { - self.on_pointer_move = Some(canvas_common.add_event( - "pointermove", - move |event: PointerEvent| { - handler( - event.pointer_id(), - PhysicalPosition { - x: event.offset_x() as f64, - y: event.offset_y() as f64, - }, - ); - }, - )); - } - - pub fn on_pointer_down(&mut self, canvas_common: &super::Common, mut handler: F) - where - F: 'static + FnMut(i32, PhysicalPosition), - { - let canvas = canvas_common.raw.clone(); - self.on_pointer_down = Some(canvas_common.add_event( - "pointerdown", - move |event: PointerEvent| { - handler( - event.pointer_id(), - PhysicalPosition { - x: event.offset_x() as f64, - y: event.offset_y() as f64, - }, - ); - canvas - .set_pointer_capture(event.pointer_id()) - .expect("Failed to set pointer capture"); - }, - )); - } - - pub fn on_pointer_up(&mut self, canvas_common: &super::Common, mut handler: F) - where - F: 'static + FnMut(i32, PhysicalPosition), - { - self.on_pointer_up = Some(canvas_common.add_event( - "pointerup", - move |event: PointerEvent| { - handler( - event.pointer_id(), - PhysicalPosition { - x: event.offset_x() as f64, - y: event.offset_y() as f64, - }, - ); - }, - )); - } - - pub fn on_pointer_cancel(&mut self, canvas_common: &super::Common, mut handler: F) - where - F: 'static + FnMut(i32, PhysicalPosition), - { - self.on_pointer_cancel = Some(canvas_common.add_event( - "pointercancel", - move |event: PointerEvent| { - handler( - event.pointer_id(), - PhysicalPosition { - x: event.offset_x() as f64, - y: event.offset_y() as f64, - }, - ); - }, - )); - } - pub fn on_fullscreen_change(&mut self, mut handler: F) where F: 'static + FnMut(), From 424c05f78498c57d7eba5b0183dd90b1939ecaf6 Mon Sep 17 00:00:00 2001 From: Dany Sluijk Date: Sun, 23 May 2021 12:08:04 +0200 Subject: [PATCH 10/10] feat: renamed events, added touch type guard --- .../web/web_sys/canvas/pointer_handler.rs | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs index 052de12d26..ad893e1d2e 100644 --- a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs +++ b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs @@ -12,10 +12,10 @@ pub(super) struct PointerHandler { on_cursor_move: Option>, on_pointer_press: Option>, on_pointer_release: Option>, - on_pointer_move: Option>, - on_pointer_down: Option>, - on_pointer_up: Option>, - on_pointer_cancel: Option>, + on_touch_move: Option>, + on_touch_down: Option>, + on_touch_up: Option>, + on_touch_cancel: Option>, } impl PointerHandler { @@ -26,10 +26,10 @@ impl PointerHandler { on_cursor_move: None, on_pointer_press: None, on_pointer_release: None, - on_pointer_move: None, - on_pointer_down: None, - on_pointer_up: None, - on_pointer_cancel: None, + on_touch_move: None, + on_touch_down: None, + on_touch_up: None, + on_touch_cancel: None, } } @@ -115,9 +115,13 @@ impl PointerHandler { where F: 'static + FnMut(i32, PhysicalPosition), { - self.on_pointer_move = Some(canvas_common.add_event( + self.on_touch_move = Some(canvas_common.add_event( "pointermove", move |event: PointerEvent| { + if event.pointer_type() != "touch" { + return; + } + handler( event.pointer_id(), PhysicalPosition { @@ -133,10 +137,13 @@ impl PointerHandler { where F: 'static + FnMut(i32, PhysicalPosition), { - let canvas = canvas_common.raw.clone(); - self.on_pointer_down = Some(canvas_common.add_event( + self.on_touch_down = Some(canvas_common.add_event( "pointerdown", move |event: PointerEvent| { + if event.pointer_type() != "touch" { + return; + } + handler( event.pointer_id(), PhysicalPosition { @@ -144,9 +151,6 @@ impl PointerHandler { y: event.offset_y() as f64, }, ); - canvas - .set_pointer_capture(event.pointer_id()) - .expect("Failed to set pointer capture"); }, )); } @@ -155,9 +159,13 @@ impl PointerHandler { where F: 'static + FnMut(i32, PhysicalPosition), { - self.on_pointer_up = Some(canvas_common.add_event( + self.on_touch_up = Some(canvas_common.add_event( "pointerup", move |event: PointerEvent| { + if event.pointer_type() != "touch" { + return; + } + handler( event.pointer_id(), PhysicalPosition { @@ -173,9 +181,13 @@ impl PointerHandler { where F: 'static + FnMut(i32, PhysicalPosition), { - self.on_pointer_cancel = Some(canvas_common.add_event( + self.on_touch_cancel = Some(canvas_common.add_event( "pointercancel", move |event: PointerEvent| { + if event.pointer_type() != "touch" { + return; + } + handler( event.pointer_id(), PhysicalPosition { @@ -193,9 +205,9 @@ impl PointerHandler { self.on_cursor_move = None; self.on_pointer_press = None; self.on_pointer_release = None; - self.on_pointer_move = None; - self.on_pointer_down = None; - self.on_pointer_up = None; - self.on_pointer_cancel = None; + self.on_touch_move = None; + self.on_touch_down = None; + self.on_touch_up = None; + self.on_touch_cancel = None; } }