niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·055a94d·Ivan Molodetskikh·1 year ago·2247 commits
| 1 | use smithay::backend::input::ButtonState; |
| 2 | use smithay::desktop::Window; |
| 3 | use smithay::input::pointer::{ |
| 4 | AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, GestureHoldBeginEvent, |
| 5 | GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, |
| 6 | GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent, |
| 7 | GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, PointerInnerHandle, |
| 8 | RelativeMotionEvent, |
| 9 | }; |
| 10 | use smithay::input::SeatHandler; |
| 11 | use smithay::utils::{IsAlive, Logical, Point}; |
| 12 | |
| 13 | use crate::niri::State; |
| 14 | |
| 15 | pub struct MoveGrab { |
| 16 | start_data: PointerGrabStartData<State>, |
| 17 | last_location: Point<f64, Logical>, |
| 18 | window: Window, |
| 19 | gesture: GestureState, |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 23 | enum GestureState { |
| 24 | Recognizing, |
| 25 | Move, |
| 26 | } |
| 27 | |
| 28 | impl MoveGrab { |
| 29 | pub fn new( |
| 30 | start_data: PointerGrabStartData<State>, |
| 31 | window: Window, |
| 32 | use_threshold: bool, |
| 33 | ) -> Self { |
| 34 | let gesture = if use_threshold { |
| 35 | GestureState::Recognizing |
| 36 | } else { |
| 37 | GestureState::Move |
| 38 | }; |
| 39 | |
| 40 | Self { |
| 41 | last_location: start_data.location, |
| 42 | start_data, |
| 43 | window, |
| 44 | gesture, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn on_ungrab(&mut self, state: &mut State) { |
| 49 | state.niri.layout.interactive_move_end(&self.window); |
| 50 | // FIXME: only redraw the window output. |
| 51 | state.niri.queue_redraw_all(); |
| 52 | state |
| 53 | .niri |
| 54 | .cursor_manager |
| 55 | .set_cursor_image(CursorImageStatus::default_named()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl PointerGrab<State> for MoveGrab { |
| 60 | fn motion( |
| 61 | &mut self, |
| 62 | data: &mut State, |
| 63 | handle: &mut PointerInnerHandle<'_, State>, |
| 64 | _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, |
| 65 | event: &MotionEvent, |
| 66 | ) { |
| 67 | // While the grab is active, no client has pointer focus. |
| 68 | handle.motion(data, None, event); |
| 69 | |
| 70 | if self.window.alive() { |
| 71 | if let Some((output, pos_within_output)) = data.niri.output_under(event.location) { |
| 72 | let output = output.clone(); |
| 73 | let event_delta = event.location - self.last_location; |
| 74 | self.last_location = event.location; |
| 75 | |
| 76 | if self.gesture == GestureState::Recognizing { |
| 77 | let c = event.location - self.start_data.location; |
| 78 | |
| 79 | // Check if the gesture moved far enough to decide. |
| 80 | if c.x * c.x + c.y * c.y >= 8. * 8. { |
| 81 | self.gesture = GestureState::Move; |
| 82 | |
| 83 | data.niri |
| 84 | .cursor_manager |
| 85 | .set_cursor_image(CursorImageStatus::Named(CursorIcon::Move)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if self.gesture != GestureState::Move { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | let ongoing = data.niri.layout.interactive_move_update( |
| 94 | &self.window, |
| 95 | event_delta, |
| 96 | output, |
| 97 | pos_within_output, |
| 98 | ); |
| 99 | if ongoing { |
| 100 | // FIXME: only redraw the previous and the new output. |
| 101 | data.niri.queue_redraw_all(); |
| 102 | return; |
| 103 | } |
| 104 | } else { |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // The move is no longer ongoing. |
| 110 | handle.unset_grab(self, data, event.serial, event.time, true); |
| 111 | } |
| 112 | |
| 113 | fn relative_motion( |
| 114 | &mut self, |
| 115 | data: &mut State, |
| 116 | handle: &mut PointerInnerHandle<'_, State>, |
| 117 | _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, |
| 118 | event: &RelativeMotionEvent, |
| 119 | ) { |
| 120 | // While the grab is active, no client has pointer focus. |
| 121 | handle.relative_motion(data, None, event); |
| 122 | } |
| 123 | |
| 124 | fn button( |
| 125 | &mut self, |
| 126 | data: &mut State, |
| 127 | handle: &mut PointerInnerHandle<'_, State>, |
| 128 | event: &ButtonEvent, |
| 129 | ) { |
| 130 | handle.button(data, event); |
| 131 | |
| 132 | // When moving with the left button, right toggles floating, and vice versa. |
| 133 | let toggle_floating_button = if self.start_data.button == 0x110 { |
| 134 | 0x111 |
| 135 | } else { |
| 136 | 0x110 |
| 137 | }; |
| 138 | if event.button == toggle_floating_button && event.state == ButtonState::Pressed { |
| 139 | data.niri.layout.toggle_window_floating(Some(&self.window)); |
| 140 | } |
| 141 | |
| 142 | if !handle.current_pressed().contains(&self.start_data.button) { |
| 143 | // The button that initiated the grab was released. |
| 144 | handle.unset_grab(self, data, event.serial, event.time, true); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fn axis( |
| 149 | &mut self, |
| 150 | data: &mut State, |
| 151 | handle: &mut PointerInnerHandle<'_, State>, |
| 152 | details: AxisFrame, |
| 153 | ) { |
| 154 | handle.axis(data, details); |
| 155 | } |
| 156 | |
| 157 | fn frame(&mut self, data: &mut State, handle: &mut PointerInnerHandle<'_, State>) { |
| 158 | handle.frame(data); |
| 159 | } |
| 160 | |
| 161 | fn gesture_swipe_begin( |
| 162 | &mut self, |
| 163 | data: &mut State, |
| 164 | handle: &mut PointerInnerHandle<'_, State>, |
| 165 | event: &GestureSwipeBeginEvent, |
| 166 | ) { |
| 167 | handle.gesture_swipe_begin(data, event); |
| 168 | } |
| 169 | |
| 170 | fn gesture_swipe_update( |
| 171 | &mut self, |
| 172 | data: &mut State, |
| 173 | handle: &mut PointerInnerHandle<'_, State>, |
| 174 | event: &GestureSwipeUpdateEvent, |
| 175 | ) { |
| 176 | handle.gesture_swipe_update(data, event); |
| 177 | } |
| 178 | |
| 179 | fn gesture_swipe_end( |
| 180 | &mut self, |
| 181 | data: &mut State, |
| 182 | handle: &mut PointerInnerHandle<'_, State>, |
| 183 | event: &GestureSwipeEndEvent, |
| 184 | ) { |
| 185 | handle.gesture_swipe_end(data, event); |
| 186 | } |
| 187 | |
| 188 | fn gesture_pinch_begin( |
| 189 | &mut self, |
| 190 | data: &mut State, |
| 191 | handle: &mut PointerInnerHandle<'_, State>, |
| 192 | event: &GesturePinchBeginEvent, |
| 193 | ) { |
| 194 | handle.gesture_pinch_begin(data, event); |
| 195 | } |
| 196 | |
| 197 | fn gesture_pinch_update( |
| 198 | &mut self, |
| 199 | data: &mut State, |
| 200 | handle: &mut PointerInnerHandle<'_, State>, |
| 201 | event: &GesturePinchUpdateEvent, |
| 202 | ) { |
| 203 | handle.gesture_pinch_update(data, event); |
| 204 | } |
| 205 | |
| 206 | fn gesture_pinch_end( |
| 207 | &mut self, |
| 208 | data: &mut State, |
| 209 | handle: &mut PointerInnerHandle<'_, State>, |
| 210 | event: &GesturePinchEndEvent, |
| 211 | ) { |
| 212 | handle.gesture_pinch_end(data, event); |
| 213 | } |
| 214 | |
| 215 | fn gesture_hold_begin( |
| 216 | &mut self, |
| 217 | data: &mut State, |
| 218 | handle: &mut PointerInnerHandle<'_, State>, |
| 219 | event: &GestureHoldBeginEvent, |
| 220 | ) { |
| 221 | handle.gesture_hold_begin(data, event); |
| 222 | } |
| 223 | |
| 224 | fn gesture_hold_end( |
| 225 | &mut self, |
| 226 | data: &mut State, |
| 227 | handle: &mut PointerInnerHandle<'_, State>, |
| 228 | event: &GestureHoldEndEvent, |
| 229 | ) { |
| 230 | handle.gesture_hold_end(data, event); |
| 231 | } |
| 232 | |
| 233 | fn start_data(&self) -> &PointerGrabStartData<State> { |
| 234 | &self.start_data |
| 235 | } |
| 236 | |
| 237 | fn unset(&mut self, data: &mut State) { |
| 238 | self.on_ungrab(data); |
| 239 | } |
| 240 | } |