From aef03b3d0089ae7bcb67c7e2d13f262844a02a1e Mon Sep 17 00:00:00 2001 From: untbu Date: Sun, 17 Dec 2023 01:42:54 +0100 Subject: [PATCH 1/5] add option to always scroll the only enabled scroll direction --- crates/egui/src/containers/scroll_area.rs | 52 ++++++++++++++++++++--- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/egui/src/containers/scroll_area.rs b/crates/egui/src/containers/scroll_area.rs index 6fd5b1dd52a..919127b7ea0 100644 --- a/crates/egui/src/containers/scroll_area.rs +++ b/crates/egui/src/containers/scroll_area.rs @@ -161,6 +161,9 @@ pub struct ScrollArea { scrolling_enabled: bool, drag_to_scroll: bool, + /// If true and scrolling is enabled for only one direction, allow horizontal scrolling without pressing shift + always_scroll_the_only_direction: bool, + /// If true for vertical or horizontal the scroll wheel will stick to the /// end position until user manually changes position. It will become true /// again once scroll handle makes contact with end. @@ -207,6 +210,7 @@ impl ScrollArea { offset_y: None, scrolling_enabled: true, drag_to_scroll: true, + always_scroll_the_only_direction: true, stick_to_end: Vec2b::FALSE, } } @@ -360,6 +364,21 @@ impl ScrollArea { self } + /// If `true` and scrolling is enabled for only one direction, + /// use scroll input from both directions to scroll the enabled direction. + /// + /// This can be used to allow horizontal scrolling without pressing shift if vertical scrolling is disabled. + /// + /// Default: `true`. + #[inline] + pub fn always_scroll_the_only_direction( + mut self, + always_scroll_the_only_direction: bool, + ) -> Self { + self.always_scroll_the_only_direction = always_scroll_the_only_direction; + self + } + /// For each axis, should the containing area shrink if the content is small? /// /// * If `true`, egui will add blank space outside the scroll area. @@ -414,6 +433,8 @@ struct Prepared { /// Smoothly interpolated boolean of whether or not to show the scroll bars. show_bars_factor: Vec2, + always_scroll_the_only_direction: bool, + /// How much horizontal and vertical space are used up by the /// width of the vertical bar, and the height of the horizontal bar? /// @@ -453,6 +474,7 @@ impl ScrollArea { offset_y, scrolling_enabled, drag_to_scroll, + always_scroll_the_only_direction, stick_to_end, } = self; @@ -587,6 +609,7 @@ impl ScrollArea { auto_shrink, scroll_enabled, show_bars_factor, + always_scroll_the_only_direction, current_bar_use, scroll_bar_visibility, inner_rect, @@ -699,6 +722,7 @@ impl Prepared { auto_shrink, scroll_enabled, mut show_bars_factor, + always_scroll_the_only_direction, current_bar_use, scroll_bar_visibility, content_ui, @@ -779,15 +803,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 always_scroll_the_only_direction + && scroll_enabled[0] != scroll_enabled[1] + { + // 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_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; } } From 2197f0c70f3e874f53b818171d2f68ff5a069a22 Mon Sep 17 00:00:00 2001 From: untbu Date: Sun, 31 Dec 2023 16:35:24 +0100 Subject: [PATCH 2/5] move setting to Style --- crates/egui/src/containers/scroll_area.rs | 28 ++--------------------- crates/egui/src/style.rs | 10 ++++++++ 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/crates/egui/src/containers/scroll_area.rs b/crates/egui/src/containers/scroll_area.rs index 919127b7ea0..5cea8f8a31a 100644 --- a/crates/egui/src/containers/scroll_area.rs +++ b/crates/egui/src/containers/scroll_area.rs @@ -161,9 +161,6 @@ pub struct ScrollArea { scrolling_enabled: bool, drag_to_scroll: bool, - /// If true and scrolling is enabled for only one direction, allow horizontal scrolling without pressing shift - always_scroll_the_only_direction: bool, - /// If true for vertical or horizontal the scroll wheel will stick to the /// end position until user manually changes position. It will become true /// again once scroll handle makes contact with end. @@ -210,7 +207,6 @@ impl ScrollArea { offset_y: None, scrolling_enabled: true, drag_to_scroll: true, - always_scroll_the_only_direction: true, stick_to_end: Vec2b::FALSE, } } @@ -364,21 +360,6 @@ impl ScrollArea { self } - /// If `true` and scrolling is enabled for only one direction, - /// use scroll input from both directions to scroll the enabled direction. - /// - /// This can be used to allow horizontal scrolling without pressing shift if vertical scrolling is disabled. - /// - /// Default: `true`. - #[inline] - pub fn always_scroll_the_only_direction( - mut self, - always_scroll_the_only_direction: bool, - ) -> Self { - self.always_scroll_the_only_direction = always_scroll_the_only_direction; - self - } - /// For each axis, should the containing area shrink if the content is small? /// /// * If `true`, egui will add blank space outside the scroll area. @@ -433,8 +414,6 @@ struct Prepared { /// Smoothly interpolated boolean of whether or not to show the scroll bars. show_bars_factor: Vec2, - always_scroll_the_only_direction: bool, - /// How much horizontal and vertical space are used up by the /// width of the vertical bar, and the height of the horizontal bar? /// @@ -474,7 +453,6 @@ impl ScrollArea { offset_y, scrolling_enabled, drag_to_scroll, - always_scroll_the_only_direction, stick_to_end, } = self; @@ -609,7 +587,6 @@ impl ScrollArea { auto_shrink, scroll_enabled, show_bars_factor, - always_scroll_the_only_direction, current_bar_use, scroll_bar_visibility, inner_rect, @@ -722,7 +699,6 @@ impl Prepared { auto_shrink, scroll_enabled, mut show_bars_factor, - always_scroll_the_only_direction, current_bar_use, scroll_bar_visibility, content_ui, @@ -804,7 +780,7 @@ impl Prepared { for d in 0..2 { if scroll_enabled[d] { let scroll_delta = ui.ctx().frame_state(|fs| { - if always_scroll_the_only_direction + if ui.style().always_scroll_the_only_direction && scroll_enabled[0] != scroll_enabled[1] { // no bidirectional scrolling; allow horizontal scrolling without pressing shift @@ -821,7 +797,7 @@ impl Prepared { state.offset[d] -= scroll_delta; // Clear scroll delta so no parent scroll will use it. ui.ctx().frame_state_mut(|fs| { - if always_scroll_the_only_direction + if ui.style().always_scroll_the_only_direction && scroll_enabled[0] != scroll_enabled[1] { fs.scroll_delta[0] = 0.0; diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index fb03bcfd805..acd063e4481 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 { @@ -1070,6 +1073,7 @@ impl Default for Style { #[cfg(debug_assertions)] debug: Default::default(), explanation_tooltips: false, + always_scroll_the_only_direction: true, } } } @@ -1323,6 +1327,7 @@ impl Style { #[cfg(debug_assertions)] debug, explanation_tooltips, + always_scroll_the_only_direction, } = self; visuals.light_dark_radio_buttons(ui); @@ -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 onls 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)); } } From f2129d715d9e7ece508e142eaac722c8be136e75 Mon Sep 17 00:00:00 2001 From: untbu Date: Sun, 31 Dec 2023 16:49:59 +0100 Subject: [PATCH 3/5] fix typo --- crates/egui/src/style.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index acd063e4481..c918d2bfee6 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -1397,7 +1397,7 @@ impl Style { "Show explanatory text when hovering DragValue:s and other egui widgets", ); - ui.checkbox(always_scroll_the_only_direction, "Always scroll the onls direction") + 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", ); From e4d99407e23303f3a82d8e65114ba1b7d4f40566 Mon Sep 17 00:00:00 2001 From: untbu Date: Sun, 7 Jan 2024 16:09:14 +0100 Subject: [PATCH 4/5] add always_scroll_enabled_direction variable --- crates/egui/src/containers/scroll_area.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/egui/src/containers/scroll_area.rs b/crates/egui/src/containers/scroll_area.rs index 5cea8f8a31a..7518009812b 100644 --- a/crates/egui/src/containers/scroll_area.rs +++ b/crates/egui/src/containers/scroll_area.rs @@ -777,12 +777,12 @@ 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| { - if ui.style().always_scroll_the_only_direction - && scroll_enabled[0] != scroll_enabled[1] - { + if always_scroll_enabled_direction { // no bidirectional scrolling; allow horizontal scrolling without pressing shift fs.scroll_delta[0] + fs.scroll_delta[1] } else { @@ -797,9 +797,7 @@ impl Prepared { state.offset[d] -= scroll_delta; // Clear scroll delta so no parent scroll will use it. ui.ctx().frame_state_mut(|fs| { - if ui.style().always_scroll_the_only_direction - && scroll_enabled[0] != scroll_enabled[1] - { + if always_scroll_enabled_direction { fs.scroll_delta[0] = 0.0; fs.scroll_delta[1] = 0.0; } else { From 312e2248eef1a16e9c1ed2af2657db7a7c2ba6bc Mon Sep 17 00:00:00 2001 From: untbu Date: Sun, 7 Jan 2024 16:10:40 +0100 Subject: [PATCH 5/5] disable setting by default --- crates/egui/src/style.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index c918d2bfee6..9bb29ab566a 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -1073,7 +1073,7 @@ impl Default for Style { #[cfg(debug_assertions)] debug: Default::default(), explanation_tooltips: false, - always_scroll_the_only_direction: true, + always_scroll_the_only_direction: false, } } }