Skip to content

Commit 200b219

Browse files
authored
fix(desktop): let computer use type into date and time pickers (#9128)
1 parent adbbe18 commit 200b219

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎apps/desktop/native/computer-use/ComputerUse.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ func holdsFocus(_ focused: CFTypeRef, _ element: AXUIElement) -> Bool {
116116
}
117117
return false
118118
}
119-
/// Whether typed characters land as text: a text, combo or date control, or anything with an editable
120-
/// text selection. Buttons, lists and web areas take characters as commands instead.
119+
/// Whether typed characters land as text: a text, combo, date or time control (`AXDateTimeArea` is an
120+
/// NSDatePicker's field), or anything with an editable text selection. Buttons, lists and web areas take characters as commands instead.
121121
func takesText(_ element: AXUIElement) -> Bool {
122-
if [kAXTextFieldRole, kAXTextAreaRole, kAXComboBoxRole, kAXDateFieldRole, kAXTimeFieldRole].contains(attribute(element, kAXRoleAttribute) as? String ?? "") { return true }
122+
if [kAXTextFieldRole, kAXTextAreaRole, kAXComboBoxRole, kAXDateFieldRole, kAXTimeFieldRole, "AXDateTimeArea"].contains(attribute(element, kAXRoleAttribute) as? String ?? "") { return true }
123123
var settable: DarwinBoolean = false
124124
return AXUIElementIsAttributeSettable(element, kAXSelectedTextRangeAttribute as CFString, &settable) == .success && settable.boolValue
125125
}

‎apps/desktop/native/computer-use/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Coordinates in action inputs are window-local points; node frames are global scr
1414

1515
AXPress and AXSetValue are direct semantic operations. Keyboard events target the app PID. Cmd+A uses the focused editor's writable accessibility selection range where supported. Coordinate mouse input requires the target app in front; `activate_app` makes that focus change explicit and confirms it. Click/drag/scroll validate the foreground process, unchanged window geometry and the app that receives the point; the topmost window-list entry is not the click target because Notification Center and the Dock keep full-screen click-through windows. A hit on a window's own background counts as that window. Background tabs and minimized windows share bounds with the window in front, so they take no coordinate input. An action that opens UI (AXPress, AXPick, AXShowMenu) and times out while the app shows a sheet, modal or menu (`cannotComplete`) counts as dispatched; any other action that times out fails as uncertain. A menu-item press that leaves the item's own menu open with the app highlighting that item (Calendar on macOS 26) is completed with Return. Sheets, including Go to Folder and out-of-process open and save panels whose contents report no window, count as the window they are attached to. Menu item labels carry their shortcuts (for example ⇧⌘N). Some apps ignore background keyboard events. `dispatched:true,verified:false` means only dispatch succeeded; the agent must observe again to establish the outcome. No foreground activation fallback is hidden inside the helper. Native key codes currently assume the US layout for shortcuts; literal text uses Unicode events. Unsupported shortcuts fail explicitly.
1616

