-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathscroll_probe_cache.py
More file actions
285 lines (245 loc) · 9.45 KB
/
Copy pathscroll_probe_cache.py
File metadata and controls
285 lines (245 loc) · 9.45 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""Helpers for probe-scroll caching logic.
This module is Talon-free so behavior can be unit tested directly.
"""
import math
from dataclasses import dataclass, field, replace
import numpy as np
try:
from .scroll_detection import BoundingBox
except ImportError:
from scroll_detection import BoundingBox
DEFAULT_OUTSIDE_MATCH_THRESHOLD = 0.90
OUTSIDE_MATCH_PIXEL_TOLERANCE = 1
MIN_CACHED_VIEWPORT_HEIGHT = 300
SCROLL_RATIO_ALIGNMENT_REL_TOL = 0.05
VIEWPORT_VERTICAL_BOUNDS_TOLERANCE = 12
NOTIFICATION_CENTER_APP_NAME = "Notification Center"
@dataclass(frozen=True)
class ScrollCacheEntry:
"""Cache entry keyed by app name for enhanced scrolling."""
viewport: BoundingBox | None
reference_before_array: np.ndarray | None
scroll_ratio: float | None = None
scroll_ratio_confirmed: bool = False
def __post_init__(self):
assert self.scroll_ratio is not None or not self.scroll_ratio_confirmed
@dataclass(frozen=True)
class ProbeSkipDecision:
"""Decision for whether the cached viewport can replace the probe scroll."""
use_cached_probe: bool
cache_key: str | None
cache_entry: ScrollCacheEntry | None = None
cache_debug_reason: str | None = None
outside_viewport_changed: bool = False
def outside_viewport_match_ratio(
current: np.ndarray, cached: np.ndarray, viewport: BoundingBox
) -> float:
"""Return tolerant per-pixel match ratio outside the viewport rectangle."""
# Reuse only applies when both captured arrays have identical dimensions.
if current.shape != cached.shape:
return 0.0
if current.size == 0:
return 0.0
height, width = current.shape[:2]
x1 = max(0, viewport.x)
y1 = max(0, viewport.y)
x2 = min(width, viewport.x + viewport.width)
y2 = min(height, viewport.y + viewport.height)
mask = np.ones((height, width), dtype=bool)
if x1 < x2 and y1 < y2:
mask[y1:y2, x1:x2] = False
if not np.any(mask):
# Viewport covers the entire capture, so there is no outside region to
# provide positive evidence the screen is unchanged. Treat as no match.
return 0.0
if current.ndim == 3:
diff = np.abs(current.astype(np.int16) - cached.astype(np.int16))
same = np.all(diff <= OUTSIDE_MATCH_PIXEL_TOLERANCE, axis=-1)
else:
diff = np.abs(current.astype(np.int16) - cached.astype(np.int16))
same = diff <= OUTSIDE_MATCH_PIXEL_TOLERANCE
return float(np.mean(same[mask]))
def refresh_viewport_cache(
entry: ScrollCacheEntry,
viewport: BoundingBox,
reference_before_array: np.ndarray,
) -> ScrollCacheEntry:
"""Return an entry with refreshed viewport/reference cache state."""
return replace(
entry,
viewport=viewport,
reference_before_array=reference_before_array.copy(),
)
def update_ratio_state(
entry: ScrollCacheEntry,
measured_ratio: float,
*,
allow_confirmed_reset_on_mismatch: bool,
) -> ScrollCacheEntry:
"""Record a measured ratio, optionally demoting confirmed ratios on mismatch."""
if entry.scroll_ratio is None:
return replace(entry, scroll_ratio=measured_ratio, scroll_ratio_confirmed=False)
if ratios_align(entry.scroll_ratio, measured_ratio):
if entry.scroll_ratio_confirmed:
return entry
return replace(entry, scroll_ratio_confirmed=True)
if entry.scroll_ratio_confirmed and not allow_confirmed_reset_on_mismatch:
return entry
return replace(
entry,
scroll_ratio=measured_ratio,
scroll_ratio_confirmed=False,
)
def ratios_align(expected_ratio: float, actual_ratio: float) -> bool:
"""Return True when two scroll ratios are close enough to be treated as aligned."""
return math.isclose(
expected_ratio,
actual_ratio,
rel_tol=SCROLL_RATIO_ALIGNMENT_REL_TOL,
abs_tol=0.01,
)
@dataclass
class AppScrollCache:
"""Encapsulates probe-scroll cache entries and reuse policy."""
entries: dict[str, ScrollCacheEntry] = field(default_factory=dict)
def cache_key_for_app(self, app_name: str | None) -> str | None:
"""Return cache key for an app name, or None when caching is bypassed."""
# See https://github.com/talonvoice/talon/issues/700
if not app_name or app_name == NOTIFICATION_CENTER_APP_NAME:
return None
return app_name
def evaluate_reuse(
self,
probe_skip_enabled: bool,
app_name: str | None,
current: np.ndarray,
cursor_local: tuple[float, float],
threshold: float = DEFAULT_OUTSIDE_MATCH_THRESHOLD,
) -> ProbeSkipDecision:
"""Decide whether the cached viewport can replace the probe scroll."""
if not probe_skip_enabled:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=None,
cache_debug_reason="probe skip disabled",
)
if app_name == NOTIFICATION_CENTER_APP_NAME:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=None,
cache_debug_reason="cache bypassed for Notification Center",
)
cache_key = self.cache_key_for_app(app_name)
if cache_key is None:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=None,
cache_debug_reason="app unavailable for cache",
)
cache_entry = self.entries.get(cache_key)
if cache_entry is None:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=cache_key,
cache_debug_reason="no cached viewport",
)
if not cache_entry.scroll_ratio_confirmed:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=cache_key,
cache_entry=cache_entry,
cache_debug_reason="scroll ratio not confirmed yet",
)
if cache_entry.viewport is None or cache_entry.reference_before_array is None:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=cache_key,
cache_entry=cache_entry,
cache_debug_reason="no cached viewport",
)
if cache_entry.viewport.height < MIN_CACHED_VIEWPORT_HEIGHT:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=cache_key,
cache_entry=cache_entry,
cache_debug_reason="cached viewport too small",
)
if not cache_entry.viewport.contains_point(cursor_local):
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=cache_key,
cache_entry=cache_entry,
cache_debug_reason="cursor outside cached viewport",
)
match_ratio = outside_viewport_match_ratio(
current,
cache_entry.reference_before_array,
cache_entry.viewport,
)
if match_ratio < threshold:
return ProbeSkipDecision(
use_cached_probe=False,
cache_key=cache_key,
cache_entry=cache_entry,
cache_debug_reason=f"outside match {match_ratio:.2f} < {threshold:.2f}",
outside_viewport_changed=True,
)
return ProbeSkipDecision(
use_cached_probe=True,
cache_key=cache_key,
cache_entry=cache_entry,
cache_debug_reason=f"outside match {match_ratio:.2f}",
)
def record_scroll_measurement(
self,
cache_key: str | None,
viewport: BoundingBox,
reference_before_array: np.ndarray,
measured_ratio: float,
*,
is_probe: bool,
used_cached_probe: bool = False,
) -> None:
"""Record a probe or calibrated measurement and refresh viewport cache."""
if not cache_key:
return
entry = self.entries.get(cache_key) or ScrollCacheEntry(
viewport=None,
reference_before_array=None,
)
viewport_changed = (
entry.viewport is not None
and not viewport.has_similar_vertical_bounds(
entry.viewport,
tolerance=VIEWPORT_VERTICAL_BOUNDS_TOLERANCE,
)
)
# Probe measurements are trusted enough to demote a confirmed ratio on
# mismatch. Calibrated measurements normally are not, because they are
# more likely to be clipped at the top or bottom of a document. The
# one exception is a cache hit whose viewport changed, since that is a
# stronger signal that the cached confirmed ratio went stale.
entry = update_ratio_state(
entry,
measured_ratio,
allow_confirmed_reset_on_mismatch=(
is_probe or (used_cached_probe and viewport_changed)
),
)
self.entries[cache_key] = refresh_viewport_cache(
entry,
viewport,
reference_before_array,
)
def invalidate_viewport(self, cache_key: str | None) -> None:
"""Clear cached viewport state while keeping any cached scroll ratio."""
if not cache_key:
return
entry = self.entries.get(cache_key)
if entry is None:
return
self.entries[cache_key] = replace(
entry,
viewport=None,
reference_before_array=None,
)