Skip to content

Commit 50acc2d

Browse files
fix(macos-helper): scroll SwiftUI forms whose rows are scroll-area children
A SwiftUI Form exposes each row and heading as a direct child of its AXScrollArea, with no single document view as tall as the content. The scroll-bar path sized the overflow from the largest child, found none taller than the area, and refused with no-scroll-bar. Size the overflow from the span all content children cover.
1 parent c8b944c commit 50acc2d

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎apple/macos-helper/Sources/AgentDeviceMacOSHelper/BackgroundInteraction.swift‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,19 @@ func scrollBarValue(current: Double, signedTravel: Double, overflow: Double) ->
259259
min(1, max(0, current + signedTravel / overflow))
260260
}
261261

262+
/// How far a scroll area's content reaches past the area along one axis. The content is the span
263+
/// all of its children cover: a SwiftUI `Form` lists each row as a child of the scroll area, with no
264+
/// single document view as tall as the content.
265+
func scrollContentOverflow(area: RectResponse, contentFrames: [RectResponse], isVertical: Bool)
266+
-> Double
267+
{
268+
let spans = contentFrames
269+
.filter { $0.width > 0 && $0.height > 0 }
270+
.map { isVertical ? ($0.y, $0.y + $0.height) : ($0.x, $0.x + $0.width) }
271+
guard let start = spans.map(\.0).min(), let end = spans.map(\.1).max() else { return 0 }
272+
return end - start - (isVertical ? area.height : area.width)
273+
}
274+
262275
func isPressableControlRole(_ role: String) -> Bool {
263276
pressableControlRoles.contains(role)
264277
}
@@ -426,15 +439,17 @@ private func performScrollBarScroll(
426439
guard let bar = elementAttribute(element, attribute: barAttribute as String),
427440
isAttributeSettable(bar, attribute: kAXValueAttribute as String),
428441
let value = numberAttribute(bar, attribute: kAXValueAttribute as String),
429-
let area = rectAttribute(element),
430-
let content = children(of: element)
431-
.filter({ role(of: $0) != "AXScrollBar" })
432-
.compactMap(rectAttribute)
433-
.max(by: { $0.width * $0.height < $1.width * $1.height })
442+
let area = rectAttribute(element)
434443
else {
435444
continue
436445
}
437-
let overflow = isVertical ? content.height - area.height : content.width - area.width
446+
let overflow = scrollContentOverflow(
447+
area: area,
448+
contentFrames: children(of: element)
449+
.filter({ role(of: $0) != "AXScrollBar" })
450+
.compactMap(rectAttribute),
451+
isVertical: isVertical
452+
)
438453
guard overflow > 0 else { continue }
439454
let next = scrollBarValue(current: value, signedTravel: signedTravel, overflow: overflow)
440455
return AXUIElementSetAttributeValue(bar, kAXValueAttribute as CFString, NSNumber(value: next))

‎apple/macos-helper/Tests/AgentDeviceMacOSHelperTests/BackgroundInteractionTests.swift‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ final class BackgroundInteractionTests: XCTestCase {
7878
XCTAssertEqual(scrollBarValue(current: 0.1, signedTravel: -600, overflow: 1200), 0)
7979
}
8080

81+
/// A SwiftUI `Form` lists its rows as children of the scroll area: together they overflow it
82+
/// although no single row is as tall as the area.
83+
func testScrollOverflowSpansEveryContentChild() {
84+
let area = RectResponse(x: 0, y: 100, width: 400, height: 600)
85+
let rows = (0..<10).map { row in
86+
RectResponse(x: 20, y: 100 + Double(row) * 120, width: 360, height: 100)
87+
}
88+
XCTAssertEqual(scrollContentOverflow(area: area, contentFrames: rows, isVertical: true), 580)
89+
XCTAssertEqual(scrollContentOverflow(area: area, contentFrames: rows, isVertical: false), -40)
90+
}
91+
92+
func testScrollOverflowIgnoresEmptyFrames() {
93+
let area = RectResponse(x: 0, y: 100, width: 400, height: 600)
94+
let document = RectResponse(x: 0, y: -400, width: 400, height: 2000)
95+
let empty = RectResponse(x: 0, y: -5000, width: 0, height: 0)
96+
XCTAssertEqual(
97+
scrollContentOverflow(area: area, contentFrames: [document, empty], isVertical: true), 1400)
98+
}
99+
81100
/// Chromium marks wrapper groups pressable; only a control role may win the press outright.
82101
func testWrapperGroupsAreNotPressableControls() {
83102
XCTAssertTrue(isPressableControlRole("AXButton"))

0 commit comments

Comments
 (0)