From 4753d656e7af7d586236f6913864b156ce12ded4 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Sun, 20 Mar 2022 19:11:38 +1100 Subject: [PATCH] Add Plot::allow_scroll --- CHANGELOG.md | 1 + egui/src/widgets/plot/mod.rs | 16 +++++++++++++--- egui_demo_lib/src/apps/demo/context_menu.rs | 4 ++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6678da2cb1b5..01c51ff87d9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w ## Unreleased ### Added ⭐ +* Added `Plot::allow_scroll`, `Plot::allow_zoom` no longer affects scrolling ([#1382](https://github.com/emilk/egui/pull/1382)). * Added `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)). * Added `Frame::canvas` ([#1362](https://github.com/emilk/egui/pull/1362)). * `Context::request_repaint` will wake up UI thread, if integrations has called `Context::set_request_repaint_callback` ([#1366](https://github.com/emilk/egui/pull/1366)). diff --git a/egui/src/widgets/plot/mod.rs b/egui/src/widgets/plot/mod.rs index 3885fc1178cb..bcc5527182d0 100644 --- a/egui/src/widgets/plot/mod.rs +++ b/egui/src/widgets/plot/mod.rs @@ -164,6 +164,7 @@ pub struct Plot { center_y_axis: bool, allow_zoom: bool, allow_drag: bool, + allow_scroll: bool, min_auto_bounds: PlotBounds, margin_fraction: Vec2, allow_boxed_zoom: bool, @@ -196,6 +197,7 @@ impl Plot { center_y_axis: false, allow_zoom: true, allow_drag: true, + allow_scroll: true, min_auto_bounds: PlotBounds::NOTHING, margin_fraction: Vec2::splat(0.05), allow_boxed_zoom: true, @@ -287,6 +289,12 @@ impl Plot { self } + /// Whether to allow scrolling in the plot. Default: `true`. + pub fn allow_scroll(mut self, on: bool) -> Self { + self.allow_scroll = on; + self + } + /// Set the side margin as a fraction of the plot size. /// /// For instance, a value of `0.1` will add 10% space on both sides. @@ -434,6 +442,7 @@ impl Plot { center_x_axis, center_y_axis, allow_zoom, + allow_scroll, allow_drag, allow_boxed_zoom, boxed_zoom_pointer_button: boxed_zoom_pointer, @@ -660,8 +669,8 @@ impl Plot { } } - if allow_zoom { - if let Some(hover_pos) = response.hover_pos() { + if let Some(hover_pos) = response.hover_pos() { + if allow_zoom { let zoom_factor = if data_aspect.is_some() { Vec2::splat(ui.input().zoom_delta()) } else { @@ -671,7 +680,8 @@ impl Plot { transform.zoom(zoom_factor, hover_pos); auto_bounds = false; } - + } + if allow_scroll { let scroll_delta = ui.input().scroll_delta; if scroll_delta != Vec2::ZERO { transform.translate_bounds(-scroll_delta); diff --git a/egui_demo_lib/src/apps/demo/context_menu.rs b/egui_demo_lib/src/apps/demo/context_menu.rs index 4476c1ec021b..9bd6f2dd0869 100644 --- a/egui_demo_lib/src/apps/demo/context_menu.rs +++ b/egui_demo_lib/src/apps/demo/context_menu.rs @@ -21,6 +21,7 @@ pub struct ContextMenus { show_axes: [bool; 2], allow_drag: bool, allow_zoom: bool, + allow_scroll: bool, center_x_axis: bool, center_y_axis: bool, width: f32, @@ -34,6 +35,7 @@ impl Default for ContextMenus { show_axes: [true, true], allow_drag: true, allow_zoom: true, + allow_scroll: true, center_x_axis: false, center_y_axis: false, width: 400.0, @@ -99,6 +101,7 @@ impl super::View for ContextMenus { ui.end_row(); if ui.checkbox(&mut self.allow_drag, "Drag").changed() || ui.checkbox(&mut self.allow_zoom, "Zoom").changed() + || ui.checkbox(&mut self.allow_scroll, "Scroll").changed() { ui.close_menu(); } @@ -128,6 +131,7 @@ impl ContextMenus { .show_axes(self.show_axes) .allow_drag(self.allow_drag) .allow_zoom(self.allow_zoom) + .allow_scroll(self.allow_scroll) .center_x_axis(self.center_x_axis) .center_x_axis(self.center_y_axis) .width(self.width)