Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add raw mouse wheel event #2782

Merged
merged 6 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,22 @@ pub fn install_canvas_events(runner_container: &mut AppRunnerContainer) -> Resul
&canvas,
"wheel",
|event: web_sys::WheelEvent, mut runner_lock| {
{
let unit = match event.delta_mode() {
web_sys::WheelEvent::DOM_DELTA_PIXEL => egui::MouseWheelUnit::Point,
web_sys::WheelEvent::DOM_DELTA_LINE => egui::MouseWheelUnit::Line,
web_sys::WheelEvent::DOM_DELTA_PAGE => egui::MouseWheelUnit::Page,
3u32..=u32::MAX => return,
YgorSouza marked this conversation as resolved.
Show resolved Hide resolved
};
let delta = egui::vec2(event.delta_x() as f32, event.delta_y() as f32);
YgorSouza marked this conversation as resolved.
Show resolved Hide resolved
let modifiers = runner_lock.input.raw.modifiers;

runner_lock.input.raw.events.push(egui::Event::MouseWheel {
unit,
delta,
modifiers,
});
}
let scroll_multiplier = match event.delta_mode() {
YgorSouza marked this conversation as resolved.
Show resolved Hide resolved
web_sys::WheelEvent::DOM_DELTA_PAGE => {
canvas_size_in_points(runner_lock.canvas_id()).y
Expand Down
20 changes: 20 additions & 0 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,26 @@ impl State {
}

fn on_mouse_wheel(&mut self, delta: winit::event::MouseScrollDelta) {
{
let (unit, delta) = match delta {
winit::event::MouseScrollDelta::LineDelta(x, y) => {
(egui::MouseWheelUnit::Line, egui::vec2(x, y))
}
winit::event::MouseScrollDelta::PixelDelta(winit::dpi::PhysicalPosition {
x,
y,
}) => (
egui::MouseWheelUnit::Point,
egui::vec2(x as f32, y as f32) / self.pixels_per_point(),
),
};
let modifiers = self.egui_input.modifiers;
self.egui_input.events.push(egui::Event::MouseWheel {
unit,
delta,
modifiers,
});
}
let delta = match delta {
winit::event::MouseScrollDelta::LineDelta(x, y) => {
let points_per_scroll_line = 50.0; // Scroll speed decided by consensus: https://github.com/emilk/egui/issues/461
Expand Down
30 changes: 30 additions & 0 deletions crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ pub enum Event {
force: f32,
},

/// A raw mouse wheel event as sent by the backend (minus the z coordinate),
/// for implementing alternative custom controls.
/// Note that the same event can also trigger [`Self::Zoom`] and [`Self::Scroll`],
/// so you probably want to handle only one of them.
MouseWheel {
/// The unit of scrolling: points, lines, or pages.
unit: MouseWheelUnit,

/// The amount scrolled horizontally and vertically. The amount and direction corresponding
/// to one step of the wheel depends on the platform.
delta: Vec2,

/// The state of the modifier keys at the time of the event.
modifiers: Modifiers,
},

/// An assistive technology (e.g. screen reader) requested an action.
#[cfg(feature = "accesskit")]
AccessKitActionRequest(accesskit::ActionRequest),
Expand Down Expand Up @@ -891,6 +907,20 @@ pub enum TouchPhase {
Cancel,
}

/// The unit associated with the numeric value of a mouse wheel event
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum MouseWheelUnit {
/// Number of ui points (logical pixels)
Point,

/// Number of lines
Line,

/// Number of pages
Page,
}

impl From<u64> for TouchId {
fn from(id: u64) -> Self {
Self(id)
Expand Down