From a732fc99bcb283527ba3048ef02add440d36acda Mon Sep 17 00:00:00 2001 From: YgorSouza <43298013+YgorSouza@users.noreply.github.com> Date: Fri, 16 Feb 2024 09:23:08 +0100 Subject: [PATCH] Plot widget - allow disabling scroll for x and y separately (#4051) To be consistent with the zoom and drag options that were added earlier. --- crates/egui_plot/src/lib.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index 75314cb..0796edc 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -148,7 +148,7 @@ pub struct Plot { center_axis: Vec2b, allow_zoom: Vec2b, allow_drag: Vec2b, - allow_scroll: bool, + allow_scroll: Vec2b, allow_double_click_reset: bool, allow_boxed_zoom: bool, default_auto_bounds: Vec2b, @@ -195,7 +195,7 @@ impl Plot { center_axis: false.into(), allow_zoom: true.into(), allow_drag: true.into(), - allow_scroll: true, + allow_scroll: true.into(), allow_double_click_reset: true, allow_boxed_zoom: true, default_auto_bounds: true.into(), @@ -329,8 +329,11 @@ impl Plot { /// Whether to allow scrolling in the plot. Default: `true`. #[inline] - pub fn allow_scroll(mut self, on: bool) -> Self { - self.allow_scroll = on; + pub fn allow_scroll(mut self, on: T) -> Self + where + T: Into, + { + self.allow_scroll = on.into(); self } @@ -1091,8 +1094,14 @@ impl Plot { mem.auto_bounds = !allow_zoom; } } - if allow_scroll { - let scroll_delta = ui.input(|i| i.smooth_scroll_delta); + if allow_scroll.any() { + let mut scroll_delta = ui.input(|i| i.smooth_scroll_delta); + if !allow_scroll.x { + scroll_delta.x = 0.0; + } + if !allow_scroll.y { + scroll_delta.y = 0.0; + } if scroll_delta != Vec2::ZERO { mem.transform.translate_bounds(-scroll_delta); mem.auto_bounds = false.into();