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.

fix(cua-driver): derive the macOS desktop point ratio from the display mode instead of a screenshot (#5340)

injaneityac825f8b9287
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs
index 3bfa28773a..bb9d8e5ac4 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/click.rs
@@ -435,11 +435,6 @@ impl Tool for ClickTool {
// x,y arrive in desktop-SCREENSHOT space (what the agent reads off the
// PNG) and must be divided by the screenshot↔logical ratio, or a
// center-pixel pick warps to the corner (off by the backing scale).
- //
- // Derive the ratio the same way `get_desktop_state` reports it: native
- // screenshot width / logical screen width. This is robust even when
- // CGDisplayPixelsWide under-reports the backing scale (it returns the
- // scaled-mode point width on some Retina configs → a bogus 1.0).
let (sx, sy) = if let Some(ref capture_id) = capture_id {
match self
.state
@@ -450,21 +445,7 @@ impl Tool for ClickTool {
Err(refusal) => return refusal,
}
} else {
- let desktop_ratio = tokio::task::spawn_blocking(|| {
- let logical_w =
- super::get_screen_size::main_screen_size().map(|(w, _, _)| w as f64);
- let shot_w = crate::capture::screenshot_display_bytes()
- .ok()
- .and_then(|png| crate::capture::png_dimensions(&png).ok())
- .map(|(w, _)| w as f64);
- match (shot_w, logical_w) {
- (Some(sw), Some(lw)) if lw > 0.0 && sw > lw => sw / lw,
- _ => 1.0,
- }
- })
- .await
- .unwrap_or(1.0);
- (sx_shot / desktop_ratio, sy_shot / desktop_ratio)
+ super::desktop_screenshot_point(sx_shot, sy_shot)
};
let button = match input.button.unwrap_or(ClickButton::Left) {
ClickButton::Left => "left",
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/drag.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/drag.rs
index 24db90a04a..40a18047a2 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/drag.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/drag.rs
@@ -143,8 +143,8 @@ impl Tool for DragTool {
Err(result) => return result,
};
let (from_x, from_y, to_x, to_y) = (input.from_x, input.from_y, input.to_x, input.to_y);
- let (from_x, from_y) = super::desktop_screenshot_point(from_x, from_y).await;
- let (to_x, to_y) = super::desktop_screenshot_point(to_x, to_y).await;
+ let (from_x, from_y) = super::desktop_screenshot_point(from_x, from_y);
+ let (to_x, to_y) = super::desktop_screenshot_point(to_x, to_y);
if let Some(refusal) = super::desktop_pointer_guard::check(
"drag",
&args,
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/mod.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/mod.rs
index b45e8b603b..4cde8b43bb 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/mod.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/mod.rs
@@ -751,23 +751,9 @@ pub fn load_driver_config() -> DriverConfig {
cfg
}
-/// Convert native pixels from `get_desktop_state` into the logical point space
-/// used by CoreGraphics input APIs. Retina scaled modes cannot rely on the
-/// nominal backing factor alone, so derive the ratio from the actual PNG.
-pub async fn desktop_screenshot_point(x: f64, y: f64) -> (f64, f64) {
- let ratio = tokio::task::spawn_blocking(|| {
- let logical_w = get_screen_size::main_screen_size().map(|(w, _, _)| w as f64);
- let shot_w = crate::capture::screenshot_display_bytes()
- .ok()
- .and_then(|png| crate::capture::png_dimensions(&png).ok())
- .map(|(w, _)| w as f64);
- match (shot_w, logical_w) {
- (Some(sw), Some(lw)) if lw > 0.0 && sw > lw => sw / lw,
- _ => 1.0,
- }
- })
- .await
- .unwrap_or(1.0);
+/// Convert native pixels from `get_desktop_state` into logical points.
+pub fn desktop_screenshot_point(x: f64, y: f64) -> (f64, f64) {
+ let ratio = get_screen_size::main_screen_size().map_or(1.0, |(_, _, s)| s);
(x / ratio, y / ratio)
}
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/move_cursor.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/move_cursor.rs
index 669f409bf6..4e6831a69c 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/move_cursor.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/move_cursor.rs
@@ -69,7 +69,7 @@ impl Tool for MoveCursorTool {
Err(result) => return result,
};
let (x, y) = (input.x, input.y);
- let (x, y) = super::desktop_screenshot_point(x, y).await;
+ let (x, y) = super::desktop_screenshot_point(x, y);
if let Some(refusal) =
super::desktop_pointer_guard::check("move_cursor", &args, vec![(x, y)]).await
{
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/scroll.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/scroll.rs
index 87a6f05c67..aa7a814dea 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/scroll.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/scroll.rs
@@ -145,7 +145,7 @@ impl Tool for ScrollTool {
ScrollDirection::Right => (0, -step),
ScrollDirection::Left => (0, step),
};
- let (x, y) = super::desktop_screenshot_point(x, y).await;
+ let (x, y) = super::desktop_screenshot_point(x, y);
if let Some(refusal) =
super::desktop_pointer_guard::check("scroll", &args, vec![(x, y)]).await
{