-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathinteraction-targeting.ts
More file actions
165 lines (148 loc) · 5.43 KB
/
interaction-targeting.ts
File metadata and controls
165 lines (148 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type { Rect, SnapshotNode } from '../utils/snapshot.ts';
import { centerOfRect } from '../utils/snapshot.ts';
import { containsPoint, pickLargestRect } from '../utils/rect-visibility.ts';
import { findNearestHittableAncestor, normalizeType } from '../utils/snapshot-processing.ts';
import { normalizeRect, resolveRectCenter } from '../utils/rect-center.ts';
const SEMANTIC_TOUCH_ROLE_FRAGMENTS = [
'button',
'link',
'menuitem',
'tabitem',
'textfield',
'searchfield',
'securetextfield',
'checkbox',
'radio',
'switch',
'cell',
];
type ActionableTouchResolutionReason =
| 'same-rect-descendant'
| 'semantic-target'
| 'hittable-ancestor'
| 'overly-broad-ancestor'
| 'original';
type ActionableTouchResolution = {
node: SnapshotNode;
reason: ActionableTouchResolutionReason;
};
export function resolveActionableTouchNode(
nodes: SnapshotNode[],
node: SnapshotNode,
): SnapshotNode {
return resolveActionableTouchResolution(nodes, node).node;
}
/** @internal Exposed for focused policy tests; runtime callers should use resolveActionableTouchNode. */
export function resolveActionableTouchResolution(
nodes: SnapshotNode[],
node: SnapshotNode,
): ActionableTouchResolution {
const descendant = findPreferredActionableDescendant(nodes, node);
if (descendant?.rect && resolveRectCenter(descendant.rect)) {
return { node: descendant, reason: 'same-rect-descendant' };
}
if (isSemanticTouchTarget(node) && node.rect && resolveRectCenter(node.rect)) {
return { node, reason: 'semantic-target' };
}
const ancestor = findNearestHittableAncestor(nodes, node);
if (ancestor?.rect && resolveRectCenter(ancestor.rect)) {
if (isOverlyBroadAncestor(node, ancestor, nodes)) {
return { node, reason: 'overly-broad-ancestor' };
}
return { node: ancestor, reason: 'hittable-ancestor' };
}
return { node, reason: 'original' };
}
function findPreferredActionableDescendant(
nodes: SnapshotNode[],
node: SnapshotNode,
): SnapshotNode | null {
const targetRect = normalizeRect(node.rect);
if (!targetRect) return null;
let current = node;
const visited = new Set<string>();
while (!visited.has(current.ref)) {
visited.add(current.ref);
const sameRectChildren = nodes.filter((candidate) => {
if (candidate.parentIndex !== current.index || !candidate.hittable) {
return false;
}
const candidateRect = normalizeRect(candidate.rect);
return candidateRect ? areRectsApproximatelyEqual(candidateRect, targetRect) : false;
});
if (sameRectChildren.length !== 1) {
break;
}
const child = sameRectChildren[0];
if (child === undefined) break;
current = child;
}
return current === node ? null : current;
}
function isSemanticTouchTarget(node: SnapshotNode): boolean {
const roles = [node.type, node.role, node.subrole].map((value) => normalizeType(value ?? ''));
return roles.some(isSemanticTouchRole);
}
function isSemanticTouchRole(role: string): boolean {
// Match Tab exactly so broad roles like Table/TabBar do not become touch targets.
return (
role === 'tab' || SEMANTIC_TOUCH_ROLE_FRAGMENTS.some((fragment) => role.includes(fragment))
);
}
function areRectsApproximatelyEqual(left: Rect, right: Rect): boolean {
const tolerance = 0.5;
return (
Math.abs(left.x - right.x) <= tolerance &&
Math.abs(left.y - right.y) <= tolerance &&
Math.abs(left.width - right.width) <= tolerance &&
Math.abs(left.height - right.height) <= tolerance
);
}
function isOverlyBroadAncestor(
node: SnapshotNode,
ancestor: SnapshotNode,
nodes: SnapshotNode[],
): boolean {
const nodeRect = normalizeRect(node.rect);
const ancestorRect = normalizeRect(ancestor.rect);
if (!nodeRect || !ancestorRect) return false;
const rootViewportRect = resolveRootViewportRect(nodes, nodeRect);
if (!rootViewportRect) return false;
if (!isRectViewportSized(ancestorRect, rootViewportRect)) return false;
return !areRectsApproximatelyEqual(nodeRect, ancestorRect);
}
function resolveRootViewportRect(nodes: SnapshotNode[], targetRect: Rect): Rect | null {
const targetCenter = centerOfRect(targetRect);
const viewportRects = nodes
.filter((node) => {
const type = (node.type ?? '').toLowerCase();
return type.includes('application') || type.includes('window');
})
.map((node) => normalizeRect(node.rect))
.filter((rect): rect is Rect => rect !== null);
if (viewportRects.length === 0) return null;
const containingRects = viewportRects.filter((rect) =>
containsPoint(rect, targetCenter.x, targetCenter.y),
);
return pickLargestRect(containingRects.length > 0 ? containingRects : viewportRects);
}
function isRectViewportSized(rect: Rect, viewportRect: Rect): boolean {
const overlapArea = intersectionArea(rect, viewportRect);
const rectArea = rect.width * rect.height;
const viewportArea = viewportRect.width * viewportRect.height;
if (overlapArea <= 0 || rectArea <= 0 || viewportArea <= 0) return false;
const viewportCoverage = overlapArea / viewportArea;
const rectCoverage = overlapArea / rectArea;
return viewportCoverage >= 0.9 && rectCoverage >= 0.8;
}
function intersectionArea(left: Rect, right: Rect): number {
const xOverlap = Math.max(
0,
Math.min(left.x + left.width, right.x + right.width) - Math.max(left.x, right.x),
);
const yOverlap = Math.max(
0,
Math.min(left.y + left.height, right.y + right.height) - Math.max(left.y, right.y),
);
return xOverlap * yOverlap;
}