Skip to content

Commit

Permalink
ScrollArea override_scroll_delta()
Browse files Browse the repository at this point in the history
  • Loading branch information
ezicheq committed Feb 17, 2023
1 parent 7215fdf commit a4f41bb
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 @@ -106,6 +106,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 @@ -145,9 +148,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 @@ -318,6 +323,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 @@ -337,6 +347,7 @@ struct Prepared {
viewport: Rect,
scrolling_enabled: bool,
stick_to_end: [bool; 2],
override_scroll_delta: Option<Vec2>,
}

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

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

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

let content_size = content_ui.min_size();
Expand Down Expand Up @@ -691,7 +705,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 a4f41bb

Please sign in to comment.