From c00c6bed7db49354d0efe2e2cc6134472332c4c1 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Sat, 8 Aug 2026 16:11:17 +0300 Subject: [PATCH] Bring back old ClickGrab location-preserving behavior See comment on the ClickGrab for why I think this is better. --- src/handlers/mod.rs | 10 ++- src/input/click_grab.rs | 151 ++++++++++++++++++++++++++++++++++++++++ src/input/mod.rs | 1 + 3 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/input/click_grab.rs diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 0b176000..9cc9da32 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -15,7 +15,7 @@ use smithay::backend::drm::DrmNode; use smithay::backend::input::{InputEvent, TabletToolDescriptor}; use smithay::desktop::{PopupKind, PopupManager}; use smithay::input::dnd::{self, DnDGrab, DndGrabHandler, DndTarget}; -use smithay::input::pointer::{CursorIcon, CursorImageStatus, Focus, PointerHandle}; +use smithay::input::pointer::{self, CursorIcon, CursorImageStatus, Focus, PointerHandle}; use smithay::input::tablet::TabletSeatHandler; use smithay::input::{keyboard, Seat, SeatHandler, SeatState}; use smithay::output::Output; @@ -65,6 +65,7 @@ use smithay::wayland::xdg_activation::{ }; pub use crate::handlers::xdg_shell::KdeDecorationsModeState; +use crate::input::click_grab::ClickGrab; use crate::layout::workspace::WorkspaceId; use crate::layout::ActivateWindow; use crate::niri::{DndIcon, NewClient, State}; @@ -124,6 +125,13 @@ impl SeatHandler for State { keyboard.led_update(led_state.into()); } } + + fn click_grab( + &mut self, + start_data: pointer::GrabStartData, + ) -> impl pointer::PointerGrab { + ClickGrab::new(start_data) + } } impl TabletSeatHandler for State { diff --git a/src/input/click_grab.rs b/src/input/click_grab.rs new file mode 100644 index 00000000..b1de1882 --- /dev/null +++ b/src/input/click_grab.rs @@ -0,0 +1,151 @@ +use smithay::input::pointer::{ + AxisFrame, ButtonEvent, GestureHoldBeginEvent, GestureHoldEndEvent, GesturePinchBeginEvent, + GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, GestureSwipeEndEvent, + GestureSwipeUpdateEvent, GrabStartData, MotionEvent, PointerGrab, PointerInnerHandle, + RelativeMotionEvent, +}; +use smithay::input::SeatHandler; +use smithay::utils::{Logical, Point}; + +// ClickGrab from Smithay, but without the grab location updates. +// +// When clicking on a partially off-screen window, such that it will scroll into the view, updating +// the focus location will cause a move event, essentially a short DnD, which might result in some +// unintended action. +// +// Not updating in this case avoids any unintentional DnD. It does cause offset cursor movement if +// you hold and keep moving, but that can't cause mistakes as easily, so it's the lesser evil. +pub struct ClickGrab { + start_data: GrabStartData, + focus: Option<(D::PointerFocus, Point)>, +} + +impl ClickGrab { + pub fn new(start_data: GrabStartData) -> Self { + Self { + focus: start_data.focus.clone(), + start_data, + } + } +} + +impl PointerGrab for ClickGrab { + fn motion( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + _focus: Option<(::PointerFocus, Point)>, + event: &MotionEvent, + ) { + handle.motion(data, self.focus.clone(), event); + } + + fn relative_motion( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + focus: Option<(::PointerFocus, Point)>, + event: &RelativeMotionEvent, + ) { + handle.relative_motion(data, focus, event); + } + + fn button( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &ButtonEvent, + ) { + handle.button(data, event); + if handle.current_pressed().is_empty() { + // no more buttons are pressed, release the grab + handle.unset_grab(self, data, event.serial, event.time, false); + } + } + + fn axis(&mut self, data: &mut D, handle: &mut PointerInnerHandle<'_, D>, details: AxisFrame) { + handle.axis(data, details); + } + + fn frame(&mut self, data: &mut D, handle: &mut PointerInnerHandle<'_, D>) { + handle.frame(data); + } + + fn gesture_swipe_begin( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GestureSwipeBeginEvent, + ) { + handle.gesture_swipe_begin(data, event); + } + + fn gesture_swipe_update( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GestureSwipeUpdateEvent, + ) { + handle.gesture_swipe_update(data, event); + } + + fn gesture_swipe_end( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GestureSwipeEndEvent, + ) { + handle.gesture_swipe_end(data, event); + } + + fn gesture_pinch_begin( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GesturePinchBeginEvent, + ) { + handle.gesture_pinch_begin(data, event); + } + + fn gesture_pinch_update( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GesturePinchUpdateEvent, + ) { + handle.gesture_pinch_update(data, event); + } + + fn gesture_pinch_end( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GesturePinchEndEvent, + ) { + handle.gesture_pinch_end(data, event); + } + + fn gesture_hold_begin( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GestureHoldBeginEvent, + ) { + handle.gesture_hold_begin(data, event); + } + + fn gesture_hold_end( + &mut self, + data: &mut D, + handle: &mut PointerInnerHandle<'_, D>, + event: &GestureHoldEndEvent, + ) { + handle.gesture_hold_end(data, event); + } + + fn start_data(&self) -> &GrabStartData { + &self.start_data + } + + fn unset(&mut self, _data: &mut D) {} +} diff --git a/src/input/mod.rs b/src/input/mod.rs index e6e023c2..1f25b089 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -55,6 +55,7 @@ use crate::utils::spawning::{spawn, spawn_sh}; use crate::utils::{center, get_monotonic_time, CastSessionId, ResizeEdge}; pub mod backend_ext; +pub mod click_grab; pub mod move_grab; pub mod pick_color_grab; pub mod pick_window_grab;