Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Fix #714: mouse wheel zooms the loupe around the pointer

PierrunoYT7f966965fc14
diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs
index c3678ae20..f76739151 100644
--- a/crates/ui-egui/src/panels/detail.rs
+++ b/crates/ui-egui/src/panels/detail.rs
@@ -153,13 +153,37 @@ pub(crate) fn pan_image(app: &mut LightcraftApp, area: Rect, img: Rect, delta: e
let _ = app.run("view.navigate", json!({"pan": pan}));
}
+/// Zoom factor of one mouse-wheel notch.
+const WHEEL_ZOOM_STEP: f32 = 1.15;
+
+/// Net vertical notches (up is positive) of unmodified line/page-unit wheel events: a real mouse wheel, not a trackpad.
+fn wheel_notches(events: &[egui::Event]) -> f32 {
+ events
+ .iter()
+ .filter_map(|e| match e {
+ egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line | egui::MouseWheelUnit::Page, delta, modifiers, .. }
+ if *modifiers == egui::Modifiers::NONE && delta.y.is_finite() =>
+ {
+ Some(delta.y)
+ }
+ _ => None,
+ })
+ .sum()
+}
+
/// Native pinch (also modifier-wheel zoom) and two-finger scroll, scoped to this image view.
pub(crate) fn navigate_gesture(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::Response, area: Rect, img: Rect, native: [usize; 2]) -> bool {
if !resp.hovered() {
return false;
}
- let (factor, delta, anchor) =
+ let (mut factor, mut delta, anchor) =
ui.input(|i| (i.zoom_delta(), i.translation_delta(), i.multi_touch().map(|t| t.center_pos).or(i.pointer.hover_pos())));
+ // A notched mouse wheel (line/page units, no modifier) zooms around the pointer; trackpads report points and still pan.
+ let notches = ui.input(|i| wheel_notches(&i.events));
+ if notches != 0.0 {
+ factor *= WHEEL_ZOOM_STEP.powf(notches);
+ delta = egui::Vec2::ZERO;
+ }
if !factor.is_finite() || factor <= 0.0 || !delta.is_finite() || (factor == 1.0 && delta == egui::Vec2::ZERO) {
return false;
}
@@ -2173,3 +2197,21 @@ mod tests {
assert_eq!(film_label(""), "");
}
}
+
+#[cfg(test)]
+mod wheel_zoom_tests {
+ use super::*;
+
+ fn wheel(unit: egui::MouseWheelUnit, y: f32, modifiers: egui::Modifiers) -> egui::Event {
+ egui::Event::MouseWheel { unit, delta: egui::vec2(0.0, y), phase: egui::TouchPhase::Move, modifiers }
+ }
+
+ #[test]
+ fn only_notched_unmodified_wheels_count() {
+ let m = egui::Modifiers::NONE;
+ assert_eq!(wheel_notches(&[wheel(egui::MouseWheelUnit::Line, 1.0, m), wheel(egui::MouseWheelUnit::Line, 2.0, m)]), 3.0);
+ assert_eq!(wheel_notches(&[wheel(egui::MouseWheelUnit::Point, 40.0, m)]), 0.0);
+ assert_eq!(wheel_notches(&[wheel(egui::MouseWheelUnit::Line, 1.0, egui::Modifiers::CTRL)]), 0.0);
+ assert_eq!(wheel_notches(&[wheel(egui::MouseWheelUnit::Line, f32::NAN, m)]), 0.0);
+ }
+}