Skip to content

Commit

Permalink
Web: Remove some unnecessary compatibility code
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jun 28, 2024
1 parent 82d9bbe commit bbf16bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ changelog entry.
to send specific data to be processed on the main thread.
- Changed `EventLoopProxy::send_event` to `EventLoopProxy::wake_up`, it now
only wakes up the loop.
- On Web, slightly improve accuracy of `DeviceEvent::MouseMotion`.

### Removed

Expand Down
21 changes: 12 additions & 9 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Shared {
));

let runner = self.clone();
let window = self.window().clone();
let mut delta = backend::event::MouseDelta::new();
*self.0.on_mouse_move.borrow_mut() = Some(EventListenerHandle::new(
self.window().clone(),
"pointermove",
Expand Down Expand Up @@ -273,23 +273,26 @@ impl Shared {
}

// pointer move event
let mut delta = backend::event::MouseDelta::init(&window, &event);
runner.send_events(backend::event::pointer_move_event(event).flat_map(|event| {
let delta = delta.delta(&event).to_physical(backend::scale_factor(&window));
let delta = delta.delta(&event);

if delta.x == 0 && delta.y == 0 {
return None.into_iter().chain(None).chain(None);
}

let x_motion = (delta.x != 0.0).then_some(Event::DeviceEvent {
let x_motion = (delta.x != 0).then_some(Event::DeviceEvent {
device_id,
event: DeviceEvent::Motion { axis: 0, value: delta.x },
event: DeviceEvent::Motion { axis: 0, value: delta.x.into() },
});

let y_motion = (delta.y != 0.0).then_some(Event::DeviceEvent {
let y_motion = (delta.y != 0).then_some(Event::DeviceEvent {
device_id,
event: DeviceEvent::Motion { axis: 1, value: delta.y },
event: DeviceEvent::Motion { axis: 1, value: delta.y.into() },
});

x_motion.into_iter().chain(y_motion).chain(iter::once(Event::DeviceEvent {
x_motion.into_iter().chain(y_motion).chain(Some(Event::DeviceEvent {
device_id,
event: DeviceEvent::MouseMotion { delta: (delta.x, delta.y) },
event: DeviceEvent::MouseMotion { delta: (delta.x.into(), delta.y.into()) },
}))
}));
}),
Expand Down
68 changes: 13 additions & 55 deletions src/platform_impl/web/web_sys/event.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::dpi::LogicalPosition;
use crate::event::{MouseButton, MouseScrollDelta};
use crate::keyboard::{Key, KeyLocation, ModifiersState, NamedKey, PhysicalKey};

use dpi::{LogicalPosition, PhysicalPosition};
use smol_str::SmolStr;
use std::cell::OnceCell;
use wasm_bindgen::prelude::wasm_bindgen;
Expand Down Expand Up @@ -95,42 +95,23 @@ pub fn mouse_position(event: &MouseEvent) -> LogicalPosition<f64> {
LogicalPosition { x: event.offset_x(), y: event.offset_y() }
}

// TODO: Remove this when Firefox supports correct movement values in coalesced events.
// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1753724>.
pub struct MouseDelta(Option<MouseDeltaInner>);

pub struct MouseDeltaInner {
old_position: LogicalPosition<f64>,
old_delta: LogicalPosition<f64>,
}
pub struct MouseDelta(Option<PhysicalPosition<i32>>);

impl MouseDelta {
pub fn init(window: &web_sys::Window, event: &PointerEvent) -> Self {
// Firefox has wrong movement values in coalesced events, we will detect that by checking
// for `pointerrawupdate` support. Presumably an implementation of `pointerrawupdate`
// should require correct movement values, otherwise uncoalesced events might be broken as
// well.
Self((!has_pointer_raw_support(window) && has_coalesced_events_support(event)).then(|| {
MouseDeltaInner {
old_position: mouse_position(event),
old_delta: LogicalPosition {
x: event.movement_x() as f64,
y: event.movement_y() as f64,
},
}
}))
pub fn new() -> Self {
Self(None)
}

pub fn delta(&mut self, event: &MouseEvent) -> LogicalPosition<f64> {
if let Some(inner) = &mut self.0 {
let new_position = mouse_position(event);
let x = new_position.x - inner.old_position.x + inner.old_delta.x;
let y = new_position.y - inner.old_position.y + inner.old_delta.y;
inner.old_position = new_position;
inner.old_delta = LogicalPosition::new(0., 0.);
LogicalPosition::new(x, y)
pub fn delta(&mut self, event: &MouseEvent) -> PhysicalPosition<i32> {
let new = PhysicalPosition::new(event.screen_x(), event.screen_y());

if let Some(old) = self.0 {
let delta = PhysicalPosition::new(new.x - old.x, new.y - old.y);
self.0 = Some(new);
delta
} else {
LogicalPosition { x: event.movement_x() as f64, y: event.movement_y() as f64 }
self.0 = Some(new);
PhysicalPosition::default()
}
}
}
Expand Down Expand Up @@ -238,29 +219,6 @@ pub fn pointer_move_event(event: PointerEvent) -> impl Iterator<Item = PointerEv
}
}

// TODO: Remove when all browsers implement it correctly.
// See <https://github.com/rust-windowing/winit/issues/2875>.
pub fn has_pointer_raw_support(window: &web_sys::Window) -> bool {
thread_local! {
static POINTER_RAW_SUPPORT: OnceCell<bool> = const { OnceCell::new() };
}

POINTER_RAW_SUPPORT.with(|support| {
*support.get_or_init(|| {
#[wasm_bindgen]
extern "C" {
type PointerRawSupport;

#[wasm_bindgen(method, getter, js_name = onpointerrawupdate)]
fn has_on_pointerrawupdate(this: &PointerRawSupport) -> JsValue;
}

let support: &PointerRawSupport = window.unchecked_ref();
!support.has_on_pointerrawupdate().is_undefined()
})
})
}

// TODO: Remove when Safari supports `getCoalescedEvents`.
// See <https://bugs.webkit.org/show_bug.cgi?id=210454>.
pub fn has_coalesced_events_support(event: &PointerEvent) -> bool {
Expand Down

0 comments on commit bbf16bc

Please sign in to comment.