Skip to content

Commit

Permalink
[ScrollArea]: Add option to always scroll the only enabled direction (#…
Browse files Browse the repository at this point in the history
…3710)

Add option to use the scroll input from both directions to scroll the
enabled direction if scrolling is enabled for only one direction. This
can be used to allow horizontal scrolling without pressing shift if
vertical scrolling is disabled.

Closes <#3624>.
  • Loading branch information
untbu authored Jan 7, 2024
1 parent 8c30e8c commit 937c09f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
26 changes: 21 additions & 5 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,17 +777,33 @@ 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 always_scroll_enabled_direction = ui.style().always_scroll_the_only_direction
&& scroll_enabled[0] != scroll_enabled[1];
for d in 0..2 {
if scroll_enabled[d] {
let scroll_delta = ui.ctx().frame_state(|fs| fs.scroll_delta);
let scroll_delta = ui.ctx().frame_state(|fs| {
if always_scroll_enabled_direction {
// no bidirectional scrolling; allow horizontal scrolling without pressing shift
fs.scroll_delta[0] + fs.scroll_delta[1]
} else {
fs.scroll_delta[d]
}
});

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;
let scrolling_up = state.offset[d] > 0.0 && scroll_delta > 0.0;
let scrolling_down = state.offset[d] < max_offset[d] && scroll_delta < 0.0;

if scrolling_up || scrolling_down {
state.offset[d] -= scroll_delta[d];
state.offset[d] -= scroll_delta;
// Clear scroll delta so no parent scroll will use it.
ui.ctx().frame_state_mut(|fs| fs.scroll_delta[d] = 0.0);
ui.ctx().frame_state_mut(|fs| {
if always_scroll_enabled_direction {
fs.scroll_delta[0] = 0.0;
fs.scroll_delta[1] = 0.0;
} else {
fs.scroll_delta[d] = 0.0;
}
});
state.scroll_stuck_to_end[d] = false;
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ pub struct Style {
///
/// This only affects a few egui widgets.
pub explanation_tooltips: bool,

/// If true and scrolling is enabled for only one direction, allow horizontal scrolling without pressing shift
pub always_scroll_the_only_direction: bool,
}

impl Style {
Expand Down Expand Up @@ -1076,6 +1079,7 @@ impl Default for Style {
#[cfg(debug_assertions)]
debug: Default::default(),
explanation_tooltips: false,
always_scroll_the_only_direction: false,
}
}
}
Expand Down Expand Up @@ -1332,6 +1336,7 @@ impl Style {
#[cfg(debug_assertions)]
debug,
explanation_tooltips,
always_scroll_the_only_direction,
} = self;

visuals.light_dark_radio_buttons(ui);
Expand Down Expand Up @@ -1401,6 +1406,11 @@ impl Style {
"Show explanatory text when hovering DragValue:s and other egui widgets",
);

ui.checkbox(always_scroll_the_only_direction, "Always scroll the only enabled direction")
.on_hover_text(
"If scrolling is enabled for only one direction, allow horizontal scrolling without pressing shift",
);

ui.vertical_centered(|ui| reset_button(ui, self));
}
}
Expand Down

0 comments on commit 937c09f

Please sign in to comment.