@buoy-gg/focus-inspector 7.0.35 → 7.0.38

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 (42) hide show
  1. package/README.md +47 -122
  2. package/lib/commonjs/detect/detectors.js +1 -351
  3. package/lib/commonjs/index.js +1 -106
  4. package/lib/commonjs/observe/focusEventObserver.js +1 -105
  5. package/lib/commonjs/observe/keyEventObserver.js +1 -94
  6. package/lib/commonjs/observe/rawEventEmitter.js +1 -44
  7. package/lib/commonjs/scan/focusableScanner.js +1 -497
  8. package/lib/commonjs/store/focusStore.js +1 -325
  9. package/lib/commonjs/sync/focusInspectorSyncAdapter.js +1 -247
  10. package/lib/module/detect/detectors.js +1 -341
  11. package/lib/module/index.js +1 -36
  12. package/lib/module/observe/focusEventObserver.js +1 -99
  13. package/lib/module/observe/keyEventObserver.js +1 -86
  14. package/lib/module/observe/rawEventEmitter.js +1 -40
  15. package/lib/module/scan/focusableScanner.js +1 -492
  16. package/lib/module/store/focusStore.js +1 -312
  17. package/lib/module/sync/focusInspectorSyncAdapter.js +1 -244
  18. package/package.json +3 -3
  19. package/lib/commonjs/detect/detectors.js.map +0 -1
  20. package/lib/commonjs/index.js.map +0 -1
  21. package/lib/commonjs/observe/focusEventObserver.js.map +0 -1
  22. package/lib/commonjs/observe/keyEventObserver.js.map +0 -1
  23. package/lib/commonjs/observe/rawEventEmitter.js.map +0 -1
  24. package/lib/commonjs/scan/focusableScanner.js.map +0 -1
  25. package/lib/commonjs/store/focusStore.js.map +0 -1
  26. package/lib/commonjs/sync/focusInspectorSyncAdapter.js.map +0 -1
  27. package/lib/module/detect/detectors.js.map +0 -1
  28. package/lib/module/index.js.map +0 -1
  29. package/lib/module/observe/focusEventObserver.js.map +0 -1
  30. package/lib/module/observe/keyEventObserver.js.map +0 -1
  31. package/lib/module/observe/rawEventEmitter.js.map +0 -1
  32. package/lib/module/scan/focusableScanner.js.map +0 -1
  33. package/lib/module/store/focusStore.js.map +0 -1
  34. package/lib/module/sync/focusInspectorSyncAdapter.js.map +0 -1
  35. package/lib/typescript/detect/detectors.d.ts.map +0 -1
  36. package/lib/typescript/index.d.ts.map +0 -1
  37. package/lib/typescript/observe/focusEventObserver.d.ts.map +0 -1
  38. package/lib/typescript/observe/keyEventObserver.d.ts.map +0 -1
  39. package/lib/typescript/observe/rawEventEmitter.d.ts.map +0 -1
  40. package/lib/typescript/scan/focusableScanner.d.ts.map +0 -1
  41. package/lib/typescript/store/focusStore.d.ts.map +0 -1
  42. package/lib/typescript/sync/focusInspectorSyncAdapter.d.ts.map +0 -1
