Skip to content

Commit 067cf52

Browse files
committed
Add Plot::allow_scroll
1 parent 5dff1e4 commit 067cf52

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
1010
* 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)).
1111
* Added `Frame::canvas` ([#1362](https://github.com/emilk/egui/pull/1362)).
1212
* `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)).
13+
* Added `Plot::allow_scroll`, `Plot::allow_zoom` no longer affects scrolling ([#1382](https://github.com/emilk/egui/pull/1382)).
1314
* Added `Ui::push_id` ([#1374](https://github.com/emilk/egui/pull/1374)).
1415
* Added `Frame::outer_margin`.
1516

egui/src/widgets/plot/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ pub struct Plot {
165165
center_y_axis: bool,
166166
allow_zoom: bool,
167167
allow_drag: bool,
168+
allow_scroll: bool,
168169
min_auto_bounds: PlotBounds,
169170
margin_fraction: Vec2,
170171
allow_boxed_zoom: bool,
@@ -197,6 +198,7 @@ impl Plot {
197198
center_y_axis: false,
198199
allow_zoom: true,
199200
allow_drag: true,
201+
allow_scroll: true,
200202
min_auto_bounds: PlotBounds::NOTHING,
201203
margin_fraction: Vec2::splat(0.05),
202204
allow_boxed_zoom: true,
@@ -288,6 +290,12 @@ impl Plot {
288290
self
289291
}
290292

293+
/// Whether to allow scrolling in the plot. Default: `true`.
294+
pub fn allow_scroll(mut self, on: bool) -> Self {
295+
self.allow_scroll = on;
296+
self
297+
}
298+
291299
/// Set the side margin as a fraction of the plot size.
292300
///
293301
/// For instance, a value of `0.1` will add 10% space on both sides.
@@ -435,6 +443,7 @@ impl Plot {
435443
center_x_axis,
436444
center_y_axis,
437445
allow_zoom,
446+
allow_scroll,
438447
allow_drag,
439448
allow_boxed_zoom,
440449
boxed_zoom_pointer_button: boxed_zoom_pointer,
@@ -661,8 +670,8 @@ impl Plot {
661670
}
662671
}
663672

664-
if allow_zoom {
665-
if let Some(hover_pos) = response.hover_pos() {
673+
if let Some(hover_pos) = response.hover_pos() {
674+
if allow_zoom {
666675
let zoom_factor = if data_aspect.is_some() {
667676
Vec2::splat(ui.input().zoom_delta())
668677
} else {
@@ -672,7 +681,8 @@ impl Plot {
672681
transform.zoom(zoom_factor, hover_pos);
673682
auto_bounds = false;
674683
}
675-
684+
}
685+
if allow_scroll {
676686
let scroll_delta = ui.input().scroll_delta;
677687
if scroll_delta != Vec2::ZERO {
678688
transform.translate_bounds(-scroll_delta);

egui_demo_lib/src/apps/demo/context_menu.rs

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct ContextMenus {
2121
show_axes: [bool; 2],
2222
allow_drag: bool,
2323
allow_zoom: bool,
24+
allow_scroll: bool,
2425
center_x_axis: bool,
2526
center_y_axis: bool,
2627
width: f32,
@@ -34,6 +35,7 @@ impl Default for ContextMenus {
3435
show_axes: [true, true],
3536
allow_drag: true,
3637
allow_zoom: true,
38+
allow_scroll: true,
3739
center_x_axis: false,
3840
center_y_axis: false,
3941
width: 400.0,
@@ -99,6 +101,7 @@ impl super::View for ContextMenus {
99101
ui.end_row();
100102
if ui.checkbox(&mut self.allow_drag, "Drag").changed()
101103
|| ui.checkbox(&mut self.allow_zoom, "Zoom").changed()
104+
|| ui.checkbox(&mut self.allow_scroll, "Scroll").changed()
102105
{
103106
ui.close_menu();
104107
}
@@ -128,6 +131,7 @@ impl ContextMenus {
128131
.show_axes(self.show_axes)
129132
.allow_drag(self.allow_drag)
130133
.allow_zoom(self.allow_zoom)
134+
.allow_scroll(self.allow_scroll)
131135
.center_x_axis(self.center_x_axis)
132136
.center_x_axis(self.center_y_axis)
133137
.width(self.width)

0 commit comments

Comments
 (0)