Skip to content

Commit

Permalink
Add Plot::allow_scroll (#1382)
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored Apr 3, 2022
1 parent 95efbbc commit c203992
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 @@ -10,6 +10,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* Added `Shape::Callback` for backend-specific painting, [with an example](https://github.com/emilk/egui/blob/master/eframe/examples/custom_3d_three-d.rs) ([#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)).
* Added `Plot::allow_scroll`, `Plot::allow_zoom` no longer affects scrolling ([#1382](https://github.com/emilk/egui/pull/1382)).
* Added `Ui::push_id` ([#1374](https://github.com/emilk/egui/pull/1374)).
* Added `Frame::outer_margin`.

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 @@ -165,6 +165,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 @@ -197,6 +198,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 @@ -288,6 +290,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 @@ -435,6 +443,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 @@ -661,8 +670,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 @@ -672,7 +681,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 c203992

Please sign in to comment.