17-
`type_text` and `input_sequence` target a text control (text fields and areas, terminals, combo boxes, date fields, or anything with an editable text selection), or a container whose focused editor already holds keyboard focus, such as a web composer; keystrokes are sent only while focus is in a text input. `input_sequence` executes 1–32 key/text steps against one such element. It validates every step before dispatch and checks the same editor, window, and secure-input ancestry between steps. Enter can continue into another step when the same editor and window retain focus. Actual focus loss, Tab, or navigation shortcuts end the sequence before later input. Explicit `activateFirst: true` activates the target app inside the batch before input; it never enables automatic refocusing between steps. Partial execution reports completed steps and an error; callers must observe before retrying. Every mutation is followed by fresh state under the same authorization and queue entry; `observeAfter` optionally requests a screenshot. Observation failure is reported separately so the mutation is never replayed. The model receives a compact, bounded projection of the native snapshot; omitted nodes are reported explicitly.
17+
`type_text` and `input_sequence` target a text control (text fields and areas, terminals, combo boxes, date and time pickers, or anything with an editable text selection), or a container whose focused editor already holds keyboard focus, such as a web composer; keystrokes are sent only while focus is in a text input. `input_sequence` executes 1–32 key/text steps against one such element. It validates every step before dispatch and checks the same editor, window, and secure-input ancestry between steps. Enter can continue into another step when the same editor and window retain focus. Actual focus loss, Tab, or navigation shortcuts end the sequence before later input. Explicit `activateFirst: true` activates the target app inside the batch before input; it never enables automatic refocusing between steps. Partial execution reports completed steps and an error; callers must observe before retrying. Every mutation is followed by fresh state under the same authorization and queue entry; `observeAfter` optionally requests a screenshot. Observation failure is reported separately so the mutation is never replayed. The model receives a compact, bounded projection of the native snapshot; omitted nodes are reported explicitly.
1818

1919
## Cancellation
2020

