Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix(desktop): aim each Windows absolute move at the first value of its pixel's bin

Marcelito56b456d110c2
diff --git a/apps/desktop/src-tauri/src/computer/winput.rs b/apps/desktop/src-tauri/src/computer/winput.rs
index 2f03d63..cc4ef71 100644
--- a/apps/desktop/src-tauri/src/computer/winput.rs
+++ b/apps/desktop/src-tauri/src/computer/winput.rs
@@ -31,7 +31,7 @@ pub struct VirtualScreen {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Step {
- /// An absolute move, normalized to 0..=65535 over the virtual screen: the centre of the
+ /// An absolute move, normalized to 0..=65535 over the virtual screen: the first value of the
/// pixel's bin, since Windows splits that range into one equal bin per pixel.
Move { x: i32, y: i32 },
Button { button: MouseButton, down: bool },
@@ -46,13 +46,15 @@ pub enum Step {
impl VirtualScreen {
/// The absolute move to `at`, clamped to the virtual screen. Windows maps a normalized
/// coordinate `n` to pixel `n * length / 65536` (one equal bin per pixel), so each pixel is
- /// aimed at the centre of its own bin: interpolating between the end pixels instead can land
- /// an interior point on its neighbour.
+ /// aimed at the first value of its own bin, `ceil(offset * 65536 / length)`. That is exact
+ /// for every axis up to 65536 pixels and never passes 65535; interpolating between the end
+ /// pixels, or aiming at a bin's centre once a bin is under two values wide, lands some
+ /// points on a neighbour. Beyond 65536 pixels absolute input cannot tell every pixel apart.
pub fn move_to(&self, at: Point) -> Step {
let axis = |value: f64, start: i32, length: i32| {
let length = i64::from(length.max(1));
let offset = ((value - f64::from(start)).round() as i64).clamp(0, length - 1);
- ((2 * offset + 1) * 65536 / (2 * length)) as i32
+ ((offset * 65536 + length - 1) / length).min(65535) as i32
};
Step::Move { x: axis(at.x, self.x, self.width), y: axis(at.y, self.y, self.height) }
}
@@ -288,11 +290,11 @@ mod tests {
#[test]
fn moves_are_absolute_over_the_whole_virtual_screen() {
- // Each pixel is the centre of its bin: the first and last ones are just inside 0 and 65535.
- assert_eq!(SCREEN.move_to(at(-1920.0, 0.0)), Step::Move { x: 8, y: 30 });
- assert_eq!(SCREEN.move_to(at(1919.0, 1079.0)), Step::Move { x: 65527, y: 65505 });
+ // Each pixel is the first value of its bin: the first one is 0, the last one inside 65535.
+ assert_eq!(SCREEN.move_to(at(-1920.0, 0.0)), Step::Move { x: 0, y: 0 });
+ assert_eq!(SCREEN.move_to(at(1919.0, 1079.0)), Step::Move { x: 65519, y: 65476 });
// The primary display's top-left corner sits in the middle of the virtual screen.
- assert_eq!(SCREEN.move_to(at(0.0, 0.0)), Step::Move { x: 32776, y: 30 });
+ assert_eq!(SCREEN.move_to(at(0.0, 0.0)), Step::Move { x: 32768, y: 0 });
}
/// The pixel Windows moves to for a normalized coordinate: absolute input splits 0..=65535
@@ -309,6 +311,9 @@ mod tests {
VirtualScreen { x: -7680, y: -2160, width: 15360, height: 4320 },
VirtualScreen { x: 0, y: 0, width: 1, height: 1 },
VirtualScreen { x: 5, y: -3, width: 2, height: 3 },
+ // Axes above 32770 pixels, where a bin is under two normalized values wide, up to the
+ // 65536 pixels absolute input can tell apart.
+ VirtualScreen { x: -20000, y: 0, width: 40000, height: 65536 },
];
for screen in screens {
for x in screen.x..screen.x + screen.width {
@@ -501,10 +506,10 @@ mod tests {
assert_eq!((landing(x, 0, 1920), landing(y, -1080, 2160)), (0, -1080));
let Step::Move { x, y } = above.move_to(at(1919.0, 1079.0)) else { unreachable!() };
assert_eq!((landing(x, 0, 1920), landing(y, -1080, 2160)), (1919, 1079));
- // One pixel is one bin: the whole range, aimed at its centre, wherever the point is.
+ // One pixel is one bin: the whole range, wherever the point is.
let tiny = VirtualScreen { x: 0, y: 0, width: 1, height: 1 };
- assert_eq!(tiny.move_to(at(0.0, 0.0)), Step::Move { x: 32768, y: 32768 });
- assert_eq!(tiny.move_to(at(3.0, 3.0)), Step::Move { x: 32768, y: 32768 });
+ assert_eq!(tiny.move_to(at(0.0, 0.0)), Step::Move { x: 0, y: 0 });
+ assert_eq!(tiny.move_to(at(3.0, 3.0)), Step::Move { x: 0, y: 0 });
}
#[test]