Skip to content

Commit

Permalink
Implement DeviceEvent::MouseWheel
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jun 13, 2023
1 parent fdcf7ef commit 1ae95d2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Web, fix scale factor resize suggestion always overwriting the canvas size.
- On macOS, fix crash when dropping `Window`.
- On Web, fix `DeviceEvent::MouseMotion` being triggered for each canvas instead of the whole window.
- On Web, add `DeviceEvent::Motion` support.
- On Web, add `DeviceEvent::Motion` and `DeviceEvent::MouseWheel` support.

# 0.28.6

Expand Down
24 changes: 23 additions & 1 deletion src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
rc::{Rc, Weak},
};
use wasm_bindgen::prelude::Closure;
use web_sys::PointerEvent;
use web_sys::{PointerEvent, WheelEvent};
use web_time::{Duration, Instant};

pub struct Shared<T: 'static>(Rc<Execution<T>>);
Expand All @@ -41,6 +41,8 @@ pub struct Execution<T: 'static> {
device_events: Cell<DeviceEvents>,
#[allow(clippy::type_complexity)]
on_mouse_move: RefCell<Option<EventListenerHandle<dyn FnMut(PointerEvent)>>>,
#[allow(clippy::type_complexity)]
on_wheel: RefCell<Option<EventListenerHandle<dyn FnMut(WheelEvent)>>>,
}

enum RunnerEnum<T: 'static> {
Expand Down Expand Up @@ -121,6 +123,7 @@ impl<T: 'static> Shared<T> {
unload_event_handle: RefCell::new(None),
device_events: Cell::default(),
on_mouse_move: RefCell::new(None),
on_wheel: RefCell::new(None),
}))
}

Expand Down Expand Up @@ -215,6 +218,24 @@ impl<T: 'static> Shared<T> {
}));
}),
));
let runner = self.clone();
let window = self.window().clone();
*self.0.on_wheel.borrow_mut() = Some(EventListenerHandle::new(
self.window(),
"wheel",
Closure::new(move |event: WheelEvent| {
if !runner.device_events() {
return;
}

if let Some(delta) = backend::event::mouse_scroll_delta(&window, &event) {
runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(0)),
event: DeviceEvent::MouseWheel { delta },
});
}
}),
));
}

pub(crate) fn set_on_scale_change<F>(&self, handler: F)
Expand Down Expand Up @@ -556,6 +577,7 @@ impl<T: 'static> Shared<T> {
*self.0.scale_change_detector.borrow_mut() = None;
*self.0.unload_event_handle.borrow_mut() = None;
*self.0.on_mouse_move.borrow_mut() = None;
*self.0.on_wheel.borrow_mut() = None;
// Dropping the `Runner` drops the event handler closure, which will in
// turn drop all `Window`s moved into the closure.
*self.0.runner.borrow_mut() = RunnerEnum::Destroyed;
Expand Down
13 changes: 9 additions & 4 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,21 @@ impl<T> EventLoopWindowTarget<T> {
}
});

runner.send_events(modifiers_changed.into_iter().chain(iter::once(
Event::WindowEvent {
let device_event = runner.device_events().then_some(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(pointer_id)),
event: DeviceEvent::MouseWheel { delta },
});

runner.send_events(modifiers_changed.into_iter().chain(device_event).chain(
iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseWheel {
device_id: RootDeviceId(DeviceId(pointer_id)),
delta,
phase: TouchPhase::Moved,
},
},
)));
}),
));
},
prevent_default,
);
Expand Down

0 comments on commit 1ae95d2

Please sign in to comment.