Add option ``scroll_zooms` for mouse wheel to zoom instead of scroll inside a `Scene`` container (#7891)

Closes <https://github.com/emilk/egui/issues/7650>
[ X] I have followed the instructions in the PR template

Previously submitted as <https://github.com/emilk/egui/pull/7666> 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 <emil.ernerfeldt@gmail.com>
This commit is contained in:
Tristan117andEmil Ernerfeldt authored and GitHub committed 2026-10-05 12:54:58 +00:00
1 parent 2078a31e09
commit e145cd59a2
3 files changed
+36 -4

No files matched your search

+27 -2
View File
@@ -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.
+7
View File
@@ -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;
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8f64ed838f4f1a3a286960a103682cfb8f04edc2a52b2459af68333ce27dc979
size 30195
oid sha256:0732a53091975f5e26fe668ec79d3301a7ff12620cf3fac924f1266d87b8d2d7
size 33259