Skip to content

Commit

Permalink
use the actual content size for interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
s-nie committed Jun 5, 2024
1 parent a287921 commit 9cca40e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ struct Prepared {
scrolling_enabled: bool,
stick_to_end: Vec2b,
animated: bool,

/// Id of the drag area. We store this until we know the size of the content to interact with.
drag_area_id: Id,
}

impl ScrollArea {
Expand Down Expand Up @@ -585,15 +588,18 @@ impl ScrollArea {
let viewport = Rect::from_min_size(Pos2::ZERO + state.offset, inner_size);
let dt = ui.input(|i| i.stable_dt).at_most(0.1);

let drag_area_id = id.with("area");

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.
let content_response = ui.interact(inner_rect, id.with("area"), Sense::drag());
// This response is not available on the first frame.
let content_response_option = ui.ctx().read_response(drag_area_id);

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 @@ -671,6 +677,7 @@ impl ScrollArea {
scrolling_enabled,
stick_to_end,
animated,
drag_area_id,
}
}

Expand Down Expand Up @@ -783,6 +790,7 @@ impl Prepared {
scrolling_enabled,
stick_to_end,
animated,
drag_area_id,
} = self;

let content_size = content_ui.min_size();
Expand Down Expand Up @@ -874,6 +882,10 @@ impl Prepared {
Rect::from_min_size(inner_rect.min, inner_size)
};

// At this point we know the actual size of the area we can interact with, so we do the interaction
// here. The result is ignored and read at the start of the frame.
let _ = ui.interact(inner_rect, drag_area_id, Sense::drag());

let outer_rect = Rect::from_min_size(inner_rect.min, inner_rect.size() + current_bar_use);

let content_is_too_large = Vec2b::new(
Expand Down

0 comments on commit 9cca40e

Please sign in to comment.