From d33929c8fab6a73321efc4d713c54e423a355d3d Mon Sep 17 00:00:00 2001 From: LookImEcho-Rick <62575172+LookImEcho-Rick@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:22:40 -0500 Subject: [PATCH] Fix Horizon options scrollbar dragging --- crates/horizon-client/src/options_horizon.rs | 295 +++++++++++++++++-- crates/horizon-client/src/options_ui.rs | 3 +- 2 files changed, 269 insertions(+), 29 deletions(-) diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index 634bb8d75..d3ecab639 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -263,15 +263,29 @@ enum Target { Style(usize), Level(usize), } +struct HorizonScrollbar { + x: f32, + top: f32, + bottom: f32, + max: f32, + thumb: f32, +} +impl HorizonScrollbar { + fn contains(&self, x: f32, y: f32) -> bool { + x >= self.x && x < self.x + 6.0 && y >= self.top && y < self.bottom + } +} +struct HorizonLayout { + sliders: Vec<(Item, f32, f32)>, + scrollbar: Option, +} +fn horizon_batch(framebuffer: [i32; 2]) -> HudBatch { + HudBatch::new([framebuffer[0].max(1) as u32, framebuffer[1].max(1) as u32]) +} impl OptionsUi { /// Lay out (and draw, on a real frame) the Horizon options shell. - /// Returns the sliders' tracks (option, x, width) for dragging. - fn horizon_layout( - &mut self, - s: &Settings, - ui: &mut Ui, - backdrop: f32, - ) -> Vec<(Item, f32, f32)> { + /// Returns the sliders' tracks and the scroll area for pointer input. + fn horizon_layout(&mut self, s: &Settings, ui: &mut Ui, backdrop: f32) -> HorizonLayout { let (w, h) = (ui.w, ui.h); if self.in_world { ui.atmosphere(horizon_theme::VEIL, 1.0); @@ -499,6 +513,7 @@ impl OptionsUi { .filter(|h| h.rect[1] >= cy0 && h.rect[3] <= cy1) .collect(); ui.hits.extend(kept); + let mut scrollbar = None; if max > 0.0 { // A thin scroll indicator. let track = bottom - top; @@ -512,11 +527,18 @@ impl OptionsUi { 1.5, color::with_alpha(color::MUTED, 0.35), ); + scrollbar = Some(HorizonScrollbar { + x: x + cw + 14.0, + top: top + oy, + bottom: bottom + oy, + max, + thumb: bar, + }); } let _ = ox; ui.opacity = 1.0; ui.offset = (0.0, 0.0); - sliders + HorizonLayout { sliders, scrollbar } } pub(crate) fn draw_horizon( &mut self, @@ -530,25 +552,47 @@ impl OptionsUi { self.horizon_layout(s, &mut ui, backdrop); ui.finish(); } - fn horizon_target(&mut self, s: &Settings, atlas: &HudAtlas, x: f32, y: f32) -> Option { - let mut batch = t::scratch_batch(); + fn horizon_target( + &mut self, + s: &Settings, + atlas: &HudAtlas, + x: f32, + y: f32, + framebuffer: [i32; 2], + ) -> (Option, Option, (f32, f32)) { + let mut batch = horizon_batch(framebuffer); let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - let hit = ui.hit((x, y))?; + let layout = self.horizon_layout(s, &mut ui, 1.0); + let scrollbar = layout.scrollbar; + // The UI layer transforms raw GUI-pixel input into its own dp. + let cursor = ui.cursor; + let Some(hit) = ui.hit((x, y)) else { + return (None, scrollbar, cursor); + }; let section = self.section.min(SECTIONS.len() - 1); if (NAV..NAV + SECTIONS.len() as u64).contains(&hit) { - return Some(Target::Nav((hit - NAV) as usize)); + return (Some(Target::Nav((hit - NAV) as usize)), scrollbar, cursor); } if hit == DONE { - return Some(Target::Done); + return (Some(Target::Done), scrollbar, cursor); } if hit == STYLE || hit == STYLE + 1 { - return Some(Target::Style((hit - STYLE) as usize)); + return ( + Some(Target::Style((hit - STYLE) as usize)), + scrollbar, + cursor, + ); } if hit == LEVEL || hit == LEVEL + 1 { - return Some(Target::Level((hit - LEVEL) as usize)); + return ( + Some(Target::Level((hit - LEVEL) as usize)), + scrollbar, + cursor, + ); } - let i = hit.checked_sub(ROW)? as usize; + let Some(i) = hit.checked_sub(ROW).map(|i| i as usize) else { + return (None, scrollbar, cursor); + }; let rows = (SECTIONS[section].rows)(s); let item = match rows.get(i) { Some(HRow::Opt(opt)) => Some(Item::Vanilla(*opt)), @@ -556,12 +600,16 @@ impl OptionsUi { _ => None, }; if let Some(item) = item - && let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) { let v = t::slider_value(&ui, sx, sw, x); - return Some(Target::Slider(item, v)); + return (Some(Target::Slider(item, v)), scrollbar, cursor); } - (i < rows.len()).then_some(Target::Row(i)) + ( + (i < rows.len()).then_some(Target::Row(i)), + scrollbar, + cursor, + ) } /// A click (or a drag with the button held) on the Horizon shell. pub(crate) fn click_horizon( @@ -576,14 +624,37 @@ impl OptionsUi { ) -> Vec { let mut events = Vec::new(); if drag { - if let Some(item) = self.hdragging { - let mut batch = t::scratch_batch(); - let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - if let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) { - set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); - events.push(Event::Changed); + let mut batch = horizon_batch(framebuffer); + let mut ui = Ui::dry(atlas, &mut batch, (x, y)); + let layout = self.horizon_layout(s, &mut ui, 1.0); + // Scrollbar geometry and stored drag positions are in Horizon dp. + let pointer_y = ui.cursor.1; + if let Some(previous_y) = self.scrollbar_drag_y { + if let Some(scrollbar) = layout.scrollbar { + // AbstractScrollArea.mouseDragged (inherited by AbstractSelectionList) + // moves by the pointer delta and sends out-of-range drags to an end. + let track = scrollbar.bottom - scrollbar.top; + self.hscroll = if pointer_y < scrollbar.top { + 0.0 + } else if pointer_y > scrollbar.bottom { + scrollbar.max + } else { + self.hscroll + + (pointer_y - previous_y) * scrollbar.max + / (track - scrollbar.thumb).max(1.0) + }; + self.hscroll = self.hscroll.clamp(0.0, scrollbar.max); + self.scrollbar_drag_y = Some(pointer_y); + } else { + self.scrollbar_drag_y = None; } + return events; + } + if let Some(item) = self.hdragging + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) + { + set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); + events.push(Event::Changed); } return events; } @@ -598,7 +669,16 @@ impl OptionsUi { }, ); } - match self.horizon_target(s, atlas, x, y) { + let (target, scrollbar, cursor) = self.horizon_target(s, atlas, x, y, framebuffer); + // AbstractScrollArea.updateScrolling grabs anywhere in the scrollbar column. + if !right + && let Some(scrollbar) = scrollbar + && scrollbar.contains(cursor.0, cursor.1) + { + self.scrollbar_drag_y = Some(cursor.1); + return events; + } + match target { Some(Target::Nav(i)) => { self.section = i; self.hscroll = 0.0; @@ -670,3 +750,162 @@ fn set_item_slider(item: Item, s: &mut Settings, v: f32) { Item::Horizon(_) => item.set_slider(s, v), } } + +#[cfg(test)] +mod tests { + use super::*; + + fn atlas() -> HudAtlas { + HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() + } + + fn section_ui(name: &str) -> OptionsUi { + let mut ui = OptionsUi::default(); + ui.section = SECTIONS + .iter() + .position(|section| section.name == name) + .unwrap(); + ui + } + + fn extras_ui() -> OptionsUi { + section_ui("Extras") + } + + fn scrollbar( + ui: &mut OptionsUi, + settings: &Settings, + atlas: &HudAtlas, + framebuffer: [i32; 2], + ) -> HorizonScrollbar { + let mut batch = horizon_batch(framebuffer); + let mut draw = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + ui.horizon_layout(settings, &mut draw, 1.0) + .scrollbar + .unwrap() + } + + fn gui_pointer(atlas: &HudAtlas, framebuffer: [i32; 2], point: (f32, f32)) -> (f32, f32) { + let mut batch = horizon_batch(framebuffer); + let ui = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + let gui_per_dp = ui.density / ui.batch.scale; + (point.0 * gui_per_dp, point.1 * gui_per_dp) + } + + #[test] + fn horizon_scrollbar_track_drag_is_bounded_and_stops_on_release() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = section_ui("Controls"); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let max = bar.max; + let track = bar.bottom - bar.top; + assert!(max > 0.0); + + // Start on the track below the thumb, then move horizontally away; + // the held scrollbar keeps following the pointer. + let x = bar.x + 2.0; + let y = bar.top + bar.thumb + (track - bar.thumb) / 2.0; + assert!(y < bar.bottom); + let (click_x, click_y) = gui_pointer(&atlas, framebuffer, (x, y)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + click_x, + click_y, + framebuffer, + false, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + let delta = 5.0; + let (drag_x, drag_y) = gui_pointer(&atlas, framebuffer, (x - 100.0, y + delta)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + drag_x, + drag_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert!((ui.hscroll - delta * max / (track - bar.thumb)).abs() < 0.001); + + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, max); + let (above_x, above_y) = gui_pointer(&atlas, framebuffer, (x, bar.top - 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + above_x, + above_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + + assert!(ui.release().is_empty()); + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + assert_eq!(settings.keys, Settings::default().keys); + } + + #[test] + fn horizon_key_rebinding_takes_precedence_over_scrollbar_press() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = extras_ui(); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let dp_y = bar.top + bar.thumb + (bar.bottom - bar.top - bar.thumb) / 2.0; + ui.selected_key = Some(extras::SORT_KEY); + let (x, y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y)); + + assert_eq!( + ui.click_horizon(&mut settings, &atlas, x, y, framebuffer, false, false,), + vec![Event::Changed] + ); + assert_eq!(settings.extras.sort_key, "key.mouse.left"); + assert_eq!(ui.selected_key, None); + assert_eq!(ui.hscroll, 0.0); + + let (x, drag_y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y + 10.0)); + ui.click_horizon(&mut settings, &atlas, x, drag_y, framebuffer, true, false); + assert_eq!(ui.hscroll, 0.0); + } +} diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index dca180e15..fc80530d9 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -806,7 +806,8 @@ pub struct OptionsUi { /// when it last moved: the handle follows the pointer while the option /// keeps its stepped value, then snaps to it (`resetValue`). slider_value: Option<(Opt, f32, Instant)>, - scrollbar_drag_y: Option, + /// The pointer's last y while a Classic or Horizon scrollbar is held. + pub(crate) scrollbar_drag_y: Option, /// Horizon style: the selected sidebar section, the one last drawn and /// the rows' scroll in dp (`options_horizon`). pub section: usize,