Computer use CLI for AI agents
Rust accessibilityaccessibility-apiai-agentsautomationclicomputer-usecomputer-use-agentcomputer-use-agentsdesktop-automationmacosmcprust
fix(macos): place the caret on an indented new line (#354)
Chris Tatebf633183f5a9
diff --git a/crates/macos/src/tree/properties/text_insertion.rs b/crates/macos/src/tree/properties/text_insertion.rsindex edb929a91..c0427172b 100644--- a/crates/macos/src/tree/properties/text_insertion.rs+++ b/crates/macos/src/tree/properties/text_insertion.rs@@ -2,15 +2,68 @@//! system rather than inferred from the element's bounds: the bounds of the//! character before the insertion point, or, after a line break or in an//! empty field, the start of the line it opens.+//!+//! A new line can start indented, as a list item or a checklist row does, and+//! at the end of the text the text system reports that empty line's position+//! without its indent. The caret then lines up with the text of the paragraph+//! above, and shows nowhere rather than at a guess when that paragraph is empty.++const CARET_WIDTH: f64 = 2.0;+const MIN_CARET_HEIGHT: f64 = 10.0;+/// How far back to look for the start of the paragraph above, in UTF-16 units.+const PARAGRAPH_WINDOW: usize = 4096;++fn caret(x: f64, line: &agent_desktop_core::Rect) -> agent_desktop_core::Rect {+ agent_desktop_core::Rect {+ x,+ y: line.y,+ width: CARET_WIDTH,+ height: line.height.max(MIN_CARET_HEIGHT),+ }+}++/// Where the paragraph that ends just before `text` starts, as a UTF-16+/// offset into `text`, which runs up to (not including) that paragraph's line+/// break; `None` when the paragraph is empty. With no line break in `text`,+/// the paragraph is taken to start at its beginning.+fn paragraph_start(text: &str) -> Option<usize> {+ let units: Vec<u16> = text.encode_utf16().collect();+ let start = units+ .iter()+ .rposition(|&unit| unit == u16::from(b'\n'))+ .map_or(0, |at| at + 1);+ (start < units.len()).then_some(start)+}++/// The caret on a line just opened by a line break. `next` is the character at+/// the insertion point, which only exists when text follows; `first` is the+/// first character of the paragraph above, `last` its last character, and+/// `insertion` the text system's own rectangle for the empty insertion point.+fn new_line_caret(+ next: Option<agent_desktop_core::Rect>,+ first: Option<agent_desktop_core::Rect>,+ last: Option<agent_desktop_core::Rect>,+ insertion: Option<agent_desktop_core::Rect>,+) -> Option<agent_desktop_core::Rect> {+ if let Some(next) = next {+ return Some(caret(next.x, &next));+ }+ let (first, last) = (first?, last?);+ let line = insertion+ .filter(|insertion| insertion.y >= last.y + last.height * 0.5)+ .unwrap_or(agent_desktop_core::Rect {+ y: last.y + last.height,+ ..last+ });+ Some(caret(first.x, &line))+}#[cfg(target_os = "macos")]mod imp {+ use super::{PARAGRAPH_WINDOW, caret, new_line_caret, paragraph_start};use crate::tree::AXElement;use agent_desktop_core::{AdapterError, Deadline, ErrorCode, Rect};- const CARET_WIDTH: f64 = 2.0;- const MIN_CARET_HEIGHT: f64 = 10.0;-/// The insertion caret of the element with keyboard focus in `pid`, or in/// the focused app when `pid` is `None`.pub(crate) fn focused_insertion_caret(@@ -48,22 +101,27 @@ mod imp {if string_for_range(element, at - 1, 1, deadline).as_deref() != Some("\n") {return Some(caret(previous.x + previous.width, &previous));}- let left = bounds_for_range(element, 0, 1, deadline).map_or(previous.x, |first| first.x);- Some(Rect {- x: left,- y: previous.y + previous.height,- width: CARET_WIDTH,- height: previous.height.max(MIN_CARET_HEIGHT),- })- }-- fn caret(x: f64, line: &Rect) -> Rect {- Rect {- x,- y: line.y,- width: CARET_WIDTH,- height: line.height.max(MIN_CARET_HEIGHT),- }+ let window = (at - 1).min(PARAGRAPH_WINDOW);+ let before = at - 1 - window;+ let (first, last) = match string_for_range(element, before, window, deadline) {+ Some(text) => match paragraph_start(&text) {+ Some(start) => (+ bounds_for_range(element, before + start, 1, deadline),+ bounds_for_range(element, at - 2, 1, deadline),+ ),+ None => (None, None),+ },+ None => (+ bounds_for_range(element, 0, 1, deadline).or(Some(previous)),+ Some(previous),+ ),+ };+ new_line_caret(+ bounds_for_range(element, at, 1, deadline),+ first,+ last,+ bounds_for_range(element, at, 0, deadline),+ )}fn bounds_for_range(@@ -163,3 +221,7 @@ mod imp {}pub(crate) use imp::{focused_insertion_caret, insertion_caret};++#[cfg(test)]+#[path = "text_insertion_tests.rs"]+mod tests;diff --git a/crates/macos/src/tree/properties/text_insertion_tests.rs b/crates/macos/src/tree/properties/text_insertion_tests.rsnew file mode 100644index 000000000..61486369d--- /dev/null+++ b/crates/macos/src/tree/properties/text_insertion_tests.rs@@ -0,0 +1,63 @@+use super::{new_line_caret, paragraph_start};+use agent_desktop_core::Rect;++fn rect(x: f64, y: f64, width: f64, height: f64) -> Rect {+ Rect {+ x,+ y,+ width,+ height,+ }+}++fn place(caret: Option<Rect>) -> Option<(f64, f64, f64)> {+ caret.map(|caret| (caret.x, caret.y, caret.height))+}++#[test]+fn the_paragraph_above_starts_after_the_last_line_break() {+ assert_eq!(paragraph_start("title\n\nfirst\nsecond"), Some(13));+ assert_eq!(paragraph_start("only line"), Some(0));+}++#[test]+fn offsets_count_utf16_units() {+ assert_eq!(paragraph_start("caf\u{e9} \u{1f600}\nnext"), Some(8));+}++#[test]+fn an_empty_paragraph_above_has_no_start() {+ assert_eq!(paragraph_start("title\n"), None);+ assert_eq!(paragraph_start(""), None);+}++#[test]+fn the_character_at_the_insertion_point_places_the_caret_when_text_follows() {+ let next = rect(62.0, 269.0, 354.0, 16.0);+ let caret = new_line_caret(Some(next), None, None, Some(rect(62.0, 253.0, 0.0, 16.0)));+ assert_eq!(place(caret), Some((62.0, 269.0, 16.0)));+}++#[test]+fn at_the_end_the_caret_lines_up_with_the_text_of_the_paragraph_above() {+ let first = rect(62.0, 247.0, 8.0, 16.0);+ let last = rect(129.0, 247.0, 6.0, 16.0);+ let insertion = rect(39.0, 266.0, 0.0, 17.0);+ let caret = new_line_caret(None, Some(first), Some(last), Some(insertion));+ assert_eq!(place(caret), Some((62.0, 266.0, 17.0)));+}++#[test]+fn an_insertion_point_reported_on_the_line_above_is_moved_below_it() {+ let first = rect(20.0, 100.0, 8.0, 16.0);+ let last = rect(90.0, 100.0, 6.0, 16.0);+ let insertion = rect(20.0, 100.0, 0.0, 16.0);+ let caret = new_line_caret(None, Some(first), Some(last), Some(insertion));+ assert_eq!(place(caret), Some((20.0, 116.0, 16.0)));+}++#[test]+fn no_caret_shows_when_there_is_nothing_to_line_up_with() {+ let insertion = Some(rect(39.0, 266.0, 0.0, 17.0));+ assert_eq!(new_line_caret(None, None, None, insertion), None);+}