Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScrollArea: Add option to always scroll the only enabled direction #3710

Merged
merged 5 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,15 +779,33 @@ impl Prepared {
if scrolling_enabled && is_hovering_outer_rect {
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 ui.style().always_scroll_the_only_direction
&& scroll_enabled[0] != scroll_enabled[1]
emilk marked this conversation as resolved.
Show resolved Hide resolved
{
// 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 ui.style().always_scroll_the_only_direction
&& scroll_enabled[0] != scroll_enabled[1]
{
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,
emilk marked this conversation as resolved.
Show resolved Hide resolved
}

impl Style {
Expand Down Expand Up @@ -1070,6 +1073,7 @@ impl Default for Style {
#[cfg(debug_assertions)]
debug: Default::default(),
explanation_tooltips: false,
always_scroll_the_only_direction: true,
}
}
}
Expand Down Expand Up @@ -1323,6 +1327,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 @@ -1392,6 +1397,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
Loading