package/README.md CHANGED
@@ -1,136 +1,61 @@
1
1
  # @buoy-gg/focus-inspector
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@buoy-gg/focus-inspector?style=flat-square&labelColor=1c1c1c&color=10B981)](https://www.npmjs.com/package/@buoy-gg/focus-inspector) [![npm downloads](https://img.shields.io/npm/dm/@buoy-gg/focus-inspector?style=flat-square&labelColor=1c1c1c&color=10B981)](https://www.npmjs.com/package/@buoy-gg/focus-inspector)
3
+ Inspect observed focus transitions and the discovered focusable inventory in a React Native TV app. Buoy Desktop shows current observations, directional history, and possible focus problems.
4
4
 
5
- **See what holds D-pad focus in your Apple TV / Android TV app from Buoy Desktop — every focus move with the direction that caused it, the full focusable inventory, and automatic flags for focus that gets stuck, vanishes, or never arrives.**
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @buoy-gg/core @buoy-gg/external-sync @buoy-gg/focus-inspector
9
+ ```
6
10
 
7
- Part of [Buoy](https://github.com/Buoy-gg/buoy) — devtools that live inside your React Native app. Install it and the Focus panel lights up in Buoy Desktop for that device.
11
+ ## Account setup
8
12
 
9
- ## Install
13
+ Start in a development build with a Free or Pro Buoy account key. From the app directory:
10
14
 
11
15
  ```bash
12
- npm install @buoy-gg/core @buoy-gg/focus-inspector
16
+ npx --package=@buoy-gg/core buoy login
17
+ ```
18
+
19
+ For Expo, initialize Buoy with the key written to `.env.local`:
20
+
21
+ ```tsx
22
+ import { Buoy } from "@buoy-gg/core";
23
+
24
+ Buoy.init({ licenseKey: process.env.EXPO_PUBLIC_BUOY_KEY });
13
25
  ```
14
26
 
27
+ For React Native CLI, pass the key through your app's environment configuration; `.env.local` is not loaded automatically. Keep the menu inside the providers used by your app. Restart the development server after installing packages. See the [Quick Start](https://buoy.gg/buoy/latest/docs/quick-start) for the complete root component.
28
+
29
+ ## Connect a TV app
30
+
31
+ Use a compatible react-native-tvos app and follow the [TV installation guide](https://buoy.gg/buoy/latest/docs/tv/installation). Mount the core integration after account initialization:
32
+
15
33
  ```tsx
16
34
  import { FloatingDevTools } from "@buoy-gg/core";
17
35
 
18
- export default function App() {
19
- return (
20
- <>
21
- {/* your app */}
22
- <FloatingDevTools headless /> {/* TV apps run headless — no bubble */}
23
- </>
24
- );
25
- }
36
+ <FloatingDevTools headless />;
26
37
  ```
27
38
 
28
- Requires `react-native-tvos` (the TV fork) and the New Architecture. On a phone build the
29
- adapter reports `supported: false` and attaches nothing.
30
-
31
- The focus listeners attach when the package loads, not when the panel opens: focus can only be
32
- observed arriving, never queried, so a tool that started listening on demand would know nothing
33
- until focus next moved — and would be blind exactly when focus is stuck. The fiber scan, which is
34
- the part with real cost, stays gated on a dashboard actually watching.
35
-
36
- ## What it shows
37
-
38
- - **Now** — the element that currently holds focus: its `testID`, component, native tag, frame
39
- and dwell time, plus a warning when focus has parked somewhere invisible or left the tree.
40
- - **Observed exits** — a D-pad compass of where each direction has actually been seen to lead
41
- from the focused element. A direction reads **unknown** until it has been pressed from there.
42
- - **Transitions** — a newest-first timeline of every focus move, with the direction credited to
43
- it and the dwell time it ended.
44
- - **Focusables** — every element the focus engine could land on, including the ones below the
45
- fold and the invisible `TVFocusGuideView` stops, with a visited/unvisited marker and coverage.
46
- - **Flags** — dead ends, focus traps, invisible stops, focus loss, and suspected-unreachable
47
- elements.
48
-
49
- Actions: `rescan`, `clearHistory`, `setTracking`, and `focusElement({ nativeTag })` — put focus
50
- somewhere and watch what the D-pad does from there.
51
-
52
- ## What this package does — and deliberately does not
53
-
54
- It **renders nothing**, and that is a hard constraint rather than a preference. On Android TV any
55
- focusable view in an overlay becomes a D-pad stop in the host app's focus order
56
- (`pointerEvents="box-none"` gates touch, not focus), so an on-device panel would change the focus
57
- order it is measuring. Buoy Desktop is the surface.
58
-
59
- It ships **no native code** — no podspec, no gradle. The focus stream is JS-only.
60
-
61
- It is **inert**. The only thing it ever writes is `focusElement`, which you have to click. The
62
- inventory is a read-only fiber walk (`memoizedProps` + `measureInWindow`); nothing is mounted and
63
- no prop is set. Verified empirically: the same 12-press script over the TV example produces a
64
- byte-identical focus sequence with the tool active and with it disabled (see the matrix below).
65
-
66
- ## How it knows
67
-
68
- **Focus** comes from `react-native/Libraries/Core/RawEventEmitter`, which mirrors every
69
- Fabric-dispatched event before React's plugins see it. On both platforms every focus change
70
- arrives there as `topFocus` / `topBlur` carrying the focused view's native tag — no native code,
71
- no app instrumentation, and crucially it sees focus landing on views with **no** `onFocus`
72
- handler. That last part is the whole point: a `TVFocusGuideView` with `destinations` is a real
73
- focus stop with no highlight, and a per-component observer would report "focus is nowhere" for
74
- exactly the bug you are chasing. The deep import is guarded; if React Native ever moves it, the
75
- snapshot degrades to `observer: "unavailable"` and the panel says so.
76
-
77
- `TVEventHandler`'s `focus`/`blur` HWEvents are *not* the source: 0.85 deleted the Fabric calls
78
- that emitted them ([fork #1037](https://github.com/react-native-tvos/react-native-tvos/issues/1037)).
79
- The 0.83 line still sends them with a tag, and they are folded in as optional enrichment only.
80
-
81
- **Direction** comes from `TVEventHandler`'s D-pad events, which carry no tag — so the two streams
82
- are joined on time, in both directions, because their delivery order is not deterministic. Key
83
- down is ignored, never key up (tvOS emits only key-up for button presses).
84
-
85
- **Nothing is computed.** There is no JS API for "which view has focus" and none for "if focus is
86
- here and the user presses RIGHT, where does it go" — Android has
87
- `FabricUIManager.findNextFocusableElement` natively but never exposes it to JS, and tvOS's UIKit
88
- engine decides at runtime and answers nothing. So every edge in this tool is one your app
89
- actually took. Geometry is used only to *downgrade* a flag ("nothing exists past this, so the
90
- screen just ends here") or to raise a *suspicion*, never to assert where the engine would go. The
91
- panel says "unvisited" and "suspected unreachable", never "unreachable".
92
-
93
- ## Detectors
94
-
95
- | Flag | Rule |
96
- |---|---|
97
- | **Dead end** | 3+ presses of one direction in a row from the same element moved nothing, and the inventory knows of something focusable further along in that direction. One successful move clears the run. |
98
- | **Trap** | A region — an element, or everything inside one `TVFocusGuideView` — that focus was never observed escaping in *any* direction where something focusable exists outside it. Annotated when the enclosing guide sets `trapFocus*`, which suggests it was asked for. |
99
- | **Invisible stop** | Focus landed on a `TVFocusGuideView` (a stop with no highlight) or on a tag the inventory has never seen. This is what "focus vanished" looks like. |
100
- | **Focus lost** | A view blurred and nothing took focus within 400ms — focus left the React Native tree. |
101
- | **Coverage** | Visited vs. inventory, with *suspected unreachable* for elements that are unvisited, have no observed edge pointing at them, and sit right beside a visited neighbour. |
102
-
103
- Flags are judged per screen: anything recorded before the current scan belonged to a screen that
104
- no longer exists and is not judged at all.
105
-
106
- ## Platform notes
107
-
108
- - **`trapFocus*` holds on tvOS and silently does not hold on Android TV.** An observed trap on
109
- Android is therefore always a real bug, whatever the props say. Do not "fix" the detector to
110
- make the platforms agree.
111
- - **Focus/blur ordering differs by platform.** tvOS Fabric delivers `topFocus(new)` and *then*
112
- `topBlur(old)`; Android delivers blur first. Both are handled — a blur for an element that no
113
- longer holds focus is a late echo, not the start of a loss.
114
- - `Platform.isTVOS` is `undefined` on Android TV. Only ever test `=== true`.
115
-
116
- ## Verified on device
117
-
118
- Apple TV 4K simulator (tvOS 26.2) and Android TV emulator (API 34), both Fabric,
119
- react-native-tvos 0.85.3-3, driven with `idb ui key` / `adb shell input keyevent` over the TV
120
- example app's focus fixtures.
121
-
122
- | Case | Apple TV | Android TV |
123
- |---|---|---|
124
- | Observer + D-pad stream attach; inventory scans | ✅ 63 focusables on the home screen | ✅ 63 focusables |
125
- | Clean traversal: correct `from → direction → to`, coverage climbs, no false flags | ✅ | ✅ |
126
- | Direction credited in both delivery orders (key before / after the focus pair) | ✅ | ✅ |
127
- | Screen edge downgraded, not flagged (LEFT from the leftmost card) | ✅ | ✅ |
128
- | Armed 4-way `trapFocus` guide → trap flagged, annotated "guide asked for this" | ✅ | ✅ correctly **not** flagged — `trapFocus` does not hold there, and focus escaped |
129
- | Trap released → flag clears on the next traversal | ✅ | n/a |
130
- | Focus parked on a `destinations` guide → invisible-stop flag | ✅ | not reproduced — the engine returned a real dead end instead, which was flagged as one |
131
- | `focusElement` moves focus (`requestTVFocus`) | ✅ | ✅ |
132
- | Inertness: identical focus sequence with the tool active vs. disabled | ✅ | — |
133
-
134
- ## License
135
-
136
- Proprietary. See the [Buoy](https://github.com/Buoy-gg/buoy) repository.
39
+ Keep the TV app headless so a focusable developer overlay does not interfere with its navigation. Open Desktop, sign in, select the device, and open the tool's panel. A phone build is not a substitute for testing TV behavior.
40
+
41
+ ## Observe a known focus path
42
+
43
+ Focus Inspector requires the TV fork and New Architecture event support. Move through a short known path and compare the highlighted element with the panel's transitions. A direction remains unknown until it has been observed. Scan the screen and inspect discovered elements that remain unvisited.
44
+
45
+ Listeners observe focus events; inventory scanning depends on dashboard watch state. Internal React Native event APIs may change between versions. If the observer is unavailable, the panel should report that state rather than claim complete capture.
46
+
47
+ ## Interpret findings
48
+
49
+ Flags identify possible dead ends, traps, invisible stops, focus loss, and unvisited elements. They are diagnostic heuristics, not proof that an element is unreachable. Intentional focus guides can constrain navigation on both tvOS and Android TV; evaluate trapFocus behavior against the TV fork version your app uses.
50
+
51
+ Actions include `rescan`, `clearHistory`, `setTracking`, and `focusElement({ nativeTag })`. The last action changes focus; it is not equivalent to traversing the same route with D-pad presses.
52
+
53
+ Verify your routes, layouts, and TV fork version, including a comparison with capture disabled if you suspect measurement interference.
54
+
55
+ [Full Focus Inspector guide](https://buoy.gg/buoy/latest/docs/tools/focus-inspector)
56
+
57
+ ## Desktop and MCP
58
+
59
+ React Native connections require `@buoy-gg/external-sync`. Follow the [Desktop connection guide](https://buoy.gg/buoy/latest/docs/desktop), sign in to Desktop separately, and confirm the selected device. [MCP](https://buoy.gg/buoy/latest/docs/mcp) also needs a process account and Pro access.
60
+
61
+ Device data can travel over your LAN to the configured broker. Account validation makes network requests. See [Telemetry](https://buoy.gg/buoy/latest/docs/telemetry) for data-flow details.
@@ -1,351 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.DIRECTIONS = exports.DEAD_END_ATTEMPTS = exports.COVERAGE_MIN_PROBES = void 0;
7
- exports.computeCoverage = computeCoverage;
8
- exports.detectDeadEnds = detectDeadEnds;
9
- exports.detectFlags = detectFlags;
10
- exports.detectInvisibleStops = detectInvisibleStops;
11
- exports.detectTraps = detectTraps;
12
- exports.observedEdges = observedEdges;
13
- /**
14
- * detectors — pure functions from the recorded stream to the three TV focus bug
15
- * classes. No timers, no RN, no fiber access, so every rule below is a unit
16
- * test rather than a device session.
17
- *
18
- * The honesty rule that shapes all of this: focus can only be OBSERVED from JS,
19
- * never computed. There is no "where would RIGHT take me" API on either
20
- * platform, and a JS re-implementation of Android's FocusFinder geometry would
21
- * lie in exactly the cases this tool exists for (guides, traps, nextFocus*
22
- * overrides). So geometry is only ever used to *downgrade* a flag ("that's the
23
- * screen edge, not a dead end") or to *suspect* one — never to assert what the
24
- * engine would do.
25
- */
26
-
27
- const DIRECTIONS = exports.DIRECTIONS = ["up", "down", "left", "right"];
28
-
29
- /** Consecutive failed presses in one direction before it counts as a dead end. */
30
- const DEAD_END_ATTEMPTS = exports.DEAD_END_ATTEMPTS = 3;
31
-
32
- /** Presses before "unvisited" is worth reporting at all — below this it's noise. */
33
- const COVERAGE_MIN_PROBES = exports.COVERAGE_MIN_PROBES = 12;
34
- function keyOf(tag, direction) {
35
- return `${tag}:${direction}`;
36
- }
37
-
38
- /**
39
- * A direction can legitimately move focus sometimes and not others (a rail
40
- * scrolls, then hits its end), so a dead end is the *trailing run* of failures,
41
- * not a lifetime tally. One successful move clears it.
42
- */
43
- function detectDeadEnds(probes, nodes, minAttempts = DEAD_END_ATTEMPTS, since = 0) {
44
- const trailing = new Map();
45
- for (const probe of probes) {
46
- if (!probe.resolved || probe.tag === null) continue;
47
- if (probe.at < since) continue;
48
- const key = keyOf(probe.tag, probe.direction);
49
- if (probe.movedTo === null) trailing.set(key, (trailing.get(key) ?? 0) + 1);else trailing.set(key, 0);
50
- }
51
- const byTag = new Map();
52
- for (const node of nodes) byTag.set(node.nativeTag, node);
53
- const flags = [];
54
- trailing.forEach((attempts, key) => {
55
- if (attempts < minAttempts) return;
56
- const [tagPart, directionPart] = key.split(":");
57
- const tag = Number(tagPart);
58
- const direction = directionPart;
59
- flags.push({
60
- tag,
61
- direction,
62
- attempts,
63
- atEdge: isAtEdge(byTag.get(tag), direction, nodes)
64
- });
65
- });
66
- return flags.sort((a, b) => b.attempts - a.attempts);
67
- }
68
-
69
- /**
70
- * "The screen ends here" — the one honest reason a direction can legitimately
71
- * move nothing.
72
- *
73
- * This is NOT the viewport test it started as. Abutting the bottom of the
74
- * WINDOW says nothing inside a ScrollView: the armed focus trap in the TV
75
- * example sits flush with the viewport bottom at its scroll position, so a
76
- * geometric edge test excused a real four-way trap as "expected". What actually
77
- * matters is whether the inventory knows of a focusable further along in that
78
- * direction — if it does, focus refusing to move is a dead end no matter where
79
- * the element is drawn.
80
- *
81
- * Geometry still only ever DOWNGRADES a flag here, which is the rule for the
82
- * whole detector layer: it never claims to know where the engine would go.
83
- */
84
- function isAtEdge(node, direction, nodes) {
85
- const frame = node?.frame;
86
- if (!frame) return false;
87
- const centerX = frame.x + frame.width / 2;
88
- const centerY = frame.y + frame.height / 2;
89
- for (const other of nodes) {
90
- if (other.nativeTag === node.nativeTag) continue;
91
- // Guides are stops, but an invisible one is never the reason a user thinks
92
- // "there is more over there" — judge the edge on real content.
93
- if (other.guide) continue;
94
- const otherFrame = other.frame;
95
- if (!otherFrame) continue;
96
- const otherX = otherFrame.x + otherFrame.width / 2;
97
- const otherY = otherFrame.y + otherFrame.height / 2;
98
- switch (direction) {
99
- case "up":
100
- if (otherY < frame.y) return false;
101
- break;
102
- case "down":
103
- if (otherY > frame.y + frame.height) return false;
104
- break;
105
- case "left":
106
- if (otherX < frame.x) return false;
107
- break;
108
- case "right":
109
- if (otherX > frame.x + frame.width) return false;
110
- break;
111
- }
112
- }
113
- return true;
114
- }
115
- function unionBox(frames) {
116
- if (frames.length === 0) return null;
117
- let left = Infinity;
118
- let top = Infinity;
119
- let right = -Infinity;
120
- let bottom = -Infinity;
121
- for (const frame of frames) {
122
- left = Math.min(left, frame.x);
123
- top = Math.min(top, frame.y);
124
- right = Math.max(right, frame.x + frame.width);
125
- bottom = Math.max(bottom, frame.y + frame.height);
126
- }
127
- return {
128
- x: left,
129
- y: top,
130
- width: right - left,
131
- height: bottom - top
132
- };
133
- }
134
- function isBeyond(box, point, direction) {
135
- switch (direction) {
136
- case "up":
137
- return point.y < box.y;
138
- case "down":
139
- return point.y > box.y + box.height;
140
- case "left":
141
- return point.x < box.x;
142
- case "right":
143
- return point.x > box.x + box.width;
144
- default:
145
- return false;
146
- }
147
- }
148
-
149
- /** Blocked in at least this many directions before "trap" is the right word. */
150
- const TRAP_MIN_BLOCKED = 2;
151
-
152
- /**
153
- * A trap is a REGION focus cannot get OUT of, which is usually not one element
154
- * and is not "all four directions are dead".
155
- *
156
- * Both of the obvious rules were tried against the armed trap in the TV example
157
- * (a row of three tiles in one `TVFocusGuideView`) and both missed it. Per
158
- * element: left/right move freely between the tiles, so no single tile
159
- * dead-ends four ways. All-four-directions on the group: the box is
160
- * left-aligned with everything else on the screen, so LEFT is a legitimate
161
- * screen edge and the union never reaches four.
162
- *
163
- * What actually defines the bug is escape: for every direction in which
164
- * something focusable EXISTS outside the container, focus has to have been
165
- * observed failing to get there. A direction with nothing beyond it is not
166
- * evidence either way, and a container with nothing outside it at all is the
167
- * whole screen, not a trap.
168
- */
169
- function detectTraps(deadEnds, nodes) {
170
- const byTag = new Map();
171
- for (const node of nodes) byTag.set(node.nativeTag, node);
172
- const groups = new Map();
173
- for (const flag of deadEnds) {
174
- const node = byTag.get(flag.tag);
175
- const containerTag = node?.guideAncestorTag ?? flag.tag;
176
- const group = groups.get(containerTag) ?? {
177
- containerTag,
178
- tags: new Set(),
179
- directions: new Set()
180
- };
181
- group.tags.add(flag.tag);
182
- group.directions.add(flag.direction);
183
- groups.set(containerTag, group);
184
- }
185
- const traps = [];
186
- groups.forEach(group => {
187
- if (group.directions.size < TRAP_MIN_BLOCKED) return;
188
-
189
- // Everything the container holds, not just the elements we have evidence
190
- // about — the box focus is stuck inside is the container's, not the probes'.
191
- const members = new Set(group.tags);
192
- for (const node of nodes) {
193
- if (node.guideAncestorTag === group.containerTag) members.add(node.nativeTag);
194
- }
195
- members.add(group.containerTag);
196
- const container = byTag.get(group.containerTag);
197
- const box = container?.guide && container.frame ? container.frame : unionBox(Array.from(members).map(tag => byTag.get(tag)?.frame).filter(frame => !!frame));
198
- if (!box) return;
199
- let escapable = 0;
200
- let reachableOutside = 0;
201
- for (const direction of DIRECTIONS) {
202
- const hasOutside = nodes.some(node => {
203
- if (node.guide || members.has(node.nativeTag) || !node.frame) return false;
204
- const center = {
205
- x: node.frame.x + node.frame.width / 2,
206
- y: node.frame.y + node.frame.height / 2
207
- };
208
- return isBeyond(box, center, direction);
209
- });
210
- if (!hasOutside) continue;
211
- reachableOutside += 1;
212
- if (!group.directions.has(direction)) escapable += 1;
213
- }
214
- // Nothing outside at all means the container IS the screen.
215
- if (reachableOutside === 0 || escapable > 0) return;
216
- const guide = container?.guide;
217
- const trapped = guide !== undefined && (guide.trap.up || guide.trap.down || guide.trap.left || guide.trap.right);
218
- traps.push({
219
- tags: Array.from(group.tags).sort((a, b) => a - b),
220
- containerTag: group.containerTag,
221
- directions: DIRECTIONS.filter(d => group.directions.has(d)),
222
- guideTrapTag: trapped ? group.containerTag : undefined
223
- });
224
- });
225
- return traps;
226
- }
227
-
228
- /**
229
- * Focus that parked somewhere with no visible highlight — the "focus vanished"
230
- * symptom. Two shapes: it landed on a TVFocusGuideView (a real focus stop the
231
- * user cannot see), or on a tag the inventory has never heard of (a view
232
- * outside the walked tree, or one that unmounted since the last scan).
233
- */
234
- function detectInvisibleStops(transitions, nodes, since = 0) {
235
- const byTag = new Map();
236
- for (const node of nodes) byTag.set(node.nativeTag, node);
237
- const flags = new Map();
238
- for (const transition of transitions) {
239
- const tag = transition.toTag;
240
- if (tag === null) continue;
241
- // A move made before the current scan cannot be judged against the current
242
- // inventory: after any navigation EVERY tag from the old screen looks like
243
- // a mystery stop, which buried the real flags under a wall of noise on the
244
- // first device run. Focus flags are per-screen, and `since` is that screen.
245
- if (transition.at < since) continue;
246
- const node = byTag.get(tag);
247
- const reason = !node ?
248
- // Nothing in the inventory: only meaningful once a scan has run at all,
249
- // otherwise every transition would be "invisible".
250
- nodes.length > 0 ? "unknown" : null : node.guide ? "guide" : null;
251
- if (!reason) continue;
252
- const existing = flags.get(tag);
253
- if (existing) {
254
- existing.count += 1;
255
- existing.at = transition.at;
256
- } else {
257
- flags.set(tag, {
258
- tag,
259
- reason,
260
- at: transition.at,
261
- count: 1
262
- });
263
- }
264
- }
265
- return Array.from(flags.values()).sort((a, b) => b.at - a.at);
266
- }
267
-
268
- /**
269
- * `since` is the timestamp of the inventory these flags are judged against —
270
- * normally the last scan. Everything older belonged to a screen that no longer
271
- * exists, and judging it here produces confident nonsense.
272
- */
273
- function detectFlags(transitions, probes, nodes, lostCount, since = 0) {
274
- const deadEnds = detectDeadEnds(probes, nodes, DEAD_END_ATTEMPTS, since);
275
- return {
276
- deadEnds,
277
- traps: detectTraps(deadEnds, nodes),
278
- invisibleStops: detectInvisibleStops(transitions, nodes, since),
279
- lostCount
280
- };
281
- }
282
-
283
- /**
284
- * Coverage. Observed absence is EVIDENCE, not proof: focus is nondeterministic
285
- * enough on tvOS that "never visited" cannot be promoted to "unreachable"
286
- * without native support that does not exist. So the strong word is reserved
287
- * for the case where geometry AND the observed graph agree.
288
- */
289
- function computeCoverage(nodes, visited, transitions, probes) {
290
- const visitedSet = new Set(visited);
291
- // Guides are counted as focusables (they really are stops), but an unvisited
292
- // guide is not a bug — it is the normal case for one that only redirects.
293
- const reachableCandidates = nodes.filter(node => !node.guide);
294
- const unvisited = reachableCandidates.filter(node => !visitedSet.has(node.nativeTag)).map(node => node.nativeTag);
295
- const meaningful = probes.length >= COVERAGE_MIN_PROBES;
296
- const edgeTargets = new Set();
297
- for (const transition of transitions) {
298
- if (transition.toTag !== null) edgeTargets.add(transition.toTag);
299
- }
300
- const byTag = new Map();
301
- for (const node of nodes) byTag.set(node.nativeTag, node);
302
- const suspectedUnreachable = meaningful ? unvisited.filter(tag => {
303
- if (edgeTargets.has(tag)) return false;
304
- const node = byTag.get(tag);
305
- if (!node?.frame) return false;
306
- // A visited neighbour sitting right next to it means the traversal DID
307
- // pass by — so focus skipping it is worth suspecting.
308
- return reachableCandidates.some(other => other.nativeTag !== tag && visitedSet.has(other.nativeTag) && isAdjacent(node, other));
309
- }) : [];
310
- return {
311
- focusables: reachableCandidates.length,
312
- visited: reachableCandidates.filter(node => visitedSet.has(node.nativeTag)).length,
313
- unvisited,
314
- suspectedUnreachable,
315
- meaningful
316
- };
317
- }
318
-
319
- /** Normalized-frame gap within which two elements are plausibly D-pad neighbours. */
320
- const ADJACENCY_GAP = 0.12;
321
- function isAdjacent(a, b) {
322
- const fa = a.frame;
323
- const fb = b.frame;
324
- if (!fa || !fb) return false;
325
- const horizontalOverlap = fa.x < fb.x + fb.width && fb.x < fa.x + fa.width;
326
- const verticalOverlap = fa.y < fb.y + fb.height && fb.y < fa.y + fa.height;
327
- if (horizontalOverlap) {
328
- const gap = Math.max(fa.y - (fb.y + fb.height), fb.y - (fa.y + fa.height));
329
- if (gap <= ADJACENCY_GAP) return true;
330
- }
331
- if (verticalOverlap) {
332
- const gap = Math.max(fa.x - (fb.x + fb.width), fb.x - (fa.x + fa.width));
333
- if (gap <= ADJACENCY_GAP) return true;
334
- }
335
- return false;
336
- }
337
-
338
- /**
339
- * The observed out-edges of one element — what the desktop's D-pad compass
340
- * renders. A direction with no entry is UNKNOWN (dim), which is a different
341
- * statement from a dead end (pressed, went nowhere).
342
- */
343
- function observedEdges(tag, transitions) {
344
- const edges = {};
345
- for (const transition of transitions) {
346
- if (transition.fromTag !== tag || transition.direction === null) continue;
347
- edges[transition.direction] = transition.toTag;
348
- }
349
- return edges;
350
- }
351
- //# sourceMappingURL=detectors.js.map
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DIRECTIONS=exports.DEAD_END_ATTEMPTS=exports.COVERAGE_MIN_PROBES=void 0,exports.computeCoverage=computeCoverage,exports.detectDeadEnds=detectDeadEnds,exports.detectFlags=detectFlags,exports.detectInvisibleStops=detectInvisibleStops,exports.detectTraps=detectTraps,exports.observedEdges=observedEdges;const DIRECTIONS=exports.DIRECTIONS=["up","down","left","right"],DEAD_END_ATTEMPTS=exports.DEAD_END_ATTEMPTS=3,COVERAGE_MIN_PROBES=exports.COVERAGE_MIN_PROBES=12;function keyOf(t,e){return`${t}:${e}`}function detectDeadEnds(t,e,n=DEAD_END_ATTEMPTS,o=0){const r=new Map;for(const e of t){if(!e.resolved||null===e.tag)continue;if(e.at<o)continue;const t=keyOf(e.tag,e.direction);null===e.movedTo?r.set(t,(r.get(t)??0)+1):r.set(t,0)}const a=new Map;for(const t of e)a.set(t.nativeTag,t);const i=[];return r.forEach((t,o)=>{if(t<n)return;const[r,s]=o.split(":"),c=Number(r),d=s;i.push({tag:c,direction:d,attempts:t,atEdge:isAtEdge(a.get(c),d,e)})}),i.sort((t,e)=>e.attempts-t.attempts)}function isAtEdge(t,e,n){const o=t?.frame;if(!o)return!1;o.x,o.width,o.y,o.height;for(const r of n){if(r.nativeTag===t.nativeTag)continue;if(r.guide)continue;const n=r.frame;if(!n)continue;const a=n.x+n.width/2,i=n.y+n.height/2;switch(e){case"up":if(i<o.y)return!1;break;case"down":if(i>o.y+o.height)return!1;break;case"left":if(a<o.x)return!1;break;case"right":if(a>o.x+o.width)return!1}}return!0}function unionBox(t){if(0===t.length)return null;let e=1/0,n=1/0,o=-1/0,r=-1/0;for(const a of t)e=Math.min(e,a.x),n=Math.min(n,a.y),o=Math.max(o,a.x+a.width),r=Math.max(r,a.y+a.height);return{x:e,y:n,width:o-e,height:r-n}}function isBeyond(t,e,n){switch(n){case"up":return e.y<t.y;case"down":return e.y>t.y+t.height;case"left":return e.x<t.x;case"right":return e.x>t.x+t.width;default:return!1}}const TRAP_MIN_BLOCKED=2;function detectTraps(t,e){const n=new Map;for(const t of e)n.set(t.nativeTag,t);const o=new Map;for(const e of t){const t=n.get(e.tag),r=t?.guideAncestorTag??e.tag,a=o.get(r)??{containerTag:r,tags:new Set,directions:new Set};a.tags.add(e.tag),a.directions.add(e.direction),o.set(r,a)}const r=[];return o.forEach(t=>{if(t.directions.size<TRAP_MIN_BLOCKED)return;const o=new Set(t.tags);for(const n of e)n.guideAncestorTag===t.containerTag&&o.add(n.nativeTag);o.add(t.containerTag);const a=n.get(t.containerTag),i=a?.guide&&a.frame?a.frame:unionBox(Array.from(o).map(t=>n.get(t)?.frame).filter(t=>!!t));if(!i)return;let s=0,c=0;for(const n of DIRECTIONS)e.some(t=>{if(t.guide||o.has(t.nativeTag)||!t.frame)return!1;const e={x:t.frame.x+t.frame.width/2,y:t.frame.y+t.frame.height/2};return isBeyond(i,e,n)})&&(c+=1,t.directions.has(n)||(s+=1));if(0===c||s>0)return;const d=a?.guide,f=void 0!==d&&(d.trap.up||d.trap.down||d.trap.left||d.trap.right);r.push({tags:Array.from(t.tags).sort((t,e)=>t-e),containerTag:t.containerTag,directions:DIRECTIONS.filter(e=>t.directions.has(e)),guideTrapTag:f?t.containerTag:void 0})}),r}function detectInvisibleStops(t,e,n=0){const o=new Map;for(const t of e)o.set(t.nativeTag,t);const r=new Map;for(const a of t){const t=a.toTag;if(null===t)continue;if(a.at<n)continue;const i=o.get(t),s=i?i.guide?"guide":null:e.length>0?"unknown":null;if(!s)continue;const c=r.get(t);c?(c.count+=1,c.at=a.at):r.set(t,{tag:t,reason:s,at:a.at,count:1})}return Array.from(r.values()).sort((t,e)=>e.at-t.at)}function detectFlags(t,e,n,o,r=0){const a=detectDeadEnds(e,n,DEAD_END_ATTEMPTS,r);return{deadEnds:a,traps:detectTraps(a,n),invisibleStops:detectInvisibleStops(t,n,r),lostCount:o}}function computeCoverage(t,e,n,o){const r=new Set(e),a=t.filter(t=>!t.guide),i=a.filter(t=>!r.has(t.nativeTag)).map(t=>t.nativeTag),s=o.length>=COVERAGE_MIN_PROBES,c=new Set;for(const t of n)null!==t.toTag&&c.add(t.toTag);const d=new Map;for(const e of t)d.set(e.nativeTag,e);const f=s?i.filter(t=>{if(c.has(t))return!1;const e=d.get(t);return!!e?.frame&&a.some(n=>n.nativeTag!==t&&r.has(n.nativeTag)&&isAdjacent(e,n))}):[];return{focusables:a.length,visited:a.filter(t=>r.has(t.nativeTag)).length,unvisited:i,suspectedUnreachable:f,meaningful:s}}const ADJACENCY_GAP=.12;function isAdjacent(t,e){const n=t.frame,o=e.frame;if(!n||!o)return!1;const r=n.x<o.x+o.width&&o.x<n.x+n.width,a=n.y<o.y+o.height&&o.y<n.y+n.height;return!!(r&&Math.max(n.y-(o.y+o.height),o.y-(n.y+n.height))<=ADJACENCY_GAP)||!!(a&&Math.max(n.x-(o.x+o.width),o.x-(n.x+n.width))<=ADJACENCY_GAP)}function observedEdges(t,e){const n={};for(const o of e)o.fromTag===t&&null!==o.direction&&(n[o.direction]=o.toTag);return n}
@@ -1,106 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "clearHistory", {
7
- enumerable: true,
8
- get: function () {
9
- return _focusStore.clearHistory;
10
- }
11
- });
12
- Object.defineProperty(exports, "computeCoverage", {
13
- enumerable: true,
14
- get: function () {
15
- return _detectors.computeCoverage;
16
- }
17
- });
18
- Object.defineProperty(exports, "detectDeadEnds", {
19
- enumerable: true,
20
- get: function () {
21
- return _detectors.detectDeadEnds;
22
- }
23
- });
24
- Object.defineProperty(exports, "detectFlags", {
25
- enumerable: true,
26
- get: function () {
27
- return _detectors.detectFlags;
28
- }
29
- });
30
- Object.defineProperty(exports, "detectInvisibleStops", {
31
- enumerable: true,
32
- get: function () {
33
- return _detectors.detectInvisibleStops;
34
- }
35
- });
36
- Object.defineProperty(exports, "detectTraps", {
37
- enumerable: true,
38
- get: function () {
39
- return _detectors.detectTraps;
40
- }
41
- });
42
- Object.defineProperty(exports, "focusInspectorSyncAdapter", {
43
- enumerable: true,
44
- get: function () {
45
- return _focusInspectorSyncAdapter.focusInspectorSyncAdapter;
46
- }
47
- });
48
- Object.defineProperty(exports, "getState", {
49
- enumerable: true,
50
- get: function () {
51
- return _focusStore.getState;
52
- }
53
- });
54
- Object.defineProperty(exports, "observedEdges", {
55
- enumerable: true,
56
- get: function () {
57
- return _detectors.observedEdges;
58
- }
59
- });
60
- Object.defineProperty(exports, "recordBlur", {
61
- enumerable: true,
62
- get: function () {
63
- return _focusStore.recordBlur;
64
- }
65
- });
66
- Object.defineProperty(exports, "recordFocus", {
67
- enumerable: true,
68
- get: function () {
69
- return _focusStore.recordFocus;
70
- }
71
- });
72
- Object.defineProperty(exports, "recordKey", {
73
- enumerable: true,
74
- get: function () {
75
- return _focusStore.recordKey;
76
- }
77
- });
78
- Object.defineProperty(exports, "resetStore", {
79
- enumerable: true,
80
- get: function () {
81
- return _focusStore.resetStore;
82
- }
83
- });
84
- Object.defineProperty(exports, "scanFocusables", {
85
- enumerable: true,
86
- get: function () {
87
- return _focusableScanner.scanFocusables;
88
- }
89
- });
90
- Object.defineProperty(exports, "setTracking", {
91
- enumerable: true,
92
- get: function () {
93
- return _focusStore.setTracking;
94
- }
95
- });
96
- Object.defineProperty(exports, "subscribe", {
97
- enumerable: true,
98
- get: function () {
99
- return _focusStore.subscribe;
100
- }
101
- });
102
- var _focusInspectorSyncAdapter = require("./sync/focusInspectorSyncAdapter");
103
- var _focusStore = require("./store/focusStore");
104
- var _focusableScanner = require("./scan/focusableScanner");
105
- var _detectors = require("./detect/detectors");
106
- //# sourceMappingURL=index.js.map
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"clearHistory",{enumerable:!0,get:function(){return _focusStore.clearHistory}}),Object.defineProperty(exports,"computeCoverage",{enumerable:!0,get:function(){return _detectors.computeCoverage}}),Object.defineProperty(exports,"detectDeadEnds",{enumerable:!0,get:function(){return _detectors.detectDeadEnds}}),Object.defineProperty(exports,"detectFlags",{enumerable:!0,get:function(){return _detectors.detectFlags}}),Object.defineProperty(exports,"detectInvisibleStops",{enumerable:!0,get:function(){return _detectors.detectInvisibleStops}}),Object.defineProperty(exports,"detectTraps",{enumerable:!0,get:function(){return _detectors.detectTraps}}),Object.defineProperty(exports,"focusInspectorSyncAdapter",{enumerable:!0,get:function(){return _focusInspectorSyncAdapter.focusInspectorSyncAdapter}}),Object.defineProperty(exports,"getState",{enumerable:!0,get:function(){return _focusStore.getState}}),Object.defineProperty(exports,"observedEdges",{enumerable:!0,get:function(){return _detectors.observedEdges}}),Object.defineProperty(exports,"recordBlur",{enumerable:!0,get:function(){return _focusStore.recordBlur}}),Object.defineProperty(exports,"recordFocus",{enumerable:!0,get:function(){return _focusStore.recordFocus}}),Object.defineProperty(exports,"recordKey",{enumerable:!0,get:function(){return _focusStore.recordKey}}),Object.defineProperty(exports,"resetStore",{enumerable:!0,get:function(){return _focusStore.resetStore}}),Object.defineProperty(exports,"scanFocusables",{enumerable:!0,get:function(){return _focusableScanner.scanFocusables}}),Object.defineProperty(exports,"setTracking",{enumerable:!0,get:function(){return _focusStore.setTracking}}),Object.defineProperty(exports,"subscribe",{enumerable:!0,get:function(){return _focusStore.subscribe}});var _focusInspectorSyncAdapter=require("./sync/focusInspectorSyncAdapter"),_focusStore=require("./store/focusStore"),_focusableScanner=require("./scan/focusableScanner"),_detectors=require("./detect/detectors");