Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScrollArea: Prevent drag interaction outside the area #4611

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub struct State {
/// and remains that way until the user moves the scroll_handle. Once unstuck (false)
/// it remains false until the scroll touches the end position, which reenables stickiness.
scroll_stuck_to_end: Vec2b,

/// Area that can be dragged. This is the size of the content from the last frame.
interact_rect: Option<Rect>,
}

impl Default for State {
Expand All @@ -52,6 +55,7 @@ impl Default for State {
vel: Vec2::ZERO,
scroll_start_offset_from_top_left: [None; 2],
scroll_stuck_to_end: Vec2b::TRUE,
interact_rect: None,
}
}
}
Expand Down Expand Up @@ -591,9 +595,11 @@ impl ScrollArea {
// 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.
let content_response = ui.interact(inner_rect, id.with("area"), Sense::drag());
let content_response_option = state
.interact_rect
.map(|rect| ui.interact(rect, id.with("area"), Sense::drag()));

if content_response.dragged() {
if content_response_option.map(|response| response.dragged()) == Some(true) {
for d in 0..2 {
if scroll_enabled[d] {
ui.input(|input| {
Expand Down Expand Up @@ -1199,6 +1205,7 @@ impl Prepared {

state.show_scroll = show_scroll_this_frame;
state.content_is_too_large = content_is_too_large;
state.interact_rect = Some(inner_rect);

state.store(ui.ctx(), id);

Expand Down
Loading