Skip to content

Commit

Permalink
Add Plot::allow_scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Mar 20, 2022
1 parent 465c961 commit e1d04ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
16 changes: 13 additions & 3 deletions egui/src/widgets/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit e1d04ae

Please sign in to comment.