input: Clip pointer movement smarter
Browse files at this commit934183ef003714f4d8dedeba55396c76ec8d5e4cParent: 72d6b381 changed file +57 -20
- src/input.rs +57 -20
src/input.rsmodified+57 -20
| @@ -294,36 +294,73 @@ impl State { | |||
| 294 | } | 294 | } |
| 295 | } | 295 | } |
| 296 | InputEvent::PointerMotion { event, .. } => { | 296 | InputEvent::PointerMotion { event, .. } => { |
| 297 | + // We need an output to be able to move the pointer. | ||
| 298 | + if self.niri.global_space.outputs().next().is_none() { | ||
| 299 | + return; | ||
| 300 | + } | ||
| 301 | + | ||
| 297 | let serial = SERIAL_COUNTER.next_serial(); | 302 | let serial = SERIAL_COUNTER.next_serial(); |
| 298 | 303 | ||
| 299 | let pointer = self.niri.seat.get_pointer().unwrap(); | 304 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 300 | - let mut pos = pointer.current_location(); | ||
| 301 | - | ||
| 302 | - pos += event.delta(); | ||
| 303 | - | ||
| 304 | - let mut min_x = i32::MAX; | ||
| 305 | - let mut min_y = i32::MAX; | ||
| 306 | - let mut max_x = 0; | ||
| 307 | - let mut max_y = 0; | ||
| 308 | - for output in self.niri.global_space.outputs() { | ||
| 309 | - // FIXME: smarter clamping. | ||
| 310 | - let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 311 | - min_x = min_x.min(geom.loc.x); | ||
| 312 | - min_y = min_y.min(geom.loc.y); | ||
| 313 | - max_x = max_x.max(geom.loc.x + geom.size.w); | ||
| 314 | - max_y = max_y.max(geom.loc.y + geom.size.h); | ||
| 315 | - } | ||
| 316 | 305 | ||
| 317 | - pos.x = pos.x.clamp(min_x as f64, max_x as f64); | 306 | + let pos = pointer.current_location(); |
| 318 | - pos.y = pos.y.clamp(min_y as f64, max_y as f64); | 307 | + |
| 308 | + // We have an output, so we can compute the new location and focus. | ||
| 309 | + let mut new_pos = pos + event.delta(); | ||
| 310 | + | ||
| 311 | + if self | ||
| 312 | + .niri | ||
| 313 | + .global_space | ||
| 314 | + .output_under(new_pos) | ||
| 315 | + .next() | ||
| 316 | + .is_none() | ||
| 317 | + { | ||
| 318 | + // We ended up outside the outputs and need to clip the movement. | ||
| 319 | + if let Some(output) = self.niri.global_space.output_under(pos).next() { | ||
| 320 | + // The pointer was previously on some output. Clip the movement against its | ||
| 321 | + // boundaries. | ||
| 322 | + let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 323 | + new_pos.x = new_pos | ||
| 324 | + .x | ||
| 325 | + .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); | ||
| 326 | + new_pos.y = new_pos | ||
| 327 | + .y | ||
| 328 | + .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 329 | + } else { | ||
| 330 | + // The pointer was not on any output in the first place. Find one for it. | ||
| 331 | + // Start by clamping the X coordinate. | ||
| 332 | + let mut min_x = i32::MAX; | ||
| 333 | + let mut max_x = 0; | ||
| 334 | + for output in self.niri.global_space.outputs() { | ||
| 335 | + let geom = self.niri.global_space.output_geometry(output).unwrap(); | ||
| 336 | + min_x = min_x.min(geom.loc.x); | ||
| 337 | + max_x = max_x.max(geom.loc.x + geom.size.w); | ||
| 338 | + } | ||
| 339 | + | ||
| 340 | + new_pos.x = new_pos.x.clamp(min_x as f64, (max_x - 1) as f64); | ||
| 341 | + | ||
| 342 | + // Now clamp the Y coordinate. Assume every X from min to max has some | ||
| 343 | + // output. | ||
| 344 | + let geom = self | ||
| 345 | + .niri | ||
| 346 | + .global_space | ||
| 347 | + .outputs() | ||
| 348 | + .map(|output| self.niri.global_space.output_geometry(output).unwrap()) | ||
| 349 | + .find(|geom| geom.contains((new_pos.x as i32, geom.loc.y))) | ||
| 350 | + .unwrap(); | ||
| 351 | + new_pos.y = new_pos | ||
| 352 | + .y | ||
| 353 | + .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); | ||
| 354 | + } | ||
| 355 | + } | ||
| 319 | 356 | ||
| 320 | - let under = self.niri.surface_under_and_global_space(pos); | 357 | + let under = self.niri.surface_under_and_global_space(new_pos); |
| 321 | 358 | ||
| 322 | pointer.motion( | 359 | pointer.motion( |
| 323 | self, | 360 | self, |
| 324 | under.clone(), | 361 | under.clone(), |
| 325 | &MotionEvent { | 362 | &MotionEvent { |
| 326 | - location: pos, | 363 | + location: new_pos, |
| 327 | serial, | 364 | serial, |
| 328 | time: event.time_msec(), | 365 | time: event.time_msec(), |
| 329 | }, | 366 | }, |