From bf7bd500625f9589d6dd1c332a85be37ce9299e5 Mon Sep 17 00:00:00 2001 From: liuyanghejerry Date: Sat, 24 Oct 2026 22:42:53 +0800 Subject: [PATCH] viewer: forward mouse wheel events as mouse_scroll (#108) SDL MouseWheel events had no arm in the event translator, so wheel input was dropped. Send one notch as 120 wire units (the Mac convention), use SDL's precise deltas for trackpads, honour the flipped direction for natural scrolling, and keep the existing focus and view-only gating. --- src/viewer/input.rs | 17 +++++++++++++++++ src/viewer/sdl.rs | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/viewer/input.rs b/src/viewer/input.rs index 99f2959..e013cc2 100644 --- a/src/viewer/input.rs +++ b/src/viewer/input.rs @@ -84,6 +84,14 @@ impl Input { let changed=if down{self.buttons.insert(button)}else{self.buttons.remove(&button)}; changed.then(||json!({"action":if down{"mouse_press"}else{"mouse_release"},"button":button})) } + /// SDL wheel units are notches; the wire uses the Mac convention (one notch = 120). + /// Positive y is wheel-up, matching the host's `mouse_scroll` (button 4). + pub fn scroll(&mut self,x:f64,y:f64)->Option { + if self.view_only {return None} + let(dx,dy)=((x*120.).round() as i32,(y*120.).round() as i32); + if dx==0&&dy==0 {return None} + Some(json!({"action":"mouse_scroll","delta_x":dx,"delta_y":dy})) + } pub fn release_all(&mut self)->Vec { let mut out=Vec::new(); for key in std::mem::take(&mut self.keys).into_values().collect::>() {out.push(json!({"action":"kbd_release","key":key}));} @@ -126,6 +134,15 @@ impl Input { assert!(view_only(None,Some("1")).unwrap());assert!(view_only(Some("--view-only"),Some("0")).unwrap()); assert!(view_only(Some("invalid"),None).is_err()); } + #[test] fn wheel_notches_use_mac_wire_units(){ + let mut i=Input::default(); + assert_eq!(i.scroll(0.,1.).unwrap(),json!({"action":"mouse_scroll","delta_x":0,"delta_y":120})); + assert_eq!(i.scroll(0.,-1.).unwrap()["delta_y"],-120); + assert_eq!(i.scroll(1.,-2.).unwrap(),json!({"action":"mouse_scroll","delta_x":120,"delta_y":-240})); + assert_eq!(i.scroll(0.,0.1).unwrap()["delta_y"],12); // precise trackpad deltas + assert!(i.scroll(0.,0.).is_none()); + assert!(Input::new(true).scroll(0.,1.).is_none()); + } #[test] fn mappings_encoding_and_focus() { for (scan,vk) in [(4,65),(29,90),(39,48),(58,112),(80,37),(224,162),(230,165),(52,222)] {assert_eq!(windows_vk(scan),Some(vk));} assert_eq!(windows_vk(100),None); diff --git a/src/viewer/sdl.rs b/src/viewer/sdl.rs index 5ac8c88..4a50fdf 100644 --- a/src/viewer/sdl.rs +++ b/src/viewer/sdl.rs @@ -81,6 +81,11 @@ fn translate_actions(events:impl IntoIterator,input:&mut Input,viewp Event::MouseMotion{x,y,..}=>viewport.motion(x as f64,y as f64), Event::MouseButtonDown{mouse_btn,..}=>button(mouse_btn).and_then(|b|input.button(b,true)), Event::MouseButtonUp{mouse_btn,..}=>button(mouse_btn).and_then(|b|input.button(b,false)), + Event::MouseWheel{x,y,precise_x,precise_y,direction,..}=>{ + let(dx,dy)=if precise_x!=0.||precise_y!=0.{(f64::from(precise_x),f64::from(precise_y))}else{(f64::from(x),f64::from(y))}; + let(dx,dy)=match direction{sdl2::mouse::MouseWheelDirection::Flipped=>(-dx,-dy),_=>(dx,dy)}; + input.scroll(dx,dy) + }, _=>None, };if let Some(m)=message{messages.push(m);} } (quit,messages) @@ -437,6 +442,24 @@ async fn pump_type_job(job: &mut TypeJob, session: &mut Session, window: &mut Wi assert!(input.release_all().is_empty()); } } + #[test] fn wheel_events_become_scroll_messages(){ + use sdl2::mouse::MouseWheelDirection; + let mut input=Input::default();let mut focused=true; + let v=Viewport::fit((800,600),(1920,1080)); + let wheel=|direction,x,y|Event::MouseWheel{timestamp:0,window_id:1,which:0,mouse_x:0,mouse_y:0,x,y,precise_x:0.,precise_y:0.,direction}; + let(_,out)=translate([wheel(MouseWheelDirection::Normal,0,1),wheel(MouseWheelDirection::Flipped,0,1),wheel(MouseWheelDirection::Normal,0,0)],&mut input,v,&mut focused); + assert_eq!(out.len(),2); + assert_eq!(out[0]["action"],"mouse_scroll"); + assert_eq!(out[0]["delta_y"],120); + assert_eq!(out[1]["delta_y"],-120); + // Trackpads report fractional precise deltas; a quarter notch is 30 wire units. + let precise=Event::MouseWheel{timestamp:0,window_id:1,which:0,mouse_x:0,mouse_y:0,x:0,y:0,precise_x:0.,precise_y:-0.25,direction:MouseWheelDirection::Normal}; + let(_,out)=translate([precise],&mut input,v,&mut focused); + assert_eq!(out[0]["delta_y"],-30); + let mut watch=Input::new(true);let mut watching=true; + let(_,out)=translate([wheel(MouseWheelDirection::Normal,0,1)],&mut watch,v,&mut watching); + assert!(out.is_empty()); + } #[test] fn watch_events_never_emit_input_and_still_close(){ let mut input=Input::new(true);let mut focused=true; assert!(input.key(4,true).is_none());assert!(input.button(1,true).is_none());assert!(!input.enabled());