From 40232d48ba3c80ba076ea1dc1fbb7d1c6fc80aac Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sun, 26 Jul 2020 22:16:21 +0000 Subject: [PATCH] Use `PhysicalPosition` in `PixelDelta` event This removes the `LogicalPosition` from the `PixelDelta`, since all other APIs have been switched to use `PhysicalPosition` instead. Fixes #1406. --- CHANGELOG.md | 9 +++++---- src/event.rs | 4 ++-- src/platform_impl/linux/wayland/pointer.rs | 21 +++++++++++---------- src/platform_impl/macos/view.rs | 6 +++++- src/platform_impl/web/stdweb/event.rs | 5 ++++- src/platform_impl/web/web_sys/event.rs | 5 ++++- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eba304d..dc9a6b38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ - On android added support for `run_return`. - On MacOS, Fixed fullscreen and dialog support for `run_return`. - On Windows, fix bug where we'd try to emit `MainEventsCleared` events during nested win32 event loops. +- On Windows, `set_ime_position` is now a no-op instead of a runtime crash. +- On Android, `set_fullscreen` is now a no-op instead of a runtime crash. +- On iOS and Android, `set_inner_size` is now a no-op instead of a runtime crash. +- **Breaking:** On Web, `set_cursor_position` and `set_cursor_grab` will now always return an error. +- **Breaking:** `PixelDelta` scroll events now return a `PhysicalPosition`. # 0.22.2 (2020-05-16) @@ -49,10 +54,6 @@ - on macOS, fix incorrect ReceivedCharacter events for some key combinations. - **Breaking:** Use `i32` instead of `u32` for position type in `WindowEvent::Moved`. - On macOS, a mouse motion event is now generated before every mouse click. -- On Windows, `set_ime_position` is now a no-op instead of a runtime crash. -- On Android, `set_fullscreen` is now a no-op instead of a runtime crash. -- On iOS and Android, `set_inner_size` is now a no-op instead of a runtime crash. -- **Breaking:** On Web, `set_cursor_position` and `set_cursor_grab` will now always return an error. # 0.21.0 (2020-02-04) diff --git a/src/event.rs b/src/event.rs index 47119fa8..9e9320c2 100644 --- a/src/event.rs +++ b/src/event.rs @@ -37,7 +37,7 @@ use instant::Instant; use std::path::PathBuf; use crate::{ - dpi::{LogicalPosition, PhysicalPosition, PhysicalSize}, + dpi::{PhysicalPosition, PhysicalSize}, platform_impl, window::{Theme, WindowId}, }; @@ -764,7 +764,7 @@ pub enum MouseScrollDelta { /// Scroll events are expressed as a PixelDelta if /// supported by the device (eg. a touchpad) and /// platform. - PixelDelta(LogicalPosition), + PixelDelta(PhysicalPosition), } /// Symbolic name for a keyboard key. diff --git a/src/platform_impl/linux/wayland/pointer.rs b/src/platform_impl/linux/wayland/pointer.rs index 5f93099f..4f2f4c2e 100644 --- a/src/platform_impl/linux/wayland/pointer.rs +++ b/src/platform_impl/linux/wayland/pointer.rs @@ -170,14 +170,15 @@ pub fn implement_pointer( wl_pointer::Axis::HorizontalScroll => x += value as f32, _ => unreachable!(), } + let scale_factor = surface::get_dpi_factor(&surface) as f64; + let delta = LogicalPosition::new(x as f64, y as f64) + .to_physical(scale_factor); sink.send_window_event( WindowEvent::MouseWheel { device_id: crate::event::DeviceId( crate::platform_impl::DeviceId::Wayland(DeviceId), ), - delta: MouseScrollDelta::PixelDelta( - (x as f64, y as f64).into(), - ), + delta: MouseScrollDelta::PixelDelta(delta), phase: TouchPhase::Moved, modifiers: modifiers_tracker.lock().unwrap().clone(), }, @@ -210,21 +211,21 @@ pub fn implement_pointer( device_id: crate::event::DeviceId( crate::platform_impl::DeviceId::Wayland(DeviceId), ), - delta: MouseScrollDelta::LineDelta(x as f32, y as f32), + delta: MouseScrollDelta::LineDelta(x, y), phase: axis_state, modifiers: modifiers_tracker.lock().unwrap().clone(), }, wid, ); } else if let Some((x, y)) = axis_buffer { + let scale_factor = surface::get_dpi_factor(&surface) as f64; + let delta = LogicalPosition::new(x, y).to_physical(scale_factor); sink.send_window_event( WindowEvent::MouseWheel { device_id: crate::event::DeviceId( crate::platform_impl::DeviceId::Wayland(DeviceId), ), - delta: MouseScrollDelta::PixelDelta( - (x as f64, y as f64).into(), - ), + delta: MouseScrollDelta::PixelDelta(delta), phase: axis_state, modifiers: modifiers_tracker.lock().unwrap().clone(), }, @@ -238,11 +239,11 @@ pub fn implement_pointer( axis_state = TouchPhase::Ended; } PtrEvent::AxisDiscrete { axis, discrete } => { - let (mut x, mut y) = axis_discrete_buffer.unwrap_or((0, 0)); + let (mut x, mut y) = axis_discrete_buffer.unwrap_or((0.0, 0.0)); match axis { // wayland vertical sign convention is the inverse of winit - wl_pointer::Axis::VerticalScroll => y -= discrete, - wl_pointer::Axis::HorizontalScroll => x += discrete, + wl_pointer::Axis::VerticalScroll => y -= discrete as f32, + wl_pointer::Axis::HorizontalScroll => x += discrete as f32, _ => unreachable!(), } axis_discrete_buffer = Some((x, y)); diff --git a/src/platform_impl/macos/view.rs b/src/platform_impl/macos/view.rs index d764902d..35751500 100644 --- a/src/platform_impl/macos/view.rs +++ b/src/platform_impl/macos/view.rs @@ -999,10 +999,14 @@ extern "C" fn scroll_wheel(this: &Object, _sel: Sel, event: id) { mouse_motion(this, event); unsafe { + let state_ptr: *mut c_void = *this.get_ivar("winitState"); + let state = &mut *(state_ptr as *mut ViewState); + let delta = { let (x, y) = (event.scrollingDeltaX(), event.scrollingDeltaY()); if event.hasPreciseScrollingDeltas() == YES { - MouseScrollDelta::PixelDelta((x as f64, y as f64).into()) + let delta = LogicalPosition::new(x, y).to_physical(state.get_scale_factor()); + MouseScrollDelta::PixelDelta(delta) } else { MouseScrollDelta::LineDelta(x as f32, y as f32) } diff --git a/src/platform_impl/web/stdweb/event.rs b/src/platform_impl/web/stdweb/event.rs index 8c534dc0..8464c616 100644 --- a/src/platform_impl/web/stdweb/event.rs +++ b/src/platform_impl/web/stdweb/event.rs @@ -36,7 +36,10 @@ pub fn mouse_scroll_delta(event: &MouseWheelEvent) -> Option { match event.delta_mode() { MouseWheelDeltaMode::Line => Some(MouseScrollDelta::LineDelta(x as f32, y as f32)), - MouseWheelDeltaMode::Pixel => Some(MouseScrollDelta::PixelDelta(LogicalPosition { x, y })), + MouseWheelDeltaMode::Pixel => { + let delta = LogicalPosition::new(x, y).to_physical(super::scale_factor()); + Some(MouseScrollDelta::PixelDelta(delta)) + } MouseWheelDeltaMode::Page => None, } } diff --git a/src/platform_impl/web/web_sys/event.rs b/src/platform_impl/web/web_sys/event.rs index 1c1bdba4..a0641841 100644 --- a/src/platform_impl/web/web_sys/event.rs +++ b/src/platform_impl/web/web_sys/event.rs @@ -35,7 +35,10 @@ pub fn mouse_scroll_delta(event: &WheelEvent) -> Option { match event.delta_mode() { WheelEvent::DOM_DELTA_LINE => Some(MouseScrollDelta::LineDelta(x as f32, y as f32)), - WheelEvent::DOM_DELTA_PIXEL => Some(MouseScrollDelta::PixelDelta(LogicalPosition { x, y })), + WheelEvent::DOM_DELTA_PIXEL => { + let delta = LogicalPosition::new(x, y).to_physical(super::scale_factor()); + Some(MouseScrollDelta::PixelDelta(delta)) + } _ => None, } }