‎apps/desktop/native/computer-use/tests/Fixture.swift‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class FixtureDelegate: NSObject, NSApplicationDelegate {
1313
var count = 0
1414
let counter = NSTextField(labelWithString: "Count: 0")
1515
let scrollCounter = NSTextField(labelWithString: "Scroll: 0")
16+
let timeLabel = NSTextField(labelWithString: "Time: 10:15")
1617
func applicationDidFinishLaunching(_ notification: Notification) {
1718
NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown, .leftMouseDragged, .leftMouseUp, .scrollWheel]) { event in
1819
print("fixture-event type=\(event.type.rawValue) window=\(event.windowNumber) x=\(event.locationInWindow.x) y=\(event.locationInWindow.y)"); fflush(stdout)
@@ -31,6 +32,8 @@ final class FixtureDelegate: NSObject, NSApplicationDelegate {
3132
let secure = NSSecureTextField(string: "fixture-only-secret"); secure.setAccessibilityIdentifier("fixture-password"); secure.setAccessibilityLabel("Fixture password")
3233
let button = NSButton(title: "Increment", target: self, action: #selector(increment)); button.setAccessibilityIdentifier("fixture-increment")
3334
let slider = NSSlider(value: 25, minValue: 0, maxValue: 100, target: nil, action: nil); slider.setAccessibilityLabel("Fixture slider")
35+
let time = NSDatePicker(); time.datePickerStyle = .textField; time.datePickerElements = .hourMinute; time.setAccessibilityLabel("Fixture time")
36+
time.dateValue = Calendar.current.date(bySettingHour: 10, minute: 15, second: 0, of: Date()) ?? Date(); time.target = self; time.action = #selector(timeChanged(_:))
3437
let marker = MarkerView(); marker.setAccessibilityElement(false)
3538
marker.translatesAutoresizingMaskIntoConstraints = false
3639
let scroll = NSScrollView(); scroll.hasVerticalScroller = true
@@ -40,7 +43,7 @@ final class FixtureDelegate: NSObject, NSApplicationDelegate {
4043
scroll.documentView = document
4144
scroll.contentView.postsBoundsChangedNotifications = true
4245
NotificationCenter.default.addObserver(self, selector: #selector(scrolled(_:)), name: NSView.boundsDidChangeNotification, object: scroll.contentView)
43-
for view in [field, secure, button, counter, slider, marker, scrollCounter, scroll] { stack.addArrangedSubview(view) }
46+
for view in [field, secure, button, counter, slider, time, timeLabel, marker, scrollCounter, scroll] { stack.addArrangedSubview(view) }
4447
window.contentView!.addSubview(stack)
4548
NSLayoutConstraint.activate([stack.leadingAnchor.constraint(equalTo: window.contentView!.leadingAnchor, constant: 30), stack.trailingAnchor.constraint(equalTo: window.contentView!.trailingAnchor, constant: -30), stack.topAnchor.constraint(equalTo: window.contentView!.topAnchor, constant: 30), field.widthAnchor.constraint(equalTo: stack.widthAnchor), secure.widthAnchor.constraint(equalTo: stack.widthAnchor), slider.widthAnchor.constraint(equalTo: stack.widthAnchor), scroll.widthAnchor.constraint(equalTo: stack.widthAnchor), scroll.heightAnchor.constraint(equalToConstant: 230), marker.widthAnchor.constraint(equalToConstant: 100), marker.heightAnchor.constraint(equalToConstant: 70)])
4649
window.makeKeyAndOrderFront(nil)
@@ -49,6 +52,10 @@ final class FixtureDelegate: NSObject, NSApplicationDelegate {
4952
print("fixture-ready"); fflush(stdout)
5053
}
5154
@objc func increment() { count += 1; counter.stringValue = "Count: \(count)" }
55+
@objc func timeChanged(_ picker: NSDatePicker) {
56+
let time = Calendar.current.dateComponents([.hour, .minute], from: picker.dateValue)
57+
timeLabel.stringValue = "Time: \(time.hour ?? 0):\(String(format: "%02d", time.minute ?? 0))"
58+
}
5259
@objc func scrolled(_ notification: Notification) {
5360
guard let clip = notification.object as? NSClipView else { return }
5461
scrollCounter.stringValue = "Scroll: \(Int(clip.bounds.origin.y))"

‎apps/desktop/native/computer-use/tests/live.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ def coordinate(snapshot, node, fx=0.5, fy=0.5):
119119
assert error['code'] == 'unsupported_action' and error['dispatchState'] == 'not_started', error
120120
assert find(state(), role='AXSlider')['value'] == slider['value'], 'Text reached a control that does not take text'
121121
snapshot = state()
122+
snapshot = mutate('click', snapshot, **coordinate(snapshot, find(snapshot, role='AXDateTimeArea'), 0.15))
123+
snapshot = mutate('type_text', snapshot, elementId=find(snapshot, role='AXDateTimeArea')['elementId'], text='3')
124+
assert any(node.get('value', node.get('label', '')) == 'Time: 3:15' for node in snapshot['nodes']), 'Typing did not reach the date picker'
122125
snapshot = mutate('click', snapshot, elementId=find(snapshot, role='AXTextField', label='Fixture editable text')['elementId'])
123126
snapshot = mutate('type_text', snapshot, elementId=find(snapshot, role='AXWindow')['elementId'], text=' window')
124127
assert find(snapshot, role='AXTextField', label='Fixture editable text')['value'].endswith(' window'), 'Window target did not type into its focused editor'
@@ -127,7 +130,7 @@ def coordinate(snapshot, node, fx=0.5, fy=0.5):
127130
button = find(snapshot, role='AXButton', label='Increment')
128131
snapshot = mutate('perform_action', snapshot, elementId=button['elementId'], accessibilityAction='AXPress')
129132
assert any(f'Count: {baseline + 2}' in node.get('value', '') or f'Count: {baseline + 2}' in node.get('label', '') for node in snapshot['nodes']), 'Explicit AX action did not increment'
130-
print('PASS: real fixture state, secure suppression, AX click/set/action, Unicode typing, keyboard chord, coordinate drag and scrolling, text refused by a slider, a window target typing into its focused editor, a title-bar click; every mutation verified in fresh state; screenshot=' + ('verified' if permissions['screenRecording'] else 'BLOCKED'))
133+
print('PASS: real fixture state, secure suppression, AX click/set/action, Unicode typing, keyboard chord, coordinate drag and scrolling, text refused by a slider, typing into a date picker, a window target typing into its focused editor, a title-bar click; every mutation verified in fresh state; screenshot=' + ('verified' if permissions['screenRecording'] else 'BLOCKED'))
131134
finally:
132135
if fixture:
133136
fixture.terminate()

0 commit comments

Comments
 (0)