drawnui-react 0.1.0-preview.9 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/README.md +229 -22
  2. package/SKIPPED.md +37 -28
  3. package/dist/controls/AnimatedFramesRenderer.d.ts +9 -0
  4. package/dist/controls/AnimatedFramesRenderer.js +18 -1
  5. package/dist/controls/DrawnGame.d.ts +44 -0
  6. package/dist/controls/DrawnGame.js +82 -0
  7. package/dist/controls/GridStructure.d.ts +25 -3
  8. package/dist/controls/GridStructure.js +81 -5
  9. package/dist/controls/SkiaBackdrop.d.ts +8 -2
  10. package/dist/controls/SkiaBackdrop.js +42 -14
  11. package/dist/controls/SkiaButton.d.ts +17 -3
  12. package/dist/controls/SkiaButton.js +26 -8
  13. package/dist/controls/SkiaCarousel.d.ts +22 -3
  14. package/dist/controls/SkiaCarousel.js +85 -9
  15. package/dist/controls/SkiaCheckbox.d.ts +0 -1
  16. package/dist/controls/SkiaCheckbox.js +2 -7
  17. package/dist/controls/SkiaDrawer.d.ts +15 -0
  18. package/dist/controls/SkiaDrawer.js +42 -7
  19. package/dist/controls/SkiaEditor.d.ts +51 -2
  20. package/dist/controls/SkiaEditor.js +292 -58
  21. package/dist/controls/SkiaGif.d.ts +7 -0
  22. package/dist/controls/SkiaGif.js +25 -4
  23. package/dist/controls/SkiaImage.d.ts +10 -0
  24. package/dist/controls/SkiaImage.js +80 -19
  25. package/dist/controls/SkiaLabel.d.ts +200 -11
  26. package/dist/controls/SkiaLabel.js +925 -93
  27. package/dist/controls/SkiaLabelFps.d.ts +12 -0
  28. package/dist/controls/SkiaLabelFps.js +35 -0
  29. package/dist/controls/SkiaLayout.d.ts +89 -9
  30. package/dist/controls/SkiaLayout.js +397 -23
  31. package/dist/controls/SkiaLottie.js +5 -3
  32. package/dist/controls/SkiaProgress.d.ts +2 -1
  33. package/dist/controls/SkiaProgress.js +8 -5
  34. package/dist/controls/SkiaRadioButton.d.ts +2 -0
  35. package/dist/controls/SkiaRadioButton.js +16 -4
  36. package/dist/controls/SkiaRichLabel.d.ts +1 -1
  37. package/dist/controls/SkiaRichLabel.js +11 -1
  38. package/dist/controls/SkiaScroll.d.ts +86 -7
  39. package/dist/controls/SkiaScroll.js +361 -40
  40. package/dist/controls/SkiaScrollBar.d.ts +27 -0
  41. package/dist/controls/SkiaScrollBar.js +51 -2
  42. package/dist/controls/SkiaShaderCarousel.d.ts +11 -1
  43. package/dist/controls/SkiaShaderCarousel.js +27 -5
  44. package/dist/controls/SkiaShape.d.ts +15 -2
  45. package/dist/controls/SkiaShape.js +57 -10
  46. package/dist/controls/SkiaShell.d.ts +282 -0
  47. package/dist/controls/SkiaShell.js +972 -0
  48. package/dist/controls/SkiaSlider.d.ts +20 -2
  49. package/dist/controls/SkiaSlider.js +61 -7
  50. package/dist/controls/SkiaSprite.d.ts +4 -0
  51. package/dist/controls/SkiaSprite.js +27 -11
  52. package/dist/controls/SkiaSvg.d.ts +4 -0
  53. package/dist/controls/SkiaSvg.js +28 -4
  54. package/dist/controls/SkiaSwitch.d.ts +0 -1
  55. package/dist/controls/SkiaSwitch.js +4 -8
  56. package/dist/controls/SkiaToggle.d.ts +25 -5
  57. package/dist/controls/SkiaToggle.js +58 -5
  58. package/dist/controls/SnappingLayout.d.ts +12 -1
  59. package/dist/controls/SnappingLayout.js +21 -2
  60. package/dist/controls/TooltipStyle.d.ts +52 -0
  61. package/dist/controls/TooltipStyle.js +88 -0
  62. package/dist/core/Accessibility.d.ts +162 -11
  63. package/dist/core/Accessibility.js +882 -22
  64. package/dist/core/Animators.d.ts +37 -1
  65. package/dist/core/Animators.js +102 -9
  66. package/dist/core/Canvas.d.ts +307 -8
  67. package/dist/core/Canvas.js +1202 -66
  68. package/dist/core/ControlStyle.d.ts +2 -0
  69. package/dist/core/ControlStyle.js +2 -0
  70. package/dist/core/DesignerActions.d.ts +32 -0
  71. package/dist/core/DesignerActions.js +68 -0
  72. package/dist/core/Easing.d.ts +12 -1
  73. package/dist/core/Easing.js +25 -1
  74. package/dist/core/Gestures.d.ts +86 -2
  75. package/dist/core/Gestures.js +184 -4
  76. package/dist/core/Hover.d.ts +51 -0
  77. package/dist/core/Hover.js +140 -0
  78. package/dist/core/ImageEffects.d.ts +2 -0
  79. package/dist/core/ImageEffects.js +2 -0
  80. package/dist/core/KeyboardManager.d.ts +40 -0
  81. package/dist/core/KeyboardManager.js +98 -8
  82. package/dist/core/PopupWindows.d.ts +94 -0
  83. package/dist/core/PopupWindows.js +207 -0
  84. package/dist/core/Props.d.ts +11 -0
  85. package/dist/core/Props.js +44 -0
  86. package/dist/core/ScrollAnimators.d.ts +6 -0
  87. package/dist/core/ScrollAnimators.js +16 -1
  88. package/dist/core/Shaping.d.ts +40 -0
  89. package/dist/core/Shaping.js +224 -0
  90. package/dist/core/SkiaControl.d.ts +306 -13
  91. package/dist/core/SkiaControl.js +681 -75
  92. package/dist/core/SkiaEffect.d.ts +29 -1
  93. package/dist/core/SkiaEffect.js +78 -16
  94. package/dist/core/SkiaImageManager.d.ts +101 -18
  95. package/dist/core/SkiaImageManager.js +318 -63
  96. package/dist/core/Styles.js +7 -1
  97. package/dist/core/Super.d.ts +33 -2
  98. package/dist/core/Super.js +58 -12
  99. package/dist/core/TextInputProxy.d.ts +31 -1
  100. package/dist/core/TextInputProxy.js +97 -15
  101. package/dist/core/Tooltips.d.ts +71 -0
  102. package/dist/core/Tooltips.js +205 -0
  103. package/dist/core/Types.d.ts +11 -1
  104. package/dist/core/Types.js +32 -1
  105. package/dist/core/ViewsAdapter.d.ts +7 -2
  106. package/dist/core/ViewsAdapter.js +10 -3
  107. package/dist/index.d.ts +8 -0
  108. package/dist/index.js +8 -0
  109. package/dist/react/SkiaShell.d.ts +8 -74
  110. package/dist/react/SkiaShell.js +163 -475
  111. package/dist/react/index.d.ts +48 -4
  112. package/dist/react/index.js +210 -43
  113. package/dist/react/reconciler.d.ts +32 -0
  114. package/dist/react/reconciler.js +71 -40
  115. package/dist/vite/index.d.ts +9 -0
  116. package/dist/vite/index.js +36 -3
  117. package/package.json +103 -5
@@ -1,4 +1,6 @@
1
1
  import { SKRect } from "./Types";
2
+ /** Controls with `AccessibilityModal` set, on every canvas (drawnui-cross 6av). Empty: no modal work at all. */
3
+ export const AccessibilityModals = new Set();
2
4
  /** ARIA role constants (DrawnUi.Models.Aria) for `AccessibilityRole`. */
3
5
  export const Aria = {
4
6
  // interactive widgets
@@ -12,11 +14,33 @@ export const Aria = {
12
14
  RoleSeparator: "separator", RoleProgressBar: "progressbar", RoleTooltip: "tooltip", RoleDialog: "dialog",
13
15
  RoleAlertDialog: "alertdialog", RoleStatus: "status", RoleAlert: "alert", RoleGroup: "group", RoleRegion: "region",
14
16
  RoleNavigation: "navigation", RoleMain: "main",
17
+ // containers of items: with one of these roles a layout is an arrow-key group (IsCompositeRole)
18
+ RoleGrid: "grid", RoleToolbar: "toolbar", RoleRadioGroup: "radiogroup", RoleMenuBar: "menubar",
15
19
  /** Removes the control from the accessibility tree even when a default role would apply (inner label of a button). */
16
20
  RolePresentation: "presentation",
17
21
  // live regions
18
22
  LivePolite: "polite", LiveAssertive: "assertive",
23
+ /**
24
+ * Container roles whose items the arrow keys walk (C# Aria.IsCompositeRole): a layout with one of them is one Tab stop,
25
+ * its current item, and the arrow keys move between its items.
26
+ */
27
+ IsCompositeRole(role) {
28
+ return role === "list" || role === "listbox" || role === "grid" || role === "toolbar" || role === "radiogroup"
29
+ || role === "tablist" || role === "menu" || role === "menubar";
30
+ },
31
+ /**
32
+ * Roles a user operates (C# Aria.IsInteractiveRole, as DrawnUi.Rust is_control_role): a node with one of them that
33
+ * cannot take input is read as disabled (aria-disabled). C# also lists listbox and scrollbar; here a listbox is an
34
+ * arrow-key group and groups are never disabled.
35
+ */
36
+ IsInteractiveRole(role) {
37
+ return role === "button" || role === "link" || role === "checkbox" || role === "radio" || role === "switch"
38
+ || role === "slider" || role === "spinbutton" || role === "textbox" || role === "searchbox" || role === "combobox"
39
+ || role === "option" || role === "tab" || role === "menuitem" || role === "menuitemcheckbox" || role === "menuitemradio";
40
+ },
19
41
  };
42
+ const asLayout = (c) => ("ChildrenFactory" in c && "Split" in c ? c : undefined);
43
+ const asScroll = (c) => ("ScrollToIndex" in c && "ViewportOffsetY" in c ? c : undefined);
20
44
  /**
21
45
  * Mirrors DrawnUi SkiaAccessibilityManager: registry of accessible controls + a rate-limited snapshot
22
46
  * (at most one rebuild per MinUpdateIntervalMs, taken at frame end) that the DOM overlay renders.
@@ -30,6 +54,7 @@ export class SkiaAccessibilityManager {
30
54
  pending = 0;
31
55
  changed = new Set();
32
56
  liveUpdated = new Set();
57
+ keyboardFocusRequested = new Set();
33
58
  /** Minimum milliseconds between snapshot rebuilds. */
34
59
  MinUpdateIntervalMs = 1000;
35
60
  Snapshot = [];
@@ -38,8 +63,459 @@ export class SkiaAccessibilityManager {
38
63
  OnChanged(cb) { this.changed.add(cb); return () => this.changed.delete(cb); }
39
64
  /** Fired immediately (bypassing the rate limit) when a live-region node's value changes. */
40
65
  OnLiveRegionUpdated(cb) { this.liveUpdated.add(cb); return () => this.liveUpdated.delete(cb); }
41
- NotifyFocused(node) { if (this.FocusedNode !== node)
42
- this.FocusedNode = node; }
66
+ /**
67
+ * Keyboard focus moved by the manager itself (arrow keys in a group): the overlay moves DOM focus to that node's
68
+ * element (C# KeyboardFocusRequested). Returns the unsubscribe function.
69
+ */
70
+ OnKeyboardFocusRequested(cb) { this.keyboardFocusRequested.add(cb); return () => this.keyboardFocusRequested.delete(cb); }
71
+ NotifyFocused(node) {
72
+ if (this.FocusedNode === node)
73
+ return;
74
+ this.NoteFocus(node);
75
+ this.FocusedNode = node;
76
+ }
77
+ /**
78
+ * Enter / Space from keyboard navigation or a screen reader: activates the node only while a tap could reach it
79
+ * (AccessibilityCanInteract), the rule every head applies.
80
+ */
81
+ static Activate(node) {
82
+ if (!node || !node.AccessibilityCanInteract)
83
+ return false;
84
+ node.OnAccessibilityActivated();
85
+ node.Superview?.AccessibilityManager.ForceRebuildOnNextFrame(); // a screen reader reads the new state right back
86
+ return true;
87
+ }
88
+ /**
89
+ * Arrow keys, Home / End, PageUp / PageDown for the node in keyboard focus: the control gets them first, only while a
90
+ * pan could reach it (a slider steps its value); keys it does not use move keyboard focus between the items of the
91
+ * group around it (MoveInGroup). True when the key was used.
92
+ */
93
+ static Key(node, key) {
94
+ if (!node)
95
+ return false;
96
+ if (node.CanReceiveGesture("Panning") && node.OnAccessibilityKey(key)) {
97
+ node.Superview?.AccessibilityManager.ForceRebuildOnNextFrame(); // the new value is read right back, not a second later
98
+ return true;
99
+ }
100
+ return node.Superview?.AccessibilityManager.MoveInGroup(node, key) ?? false;
101
+ }
102
+ // ---- arrow-key groups (C# SkiaAccessibilityManager, "Arrow-key groups") ----
103
+ /** Last focused node of each group: where Tab enters the group. */
104
+ groupFocus = new WeakMap();
105
+ groupRequest;
106
+ /**
107
+ * The arrow-key group around `control`: its nearest ancestor with a composite role (Aria.IsCompositeRole, e.g.
108
+ * Aria.RoleList on a SkiaStack), and the item of that group holding the control (one of its children, a cell).
109
+ */
110
+ static TryFindGroup(control) {
111
+ const path = [];
112
+ for (let child = control, parent = control.Parent; parent; child = parent, parent = parent.Parent) {
113
+ path.push(child);
114
+ if (!Aria.IsCompositeRole(parent.AccessibilityRole))
115
+ continue;
116
+ // drawnui-cross 6ao: the role may sit on a wrapper (a menu shape around a column of lines); the items are the
117
+ // children of the control that holds them, found down the path through single children that are not nodes
118
+ let holder = parent, k = path.length - 1;
119
+ while (k > 0 && ViewsOf(holder).length === 1 && !IsInteractiveNode(path[k])) {
120
+ holder = path[k];
121
+ k--;
122
+ }
123
+ return { Group: parent, Item: path[k], Holder: holder };
124
+ }
125
+ return undefined;
126
+ }
127
+ /** Remembers where keyboard focus is inside its group, so the next Tab into the group lands there. */
128
+ NoteFocus(node) {
129
+ const found = node && SkiaAccessibilityManager.TryFindGroup(node);
130
+ if (found)
131
+ this.groupFocus.set(found.Group, node);
132
+ }
133
+ /**
134
+ * Whether Tab / Shift+Tab stop on this node. A group of items is one Tab stop, like a native list: its current item
135
+ * (the one keyboard focus last had there, else the group's first usable node) plus the controls inside that same
136
+ * item; the arrow keys move between items. Every node outside a group is a stop.
137
+ */
138
+ IsTabStop(node) {
139
+ // an open popup window keeps the keyboard: Tab never leaves it, as with native menus
140
+ const scope = this.KeyboardScope;
141
+ if (scope) {
142
+ let c = node;
143
+ while (c && c !== scope)
144
+ c = c.Parent;
145
+ if (!c)
146
+ return false;
147
+ }
148
+ const found = SkiaAccessibilityManager.TryFindGroup(node);
149
+ if (!found)
150
+ return true;
151
+ let stopItem;
152
+ const remembered = this.groupFocus.get(found.Group);
153
+ const current = remembered && !remembered.IsDisposed && remembered.AccessibilityCanInteract && this.Snapshot.some((n) => n.Source === remembered)
154
+ ? SkiaAccessibilityManager.TryFindGroup(remembered) : undefined;
155
+ if (current && current.Group === found.Group)
156
+ stopItem = current.Item;
157
+ else {
158
+ for (const n of this.Snapshot) {
159
+ if (!n.Source.AccessibilityCanInteract)
160
+ continue;
161
+ const g = SkiaAccessibilityManager.TryFindGroup(n.Source);
162
+ if (g && g.Group === found.Group) {
163
+ stopItem = g.Item;
164
+ break;
165
+ }
166
+ }
167
+ }
168
+ return !stopItem || stopItem === found.Item;
169
+ }
170
+ /**
171
+ * Moves keyboard focus between the items of the group around `focused`, by item index like a native list: Down / Up in
172
+ * a Column, Right / Left in a Row, all four in a Wrap, a Grid or a Split layout (Up / Down by one row), Home / End to the
173
+ * first / last item, PageDown / PageUp by one viewport of the scroll around it. The ends stop the arrows, unless the group has KeyboardWrap. The target is the
174
+ * item itself when it is an interactive node, else its first interactive node; items the pointer cannot use are
175
+ * skipped. An item not realized (recycled cells) is scrolled in with ScrollToIndex when the group is the scroll's
176
+ * content, and focused once drawn. True when the key belongs to the group.
177
+ */
178
+ MoveInGroup(focused, key) {
179
+ const found = SkiaAccessibilityManager.TryFindGroup(focused);
180
+ if (!found)
181
+ return false;
182
+ const { Holder: group, Item: item } = found;
183
+ // a menu, list or radio group whose items are not the children of one layout (a panel with a title, a divider and
184
+ // columns of items): its items in reading order, one after another (drawnui-cross 6ao rule 1b, as DrawnUi.Rust)
185
+ const role = found.Group.AccessibilityRole;
186
+ const linear = role === Aria.RoleMenu || role === Aria.RoleMenuBar || role === Aria.RoleList || role === Aria.RoleListBox || role === Aria.RoleRadioGroup;
187
+ const holdsMany = (child) => this.Snapshot.filter((n) => n.CanInteract && IsInside(n.Source, child)).length > 1;
188
+ if (linear && item !== focused && ViewsOf(group).some(holdsMany)) {
189
+ const items = this.Snapshot.filter((n) => n.CanInteract && SkiaAccessibilityManager.TryFindGroup(n.Source)?.Group === found.Group).map((n) => n.Source);
190
+ const current = items.indexOf(focused);
191
+ if (current < 0)
192
+ return false;
193
+ const n = items.length, wrap = found.Group.KeyboardWrap || group.KeyboardWrap;
194
+ const target = key === "ArrowDown" || key === "ArrowRight" ? (wrap ? (current + 1) % n : Math.min(current + 1, n - 1))
195
+ : key === "ArrowUp" || key === "ArrowLeft" ? (wrap ? (current - 1 + n) % n : Math.max(current - 1, 0))
196
+ : key === "Home" ? 0 : key === "End" ? n - 1 : -1;
197
+ if (target < 0)
198
+ return false;
199
+ if (target !== current) {
200
+ this.RequestKeyboardFocus(items[target]);
201
+ found.Group.Superview?.Update();
202
+ }
203
+ return true;
204
+ }
205
+ const scroll = FindScroll(group), axis = AxisOf(group), count = ItemCount(group);
206
+ const current = this.groupRequest?.Group === group ? this.groupRequest.Index : IndexOf(group, item);
207
+ if (current < 0 || count < 1)
208
+ return false;
209
+ const row = axis === "Both" ? RowLength(group) : 1;
210
+ const vertical = axis !== "Horizontal";
211
+ const page = () => PageSize(scroll, item, vertical) * row;
212
+ let target, step;
213
+ const layout = asLayout(group), masonry = layout?.Type === "Masonry" ? MasonryTarget(layout, current, key, scroll) : undefined;
214
+ if (masonry !== undefined) {
215
+ target = masonry;
216
+ step = key === "ArrowDown" || key === "ArrowRight" || key === "PageDown" ? 1 : -1;
217
+ }
218
+ else if ((key === "ArrowDown" && axis === "Vertical") || (key === "ArrowRight" && axis !== "Vertical")) {
219
+ target = current + 1;
220
+ step = 1;
221
+ }
222
+ else if ((key === "ArrowUp" && axis === "Vertical") || (key === "ArrowLeft" && axis !== "Vertical")) {
223
+ target = current - 1;
224
+ step = -1;
225
+ }
226
+ else if (key === "ArrowDown" && axis === "Both") {
227
+ target = Math.min(count - 1, current + row);
228
+ step = 1;
229
+ }
230
+ else if (key === "ArrowUp" && axis === "Both") {
231
+ target = Math.max(0, current - row);
232
+ step = -1;
233
+ }
234
+ else if (key === "Home") {
235
+ target = 0;
236
+ step = 1;
237
+ }
238
+ else if (key === "End") {
239
+ target = count - 1;
240
+ step = -1;
241
+ }
242
+ else if (key === "PageDown" && scroll) {
243
+ target = Math.min(count - 1, current + page());
244
+ step = 1;
245
+ }
246
+ else if (key === "PageUp" && scroll) {
247
+ target = Math.max(0, current - page());
248
+ step = -1;
249
+ }
250
+ else
251
+ return false;
252
+ // KeyboardWrap on the group: the arrows go round, the last item to the first and back
253
+ if ((target < 0 || target >= count) && key.startsWith("Arrow") && (found.Group.KeyboardWrap || group.KeyboardWrap))
254
+ target = target < 0 ? count - 1 : 0;
255
+ if (target < 0 || target >= count || target === current)
256
+ return true; // at the end of the group nothing happens, the key still belongs to it
257
+ this.groupRequest = { Group: group, Scroll: scroll, Index: target, Step: step, Deadline: performance.now() + 3000, ScrollIssued: false };
258
+ group.Superview?.Update(); // resolved at the end of the next frame, together with the snapshot
259
+ return true;
260
+ }
261
+ // ---- keyboard focus moved by popup windows (drawnui-cross 6ao rule 2, and back to the opener on close) ----
262
+ focusRequest;
263
+ popupFocusRoot;
264
+ focusAnyInside = false;
265
+ popupFocusDeadline = 0;
266
+ /** The keyboard goes to this control at the end of the next frame (a popup closed: back to what opened it). */
267
+ RequestKeyboardFocus(control) {
268
+ if (this.appFocusThisFrame)
269
+ return;
270
+ this.focusRequest = control;
271
+ this.focusRequestDeadline = performance.now() + SkiaAccessibilityManager.FocusRequestWaitMs;
272
+ }
273
+ /**
274
+ * How long a keyboard focus request waits for its control to take input again (drawnui-cross 6bs): an app that shows
275
+ * the opener of a dialog again only once the dialog is gone (a HUD hidden under it) still gets the keyboard back there.
276
+ */
277
+ static FocusRequestWaitMs = 1000;
278
+ focusRequestDeadline = 0;
279
+ appFocus;
280
+ appFocusDeadline = 0;
281
+ appFocusThisFrame = false; // until the frame end: the app said where the focus goes, the library's moves give way
282
+ clearFocusRequest = false;
283
+ /**
284
+ * App code moves the keyboard and screen-reader focus at the end of the next frame (C# DrawnView.SetAccessibilityFocus,
285
+ * Rust `Cx::set_accessibility_focus`), for a custom menu that moves its highlight: to `control`, which gets it once
286
+ * drawn, within 3 seconds. It wins over the library's own moves in the same frame (a group's arrow move, a closed popup
287
+ * window giving the keyboard back to its opener); undefined cancels a move still waiting, the focus stays where it is.
288
+ * The focus ring shows only if the keyboard is in use (the browser's focus-visible).
289
+ */
290
+ SetAccessibilityFocus(control) {
291
+ this.appFocusThisFrame = true;
292
+ this.focusRequest = undefined;
293
+ this.groupRequest = undefined;
294
+ this.appFocus = control;
295
+ this.appFocusDeadline = performance.now() + 3000;
296
+ control?.Superview?.Update();
297
+ }
298
+ /**
299
+ * No drawn control keeps the keyboard and screen-reader focus (C# KeyboardFocusNode = null): at the end of the next
300
+ * frame the DOM focus goes back to the canvas and the keys belong to the app again.
301
+ */
302
+ ClearKeyboardFocus() { this.appFocus = undefined; this.clearFocusRequest = true; }
303
+ ProcessAppFocus(scale, w, h, requestFrame) {
304
+ const wanted = this.appFocus;
305
+ if (wanted.IsDisposed || performance.now() > this.appFocusDeadline) {
306
+ this.appFocus = undefined;
307
+ return;
308
+ }
309
+ this.Rebuild(scale, w, h);
310
+ if (!this.Snapshot.some((n) => n.Source === wanted)) {
311
+ requestFrame();
312
+ return;
313
+ } // not drawn yet: look again next frame
314
+ this.appFocus = undefined;
315
+ this.Focus(wanted);
316
+ }
317
+ popupScope;
318
+ /**
319
+ * While set, Tab stays inside this control: only its nodes are Tab stops and Tab / Shift+Tab go round them: the top
320
+ * popup window that takes input (a menu, a dropdown; set by the canvas), else the active modal.
321
+ */
322
+ get KeyboardScope() { return this.popupScope ?? this.ActiveModal; }
323
+ set KeyboardScope(v) { this.popupScope = v; }
324
+ // ---- modal accessibility scope (drawnui-cross 6av, C# AccessibilityModal) ----
325
+ /** The modal that accessibility is limited to: the topmost `AccessibilityModal` control that shows. Undefined when none. */
326
+ ActiveModal;
327
+ /**
328
+ * The node the overlay names the active modal after (drawnui-cross 6bs): the modal's own when it has a name, else the
329
+ * first named dialog inside it, so a nameless popup host around a dialog reads as that one dialog, not as two.
330
+ */
331
+ get ActiveModalNode() {
332
+ const modal = this.ActiveModal;
333
+ if (!modal)
334
+ return undefined;
335
+ const own = this.Snapshot.find((n) => n.Source === modal);
336
+ if (own?.Label)
337
+ return own;
338
+ return this.Snapshot.find((n) => (n.Role === Aria.RoleDialog || n.Role === Aria.RoleAlertDialog) && !!n.Label && n.Source !== modal && IsInside(n.Source, modal)) ?? own;
339
+ }
340
+ // where the keyboard was when each modal became active, to go back there when it goes
341
+ modalReturns = new Map();
342
+ /** A control behind the active modal (not in it, not in a popup window over it): no focus for navigation. */
343
+ IsOutsideModal(control) {
344
+ const modal = this.ActiveModal;
345
+ return !!modal && !IsInside(control, modal) && TopRoot(control) === TopRoot(modal);
346
+ }
347
+ /** Frame end, only while some control is flagged modal or one was active: follows the top modal. */
348
+ UpdateActiveModal() {
349
+ let next;
350
+ for (const m of AccessibilityModals) {
351
+ if (m.IsDisposed || !m.IsVisible || m.Opacity <= 0 || AccessibilityManagerHiddenByAncestor(m)
352
+ || m.Superview?.AccessibilityManager !== this || !IsDrawn(m))
353
+ continue;
354
+ if (!next || IsDrawnAbove(m, next))
355
+ next = m;
356
+ }
357
+ const previous = this.ActiveModal;
358
+ if (previous === next)
359
+ return;
360
+ this.ActiveModal = next;
361
+ this.ForceRebuildOnNextFrame(); // readers and Tab switch to the new scope in this frame
362
+ const keyboard = this.FocusedNode;
363
+ const back = previous && this.modalReturns.get(previous);
364
+ if (previous)
365
+ this.modalReturns.delete(previous);
366
+ // back unless the keyboard went elsewhere meanwhile; a keyboard node that left with the modal (disposed, detached) was in it
367
+ const leftWithIt = !!keyboard && (keyboard.IsDisposed || !keyboard.Superview);
368
+ const fromPrevious = !!back && (!keyboard || leftWithIt || IsInside(keyboard, previous));
369
+ if (next) {
370
+ // the keyboard is in use: into the modal, and back to `from` when it goes. A dialog handing over to another (How to
371
+ // play, then Paused) passes its own return on, in the same frame or while that return is still waiting (6bs).
372
+ const waiting = this.focusRequest && !this.focusRequest.IsDisposed && !IsInside(this.focusRequest, next) ? this.focusRequest : undefined;
373
+ const from = fromPrevious ? back : waiting ?? (keyboard && !leftWithIt && !IsInside(keyboard, next) ? keyboard : undefined);
374
+ if (from) {
375
+ this.modalReturns.set(next, from);
376
+ this.popupFocusRoot = next;
377
+ this.focusAnyInside = true;
378
+ this.popupFocusDeadline = performance.now() + SkiaAccessibilityManager.FocusRequestWaitMs;
379
+ if (from === this.focusRequest)
380
+ this.focusRequest = undefined;
381
+ }
382
+ }
383
+ if (fromPrevious && (!next || IsInside(back, next)))
384
+ this.RequestKeyboardFocus(back);
385
+ }
386
+ /** The Tab stop after (or before) `from` in reading order; inside a KeyboardScope it goes round, never leaving. */
387
+ NextTabStop(from, forward) {
388
+ const stops = this.Snapshot.filter((n) => n.CanInteract && this.IsTabStop(n.Source)).map((n) => n.Source);
389
+ if (stops.length === 0)
390
+ return undefined;
391
+ let current = from ? stops.indexOf(from) : -1;
392
+ if (current < 0)
393
+ current = forward ? -1 : stops.length;
394
+ const next = forward ? current + 1 : current - 1;
395
+ if (this.KeyboardScope)
396
+ return stops[(next + stops.length) % stops.length];
397
+ return next >= 0 && next < stops.length ? stops[next] : undefined;
398
+ }
399
+ /**
400
+ * Tab and Shift+Tab through a templated list whose cells are made on demand (DrawnUi.Net ac70a1d9): from a cell the
401
+ * keyboard goes to the next (previous) item, scrolled in and focused once drawn, unless the next stop is in the same
402
+ * cell; at the list's end it leaves the list. Entering the list it goes to its first (last) item. The overlay's DOM order
403
+ * lags the cells made while the list scrolls, so inside a list the browser's own Tab order is not used. True when it
404
+ * took the key.
405
+ */
406
+ TabThroughList(from, forward) {
407
+ if (this.KeyboardScope)
408
+ return false;
409
+ const next = this.NextTabStop(from, forward);
410
+ const here = TemplatedCellOf(from), there = next ? TemplatedCellOf(next) : undefined;
411
+ let list, target;
412
+ if (here) {
413
+ const step = forward ? 1 : -1;
414
+ if (there && there.Cell === here.Cell)
415
+ return false; // the next stop is in the same cell: the usual order
416
+ // a Tab while the previous one is still scrolling its item in goes on from that item (as the arrow keys)
417
+ const pending = this.groupRequest?.Group === here.List ? this.groupRequest.Index : undefined;
418
+ list = here.List;
419
+ target = (pending ?? here.Cell.ContextIndex) + step;
420
+ }
421
+ else if (there) {
422
+ list = there.List;
423
+ target = forward ? 0 : ItemCount(list) - 1;
424
+ if (there.Cell.ContextIndex === target)
425
+ return false;
426
+ }
427
+ else
428
+ return false;
429
+ if (target < 0 || target >= ItemCount(list))
430
+ return false;
431
+ this.groupRequest = { Group: list, Scroll: FindScroll(list), Index: target, Step: forward ? 1 : -1, Deadline: performance.now() + 3000, ScrollIssued: false };
432
+ list.Superview?.Update();
433
+ return true;
434
+ }
435
+ /**
436
+ * A popup window just opened: at the end of the frame that draws it, the keyboard goes to its arrow-key group's current
437
+ * choice (a checked radio, menuitemradio, option or tab), else to its first interactive item, however it was opened.
438
+ */
439
+ FocusOpenedPopup(root) { this.popupFocusRoot = root; this.focusAnyInside = false; this.popupFocusDeadline = performance.now() + SkiaAccessibilityManager.FocusRequestWaitMs; }
440
+ ProcessFocusRequests(scale, w, h, requestFrame) {
441
+ const request = this.focusRequest, root = this.popupFocusRoot;
442
+ this.focusRequest = this.popupFocusRoot = undefined;
443
+ if (root && !root.IsDisposed && root.Superview) {
444
+ this.Rebuild(scale, w, h);
445
+ const inside = (c) => { for (let p = c; p; p = p.Parent)
446
+ if (p === root)
447
+ return true; return false; };
448
+ const choice = (role) => role === Aria.RoleRadio || role === Aria.RoleMenuItemRadio || role === "option" || role === "tab";
449
+ // a popup: the items of its arrow-key group; a modal: its first control that takes input
450
+ const items = this.Snapshot.filter((n) => n.CanInteract && inside(n.Source) && (this.focusAnyInside || SkiaAccessibilityManager.TryFindGroup(n.Source)));
451
+ const target = items.find((n) => n.IsPressed === true && choice(n.Role)) ?? items[0];
452
+ if (target) {
453
+ this.FocusFromKeyboard(target.Source, scale, w, h);
454
+ return;
455
+ }
456
+ // a modal with nothing to take input yet (its buttons show a moment later): looked at again on the next frames, for a moment
457
+ if (this.focusAnyInside && performance.now() < this.popupFocusDeadline) {
458
+ this.popupFocusRoot = root;
459
+ this.focusRequest ??= request;
460
+ requestFrame();
461
+ return;
462
+ }
463
+ }
464
+ if (!request || request.IsDisposed || !request.Superview)
465
+ return;
466
+ if (request.AccessibilityCanInteract)
467
+ this.FocusFromKeyboard(request, scale, w, h);
468
+ // not taking input yet (shown again only after a dialog closed): asked again on the next frames, for a moment
469
+ else if (performance.now() < this.focusRequestDeadline) {
470
+ this.focusRequest = request;
471
+ requestFrame();
472
+ }
473
+ }
474
+ /** Frame end: focus the requested item once it is drawn, skipping items that cannot take input. */
475
+ ProcessGroupFocus(scale, w, h, requestFrame) {
476
+ const request = this.groupRequest;
477
+ if (!request)
478
+ return;
479
+ const cell = ItemAt(request.Group, request.Index);
480
+ if (cell && IsDrawn(cell)) {
481
+ const node = FindTarget(cell);
482
+ if (node) {
483
+ this.groupRequest = undefined;
484
+ this.FocusFromKeyboard(node, scale, w, h);
485
+ return;
486
+ }
487
+ // this item cannot take input: go on to the next one in the same direction
488
+ request.Index += request.Step;
489
+ if (request.Index < 0 || request.Index >= ItemCount(request.Group)) {
490
+ this.groupRequest = undefined;
491
+ return;
492
+ }
493
+ request.Deadline = performance.now() + 3000;
494
+ request.ScrollIssued = false;
495
+ }
496
+ else if (performance.now() > request.Deadline || request.Group.IsDisposed) {
497
+ this.groupRequest = undefined;
498
+ return;
499
+ }
500
+ if (!request.ScrollIssued) {
501
+ request.ScrollIssued = true;
502
+ const next = ItemAt(request.Group, request.Index);
503
+ if ((!next || !IsDrawn(next)) && request.Scroll && request.Scroll.Content === request.Group) {
504
+ request.Scroll.ScrollToIndex(request.Index, true, request.Step > 0 ? "End" : "Start");
505
+ }
506
+ }
507
+ requestFrame(); // keep frames coming until the item is drawn
508
+ }
509
+ FocusFromKeyboard(node, scale, w, h) {
510
+ this.Rebuild(scale, w, h); // the overlay looks the node up in the snapshot: make sure it is there now
511
+ this.Focus(node);
512
+ }
513
+ Focus(node) {
514
+ this.NoteFocus(node);
515
+ this.FocusedNode = node;
516
+ for (const cb of this.keyboardFocusRequested)
517
+ cb(node);
518
+ }
43
519
  Register(node) { this.nodes.add(node); this.dirty = true; }
44
520
  NotifyUpdated(node) {
45
521
  if (!this.nodes.has(node))
@@ -49,7 +525,29 @@ export class SkiaAccessibilityManager {
49
525
  for (const cb of this.liveUpdated)
50
526
  cb(node);
51
527
  }
52
- ForceRebuildOnNextFrame() { this.lastRebuild = 0; this.dirty = true; }
528
+ /** What the last snapshot says for each live region, to rebuild on the next frame when that changes. */
529
+ liveSaid = new Map();
530
+ LiveChanged() {
531
+ for (const [control, said] of this.liveSaid)
532
+ if (!control.IsDisposed && (control.AccessibilityLabel ?? "") !== said)
533
+ return true;
534
+ return false;
535
+ }
536
+ ForceRebuildOnNextFrame() { this.lastRebuild = -Infinity; this.dirty = true; }
537
+ /** The scrolls moving the keyboard's control into view, and when each was asked (FollowScroll). */
538
+ following = new Map();
539
+ /**
540
+ * Scrolls are bringing the keyboard's control into view: every frame they draw rebuilds the overlay until each has
541
+ * stopped, so the overlay's rects are where the controls are and the controls they bring near are there for the next
542
+ * Tab (the timed rebuild could come a second later, and Tab would fall out of the canvas).
543
+ */
544
+ FollowScroll(scrolls) {
545
+ const now = performance.now();
546
+ for (const s of scrolls)
547
+ this.following.set(s, now);
548
+ if (scrolls.length)
549
+ this.dirty = true;
550
+ }
53
551
  Unregister(node) {
54
552
  if (this.nodes.delete(node)) {
55
553
  node.OnAccessibilityUnregistered();
@@ -70,27 +568,61 @@ export class SkiaAccessibilityManager {
70
568
  * per MinUpdateIntervalMs otherwise (keeps rects in sync with scrolling); notifies only when the snapshot differs.
71
569
  */
72
570
  OnFrameEnd(scale, canvasWidthPx, canvasHeightPx, requestFrame) {
571
+ if (AccessibilityModals.size > 0 || this.ActiveModal)
572
+ this.UpdateActiveModal();
573
+ this.ProcessGroupFocus(scale, canvasWidthPx, canvasHeightPx, requestFrame);
574
+ if (this.clearFocusRequest) {
575
+ this.clearFocusRequest = false;
576
+ this.FocusedNode = undefined;
577
+ for (const cb of this.keyboardFocusRequested)
578
+ cb(undefined);
579
+ }
580
+ if (this.appFocus)
581
+ this.ProcessAppFocus(scale, canvasWidthPx, canvasHeightPx, requestFrame);
582
+ if (this.focusRequest || this.popupFocusRoot)
583
+ this.ProcessFocusRequests(scale, canvasWidthPx, canvasHeightPx, requestFrame);
584
+ this.appFocusThisFrame = false;
73
585
  if (this.nodes.size === 0 && this.Snapshot.length === 0)
74
586
  return;
75
587
  const now = performance.now();
588
+ if (this.following.size > 0) {
589
+ this.Rebuild(scale, canvasWidthPx, canvasHeightPx);
590
+ // a scroll is followed until it stopped (its animation starts with the next frame; 3 s at most)
591
+ for (const [s, at] of this.following)
592
+ if ((!s.IsScrolling && now - at > 50) || now - at > 3000)
593
+ this.following.delete(s);
594
+ return;
595
+ }
76
596
  const wait = this.MinUpdateIntervalMs - (now - this.lastRebuild);
597
+ // a live region whose text changed is read on the next frame, not at the next timed rebuild (accessibility-plan B2)
598
+ if (wait > 0 && this.liveSaid.size > 0 && this.LiveChanged()) {
599
+ this.Rebuild(scale, canvasWidthPx, canvasHeightPx);
600
+ return;
601
+ }
77
602
  if (wait > 0) {
78
603
  // rate-limited: rendering is on demand, so make sure a frame comes back once the interval has passed
79
604
  if (!this.pending)
80
605
  this.pending = window.setTimeout(() => { this.pending = 0; requestFrame(); }, wait);
81
606
  return;
82
607
  }
608
+ this.Rebuild(scale, canvasWidthPx, canvasHeightPx);
609
+ }
610
+ Rebuild(scale, canvasWidthPx, canvasHeightPx) {
83
611
  this.dirty = false;
84
- this.lastRebuild = now;
85
- const list = [];
612
+ this.lastRebuild = performance.now();
613
+ let list = [];
614
+ const modal = this.ActiveModal;
86
615
  for (const n of this.nodes) {
87
616
  if (!n.Superview) {
88
617
  this.nodes.delete(n);
89
618
  n.OnAccessibilityUnregistered();
90
619
  continue;
91
620
  } // detached from the tree
92
- if (!n.IsVisible || !n.IsAccessibilityElement || n.AccessibilityRole === Aria.RolePresentation)
621
+ // at opacity 0 a control is not seen, so it is not read either (DrawnUi.Net 4bd759e8)
622
+ if (!n.IsVisible || n.Opacity <= 0 || !n.IsAccessibilityElement || n.AccessibilityRole === Aria.RolePresentation)
93
623
  continue;
624
+ if (modal && this.IsOutsideModal(n))
625
+ continue; // a modal is shown: only its nodes, and those of popup windows over it
94
626
  if (AccessibilityManagerHiddenByAncestor(n))
95
627
  continue; // e.g. the root page kept mounted under a pushed shell page
96
628
  const px = n.GetAccessibilityPixelRect();
@@ -101,13 +633,20 @@ export class SkiaAccessibilityManager {
101
633
  if (px.Right < -canvasWidthPx || px.Bottom < -canvasHeightPx || px.Left > 2 * canvasWidthPx || px.Top > 2 * canvasHeightPx)
102
634
  continue;
103
635
  list.push({
104
- Id: n.AccessibilityId, Label: n.AccessibilityLabel, Hint: n.AccessibilityHint, Role: n.AccessibilityRole,
636
+ Id: n.AccessibilityId, Label: n.AccessibilityLabel, Hint: n.AccessibilityHint, Role: n.AccessibilityRole, Value: n.AccessibilityValue,
105
637
  Rect: new SKRect(px.Left / scale, px.Top / scale, px.Right / scale, px.Bottom / scale),
106
- CanInteract: n.AccessibilityCanInteract, IsPressed: n.AccessibilityIsPressed, Live: n.AccessibilityLive, Source: n,
107
- TextLines: n.AccessibilityTextSelectable ? n.GetAccessibilityTextLines(scale) : undefined,
638
+ CanInteract: n.AccessibilityCanInteract, IsPressed: n.AccessibilityIsPressed, Expanded: n.AccessibilityExpanded, Live: n.AccessibilityLive, Source: n,
639
+ Disabled: !n.AccessibilityCanInteract && Aria.IsInteractiveRole(n.AccessibilityRole),
108
640
  });
109
641
  }
110
- list.sort((a, b) => a.Rect.Top - b.Rect.Top || a.Rect.Left - b.Rect.Left);
642
+ list = PresentationalChildren(list);
643
+ list = SayNamesOnce(list);
644
+ list = PageOrder(list, scale);
645
+ this.liveSaid.clear();
646
+ // the control's own name (a name given to a child that says it is no change)
647
+ for (const n of list)
648
+ if (n.Live)
649
+ this.liveSaid.set(n.Source, n.Source.AccessibilityLabel ?? "");
111
650
  if (SkiaAccessibilityManager.Same(list, this.Snapshot))
112
651
  return;
113
652
  this.Snapshot = list;
@@ -119,25 +658,346 @@ export class SkiaAccessibilityManager {
119
658
  return false;
120
659
  for (let i = 0; i < a.length; i++) {
121
660
  const x = a[i], y = b[i];
122
- if (x.Source !== y.Source || x.Label !== y.Label || x.Hint !== y.Hint || x.Role !== y.Role || x.CanInteract !== y.CanInteract
123
- || x.IsPressed !== y.IsPressed || x.Live !== y.Live
661
+ if (x.Source !== y.Source || x.Label !== y.Label || x.Hint !== y.Hint || x.Role !== y.Role || x.CanInteract !== y.CanInteract || x.Disabled !== y.Disabled
662
+ || x.IsPressed !== y.IsPressed || x.Expanded !== y.Expanded || x.Live !== y.Live || !SameValue(x.Value, y.Value)
124
663
  || Math.abs(x.Rect.Left - y.Rect.Left) > 0.5 || Math.abs(x.Rect.Top - y.Rect.Top) > 0.5
125
664
  || Math.abs(x.Rect.Right - y.Rect.Right) > 0.5 || Math.abs(x.Rect.Bottom - y.Rect.Bottom) > 0.5)
126
665
  return false;
127
- const tx = x.TextLines, ty = y.TextLines;
128
- if (!!tx !== !!ty)
129
- return false;
130
- if (tx && ty) {
131
- if (tx.length !== ty.length)
132
- return false;
133
- for (let j = 0; j < tx.length; j++)
134
- if (tx[j].Text !== ty[j].Text || Math.abs(tx[j].Left - ty[j].Left) > 0.5 || Math.abs(tx[j].Top - ty[j].Top) > 0.5 || tx[j].FontSize !== ty[j].FontSize)
135
- return false;
136
- }
137
666
  }
138
667
  return true;
139
668
  }
140
669
  }
670
+ /**
671
+ * Each name is said once in the flat overlay (DrawnUi.Rust said_by_child): a text or a heading whose label repeats the
672
+ * label of the node it is in (its nearest ancestor node) is said by one of them. Under an interactive node (a card
673
+ * button) the child is left out, the button keeps its name; under any other node (a group card and its title) the
674
+ * node loses its name and the heading says it.
675
+ */
676
+ /** Roles whose content is their look, not more nodes (ARIA "children presentational", C# Aria.ChildrenArePresentational). */
677
+ const PresentationalChildrenRoles = new Set(["button", "checkbox", "radio", "switch", "tab", "option", "slider", "progressbar", "meter", "scrollbar", "separator", "img", "menuitemcheckbox", "menuitemradio"]);
678
+ /**
679
+ * The texts inside a button, a radio, a tab, a slider, an image... are its look: its name says them, so they are no nodes
680
+ * (C# SkiaAccessibilityManager.InsidePresentational). A control nested in it and selectable text stay; a link, a group,
681
+ * a list keep their content.
682
+ */
683
+ function PresentationalChildren(list) {
684
+ const bySource = new Map(list.map((x) => [x.Source, x]));
685
+ const inside = (x) => {
686
+ if (Aria.IsInteractiveRole(x.Role) || x.Source.AccessibilityTextSelectable)
687
+ return false;
688
+ for (let p = x.Source.Parent; p; p = p.Parent) {
689
+ const up = bySource.get(p);
690
+ if (up && PresentationalChildrenRoles.has(up.Role))
691
+ return true;
692
+ }
693
+ return false;
694
+ };
695
+ return list.some(inside) ? list.filter((x) => !inside(x)) : list;
696
+ }
697
+ function SayNamesOnce(list) {
698
+ const bySource = new Map(list.map((x) => [x.Source, x]));
699
+ const said = new Set();
700
+ for (const x of list) {
701
+ if (!x.Label || (x.Role !== Aria.RoleText && x.Role !== Aria.RoleHeading))
702
+ continue;
703
+ let owner;
704
+ for (let p = x.Source.Parent; p && !owner; p = p.Parent)
705
+ owner = bySource.get(p);
706
+ if (!owner || owner.Label !== x.Label)
707
+ continue;
708
+ // a dialog keeps its name though its title says it too: readers announce the dialog by name on entry
709
+ if (owner.Role === Aria.RoleDialog || owner.Role === Aria.RoleAlertDialog)
710
+ continue;
711
+ if (owner.CanInteract)
712
+ said.add(x);
713
+ else
714
+ owner.Label = undefined;
715
+ }
716
+ return said.size ? list.filter((x) => !said.has(x)) : list;
717
+ }
718
+ function SameValue(a, b) {
719
+ return a === b || (!!a && !!b && a.Now === b.Now && a.Min === b.Min && a.Max === b.Max && a.Step === b.Step && a.Text === b.Text);
720
+ }
721
+ // ---- group helpers (duck-typed layouts and scrolls) ----
722
+ function IsInside(control, root) {
723
+ for (let c = control; c; c = c.Parent)
724
+ if (c === root)
725
+ return true;
726
+ return false;
727
+ }
728
+ /** The top of the tree a control is in: the canvas content, or a popup window's root. */
729
+ function TopRoot(control) {
730
+ let c = control;
731
+ while (c.Parent)
732
+ c = c.Parent;
733
+ return c;
734
+ }
735
+ /** Drawing order: `a` draws over `b` (ZIndex, then tree order; a nested control over its ancestor). */
736
+ function IsDrawnAbove(a, b) {
737
+ const path = (c) => { const p = []; for (let x = c; x; x = x.Parent)
738
+ p.unshift(x); return p; };
739
+ const pa = path(a), pb = path(b);
740
+ let i = 0;
741
+ while (i < pa.length && i < pb.length && pa[i] === pb[i])
742
+ i++;
743
+ if (i === pa.length)
744
+ return false; // a contains b
745
+ if (i === pb.length)
746
+ return true; // b contains a
747
+ if (i === 0)
748
+ return false; // different trees (a popup window): the first one found stays
749
+ const sa = pa[i], sb = pb[i];
750
+ if (sa.ZIndex !== sb.ZIndex)
751
+ return sa.ZIndex > sb.ZIndex;
752
+ const views = ViewsOf(sa.Parent);
753
+ return views.indexOf(sa) > views.indexOf(sb);
754
+ }
755
+ /** A node the keyboard can land on: a role that is not presentation and takes input. */
756
+ function IsInteractiveNode(c) {
757
+ return !!c.AccessibilityRole && c.AccessibilityRole !== Aria.RolePresentation && c.IsAccessibilityElement && c.AccessibilityCanInteract;
758
+ }
759
+ /** Children of a control (a layout's Views, a scroll's Content). */
760
+ function ViewsOf(c) {
761
+ if ("Views" in c)
762
+ return c.Views;
763
+ const content = c.Content;
764
+ return content ? [content] : [];
765
+ }
766
+ function FindScroll(group) {
767
+ for (let p = group.Parent; p; p = p.Parent) {
768
+ const s = asScroll(p);
769
+ if (s)
770
+ return s;
771
+ }
772
+ return undefined;
773
+ }
774
+ /** Down / Up in a Column, Right / Left in a Row, all four in a Wrap, a Grid or a Split layout. */
775
+ function AxisOf(group) {
776
+ const l = asLayout(group);
777
+ if (!l)
778
+ return "Vertical";
779
+ if (l.Type === "Column")
780
+ return l.Split <= 1 ? "Vertical" : "Both";
781
+ if (l.Type === "Row")
782
+ return "Horizontal";
783
+ if (l.Type === "Absolute")
784
+ return "Vertical";
785
+ return "Both";
786
+ }
787
+ /** Items per row of a 2D group: Split when set, else the items sharing the row of `item`. */
788
+ /**
789
+ * Items per row of a 2D group: the Split, else the items on the row of the group's first item. C# counts the focused
790
+ * item's row, which the short last row of a wrap leaves short (Up from it skipped items); DrawnUi.Rust afef66f.
791
+ */
792
+ function RowLength(group) {
793
+ const l = asLayout(group);
794
+ if (l && l.Split > 1)
795
+ return l.Split;
796
+ const first = ItemAt(group, 0) ?? FirstRealized(group);
797
+ if (!first)
798
+ return 1;
799
+ const top = first.GetAccessibilityPixelRect();
800
+ if (top.Height <= 0)
801
+ return 1;
802
+ const mid = (top.Top + top.Bottom) / 2;
803
+ let count = 0;
804
+ for (const child of ViewsOf(group)) {
805
+ const r = child.GetAccessibilityPixelRect();
806
+ if (r.Height > 0 && Math.abs((r.Top + r.Bottom) / 2 - mid) < top.Height / 2)
807
+ count++;
808
+ }
809
+ return Math.max(1, count);
810
+ }
811
+ function ItemCount(group) {
812
+ const l = asLayout(group);
813
+ return l?.IsTemplated ? l.ItemsSource?.length ?? 0 : ViewsOf(group).length;
814
+ }
815
+ function IndexOf(group, item) {
816
+ const l = asLayout(group);
817
+ return l?.IsTemplated ? item.ContextIndex : ViewsOf(group).indexOf(item);
818
+ }
819
+ /** The view of an item: the realized cell of a templated layout (none when its cell is not in use), else the child. */
820
+ function ItemAt(group, index) {
821
+ const l = asLayout(group);
822
+ if (l?.IsTemplated) {
823
+ const v = l.ChildrenFactory.GetViewForIndex(index);
824
+ return v && v.ContextIndex === index ? v : undefined;
825
+ }
826
+ return index >= 0 && index < ViewsOf(group).length ? ViewsOf(group)[index] : undefined;
827
+ }
828
+ /** The realized item with the lowest index (item 0 of a scrolled recycled group may not be realized). */
829
+ function FirstRealized(group) {
830
+ let best;
831
+ for (const v of ViewsOf(group))
832
+ if (!best || IndexOf(group, v) < IndexOf(group, best))
833
+ best = v;
834
+ return best;
835
+ }
836
+ /** Arranged on the canvas: realized and laid out (a templated layout keeps only the cells it draws in use). */
837
+ function IsDrawn(control) {
838
+ const r = control.GetAccessibilityPixelRect();
839
+ return !!control.Superview && r.Width > 0 && r.Height > 0;
840
+ }
841
+ /** Items per viewport of the scroll around the group. */
842
+ /**
843
+ * C# MasonryTarget (2ad608d1): the target of an arrow or page key in a Masonry group, from the placed items. Up / Down go
844
+ * to the previous / next item of the same column, Left / Right to the item of the neighbour column nearest in height,
845
+ * PageUp / PageDown up to one viewport along the column (else the next / previous item of the column). `current` when
846
+ * there is no item that way; undefined for other keys or no placement (the generic rules apply).
847
+ */
848
+ function MasonryTarget(layout, current, key, scroll) {
849
+ if (key !== "ArrowDown" && key !== "ArrowUp" && key !== "ArrowLeft" && key !== "ArrowRight" && key !== "PageDown" && key !== "PageUp")
850
+ return undefined;
851
+ const cells = layout.GetMasonryPlacement?.() ?? [];
852
+ const focused = cells.find((c) => c.Index === current);
853
+ if (!focused)
854
+ return undefined;
855
+ const lefts = [...new Set(cells.map((c) => Math.round(c.Rect.Left)))].sort((a, b) => a - b);
856
+ const columnOf = (c) => lefts.indexOf(Math.round(c.Rect.Left));
857
+ const col = columnOf(focused), top = focused.Rect.Top, mid = (focused.Rect.Top + focused.Rect.Bottom) / 2;
858
+ const column = cells.filter((c) => columnOf(c) === col).sort((a, b) => a.Rect.Top - b.Rect.Top);
859
+ const below = column.filter((c) => c.Rect.Top > top), above = column.filter((c) => c.Rect.Top < top);
860
+ const viewport = scroll ? scroll.DrawingRect.Height : 0;
861
+ let found;
862
+ if (key === "ArrowDown")
863
+ found = below[0];
864
+ else if (key === "ArrowUp")
865
+ found = above[above.length - 1];
866
+ else if (key === "ArrowRight" || key === "ArrowLeft") {
867
+ const next = col + (key === "ArrowRight" ? 1 : -1);
868
+ let best = Infinity;
869
+ for (const c of cells) {
870
+ if (columnOf(c) !== next)
871
+ continue;
872
+ const d = Math.abs((c.Rect.Top + c.Rect.Bottom) / 2 - mid);
873
+ if (d < best) {
874
+ best = d;
875
+ found = c;
876
+ }
877
+ }
878
+ }
879
+ else {
880
+ if (!scroll)
881
+ return undefined;
882
+ if (key === "PageDown") {
883
+ const within = below.filter((c) => c.Rect.Top <= top + viewport);
884
+ found = within[within.length - 1] ?? below[0];
885
+ }
886
+ else {
887
+ const within = above.filter((c) => c.Rect.Top >= top - viewport);
888
+ found = within[0] ?? above[above.length - 1];
889
+ }
890
+ }
891
+ return found?.Index ?? current;
892
+ }
893
+ function PageSize(scroll, item, vertical) {
894
+ const v = scroll.DrawingRect, size = item.MeasuredSize.Pixels;
895
+ const viewport = vertical ? v.Height : v.Width, s = vertical ? size.Height : size.Width;
896
+ return s > 0 ? Math.max(1, Math.floor(viewport / s)) : 1;
897
+ }
898
+ /** The item itself when it is an interactive node, else its first interactive node in tree order. */
899
+ function FindTarget(item) {
900
+ if (item.IsAccessibilityElement && item.AccessibilityCanInteract)
901
+ return item;
902
+ for (const child of ViewsOf(item)) {
903
+ const found = FindTarget(child);
904
+ if (found)
905
+ return found;
906
+ }
907
+ return undefined;
908
+ }
909
+ /** The cell of a templated layout that holds a control, and that layout. */
910
+ function TemplatedCellOf(control) {
911
+ for (let c = control; c?.Parent; c = c.Parent)
912
+ if (asLayout(c.Parent)?.IsTemplated)
913
+ return { List: c.Parent, Cell: c };
914
+ return undefined;
915
+ }
916
+ /** The scrolls a control is inside, outermost first. */
917
+ /**
918
+ * Page order (as DrawnUi.Net SkiaAccessibilityManager.OrderLocked and DrawnUi.Rust): the nodes of a scroll take the scroll's
919
+ * place among the others and are ordered inside it, so content scrolled above or below the viewport never lands before a
920
+ * fixed header or after a footer. Outside scrolls: by top, a row (tops within half the smaller height) read left to right.
921
+ * Inside a scroll: tree order, the order of its content (the Tab order of browsers is the document order too), so a 3D
922
+ * viewer's dots never interleave with the controls beside it.
923
+ */
924
+ function PageOrder(list, scale) {
925
+ const groups = new Map();
926
+ const tops = [];
927
+ for (const n of list) {
928
+ const scroll = OutermostDrawnScroll(n.Source);
929
+ if (!scroll) {
930
+ tops.push({ Rect: n.Rect, Node: n });
931
+ continue;
932
+ }
933
+ let members = groups.get(scroll);
934
+ if (!members) {
935
+ groups.set(scroll, members = []);
936
+ const r = scroll.GetAccessibilityPixelRect();
937
+ tops.push({ Rect: { Top: r.Top / scale, Left: r.Left / scale, Height: r.Height / scale }, Scroll: scroll });
938
+ }
939
+ members.push(n);
940
+ }
941
+ tops.sort((a, b) => a.Rect.Top - b.Rect.Top || a.Rect.Left - b.Rect.Left);
942
+ // reading order: tops within half the smaller height form one row, read left to right (C# RectComparer + LeftComparer)
943
+ for (let start = 0, i = 1; i <= tops.length; i++) {
944
+ if (i < tops.length) {
945
+ const first = tops[start].Rect, next = tops[i].Rect;
946
+ if (next.Top - first.Top < Math.min(first.Height, next.Height) / 2)
947
+ continue;
948
+ }
949
+ if (i - start > 1) {
950
+ const row = tops.slice(start, i).sort((a, b) => a.Rect.Left - b.Rect.Left);
951
+ for (let k = 0; k < row.length; k++)
952
+ tops[start + k] = row[k];
953
+ }
954
+ start = i;
955
+ }
956
+ const out = [];
957
+ for (const t of tops) {
958
+ if (t.Node) {
959
+ out.push(t.Node);
960
+ continue;
961
+ }
962
+ const members = groups.get(t.Scroll);
963
+ if (members.length > 1) {
964
+ const paths = new Map(members.map((m) => [m, TreePath(m.Source, t.Scroll)]));
965
+ members.sort((a, b) => ComparePaths(paths.get(a), paths.get(b)));
966
+ }
967
+ out.push(...members);
968
+ }
969
+ return out;
970
+ }
971
+ /** The outermost scroll around a control that is on screen (has a drawn rect), undefined when none is. */
972
+ function OutermostDrawnScroll(control) {
973
+ let found;
974
+ for (let p = control.Parent; p; p = p.Parent) {
975
+ if (!asScroll(p))
976
+ continue;
977
+ const r = p.GetAccessibilityPixelRect();
978
+ if (r.Width > 0 && r.Height > 0)
979
+ found = p;
980
+ }
981
+ return found;
982
+ }
983
+ /** Child indices from root down to control; a recycled cell counts by its item (C# TreePath). */
984
+ function TreePath(control, root) {
985
+ const path = [];
986
+ for (let c = control; c && c !== root; c = c.Parent) {
987
+ const parent = c.Parent;
988
+ if (!parent)
989
+ break;
990
+ const layout = asLayout(parent);
991
+ path.push(layout?.IsTemplated ? c.ContextIndex : ViewsOf(parent).indexOf(c));
992
+ }
993
+ return path.reverse();
994
+ }
995
+ function ComparePaths(a, b) {
996
+ for (let i = 0; i < Math.min(a.length, b.length); i++)
997
+ if (a[i] !== b[i])
998
+ return a[i] - b[i];
999
+ return a.length - b.length; // a container before what it holds
1000
+ }
141
1001
  /** True when any ancestor is hidden (IsVisible false or Opacity 0): the subtree is not drawn, so it gets no nodes. */
142
1002
  function AccessibilityManagerHiddenByAncestor(control) {
143
1003
  let p = control.Parent;