niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use smithay::desktop::Window; |
| 2 | use smithay::input::pointer::{ |
| 3 | AxisFrame, ButtonEvent, CursorImageStatus, GestureHoldBeginEvent, GestureHoldEndEvent, |
| 4 | GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, |
| 5 | GestureSwipeEndEvent, GestureSwipeUpdateEvent, GrabStartData as PointerGrabStartData, |
| 6 | MotionEvent, PointerGrab, PointerInnerHandle, RelativeMotionEvent, |
| 7 | }; |
| 8 | use smithay::input::tablet::tool::{TabletToolGrab, TabletToolInnerHandle}; |
| 9 | use smithay::input::tablet::TabletSeatHandler; |
| 10 | use smithay::input::touch::{ |
| 11 | self, GrabStartData as TouchGrabStartData, TouchGrab, TouchInnerHandle, |
| 12 | }; |
| 13 | use smithay::input::{tablet, SeatHandler}; |
| 14 | use smithay::utils::{IsAlive, Logical, Point, SERIAL_COUNTER}; |
| 15 | |
| 16 | use crate::input::AnyStartData; |
| 17 | use crate::niri::State; |
| 18 | use crate::utils::get_monotonic_time; |
| 19 | |
| 20 | pub struct ResizeGrab { |
| 21 | start_data: AnyStartData<State>, |
| 22 | window: Window, |
| 23 | |
| 24 | // Accumulated and applied in frame(). |
| 25 | new_location: Point<f64, Logical>, |
| 26 | } |
| 27 | |
| 28 | impl ResizeGrab { |
| 29 | pub fn new(start_data: AnyStartData<State>, window: Window) -> Self { |
| 30 | let location = start_data.location(); |
| 31 | |
| 32 | Self { |
| 33 | start_data, |
| 34 | window, |
| 35 | new_location: location, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn on_ungrab(&mut self, state: &mut State) { |
| 40 | state.niri.layout.interactive_resize_end(&self.window); |
| 41 | |
| 42 | if !self.start_data.is_touch() { |
| 43 | state |
| 44 | .niri |
| 45 | .cursor_manager |
| 46 | .set_cursor_image(CursorImageStatus::default_named()); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn on_frame(&mut self, data: &mut State) -> bool { |
| 51 | if !self.window.alive() { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | let delta = self.new_location - self.start_data.location(); |
| 56 | data.niri |
| 57 | .layout |
| 58 | .interactive_resize_update(&self.window, delta) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl PointerGrab<State> for ResizeGrab { |
| 63 | fn motion( |
| 64 | &mut self, |
| 65 | data: &mut State, |
| 66 | handle: &mut PointerInnerHandle<'_, State>, |
| 67 | _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, |
| 68 | event: &MotionEvent, |
| 69 | ) { |
| 70 | // While the grab is active, no client has pointer focus. |
| 71 | handle.motion(data, None, event); |
| 72 | |
| 73 | self.new_location = event.location; |
| 74 | } |
| 75 | |
| 76 | fn relative_motion( |
| 77 | &mut self, |
| 78 | data: &mut State, |
| 79 | handle: &mut PointerInnerHandle<'_, State>, |
| 80 | _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>, |
| 81 | event: &RelativeMotionEvent, |
| 82 | ) { |
| 83 | // While the grab is active, no client has pointer focus. |
| 84 | handle.relative_motion(data, None, event); |
| 85 | } |
| 86 | |
| 87 | fn button( |
| 88 | &mut self, |
| 89 | data: &mut State, |
| 90 | handle: &mut PointerInnerHandle<'_, State>, |
| 91 | event: &ButtonEvent, |
| 92 | ) { |
| 93 | handle.button(data, event); |
| 94 | |
| 95 | if handle.current_pressed().is_empty() { |
| 96 | // No more buttons are pressed, release the grab. |
| 97 | handle.unset_grab(self, data, event.serial, event.time, true); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | fn axis( |
| 102 | &mut self, |
| 103 | data: &mut State, |
| 104 | handle: &mut PointerInnerHandle<'_, State>, |
| 105 | details: AxisFrame, |
| 106 | ) { |
| 107 | handle.axis(data, details); |
| 108 | } |
| 109 | |
| 110 | fn frame(&mut self, data: &mut State, handle: &mut PointerInnerHandle<'_, State>) { |
| 111 | handle.frame(data); |
| 112 | |
| 113 | if !self.on_frame(data) { |
| 114 | // The gesture is no longer ongoing. |
| 115 | handle.unset_grab( |
| 116 | self, |
| 117 | data, |
| 118 | SERIAL_COUNTER.next_serial(), |
| 119 | get_monotonic_time().as_millis() as u32, |
| 120 | true, |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn gesture_swipe_begin( |
| 126 | &mut self, |
| 127 | data: &mut State, |
| 128 | handle: &mut PointerInnerHandle<'_, State>, |
| 129 | event: &GestureSwipeBeginEvent, |
| 130 | ) { |
| 131 | handle.gesture_swipe_begin(data, event); |
| 132 | } |
| 133 | |
| 134 | fn gesture_swipe_update( |
| 135 | &mut self, |
| 136 | data: &mut State, |
| 137 | handle: &mut PointerInnerHandle<'_, State>, |
| 138 | event: &GestureSwipeUpdateEvent, |
| 139 | ) { |
| 140 | handle.gesture_swipe_update(data, event); |
| 141 | } |
| 142 | |
| 143 | fn gesture_swipe_end( |
| 144 | &mut self, |
| 145 | data: &mut State, |
| 146 | handle: &mut PointerInnerHandle<'_, State>, |
| 147 | event: &GestureSwipeEndEvent, |
| 148 | ) { |
| 149 | handle.gesture_swipe_end(data, event); |
| 150 | } |
| 151 | |
| 152 | fn gesture_pinch_begin( |
| 153 | &mut self, |
| 154 | data: &mut State, |
| 155 | handle: &mut PointerInnerHandle<'_, State>, |
| 156 | event: &GesturePinchBeginEvent, |
| 157 | ) { |
| 158 | handle.gesture_pinch_begin(data, event); |
| 159 | } |
| 160 | |
| 161 | fn gesture_pinch_update( |
| 162 | &mut self, |
| 163 | data: &mut State, |
| 164 | handle: &mut PointerInnerHandle<'_, State>, |
| 165 | event: &GesturePinchUpdateEvent, |
| 166 | ) { |
| 167 | handle.gesture_pinch_update(data, event); |
| 168 | } |
| 169 | |
| 170 | fn gesture_pinch_end( |
| 171 | &mut self, |
| 172 | data: &mut State, |
| 173 | handle: &mut PointerInnerHandle<'_, State>, |
| 174 | event: &GesturePinchEndEvent, |
| 175 | ) { |
| 176 | handle.gesture_pinch_end(data, event); |
| 177 | } |
| 178 | |
| 179 | fn gesture_hold_begin( |
| 180 | &mut self, |
| 181 | data: &mut State, |
| 182 | handle: &mut PointerInnerHandle<'_, State>, |
| 183 | event: &GestureHoldBeginEvent, |
| 184 | ) { |
| 185 | handle.gesture_hold_begin(data, event); |
| 186 | } |
| 187 | |
| 188 | fn gesture_hold_end( |
| 189 | &mut self, |
| 190 | data: &mut State, |
| 191 | handle: &mut PointerInnerHandle<'_, State>, |
| 192 | event: &GestureHoldEndEvent, |
| 193 | ) { |
| 194 | handle.gesture_hold_end(data, event); |
| 195 | } |
| 196 | |
| 197 | fn start_data(&self) -> &PointerGrabStartData<State> { |
| 198 | self.start_data.unwrap_pointer() |
| 199 | } |
| 200 | |
| 201 | fn unset(&mut self, data: &mut State) { |
| 202 | self.on_ungrab(data); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl TouchGrab<State> for ResizeGrab { |
| 207 | fn down( |
| 208 | &mut self, |
| 209 | data: &mut State, |
| 210 | handle: &mut TouchInnerHandle<'_, State>, |
| 211 | _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, |
| 212 | event: &touch::DownEvent, |
| 213 | ) { |
| 214 | handle.down(data, None, event); |
| 215 | } |
| 216 | |
| 217 | fn up( |
| 218 | &mut self, |
| 219 | data: &mut State, |
| 220 | handle: &mut TouchInnerHandle<'_, State>, |
| 221 | event: &touch::UpEvent, |
| 222 | ) { |
| 223 | handle.up(data, event); |
| 224 | |
| 225 | if event.slot != self.start_data.unwrap_touch().slot { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | handle.unset_grab(self, data); |
| 230 | } |
| 231 | |
| 232 | fn motion( |
| 233 | &mut self, |
| 234 | data: &mut State, |
| 235 | handle: &mut TouchInnerHandle<'_, State>, |
| 236 | _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, |
| 237 | event: &touch::MotionEvent, |
| 238 | ) { |
| 239 | handle.motion(data, None, event); |
| 240 | |
| 241 | if event.slot != self.start_data.unwrap_touch().slot { |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | self.new_location = event.location; |
| 246 | } |
| 247 | |
| 248 | fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { |
| 249 | handle.frame(data); |
| 250 | |
| 251 | if !self.on_frame(data) { |
| 252 | // The gesture is no longer ongoing. |
| 253 | handle.unset_grab(self, data); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { |
| 258 | handle.cancel(data); |
| 259 | handle.unset_grab(self, data); |
| 260 | } |
| 261 | |
| 262 | fn shape( |
| 263 | &mut self, |
| 264 | data: &mut State, |
| 265 | handle: &mut TouchInnerHandle<'_, State>, |
| 266 | event: &touch::ShapeEvent, |
| 267 | ) { |
| 268 | handle.shape(data, event); |
| 269 | } |
| 270 | |
| 271 | fn orientation( |
| 272 | &mut self, |
| 273 | data: &mut State, |
| 274 | handle: &mut TouchInnerHandle<'_, State>, |
| 275 | event: &touch::OrientationEvent, |
| 276 | ) { |
| 277 | handle.orientation(data, event); |
| 278 | } |
| 279 | |
| 280 | fn start_data(&self) -> &TouchGrabStartData<State> { |
| 281 | self.start_data.unwrap_touch() |
| 282 | } |
| 283 | |
| 284 | fn unset(&mut self, data: &mut State) { |
| 285 | self.on_ungrab(data); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | impl TabletToolGrab<State> for ResizeGrab { |
| 290 | fn start_data(&self) -> &tablet::tool::GrabStartData<State> { |
| 291 | self.start_data.unwrap_tablet_tool() |
| 292 | } |
| 293 | |
| 294 | fn proximity_out( |
| 295 | &mut self, |
| 296 | data: &mut State, |
| 297 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 298 | event: &tablet::tool::ProximityOutEvent, |
| 299 | ) { |
| 300 | handle.proximity_out(data, event); |
| 301 | handle.unset_grab(self, data, event.serial, event.time, false); |
| 302 | } |
| 303 | |
| 304 | fn motion( |
| 305 | &mut self, |
| 306 | data: &mut State, |
| 307 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 308 | _focus: Option<(<State as TabletSeatHandler>::ToolFocus, Point<f64, Logical>)>, |
| 309 | event: &tablet::tool::MotionEvent, |
| 310 | ) { |
| 311 | handle.motion(data, None, event); |
| 312 | |
| 313 | self.new_location = event.location; |
| 314 | } |
| 315 | |
| 316 | fn down( |
| 317 | &mut self, |
| 318 | data: &mut State, |
| 319 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 320 | event: &tablet::tool::DownEvent, |
| 321 | ) { |
| 322 | handle.down(data, event); |
| 323 | } |
| 324 | |
| 325 | fn up( |
| 326 | &mut self, |
| 327 | data: &mut State, |
| 328 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 329 | event: &tablet::tool::UpEvent, |
| 330 | ) { |
| 331 | handle.up(data, event); |
| 332 | handle.unset_grab(self, data, event.serial, event.time, true); |
| 333 | } |
| 334 | |
| 335 | fn button( |
| 336 | &mut self, |
| 337 | data: &mut State, |
| 338 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 339 | event: &tablet::tool::ButtonEvent, |
| 340 | ) { |
| 341 | handle.button(data, event); |
| 342 | } |
| 343 | |
| 344 | fn axis( |
| 345 | &mut self, |
| 346 | data: &mut State, |
| 347 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 348 | frame: tablet::tool::AxisFrame, |
| 349 | ) { |
| 350 | handle.axis(data, frame); |
| 351 | } |
| 352 | |
| 353 | fn frame( |
| 354 | &mut self, |
| 355 | data: &mut State, |
| 356 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 357 | time: u32, |
| 358 | ) { |
| 359 | handle.frame(data, time); |
| 360 | |
| 361 | if !self.on_frame(data) { |
| 362 | // The gesture is no longer ongoing. |
| 363 | handle.unset_grab( |
| 364 | self, |
| 365 | data, |
| 366 | SERIAL_COUNTER.next_serial(), |
| 367 | get_monotonic_time().as_millis() as u32, |
| 368 | true, |
| 369 | ); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | fn unset(&mut self, data: &mut State) { |
| 374 | self.on_ungrab(data); |
| 375 | } |
| 376 | } |