|
| 1 | +use sctk::reexports::client::protocol::wl_pointer::AxisSource; |
| 2 | +use sctk::reexports::client::{protocol, Connection, QueueHandle}; |
| 3 | +use sctk::seat::keyboard::Modifiers; |
| 4 | +use sctk::seat::pointer::{PointerEvent, PointerEventKind, PointerHandler, *}; |
| 5 | + |
| 6 | +use super::Window; |
| 7 | + |
| 8 | +// According to https://wayland.freedesktop.org/libinput/doc/1.19.0/wheel-api.html |
| 9 | +// wheel typically has this angle per step. |
| 10 | +// This actually should be configured and auto-detected (from udev probably?) but |
| 11 | +// for now it should work for most cases and could be tuned via config. |
| 12 | +const SCROLL_PER_STEP: f64 = 15.0; |
| 13 | + |
| 14 | +pub struct Params { |
| 15 | + pub launch_on_middle: bool, |
| 16 | + pub wheel_scroll_multiplier: f64, |
| 17 | +} |
| 18 | + |
| 19 | +impl PointerHandler for Window { |
| 20 | + fn pointer_frame( |
| 21 | + &mut self, |
| 22 | + _conn: &Connection, |
| 23 | + _qh: &QueueHandle<Self>, |
| 24 | + _pointer: &protocol::wl_pointer::WlPointer, |
| 25 | + events: &[PointerEvent], |
| 26 | + ) { |
| 27 | + let mut changed = false; |
| 28 | + let config = self.config.param::<Params>(); |
| 29 | + |
| 30 | + for event in events { |
| 31 | + // Ignore events for other surfaces |
| 32 | + if event.surface != *self.surface { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + match event.kind { |
| 37 | + // TODO: implement precise clicks on items |
| 38 | + // PointerEventKind::Release { |
| 39 | + // button: BTN_LEFT, .. |
| 40 | + // } => .., |
| 41 | + PointerEventKind::Release { |
| 42 | + button: BTN_MIDDLE, .. |
| 43 | + } if config.launch_on_middle => { |
| 44 | + let with_fork = matches!(self.key_modifiers, Modifiers { ctrl: true, .. }); |
| 45 | + if let Err(err) = self.state.eval_input(with_fork) { |
| 46 | + self.error = Some(err); |
| 47 | + } |
| 48 | + } |
| 49 | + PointerEventKind::Release { |
| 50 | + button: BTN_RIGHT, .. |
| 51 | + } => self.exit = true, |
| 52 | + PointerEventKind::Release { |
| 53 | + button: BTN_BACK, .. |
| 54 | + } => self.state.prev_subitem(), |
| 55 | + PointerEventKind::Release { |
| 56 | + button: BTN_FORWARD, |
| 57 | + .. |
| 58 | + } => self.state.next_subitem(), |
| 59 | + PointerEventKind::Axis { |
| 60 | + vertical: |
| 61 | + AxisScroll { |
| 62 | + absolute, |
| 63 | + discrete: _, |
| 64 | + // XXX: handle this one? |
| 65 | + stop: _, |
| 66 | + }, |
| 67 | + source: |
| 68 | + Some(AxisSource::Wheel) |
| 69 | + | Some(AxisSource::Finger) |
| 70 | + | Some(AxisSource::Continuous), |
| 71 | + time: _, |
| 72 | + horizontal: _, |
| 73 | + } => { |
| 74 | + self.wheel_scroll_pending += absolute; |
| 75 | + } |
| 76 | + PointerEventKind::Enter { .. } |
| 77 | + | PointerEventKind::Leave { .. } |
| 78 | + | PointerEventKind::Motion { .. } |
| 79 | + | PointerEventKind::Press { .. } |
| 80 | + | PointerEventKind::Release { .. } |
| 81 | + | PointerEventKind::Axis { .. } => continue, |
| 82 | + } |
| 83 | + changed = true; |
| 84 | + } |
| 85 | + |
| 86 | + if changed { |
| 87 | + let scroll_per_step = SCROLL_PER_STEP |
| 88 | + * if config.wheel_scroll_multiplier > 0.0 { |
| 89 | + config.wheel_scroll_multiplier |
| 90 | + } else { |
| 91 | + 1.0 |
| 92 | + }; |
| 93 | + let wheel_steps = (self.wheel_scroll_pending / scroll_per_step) as i32; |
| 94 | + if wheel_steps != 0 { |
| 95 | + self.wheel_scroll_pending -= f64::from(wheel_steps) * scroll_per_step; |
| 96 | + } |
| 97 | + let is_wheel_down = wheel_steps > 0; |
| 98 | + for _ in 0..wheel_steps.abs() { |
| 99 | + if is_wheel_down { |
| 100 | + self.state.next_item(); |
| 101 | + } else { |
| 102 | + self.state.prev_item(); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments