Skip to content

Commit fe1c22c

Browse files
authored
fix(data-grid): mount accessibility cells for rows a read prepared before they came on screen (#3891)
1 parent ddc50ed commit fe1c22c

6 files changed

Lines changed: 170 additions & 12 deletions

File tree

‎.claude/rules/data-grid.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ Three layers, knowledge pointing one way: an owner's adapter, then `DataGridMode
6666
- **AppKit builds the AXCell tree from cell views only**, so while `DataGridAccessibility.isActive` the main table mounts a `DataGridAccessibilityCellView` (identifier `data-grid-cell`) for each on-screen cell. It draws nothing and takes no clicks. Keep that path working whenever row drawing changes.
6767
- **A label reads "Row N, column M: value"**: N is the row number on screen, page offset included, and M the display position. UI tests find cells by it (`.claude/rules/ui-tests.md`, The grid).
6868
- **The frozen pane is hidden from accessibility.** A frozen column's stand-in in the main table reports the frame the frozen pane shows it at.
69-
- **Only the viewport mounts.** Mounting for every row starved the main thread behind a 1,000-row page (#2392); rows scrolled into view remount.
69+
- **Only the viewport mounts.** Mounting for every row starved the main thread behind a 1,000-row page (#2392). A client's read prepares the rows it walks, off-screen ones without a stand-in, and AppKit never asks for a prepared row again. So each on-screen row still without one remounts after a scroll, a layout of the grid and a read of the table: a fitted grid grows to its rows, and a page moves a grid, with no scroll.

‎apple/TablePro/Views/DataGrid/DataGridAccessibilityCellView.swift‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,32 @@ extension DataGridViewController {
156156
return cell
157157
}
158158

159-
// A row prepared off screen was answered with no view and is never asked again, so revealed rows reload.
159+
// A row prepared off screen was answered with no view and is never asked again, so an on-screen row without
160+
// its stand-in reloads. Asked of the rows themselves: a client's read prepares rows the grid never showed.
160161
func remountAccessibilityCells(force: Bool = false) {
161162
guard DataGridAccessibility.isActive, !isEditingCell else { return }
162163
let table = main.tableView
163164
let visible = table.rows(in: table.visibleRect)
164-
guard visible.length > 0, force || !NSEqualRanges(visible, accessibilityMountedRows) else { return }
165165
let upperBound = min(visible.location + visible.length, table.numberOfRows)
166166
guard visible.location >= 0, upperBound > visible.location, table.numberOfColumns > 0 else { return }
167-
accessibilityMountedRows = visible
168-
table.reloadData(
169-
forRowIndexes: IndexSet(integersIn: visible.location..<upperBound),
170-
columnIndexes: IndexSet(integersIn: 0..<table.numberOfColumns)
171-
)
167+
let rows = IndexSet(integersIn: visible.location..<upperBound).filteredIndexSet { row in
168+
guard table.rowView(atRow: row, makeIfNecessary: false) != nil else { return false }
169+
return force || !(table.view(atColumn: 0, row: row, makeIfNecessary: false) is DataGridAccessibilityCellView)
170+
}
171+
guard !rows.isEmpty else { return }
172+
table.reloadData(forRowIndexes: rows, columnIndexes: IndexSet(integersIn: 0..<table.numberOfColumns))
173+
}
174+
175+
// Rows also come on screen with no scroll: a fitted grid grows to its rows, a pane resizes, a page moves the
176+
// grid. Deferred past the layout pass or accessibility read that asks, which a reload inside would re-enter.
177+
func scheduleAccessibilityRemount() {
178+
guard DataGridAccessibility.isActive, !isAccessibilityRemountScheduled else { return }
179+
isAccessibilityRemountScheduled = true
180+
DispatchQueue.main.async { [weak self] in
181+
guard let self else { return }
182+
self.isAccessibilityRemountScheduled = false
183+
self.remountAccessibilityCells()
184+
}
172185
}
173186

174187
// Clamped to the visible rows: AppKit would otherwise ask for every cell of a million-row selection.

‎apple/TablePro/Views/DataGrid/DataGridTableView.swift‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,23 @@ final class DataGridTableView: NSTableView {
258258

259259
override func accessibilityChildren() -> [Any]? {
260260
guard paneKind == .main else { return [] }
261-
DataGridAccessibility.markActive()
261+
noteAccessibilityRead()
262262
return super.accessibilityChildren()
263263
}
264264

265265
override func accessibilityCell(forColumn column: Int, row: Int) -> Any? {
266266
guard paneKind == .main else { return nil }
267-
DataGridAccessibility.markActive()
267+
noteAccessibilityRead()
268268
return view(atColumn: column, row: row, makeIfNecessary: false) as? DataGridAccessibilityCellView
269269
?? super.accessibilityCell(forColumn: column, row: row)
270270
}
271271

272+
// A read prepares every row it walks, off screen ones without a stand-in, and some reach the screen with no scroll.
273+
private func noteAccessibilityRead() {
274+
DataGridAccessibility.markActive()
275+
controller?.scheduleAccessibilityRemount()
276+
}
277+
272278
override func accessibilitySelectedCells() -> [Any]? {
273279
guard paneKind == .main else { return [] }
274280
return controller?.accessibilitySelectedCells() ?? super.accessibilitySelectedCells()

‎apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ extension DataGridViewController {
5454
for pane in panes {
5555
pane.tableView.reloadData()
5656
}
57-
accessibilityMountedRows = NSRange(location: 0, length: 0)
5857
drawnSelection = model.selection
5958
// A view-based reloadData() drops the selection without telling anyone (probe P2), so it is written back.
6059
mirrorSelection()

‎apple/TablePro/Views/DataGrid/DataGridViewController.swift‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView {
2929
var knownWidths: [DataGridColumnID: CGFloat] = [:]
3030
var knownWidthsSawRows = false
3131
var savedWidthIDs: Set<DataGridColumnID> = []
32-
var accessibilityMountedRows = NSRange(location: 0, length: 0)
32+
var isAccessibilityRemountScheduled = false
3333
var needsViewportRestore = false
3434
var pendingFocus = false
3535
var lastDataGridSettings = AppSettingsManager.shared.dataGrid
@@ -159,6 +159,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView {
159159
applyStagedViewport()
160160
}
161161
updateFittingHeight()
162+
scheduleAccessibilityRemount()
162163
if pendingPageReveal != nil {
163164
// The page scrolls once its own layout is done, so its document already holds the grid's new height.
164165
DispatchQueue.main.async { [weak self] in self?.performPageReveal() }
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//
2+
// DataGridAccessibilityMountTests.swift
3+
// TableProTests
4+
//
5+
6+
import AppKit
7+
@testable import TablePro
8+
import TableProTestSupport
9+
import Testing
10+
11+
@MainActor
12+
private final class FlippedPage: NSView {
13+
override var isFlipped: Bool { true }
14+
}
15+
16+
// A fitsRows grid on a page that scrolls, placed by hand the way the Definition page places it.
17+
@MainActor
18+
private final class FittedGridOnPage {
19+
let grid: HostedDataGrid
20+
private let page = NSScrollView(frame: NSRect(x: 0, y: 0, width: 800, height: 400))
21+
private let document = FlippedPage(frame: .zero)
22+
23+
init(rows: TableRows, top: CGFloat, height: CGFloat) {
24+
var configuration = DataGridConfiguration()
25+
configuration.fitsRows = true
26+
grid = HostedDataGrid(rows: rows, configuration: configuration)
27+
page.hasVerticalScroller = true
28+
page.documentView = document
29+
grid.window.contentView = page
30+
let gridView = grid.controller.view
31+
gridView.removeFromSuperview()
32+
gridView.autoresizingMask = []
33+
document.addSubview(gridView)
34+
place(top: top, height: height)
35+
// HostedDataGrid first laid the grid out in a plain window; a reload starts it over where it now sits.
36+
grid.model.reloadRows()
37+
grid.layout()
38+
}
39+
40+
var fittedHeight: CGFloat { grid.controller.measuredFittingHeight }
41+
42+
func place(top: CGFloat, height: CGFloat) {
43+
document.frame = NSRect(x: 0, y: 0, width: page.bounds.width, height: max(page.bounds.height, top + height))
44+
grid.controller.view.frame = NSRect(x: 0, y: top, width: page.bounds.width, height: height)
45+
grid.layout()
46+
}
47+
48+
// What an assistive client does on its first look: down to each cell's children, which prepares the cell's row.
49+
func readEveryRow() {
50+
let rows = grid.main.accessibilityAttributeValue(.children) as? [NSObject] ?? []
51+
for row in rows {
52+
let cells = row.accessibilityAttributeValue(.children) as? [NSObject] ?? []
53+
for cell in cells {
54+
_ = cell.accessibilityAttributeValue(.children)
55+
}
56+
}
57+
}
58+
59+
var preparedRows: IndexSet {
60+
IndexSet((0..<grid.main.numberOfRows).filter { grid.main.rowView(atRow: $0, makeIfNecessary: false) != nil })
61+
}
62+
63+
var mountedRows: IndexSet {
64+
IndexSet((0..<grid.main.numberOfRows).filter {
65+
grid.main.view(atColumn: 0, row: $0, makeIfNecessary: false) is DataGridAccessibilityCellView
66+
})
67+
}
68+
69+
var visibleRows: IndexSet {
70+
let range = grid.main.rows(in: grid.main.visibleRect)
71+
return IndexSet(integersIn: range.location..<(range.location + range.length))
72+
}
73+
74+
func close() {
75+
grid.close()
76+
}
77+
}
78+
79+
extension GlobalStateSuites {
80+
@MainActor
81+
struct DataGridAccessibilityMountTests {
82+
private let everyRow = IndexSet(integersIn: 0..<5)
83+
84+
// A fitted grid mounts at height 0 and grows once it has measured its rows. A client that reads it in
85+
// between prepares every row while none is on screen, so none got a stand-in, and growing is no scroll.
86+
@Test("A fitted grid read before it grows publishes every row once it has grown")
87+
func rowsReadBeforeTheGridGrowsMount() async throws {
88+
let wasActive = DataGridAccessibility.isActive
89+
DataGridAccessibility.isActive = true
90+
defer { DataGridAccessibility.isActive = wasActive }
91+
let page = FittedGridOnPage(rows: GridFixtureRows.people, top: 40, height: 0)
92+
defer { page.close() }
93+
94+
page.readEveryRow()
95+
try #require(page.preparedRows == everyRow)
96+
try #require(page.mountedRows.isEmpty)
97+
98+
page.place(top: 40, height: page.fittedHeight)
99+
#expect(page.visibleRows == everyRow)
100+
101+
try await waitUntil { page.mountedRows == everyRow }
102+
}
103+
104+
// The page moves a grid when a section above it collapses, which neither scrolls nor resizes the grid.
105+
@Test("A grid moved onto the page publishes its rows to the next read")
106+
func rowsMovedOntoThePageMountOnTheNextRead() async throws {
107+
let wasActive = DataGridAccessibility.isActive
108+
DataGridAccessibility.isActive = true
109+
defer { DataGridAccessibility.isActive = wasActive }
110+
let page = FittedGridOnPage(rows: GridFixtureRows.people, top: 40, height: 0)
111+
defer { page.close() }
112+
let height = page.fittedHeight
113+
page.place(top: 1_000, height: height)
114+
page.readEveryRow()
115+
try #require(page.visibleRows.isEmpty)
116+
117+
page.place(top: 40, height: height)
118+
try #require(page.visibleRows == everyRow)
119+
page.readEveryRow()
120+
121+
try await waitUntil { page.mountedRows == everyRow }
122+
}
123+
124+
@Test("Only the rows on the page get a stand-in, however many a client reads")
125+
func onlyRowsOnThePageMount() async throws {
126+
let wasActive = DataGridAccessibility.isActive
127+
DataGridAccessibility.isActive = true
128+
defer { DataGridAccessibility.isActive = wasActive }
129+
let page = FittedGridOnPage(rows: GridFixtureRows.wide(rows: 80, columns: 3), top: 0, height: 0)
130+
defer { page.close() }
131+
page.place(top: 0, height: page.fittedHeight)
132+
let onPage = page.visibleRows
133+
try #require(!onPage.isEmpty && onPage.count < 80)
134+
135+
page.readEveryRow()
136+
try await waitUntil { page.mountedRows == onPage }
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)