[ScrollArea]: Add option to always scroll the only enabled direction (#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 <https://github.com/emilk/egui/issues/3624>.
This commit is contained in:
untbu 2024-01-07 21:20:31 +00:00 committed by GitHub
parent 8c30e8c5f7
commit 937c09f14a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View File

@ -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;
}
}

View File

@ -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 {
@ -1076,6 +1079,7 @@ impl Default for Style {
#[cfg(debug_assertions)]
debug: Default::default(),
explanation_tooltips: false,
always_scroll_the_only_direction: false,
}
}
}
@ -1332,6 +1336,7 @@ impl Style {
#[cfg(debug_assertions)]
debug,
explanation_tooltips,
always_scroll_the_only_direction,
} = self;
visuals.light_dark_radio_buttons(ui);
@ -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));
}
}