Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

refactor(cursor-overlay): extract rotate_toward into shared util crate (#1662)

Francesco Bonacci6978a0954bd6
diff --git a/libs/cua-driver-rs/crates/cursor-overlay/src/lib.rs b/libs/cua-driver-rs/crates/cursor-overlay/src/lib.rs
index b2c6011b1b..d4349115a5 100644
--- a/libs/cua-driver-rs/crates/cursor-overlay/src/lib.rs
+++ b/libs/cua-driver-rs/crates/cursor-overlay/src/lib.rs
@@ -14,6 +14,7 @@ pub mod bezier;
pub mod path_planner;
pub mod shape;
pub mod capture_utils;
+pub mod util;
pub use palette::Palette;
pub use motion::MotionConfig;
diff --git a/libs/cua-driver-rs/crates/cursor-overlay/src/util.rs b/libs/cua-driver-rs/crates/cursor-overlay/src/util.rs
new file mode 100644
index 0000000000..7cf6ebbbeb
--- /dev/null
+++ b/libs/cua-driver-rs/crates/cursor-overlay/src/util.rs
@@ -0,0 +1,57 @@
+//! Tiny shared utilities for per-OS overlay renderers.
+//!
+//! These were duplicated verbatim in `platform-windows/src/overlay.rs` and
+//! `platform-linux/src/overlay.rs`. Centralising them here keeps the math
+//! truth in one place + lets future ports (Wayland-native, X11 v2, etc.)
+//! reuse the same primitives without re-deriving them.
+
+/// Rotate the `current` angle toward `desired` by at most `max_step` radians
+/// per call, normalising both inputs to (-π, π].
+///
+/// Used by the per-OS overlay render loops to ease the cursor's heading
+/// toward its planned-path tangent without instant snap. Both Windows + Linux
+/// renderers want identical semantics here — exact constants matter because
+/// the easing rate is tuned against the same Motion config knobs.
+pub fn rotate_toward(current: f64, desired: f64, max_step: f64) -> f64 {
+ let mut diff = desired - current;
+ while diff > std::f64::consts::PI {
+ diff -= 2.0 * std::f64::consts::PI;
+ }
+ while diff < -std::f64::consts::PI {
+ diff += 2.0 * std::f64::consts::PI;
+ }
+ current + diff.clamp(-max_step, max_step)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ const PI: f64 = std::f64::consts::PI;
+
+ #[test]
+ fn no_motion_when_aligned() {
+ assert_eq!(rotate_toward(0.5, 0.5, 0.1), 0.5);
+ }
+
+ #[test]
+ fn clamps_to_max_step() {
+ // desired is +1.0 ahead, max_step = 0.1, so we only move 0.1
+ assert!((rotate_toward(0.0, 1.0, 0.1) - 0.1).abs() < 1e-12);
+ }
+
+ #[test]
+ fn picks_short_way_around() {
+ // current = 0.95π, desired = -0.95π (almost a full rotation away).
+ // Short way is +0.1π (across the wrap), NOT -1.9π.
+ let out = rotate_toward(0.95 * PI, -0.95 * PI, 0.5);
+ // We expect motion in the POSITIVE direction (forward wrap).
+ assert!(out > 0.95 * PI, "expected positive motion, got {}", out);
+ }
+
+ #[test]
+ fn negative_direction_when_shorter() {
+ // current = -0.95π, desired = 0.95π — short way is the negative wrap.
+ let out = rotate_toward(-0.95 * PI, 0.95 * PI, 0.5);
+ assert!(out < -0.95 * PI, "expected negative motion, got {}", out);
+ }
+}
diff --git a/libs/cua-driver-rs/crates/platform-linux/src/overlay.rs b/libs/cua-driver-rs/crates/platform-linux/src/overlay.rs
index d3216af9a7..07a4971e72 100644
--- a/libs/cua-driver-rs/crates/platform-linux/src/overlay.rs
+++ b/libs/cua-driver-rs/crates/platform-linux/src/overlay.rs
@@ -236,12 +236,9 @@ impl RenderState {
}
}
-fn rotate_toward(current: f64, desired: f64, max_step: f64) -> f64 {
- let mut diff = desired - current;
- while diff > std::f64::consts::PI { diff -= 2.0 * std::f64::consts::PI; }
- while diff < -std::f64::consts::PI { diff += 2.0 * std::f64::consts::PI; }
- current + diff.clamp(-max_step, max_step)
-}
+// Shared with platform-windows — pulled into `cursor_overlay::util` so both
+// per-OS render loops use the exact same easing primitive.
+use cursor_overlay::util::rotate_toward;
// ── Renderer (shared tiny-skia logic) ─────────────────────────────────────
diff --git a/libs/cua-driver-rs/crates/platform-windows/src/overlay.rs b/libs/cua-driver-rs/crates/platform-windows/src/overlay.rs
index 7164415ff1..648b81b3a0 100644
--- a/libs/cua-driver-rs/crates/platform-windows/src/overlay.rs
+++ b/libs/cua-driver-rs/crates/platform-windows/src/overlay.rs
@@ -303,12 +303,9 @@ impl RenderState {
}
}
-fn rotate_toward(current: f64, desired: f64, max_step: f64) -> f64 {
- let mut diff = desired - current;
- while diff > std::f64::consts::PI { diff -= 2.0 * std::f64::consts::PI; }
- while diff < -std::f64::consts::PI { diff += 2.0 * std::f64::consts::PI; }
- current + diff.clamp(-max_step, max_step)
-}
+// Shared with platform-linux — pulled into `cursor_overlay::util` so both
+// per-OS render loops use the exact same easing primitive.
+use cursor_overlay::util::rotate_toward;
// ── tiny-skia render (shared logic) ──────────────────────────────────────