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:
parent
c8578c9a6b
commit
48045e57da
|
|
@ -503,36 +503,6 @@ pub(crate) fn install_canvas_events(runner_ref: &WebRunner) -> Result<(), JsValu
|
||||||
modifiers,
|
modifiers,
|
||||||
});
|
});
|
||||||
|
|
||||||
let scroll_multiplier = match unit {
|
|
||||||
egui::MouseWheelUnit::Page => {
|
|
||||||
canvas_size_in_points(runner.canvas(), runner.egui_ctx()).y
|
|
||||||
}
|
|
||||||
egui::MouseWheelUnit::Line => {
|
|
||||||
#[allow(clippy::let_and_return)]
|
|
||||||
let points_per_scroll_line = 8.0; // Note that this is intentionally different from what we use in winit.
|
|
||||||
points_per_scroll_line
|
|
||||||
}
|
|
||||||
egui::MouseWheelUnit::Point => 1.0,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut delta = scroll_multiplier * delta;
|
|
||||||
|
|
||||||
// Report a zoom event in case CTRL (on Windows or Linux) or CMD (on Mac) is pressed.
|
|
||||||
// This if-statement is equivalent to how `Modifiers.command` is determined in
|
|
||||||
// `modifiers_from_kb_event()`, but we cannot directly use that fn for a [`WheelEvent`].
|
|
||||||
if event.ctrl_key() || event.meta_key() {
|
|
||||||
let factor = (delta.y / 200.0).exp();
|
|
||||||
runner.input.raw.events.push(egui::Event::Zoom(factor));
|
|
||||||
} else {
|
|
||||||
if event.shift_key() {
|
|
||||||
// Treat as horizontal scrolling.
|
|
||||||
// Note: one Mac we already get horizontal scroll events when shift is down.
|
|
||||||
delta = egui::vec2(delta.x + delta.y, 0.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
runner.input.raw.events.push(egui::Event::Scroll(delta));
|
|
||||||
}
|
|
||||||
|
|
||||||
runner.needs_repaint.repaint_asap();
|
runner.needs_repaint.repaint_asap();
|
||||||
event.stop_propagation();
|
event.stop_propagation();
|
||||||
event.prevent_default();
|
event.prevent_default();
|
||||||
|
|
|
||||||
|
|
@ -694,29 +694,6 @@ impl State {
|
||||||
modifiers,
|
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
|
|
||||||
egui::vec2(x, y) * points_per_scroll_line
|
|
||||||
}
|
|
||||||
winit::event::MouseScrollDelta::PixelDelta(delta) => {
|
|
||||||
egui::vec2(delta.x as f32, delta.y as f32) / pixels_per_point
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if self.egui_input.modifiers.ctrl || self.egui_input.modifiers.command {
|
|
||||||
// Treat as zoom instead:
|
|
||||||
let factor = (delta.y / 200.0).exp();
|
|
||||||
self.egui_input.events.push(egui::Event::Zoom(factor));
|
|
||||||
} else if self.egui_input.modifiers.shift {
|
|
||||||
// Treat as horizontal scrolling.
|
|
||||||
// Note: one Mac we already get horizontal scroll events when shift is down.
|
|
||||||
self.egui_input
|
|
||||||
.events
|
|
||||||
.push(egui::Event::Scroll(egui::vec2(delta.x + delta.y, 0.0)));
|
|
||||||
} else {
|
|
||||||
self.egui_input.events.push(egui::Event::Scroll(delta));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_keyboard_input(&mut self, event: &winit::event::KeyEvent) {
|
fn on_keyboard_input(&mut self, event: &winit::event::KeyEvent) {
|
||||||
|
|
|
||||||
|
|
@ -426,21 +426,6 @@ pub enum Event {
|
||||||
/// On touch-up first send `PointerButton{pressed: false, …}` followed by `PointerLeft`.
|
/// On touch-up first send `PointerButton{pressed: false, …}` followed by `PointerLeft`.
|
||||||
PointerGone,
|
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 scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
|
||||||
/// * `zoom = 1`: no change.
|
/// * `zoom = 1`: no change.
|
||||||
/// * `zoom < 1`: pinch together
|
/// * `zoom < 1`: pinch together
|
||||||
|
|
@ -473,16 +458,22 @@ pub enum Event {
|
||||||
force: Option<f32>,
|
force: Option<f32>,
|
||||||
},
|
},
|
||||||
|
|
||||||
/// A raw mouse wheel event as sent by the backend (minus the z coordinate),
|
/// A raw mouse wheel event as sent by the backend.
|
||||||
/// for implementing alternative custom controls.
|
///
|
||||||
/// Note that the same event can also trigger [`Self::Zoom`] and [`Self::Scroll`],
|
/// Used for scrolling.
|
||||||
/// so you probably want to handle only one of them.
|
|
||||||
MouseWheel {
|
MouseWheel {
|
||||||
/// The unit of scrolling: points, lines, or pages.
|
/// The unit of `delta`: points, lines, or pages.
|
||||||
unit: MouseWheelUnit,
|
unit: MouseWheelUnit,
|
||||||
|
|
||||||
/// The amount scrolled horizontally and vertically. The amount and direction corresponding
|
/// The direction of the vector indicates how to move the _content_ that is being viewed.
|
||||||
/// to one step of the wheel depends on the platform.
|
/// 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,
|
delta: Vec2,
|
||||||
|
|
||||||
/// The state of the modifier keys at the time of the event.
|
/// The state of the modifier keys at the time of the event.
|
||||||
|
|
|
||||||
|
|
@ -216,8 +216,37 @@ impl InputState {
|
||||||
keys_down.remove(key);
|
keys_down.remove(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Scroll(delta) => {
|
Event::MouseWheel {
|
||||||
raw_scroll_delta += *delta;
|
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) => {
|
Event::Zoom(factor) => {
|
||||||
zoom_factor_delta *= *factor;
|
zoom_factor_delta *= *factor;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue