Skip to content

Commit

Permalink
Plot widget - allow disabling scroll for x and y separately (#4051)
Browse files Browse the repository at this point in the history
To be consistent with the zoom and drag options that were added earlier.
  • Loading branch information
YgorSouza authored Feb 16, 2024
1 parent ff66fbb commit a732fc9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<T>(mut self, on: T) -> Self
where
T: Into<Vec2b>,
{
self.allow_scroll = on.into();
self
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a732fc9

Please sign in to comment.