From 8aea6cc15836ccb2eea1cdac6eb3b253eeb59d8f Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Thu, 28 May 2026 13:04:21 -0700 Subject: [PATCH] image-copy: Function for duplicated toplevel cursor enter/motion code Deduplicate this before making it more complicated. Also allows early returns to avoid deep nesting. And avoid duplicating calculations here for multiple sessions (though normally there won't be multiple capture sessions of same toplevel with cursor metadata, probably). --- src/shell/focus/target.rs | 76 +++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/shell/focus/target.rs b/src/shell/focus/target.rs index ef749a64..b606dc17 100644 --- a/src/shell/focus/target.rs +++ b/src/shell/focus/target.rs @@ -225,6 +225,42 @@ impl PointerFocusTarget { _ => false, } } + + // Update image copy cursor position/hotspot for enter/motion event + fn update_image_copy_cursor_position( + &self, + seat: &Seat, + data: &mut State, + event: &PointerMotionEvent, + ) { + let Some(toplevel) = self.toplevel(&data.common.shell.read()) else { + return; + }; + let cursor_sessions = toplevel.cursor_sessions(); + if cursor_sessions.is_empty() { + return; + } + + let cursor_pos = Some( + event + .location + .to_buffer(1.0, Transform::Normal, &toplevel.geometry().size.to_f64()) + .to_i32_round(), + ); + + let cursor_hotspot = if let Some((_, hotspot)) = + seat.cursor_geometry((0.0, 0.0), Duration::from_millis(event.time as u64).into()) + { + hotspot + } else { + Point::from((0, 0)) + }; + + for session in cursor_sessions { + session.set_cursor_pos(cursor_pos); + session.set_cursor_hotspot(cursor_hotspot); + } + } } impl KeyboardFocusTarget { @@ -337,47 +373,11 @@ impl IsAlive for KeyboardFocusTarget { impl PointerTarget for PointerFocusTarget { fn enter(&self, seat: &Seat, data: &mut State, event: &PointerMotionEvent) { - let toplevel = self.toplevel(&data.common.shell.read()); - if let Some(element) = toplevel { - for session in element.cursor_sessions() { - session.set_cursor_pos(Some( - event - .location - .to_buffer(1.0, Transform::Normal, &element.geometry().size.to_f64()) - .to_i32_round(), - )); - if let Some((_, hotspot)) = seat - .cursor_geometry((0.0, 0.0), Duration::from_millis(event.time as u64).into()) - { - session.set_cursor_hotspot(hotspot); - } else { - session.set_cursor_hotspot((0, 0)); - } - } - } - + self.update_image_copy_cursor_position(seat, data, event); self.inner_pointer_target().enter(seat, data, event); } fn motion(&self, seat: &Seat, data: &mut State, event: &PointerMotionEvent) { - let toplevel = self.toplevel(&data.common.shell.read()); - if let Some(element) = toplevel { - for session in element.cursor_sessions() { - session.set_cursor_pos(Some( - event - .location - .to_buffer(1.0, Transform::Normal, &element.geometry().size.to_f64()) - .to_i32_round(), - )); - if let Some((_, hotspot)) = seat - .cursor_geometry((0.0, 0.0), Duration::from_millis(event.time as u64).into()) - { - session.set_cursor_hotspot(hotspot); - } else { - session.set_cursor_hotspot((0, 0)); - } - } - } - + self.update_image_copy_cursor_position(seat, data, event); self.inner_pointer_target().motion(seat, data, event); } fn relative_motion(&self, seat: &Seat, data: &mut State, event: &RelativeMotionEvent) {