Skip to content

Commit 4a30494

Browse files
authored
fix(computer-use): find a save panel control's window through its ancestors when it reports none (#10116)
1 parent 9b8d72d commit 4a30494

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,22 @@ func takesText(_ element: AXUIElement) -> Bool {
167167
return AXUIElementIsAttributeSettable(element, kAXSelectedTextRangeAttribute as CFString, &settable) == .success && settable.boolValue
168168
}
169169
/// The window holding `element`: the element itself when it is a window or sheet, which report no
170-
/// window of their own, otherwise the window it reports.
170+
/// window of their own, otherwise the window it reports. Controls of an open or save panel belong to
171+
/// the panel's service process and report no window, so for them the nearest window or sheet among
172+
/// their ancestors is used.
171173
func containingWindow(_ element: AXUIElement) -> AXUIElement? {
172-
if [kAXWindowRole, kAXSheetRole].contains(attribute(element, kAXRoleAttribute) as? String ?? "") { return element }
173-
guard let raw = attribute(element, kAXWindowAttribute), CFGetTypeID(raw) == AXUIElementGetTypeID() else { return nil }
174-
return unsafeBitCast(raw, to: AXUIElement.self)
174+
let windowRoles = [kAXWindowRole, kAXSheetRole]
175+
if windowRoles.contains(attribute(element, kAXRoleAttribute) as? String ?? "") { return element }
176+
if let raw = attribute(element, kAXWindowAttribute), CFGetTypeID(raw) == AXUIElementGetTypeID() {
177+
return unsafeBitCast(raw, to: AXUIElement.self)
178+
}
179+
var current = element
180+
for _ in 0..<64 {
181+
guard let raw = attribute(current, kAXParentAttribute), CFGetTypeID(raw) == AXUIElementGetTypeID() else { return nil }
182+
current = unsafeBitCast(raw, to: AXUIElement.self)
183+
if windowRoles.contains(attribute(current, kAXRoleAttribute) as? String ?? "") { return current }
184+
}
185+
return nil
175186
}
176187
func axActions(_ element: AXUIElement) -> [String] {
177188
var names: CFArray?

0 commit comments

Comments
 (0)