From 0e2656b77cf5281943d753b7e228129868cb9ce1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 20 Dec 2022 11:09:53 +0100 Subject: [PATCH] Add ScrollArea::drag_to_scroll --- CHANGELOG.md | 1 + crates/egui/src/containers/scroll_area.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84b76961..6e678135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG * Add `Slider::drag_value_speed`, which lets you ask for finer precision when dragging the slider value rather than the actual slider. * Add `Memory::any_popup_open`, which returns true if any popup is currently open ([#2464](https://github.com/emilk/egui/pull/2464)). * Add `Plot::clamp_grid` to only show grid where there is data ([#2480](https://github.com/emilk/egui/pull/2480)). +* Add `ScrollArea::drag_to_scroll` if you want to turn off that feature. ### Changed 🔧 * Improved plot grid appearance ([#2412](https://github.com/emilk/egui/pull/2412)). diff --git a/crates/egui/src/containers/scroll_area.rs b/crates/egui/src/containers/scroll_area.rs index 345b5972..7af37e40 100644 --- a/crates/egui/src/containers/scroll_area.rs +++ b/crates/egui/src/containers/scroll_area.rs @@ -97,8 +97,10 @@ pub struct ScrollArea { id_source: Option, offset_x: Option, offset_y: Option, + /// If false, we ignore scroll events. scrolling_enabled: bool, + drag_to_scroll: bool, /// If true for vertical or horizontal the scroll wheel will stick to the /// end position until user manually changes position. It will become true @@ -141,6 +143,7 @@ impl ScrollArea { offset_x: None, offset_y: None, scrolling_enabled: true, + drag_to_scroll: true, stick_to_end: [false; 2], } } @@ -267,6 +270,18 @@ impl ScrollArea { self } + /// Can the user drag the scroll area to scroll? + /// + /// This is useful for touch screens. + /// + /// If `true`, the [`ScrollArea`] will sense drags. + /// + /// Default: `true`. + pub fn drag_to_scroll(mut self, drag_to_scroll: bool) -> Self { + self.drag_to_scroll = drag_to_scroll; + self + } + /// For each axis, should the containing area shrink if the content is small? /// /// * If `true`, egui will add blank space outside the scroll area. @@ -336,6 +351,7 @@ impl ScrollArea { offset_x, offset_y, scrolling_enabled, + drag_to_scroll, stick_to_end, } = self; @@ -422,7 +438,9 @@ impl ScrollArea { let viewport = Rect::from_min_size(Pos2::ZERO + state.offset, inner_size); - if scrolling_enabled && (state.content_is_too_large[0] || state.content_is_too_large[1]) { + if (scrolling_enabled && drag_to_scroll) + && (state.content_is_too_large[0] || state.content_is_too_large[1]) + { // Drag contents to scroll (for touch screens mostly). // We must do this BEFORE adding content to the `ScrollArea`, // or we will steal input from the widgets we contain.