Skip to content

Commit

Permalink
ScrollArea override_scroll_delta()
Browse files Browse the repository at this point in the history
  • Loading branch information
ezicheq authored and mikedilger committed Sep 29, 2023
1 parent 12b6f2c commit f4b39ec
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub struct ScrollArea {
/// end position until user manually changes position. It will become true
/// again once scroll handle makes contact with end.
stick_to_end: [bool; 2],

/// Override for scroll delta. Normally taken from frame_state
override_scroll_delta: Option<Vec2>,
}

impl ScrollArea {
Expand Down Expand Up @@ -158,9 +161,11 @@ impl ScrollArea {
scrolling_enabled: true,
drag_to_scroll: true,
stick_to_end: [false; 2],
override_scroll_delta: None,
}
}


/// The maximum width of the outer frame of the scroll area.
///
/// Use `f32::INFINITY` if you want the scroll area to expand to fit the surrounding [`Ui`] (default).
Expand Down Expand Up @@ -332,6 +337,11 @@ impl ScrollArea {
self.stick_to_end[1] = stick;
self
}

pub fn override_scroll_delta(mut self, delta: Vec2) -> Self {
self.override_scroll_delta = Some(delta);
self
}
}

struct Prepared {
Expand All @@ -357,6 +367,7 @@ struct Prepared {

scrolling_enabled: bool,
stick_to_end: [bool; 2],
override_scroll_delta: Option<Vec2>,
}

impl ScrollArea {
Expand All @@ -373,6 +384,7 @@ impl ScrollArea {
scrolling_enabled,
drag_to_scroll,
stick_to_end,
override_scroll_delta,
} = self;

let ctx = ui.ctx().clone();
Expand Down Expand Up @@ -519,6 +531,7 @@ impl ScrollArea {
viewport,
scrolling_enabled,
stick_to_end,
override_scroll_delta,
}
}

Expand Down Expand Up @@ -629,6 +642,7 @@ impl Prepared {
viewport: _,
scrolling_enabled,
stick_to_end,
override_scroll_delta,
} = self;

let content_size = content_ui.min_size();
Expand Down Expand Up @@ -702,7 +716,10 @@ impl Prepared {
if scrolling_enabled && ui.rect_contains_pointer(outer_rect) {
for d in 0..2 {
if has_bar[d] {
let scroll_delta = ui.ctx().frame_state(|fs| fs.scroll_delta);
let scroll_delta = match override_scroll_delta {
Some(delta) => delta,
None => ui.ctx().frame_state(|fs| fs.scroll_delta)
};

let scrolling_up = state.offset[d] > 0.0 && scroll_delta[d] > 0.0;
let scrolling_down = state.offset[d] < max_offset[d] && scroll_delta[d] < 0.0;
Expand Down

0 comments on commit f4b39ec

Please sign in to comment.