Skip to content

Commit

Permalink
Fix scroll_with_delta only scrolling if the ScrollArea is focussed
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Apr 1, 2024
1 parent 3ee4890 commit 46d2303
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ impl Prepared {

let max_offset = content_size - inner_rect.size();
let is_hovering_outer_rect = ui.rect_contains_pointer(outer_rect);
if scrolling_enabled && is_hovering_outer_rect {
let force_current_scroll_area = ui.input(|i| i.force_current_scroll_area);
if scrolling_enabled && is_hovering_outer_rect || force_current_scroll_area {
let always_scroll_enabled_direction = ui.style().always_scroll_the_only_direction
&& scroll_enabled[0] != scroll_enabled[1];
for d in 0..2 {
Expand Down Expand Up @@ -879,6 +880,7 @@ impl Prepared {
} else {
input.smooth_scroll_delta[d] = 0.0;
}
input.force_current_scroll_area = false;
});

state.scroll_stuck_to_end[d] = false;
Expand Down
10 changes: 10 additions & 0 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ pub struct InputState {
/// at the end of the frame this will be zero if a scroll-area consumed the delta.
pub smooth_scroll_delta: Vec2,

/// Force the current scroll area to consume the [Self::smooth_scroll_delta], independent of the focus / mouse position.
/// This is used when using [crate::Ui::scroll_with_delta] to scroll the current scroll area.
pub(crate) force_current_scroll_area: bool,

/// Zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
///
/// * `zoom = 1`: no change.
Expand Down Expand Up @@ -154,6 +158,7 @@ impl Default for InputState {
unprocessed_scroll_delta: Vec2::ZERO,
raw_scroll_delta: Vec2::ZERO,
smooth_scroll_delta: Vec2::ZERO,
force_current_scroll_area: false,
zoom_factor_delta: 1.0,
screen_rect: Rect::from_min_size(Default::default(), vec2(10_000.0, 10_000.0)),
pixels_per_point: 1.0,
Expand Down Expand Up @@ -255,6 +260,7 @@ impl InputState {
unprocessed_scroll_delta,
raw_scroll_delta,
smooth_scroll_delta,
force_current_scroll_area: false,
zoom_factor_delta,
screen_rect,
pixels_per_point,
Expand Down Expand Up @@ -1113,6 +1119,7 @@ impl InputState {
unprocessed_scroll_delta,
raw_scroll_delta,
smooth_scroll_delta,
force_current_scroll_area,

zoom_factor_delta,
screen_rect,
Expand Down Expand Up @@ -1157,6 +1164,9 @@ impl InputState {
ui.label(format!(
"smooth_scroll_delta: {smooth_scroll_delta:?} points"
));
ui.label(format!(
"force_current_scroll_area: {force_current_scroll_area}"
));
ui.label(format!("zoom_factor_delta: {zoom_factor_delta:4.2}x"));
ui.label(format!("screen_rect: {screen_rect:?} points"));
ui.label(format!(
Expand Down
6 changes: 4 additions & 2 deletions crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,10 @@ impl Ui {
/// # });
/// ```
pub fn scroll_with_delta(&self, delta: Vec2) {
self.ctx()
.input_mut(|input| input.smooth_scroll_delta += delta);
self.ctx().input_mut(|input| {
input.smooth_scroll_delta += delta;
input.force_current_scroll_area = true;
});
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/egui_demo_lib/src/demo/scrolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ impl super::View for ScrollTo {
let mut go_to_scroll_offset = false;
let mut scroll_top = false;
let mut scroll_bottom = false;
let mut scroll_delta = None;

ui.horizontal(|ui| {
ui.label("Scroll to a specific item index:");
Expand Down Expand Up @@ -292,6 +293,9 @@ impl super::View for ScrollTo {
ui.horizontal(|ui| {
scroll_top |= ui.button("Scroll to top").clicked();
scroll_bottom |= ui.button("Scroll to bottom").clicked();
if ui.button("Scroll down by 64px").clicked() {
scroll_delta = Some(Vec2::new(0.0, -64.0));
}
});

let mut scroll_area = ScrollArea::vertical().max_height(200.0).auto_shrink(false);
Expand All @@ -305,6 +309,10 @@ impl super::View for ScrollTo {
if scroll_top {
ui.scroll_to_cursor(Some(Align::TOP));
}
if let Some(scroll_delta) = scroll_delta {
ui.scroll_with_delta(scroll_delta);
}

ui.vertical(|ui| {
for item in 1..=num_items {
if track_item && item == self.track_item {
Expand Down

0 comments on commit 46d2303

Please sign in to comment.