From 937c09f14aaf7dd6dd6291be9e0de3728e3c6f12 Mon Sep 17 00:00:00 2001 From: untbu Date: Sun, 7 Jan 2024 21:20:31 +0000 Subject: [PATCH] [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 . --- crates/egui/src/containers/scroll_area.rs | 26 ++++++++++++++++++----- crates/egui/src/style.rs | 10 +++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/egui/src/containers/scroll_area.rs b/crates/egui/src/containers/scroll_area.rs index 6fd5b1dd52a..7518009812b 100644 --- a/crates/egui/src/containers/scroll_area.rs +++ b/crates/egui/src/containers/scroll_area.rs @@ -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; } } diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 4cbf6255d97..7ae9bd5a153 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -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)); } }