Remove Event::Scroll and handle it in egui (#4524)

For integrations: just emit `egui::Event::MouseWheel` (like before).
egui will interpret that as zoom or pan.

On the way towards https://github.com/emilk/egui/issues/4401
This commit is contained in:
Emil Ernerfeldt
2024-05-22 21:35:15 +02:00
committed by GitHub
parent c8578c9a6b
commit 48045e57da
4 changed files with 44 additions and 77 deletions
+13 -22
View File
@@ -426,21 +426,6 @@ pub enum Event {
/// On touch-up first send `PointerButton{pressed: false, …}` followed by `PointerLeft`.
PointerGone,
/// How many points (logical pixels) the user scrolled.
///
/// The direction of the vector indicates how to move the _content_ that is being viewed.
/// So if you get positive values, the content being viewed should move to the right and down,
/// revealing new things to the left and up.
///
/// A positive X-value indicates the content is being moved right,
/// as when swiping right on a touch-screen or track-pad with natural scrolling.
///
/// A positive Y-value indicates the content is being moved down,
/// as when swiping down on a touch-screen or track-pad with natural scrolling.
///
/// Shift-scroll should result in horizontal scrolling (it is up to the integrations to do this).
Scroll(Vec2),
/// Zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
/// * `zoom = 1`: no change.
/// * `zoom < 1`: pinch together
@@ -473,16 +458,22 @@ pub enum Event {
force: Option<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.
/// A raw mouse wheel event as sent by the backend.
///
/// Used for scrolling.
MouseWheel {
/// The unit of scrolling: points, lines, or pages.
/// The unit of `delta`: 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.
/// The direction of the vector indicates how to move the _content_ that is being viewed.
/// So if you get positive values, the content being viewed should move to the right and down,
/// revealing new things to the left and up.
///
/// A positive X-value indicates the content is being moved right,
/// as when swiping right on a touch-screen or track-pad with natural scrolling.
///
/// A positive Y-value indicates the content is being moved down,
/// as when swiping down on a touch-screen or track-pad with natural scrolling.
delta: Vec2,
/// The state of the modifier keys at the time of the event.
+31 -2
View File
@@ -216,8 +216,37 @@ impl InputState {
keys_down.remove(key);
}
}
Event::Scroll(delta) => {
raw_scroll_delta += *delta;
Event::MouseWheel {
unit,
delta,
modifiers,
} => {
let delta = match unit {
MouseWheelUnit::Point => *delta,
MouseWheelUnit::Line => {
// TODO(emilk): figure out why these constants need to be different on web and on native (winit).
let is_web = cfg!(target_arch = "wasm32");
let points_per_scroll_line = if is_web {
8.0
} else {
50.0 // Scroll speed decided by consensus: https://github.com/emilk/egui/issues/461
};
points_per_scroll_line * *delta
}
MouseWheelUnit::Page => screen_rect.height() * *delta,
};
if modifiers.ctrl || modifiers.command {
// Treat as zoom instead:
let factor = (delta.y / 200.0).exp();
zoom_factor_delta *= factor;
} else if modifiers.shift {
// Treat as horizontal scrolling.
// Note: one Mac we already get horizontal scroll events when shift is down.
raw_scroll_delta.x += delta.x + delta.y;
} else {
raw_scroll_delta += delta;
}
}
Event::Zoom(factor) => {
zoom_factor_delta *= *factor;