Plot widget - allow disabling scroll for x and y separately (#4051)

To be consistent with the zoom and drag options that were added earlier.
This commit is contained in:
YgorSouza 2024-02-16 09:23:08 +01:00 committed by GitHub
parent 62e80c7729
commit 34e8af87d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 6 deletions

View File

@ -878,6 +878,7 @@ struct ChartsDemo {
vertical: bool, vertical: bool,
allow_zoom: Vec2b, allow_zoom: Vec2b,
allow_drag: Vec2b, allow_drag: Vec2b,
allow_scroll: Vec2b,
} }
impl Default for ChartsDemo { impl Default for ChartsDemo {
@ -887,6 +888,7 @@ impl Default for ChartsDemo {
chart: Chart::default(), chart: Chart::default(),
allow_zoom: true.into(), allow_zoom: true.into(),
allow_drag: true.into(), allow_drag: true.into(),
allow_scroll: true.into(),
} }
} }
} }
@ -921,6 +923,11 @@ impl ChartsDemo {
ui.checkbox(&mut self.allow_drag.x, "X"); ui.checkbox(&mut self.allow_drag.x, "X");
ui.checkbox(&mut self.allow_drag.y, "Y"); ui.checkbox(&mut self.allow_drag.y, "Y");
}); });
ui.horizontal(|ui| {
ui.label("Allow scroll:");
ui.checkbox(&mut self.allow_scroll.x, "X");
ui.checkbox(&mut self.allow_scroll.y, "Y");
});
}); });
}); });
}); });
@ -958,6 +965,7 @@ impl ChartsDemo {
.y_axis_width(3) .y_axis_width(3)
.allow_zoom(self.allow_zoom) .allow_zoom(self.allow_zoom)
.allow_drag(self.allow_drag) .allow_drag(self.allow_drag)
.allow_scroll(self.allow_scroll)
.show(ui, |plot_ui| plot_ui.bar_chart(chart)) .show(ui, |plot_ui| plot_ui.bar_chart(chart))
.response .response
} }

View File

@ -148,7 +148,7 @@ pub struct Plot {
center_axis: Vec2b, center_axis: Vec2b,
allow_zoom: Vec2b, allow_zoom: Vec2b,
allow_drag: Vec2b, allow_drag: Vec2b,
allow_scroll: bool, allow_scroll: Vec2b,
allow_double_click_reset: bool, allow_double_click_reset: bool,
allow_boxed_zoom: bool, allow_boxed_zoom: bool,
default_auto_bounds: Vec2b, default_auto_bounds: Vec2b,
@ -195,7 +195,7 @@ impl Plot {
center_axis: false.into(), center_axis: false.into(),
allow_zoom: true.into(), allow_zoom: true.into(),
allow_drag: true.into(), allow_drag: true.into(),
allow_scroll: true, allow_scroll: true.into(),
allow_double_click_reset: true, allow_double_click_reset: true,
allow_boxed_zoom: true, allow_boxed_zoom: true,
default_auto_bounds: true.into(), default_auto_bounds: true.into(),
@ -329,8 +329,11 @@ impl Plot {
/// Whether to allow scrolling in the plot. Default: `true`. /// Whether to allow scrolling in the plot. Default: `true`.
#[inline] #[inline]
pub fn allow_scroll(mut self, on: bool) -> Self { pub fn allow_scroll<T>(mut self, on: T) -> Self
self.allow_scroll = on; where
T: Into<Vec2b>,
{
self.allow_scroll = on.into();
self self
} }
@ -1091,8 +1094,14 @@ impl Plot {
mem.auto_bounds = !allow_zoom; mem.auto_bounds = !allow_zoom;
} }
} }
if allow_scroll { if allow_scroll.any() {
let scroll_delta = ui.input(|i| i.smooth_scroll_delta); 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 { if scroll_delta != Vec2::ZERO {
mem.transform.translate_bounds(-scroll_delta); mem.transform.translate_bounds(-scroll_delta);
mem.auto_bounds = false.into(); mem.auto_bounds = false.into();