From e145cd59a281da1ef72558e63f3bd46b91f07848 Mon Sep 17 00:00:00 2001 From: Tristan117 Date: Mon, 5 Oct 2026 23:54:58 +1100 Subject: [PATCH] Add option ```scroll_zooms``` for mouse wheel to zoom instead of scroll inside a ```Scene``` container (#7891) Closes [ X] I have followed the instructions in the PR template Previously submitted as but replaced to clean up the git history. This feature allows building image viewers and similar applications using a `Scene` container where the desired mouse wheel input without a modifier is to zoom rather than scroll/pan. Alternatives considered: Swap zoom and scroll variables used in `Scene` - Would cause touch input zoom to scroll/pan instead which would be inconsistent with standard user interface interaction - This also means there is no simple way to swap zoom and vertical scroll modifiers for this use case e.g. ctrl+mousewheel pans vertically while mousewheel alone zooms. - What is implemented results in ctrl-mousewheel and mousewheel only being identical inputs. Modify `ctx.option.zoom_modifier` - Doesn't work because `Modifiers:NONE` never matches to anything (and therefore disables mouse wheel zoom globally). - Refactoring this to provide a different option per container is a large amount of work and does not seem to be a use case that is likely to be used outside of `Scene` The logic used inside `Scene` to achieve this is fairly simple and only adds one extra dependency on `input_options.scroll_zoom_speed` to ensure that the zoom speed setting change is consistent across the application. --------- Co-authored-by: Emil Ernerfeldt --- crates/egui/src/containers/scene.rs | 29 +++++++++++++++++-- crates/egui_demo_lib/src/demo/scene.rs | 7 +++++ .../tests/snapshots/demos/Scene.png | 4 +-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/egui/src/containers/scene.rs b/crates/egui/src/containers/scene.rs index 799c93d1..6e3f17db 100644 --- a/crates/egui/src/containers/scene.rs +++ b/crates/egui/src/containers/scene.rs @@ -55,6 +55,7 @@ pub struct Scene { sense: Sense, max_inner_size: Vec2, drag_pan_buttons: DragPanButtons, + scroll_zooms: bool, } /// Specifies which pointer buttons can be used to pan the scene by dragging. @@ -87,6 +88,7 @@ impl Default for Scene { sense: Sense::click_and_drag(), max_inner_size: Vec2::splat(1000.0), drag_pan_buttons: DragPanButtons::all(), + scroll_zooms: false, } } } @@ -137,6 +139,16 @@ impl Scene { self } + /// Specify whether scrolling the mousewheel without a modifier pans or + /// zooms the Scene. Touch input is not affected by this setting. + /// + /// By default, this is `false`. + #[inline] + pub fn scroll_zooms(mut self, scroll_zooms: bool) -> Self { + self.scroll_zooms = scroll_zooms; + self + } + /// `scene_rect` contains the view bounds of the inner [`Ui`]. /// /// `scene_rect` will be mutated by any panning/zooming done by the user. @@ -251,8 +263,21 @@ impl Scene { && resp.contains_pointer() { let pointer_in_scene = to_global.inverse() * mouse_pos; - let zoom_delta = ui.input(|i| i.zoom_delta()); - let pan_delta = ui.input(|i| i.smooth_scroll_delta()); + let mut zoom_delta = ui.ctx().input(|i| i.zoom_delta()); + let mut pan_delta = Vec2::ZERO; + + // If scroll_zooms is set to true the scroll input will be consumed and + // added to any zoom input. This is required to support both mouse wheel + // and touch events. + if self.scroll_zooms { + let scroll_zoom_speed = ui.ctx().options(|opt| opt.input_options.scroll_zoom_speed); + let scroll_delta = ui + .ctx() + .input(|i| i.smooth_scroll_delta.x + i.smooth_scroll_delta.y); + zoom_delta += scroll_delta * scroll_zoom_speed; + } else { + pan_delta = ui.ctx().input(|i| i.smooth_scroll_delta); + } // Most of the time we can return early. This is also important to // avoid `ui_from_scene` to change slightly due to floating point errors. diff --git a/crates/egui_demo_lib/src/demo/scene.rs b/crates/egui_demo_lib/src/demo/scene.rs index 0cafba0d..824d2377 100644 --- a/crates/egui_demo_lib/src/demo/scene.rs +++ b/crates/egui_demo_lib/src/demo/scene.rs @@ -6,6 +6,7 @@ use super::widget_gallery; pub struct SceneDemo { widget_gallery: widget_gallery::WidgetGallery, scene_rect: Rect, + scroll_zooms: bool, } impl Default for SceneDemo { @@ -13,6 +14,7 @@ impl Default for SceneDemo { Self { widget_gallery: widget_gallery::WidgetGallery::default().with_date_button(false), // disable date button so that we don't fail the snapshot test scene_rect: Rect::ZERO, // `egui::Scene` will initialize this to something valid + scroll_zooms: false, } } } @@ -40,6 +42,10 @@ impl crate::View for SceneDemo { "You can pan by scrolling, and zoom using cmd-scroll. \ Double click on the background to reset view.", ); + ui.checkbox( + &mut self.scroll_zooms, + "Change mousewheel behavior to zoom instead of scroll", + ); ui.vertical_centered(|ui| { ui.add(crate::egui_github_link_file!()); }); @@ -54,6 +60,7 @@ impl crate::View for SceneDemo { .show(ui, |ui| { let scene = Scene::new() .max_inner_size([350.0, 1000.0]) + .scroll_zooms(self.scroll_zooms) .zoom_range(0.1..=2.0); let mut reset_view = false; diff --git a/crates/egui_demo_lib/tests/snapshots/demos/Scene.png b/crates/egui_demo_lib/tests/snapshots/demos/Scene.png index 260ce0cf..579fa213 100644 --- a/crates/egui_demo_lib/tests/snapshots/demos/Scene.png +++ b/crates/egui_demo_lib/tests/snapshots/demos/Scene.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8f64ed838f4f1a3a286960a103682cfb8f04edc2a52b2459af68333ce27dc979 -size 30195 +oid sha256:0732a53091975f5e26fe668ec79d3301a7ff12620cf3fac924f1266d87b8d2d7 +size 33259