From 27fb88d24c26d35793066e767ea2cec3b3e78517 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 19 May 2025 13:29:02 +0100 Subject: [PATCH 1/3] Fix crop normalization logic --- crates/cursor-capture/src/position.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/cursor-capture/src/position.rs b/crates/cursor-capture/src/position.rs index 13731825605..e5fe314c4b4 100644 --- a/crates/cursor-capture/src/position.rs +++ b/crates/cursor-capture/src/position.rs @@ -134,14 +134,19 @@ impl NormalizedCursorPosition { } pub fn with_crop(&self, position: (f32, f32), size: (f32, f32)) -> Self { + // Translate the current normalized coordinates back to the "raw" space + // before applying the new crop. The existing implementation divided by + // the crop size and subtracted the position which resulted in invalid + // values. We actually need to scale by the current crop size and add + // the crop position to undo the previous crop transformation. let raw_normalized = ( - self.x / self.crop_size.0 - self.crop_position.0, - self.y / self.crop_size.1 - self.crop_position.1, + self.x * self.crop_size.0 + self.crop_position.0, + self.y * self.crop_size.1 + self.crop_position.1, ); Self { - x: raw_normalized.0 / size.0 - position.0, - y: raw_normalized.1 / size.1 - position.1, + x: (raw_normalized.0 - position.0) / size.0, + y: (raw_normalized.1 - position.1) / size.1, crop_position: position, crop_size: size, display: self.display, From 17b85aed3d1794c39d4ae8548f6ba666dd998582 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Tue, 20 May 2025 15:20:38 +0800 Subject: [PATCH 2/3] cleanup --- crates/cursor-capture/src/position.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/crates/cursor-capture/src/position.rs b/crates/cursor-capture/src/position.rs index e5fe314c4b4..ee0ecc2fc63 100644 --- a/crates/cursor-capture/src/position.rs +++ b/crates/cursor-capture/src/position.rs @@ -134,19 +134,14 @@ impl NormalizedCursorPosition { } pub fn with_crop(&self, position: (f32, f32), size: (f32, f32)) -> Self { - // Translate the current normalized coordinates back to the "raw" space - // before applying the new crop. The existing implementation divided by - // the crop size and subtracted the position which resulted in invalid - // values. We actually need to scale by the current crop size and add - // the crop position to undo the previous crop transformation. - let raw_normalized = ( + let raw_px = ( self.x * self.crop_size.0 + self.crop_position.0, self.y * self.crop_size.1 + self.crop_position.1, ); Self { - x: (raw_normalized.0 - position.0) / size.0, - y: (raw_normalized.1 - position.1) / size.1, + x: (raw_px.0 - position.0) / size.0, + y: (raw_px.1 - position.1) / size.1, crop_position: position, crop_size: size, display: self.display, From ba464be432a7df43fde908349ba7e6e6b7078f4f Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Tue, 20 May 2025 15:51:21 +0800 Subject: [PATCH 3/3] add tests --- crates/cursor-capture/src/position.rs | 143 ++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/crates/cursor-capture/src/position.rs b/crates/cursor-capture/src/position.rs index ee0ecc2fc63..26cab0807ea 100644 --- a/crates/cursor-capture/src/position.rs +++ b/crates/cursor-capture/src/position.rs @@ -157,3 +157,146 @@ impl std::fmt::Debug for NormalizedCursorPosition { .finish() } } + +#[cfg(test)] +mod tests { + use super::*; + use cap_displays::Display; + + // Helper function to create a mock Display for testing + fn mock_display() -> Display { + Display::list()[0].clone() + } + + #[test] + fn test_with_crop_no_change() { + let display = mock_display(); + let original_normalized = NormalizedCursorPosition { + x: 0.5, + y: 0.5, + crop_position: (0.0, 0.0), + crop_size: (1.0, 1.0), + display, + }; + + let cropped_position = (0.0, 0.0); + let cropped_size = (1.0, 1.0); + let new_normalized = original_normalized.with_crop(cropped_position, cropped_size); + + assert_eq!(new_normalized.x, 0.5); + assert_eq!(new_normalized.y, 0.5); + assert_eq!(new_normalized.crop_position(), (0.0, 0.0)); + assert_eq!(new_normalized.crop_size(), (1.0, 1.0)); + } + + #[test] + fn test_with_crop_centered() { + let display = mock_display(); + let original_normalized = NormalizedCursorPosition { + x: 0.5, + y: 0.5, + crop_position: (0.0, 0.0), + crop_size: (1.0, 1.0), + display, + }; + + let cropped_position = (0.25, 0.25); + let cropped_size = (0.5, 0.5); + let new_normalized = original_normalized.with_crop(cropped_position, cropped_size); + + // Original point (0.5, 0.5) is in the center of the (0,0) to (1,1) range. + // The new crop is from (0.25, 0.25) to (0.75, 0.75). + // The original point (0.5, 0.5) should still be in the center of this new crop. + let expected_x = (0.5 * 1.0 + 0.0 - 0.25) / 0.5; + let expected_y = (0.5 * 1.0 + 0.0 - 0.25) / 0.5; + + assert!((new_normalized.x - expected_x).abs() < f32::EPSILON); + assert!((new_normalized.y - expected_y).abs() < f32::EPSILON); + assert_eq!(new_normalized.crop_position(), (0.25, 0.25)); + assert_eq!(new_normalized.crop_size(), (0.5, 0.5)); + } + + #[test] + fn test_with_crop_top_left_of_crop() { + let display = mock_display(); + + let cropped_position = (0.25, 0.25); + let cropped_size = (0.5, 0.5); + + let original_normalized_at_crop_tl = NormalizedCursorPosition { + x: 0.25, + y: 0.25, + crop_position: (0.0, 0.0), + crop_size: (1.0, 1.0), + display, + }; + + let new_normalized = + original_normalized_at_crop_tl.with_crop(cropped_position, cropped_size); + + // The point that was at the top-left of the crop in the original space + // should now be at (0.0, 0.0) in the new cropped space. + assert!((new_normalized.x - 0.0).abs() < f32::EPSILON); + assert!((new_normalized.y - 0.0).abs() < f32::EPSILON); + assert_eq!(new_normalized.crop_position(), (0.25, 0.25)); + assert_eq!(new_normalized.crop_size(), (0.5, 0.5)); + } + + #[test] + fn test_with_crop_bottom_right_of_crop() { + let display = mock_display(); + + let cropped_position = (0.25, 0.25); + let cropped_size = (0.5, 0.5); + + let original_normalized_at_crop_br = NormalizedCursorPosition { + x: 0.75, + y: 0.75, + crop_position: (0.0, 0.0), + crop_size: (1.0, 1.0), + display, + }; + + let new_normalized = + original_normalized_at_crop_br.with_crop(cropped_position, cropped_size); + + // The point that was at the bottom-right of the crop in the original space + // should now be at (1.0, 1.0) in the new cropped space. + assert!((new_normalized.x - 1.0).abs() < f32::EPSILON); + assert!((new_normalized.y - 1.0).abs() < f32::EPSILON); + assert_eq!(new_normalized.crop_position(), (0.25, 0.25)); + assert_eq!(new_normalized.crop_size(), (0.5, 0.5)); + } + + #[test] + fn test_with_crop_from_existing_crop() { + let display = mock_display(); + let original_normalized = NormalizedCursorPosition { + x: 0.5, // This 0.5 is within the first crop + y: 0.5, // This 0.5 is within the first crop + crop_position: (0.1, 0.1), + crop_size: (0.8, 0.8), + display, + }; + + // The raw position of the cursor is 0.5 within the 0.1 to 0.9 range. + // Raw x = 0.5 * 0.8 + 0.1 = 0.4 + 0.1 = 0.5 + // Raw y = 0.5 * 0.8 + 0.1 = 0.4 + 0.1 = 0.5 + + let second_crop_position = (0.2, 0.2); + let second_crop_size = (0.6, 0.6); + + // The second crop is from 0.2 to 0.8 in the original space. + // The raw position is (0.5, 0.5). + // In the second crop space, this should be: + // x = (0.5 - 0.2) / 0.6 = 0.3 / 0.6 = 0.5 + // y = (0.5 - 0.2) / 0.6 = 0.3 / 0.6 = 0.5 + + let new_normalized = original_normalized.with_crop(second_crop_position, second_crop_size); + + assert!((new_normalized.x - 0.5).abs() < f32::EPSILON); + assert!((new_normalized.y - 0.5).abs() < f32::EPSILON); + assert_eq!(new_normalized.crop_position(), (0.2, 0.2)); + assert_eq!(new_normalized.crop_size(), (0.6, 0.6)); + } +}