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 4753d65
Show file tree
Hide file tree
Showing 3 changed files with 18 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
4 changes: 4 additions & 0 deletions egui_demo_lib/src/apps/demo/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4753d65

Please sign in to comment.