dsh-native-uia 0.2.0 → 0.6.0

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 (55) hide show
  1. package/README.md +81 -398
  2. package/client/client.js +236 -48
  3. package/cordis.patch.yml +7 -8
  4. package/locale/en.json +2 -2
  5. package/locale/zh.json +2 -2
  6. package/package.json +8 -17
  7. package/scripts/check-lease.mjs +1 -1
  8. package/scripts/lib/dsh-tools-stub.mjs +14 -0
  9. package/scripts/lib/load-tools-source.mjs +17 -16
  10. package/scripts/lib/sources.mjs +62 -0
  11. package/scripts/load-tools.mjs +74 -0
  12. package/scripts/measure-parse.mjs +2 -2
  13. package/scripts/regression-tools.mjs +155 -6
  14. package/scripts/regression-worker.ps1 +29 -0
  15. package/scripts/verify-live-style.mjs +46 -0
  16. package/scripts/verify-live.mjs +12 -18
  17. package/scripts/verify.mjs +953 -192
  18. package/src/activity-window.js +71 -7
  19. package/src/dashboard.js +27 -69
  20. package/src/guard.js +39 -10
  21. package/src/index.js +187 -77
  22. package/src/notes.js +83 -0
  23. package/src/state.js +97 -10
  24. package/src/themes.js +66 -0
  25. package/src/tools/defs/act.js +671 -0
  26. package/src/tools/defs/lifecycle.js +205 -0
  27. package/src/tools/defs/observe.js +325 -0
  28. package/src/tools/defs/pointer.js +328 -0
  29. package/src/tools/format.js +338 -0
  30. package/src/tools/index.js +125 -0
  31. package/src/tools/ocr.js +91 -0
  32. package/src/tools/recipes.js +49 -0
  33. package/src/tools/runtime.js +42 -0
  34. package/src/worker-client.js +32 -10
  35. package/worker/assets/dsh-whale.path +1 -0
  36. package/worker/indicator-server.ps1 +1107 -72
  37. package/worker/lib/01-core.ps1 +64 -0
  38. package/worker/lib/02-uia.ps1 +224 -0
  39. package/worker/lib/03-window.ps1 +194 -0
  40. package/worker/lib/04-match.ps1 +435 -0
  41. package/worker/lib/05-value.ps1 +254 -0
  42. package/worker/lib/06-snapshot.ps1 +412 -0
  43. package/worker/lib/07-action.ps1 +329 -0
  44. package/worker/lib/08-command.ps1 +1495 -0
  45. package/worker/lib/09-pointer.ps1 +840 -0
  46. package/worker/lib/10-dispatch.ps1 +132 -0
  47. package/worker/ps-stdio.ps1 +7 -2
  48. package/worker/uia-worker.ps1 +599 -4220
  49. package/locale/desktop/en.json +0 -6
  50. package/locale/desktop/zh.json +0 -6
  51. package/scripts/regression-halt-latch.mjs +0 -100
  52. package/src/native-uia-remote.js +0 -80
  53. package/src/panel-face.js +0 -14
  54. package/src/tools.js +0 -1482
  55. package/web/control.html +0 -1413
@@ -0,0 +1,329 @@
1
+ # 07-action.ps1 -- element actions and wait conditions
2
+ #
3
+ # Part of worker/uia-worker.ps1, dot-sourced by it in the order scripts/lib/sources.mjs declares.
4
+ # It lives in its own file only so a section can be read without scrolling the whole RPC server.
5
+ # Dot-sourcing shares the entry's script scope, so a helper here may use $script:W and may call
6
+ # any function from any other part: neighbour order is not a dependency.
7
+ # ---------------------------------------------------------------- actions
8
+ function Is-ForegroundHwnd($hwnd) {
9
+ try { return ([Native]::GetForegroundWindow() -eq [IntPtr]$hwnd) } catch { return $false }
10
+ }
11
+
12
+ function Foreground-Hwnd {
13
+ try { return [Native]::GetForegroundWindow() } catch { return [IntPtr]::Zero }
14
+ }
15
+
16
+ function Cursor-Pos {
17
+ try { $p = [System.Windows.Forms.Cursor]::Position; return @{ x = [int]$p.X; y = [int]$p.Y } } catch { return @{ x = -1; y = -1 } }
18
+ }
19
+
20
+ function Test-Escape {
21
+ # VK_ESCAPE high bit = physically held right now. Checked between batch steps and inside the
22
+ # pointer glide so a user pressing ESC stops the run promptly instead of only at the next command.
23
+ try { return (([int][Native]::GetAsyncKeyState(0x1B)) -band 0x8000) -ne 0 } catch { return $false }
24
+ }
25
+
26
+ function Invoke-PixelAction($re, $action, $value) {
27
+ # Drive the physical pointer at the element's own BoundingRectangle. Only used when the mode is
28
+ # foreground, or auto and the UIA path could not act on this element.
29
+ $rc = $re.rect
30
+ if ($null -eq $rc -or [int]$rc.w -le 0 -or [int]$rc.h -le 0) {
31
+ Err 'no_rect' ("pixel path needs a non-empty bounding rect for " + $re.controlType + " '" + (TruncStr $re.name 40) + "'")
32
+ }
33
+ $cx = [int]($rc.x + [Math]::Round($rc.w / 2)); $cy = [int]($rc.y + [Math]::Round($rc.h / 2))
34
+ $exp = @{ windowId = [string]$re.windowId }
35
+ switch ($action) {
36
+ { $_ -in @('invoke', 'select', 'toggle', 'expand', 'collapse', 'focus') } {
37
+ [void](Cmd-PointerClick @{ x = $cx; y = $cy; button = 'left'; expect = $exp }); return 'pixel-click'
38
+ }
39
+ 'set_value' {
40
+ [void](Cmd-PointerClick @{ x = $cx; y = $cy; button = 'left'; expect = $exp })
41
+ [void](Cmd-Key @{ keys = 'ctrl+a' })
42
+ # An empty value means "clear the field". Typing '' over the ctrl+a selection leaves it
43
+ # untouched, so delete the selection explicitly instead.
44
+ if ([string]$value -eq '') { [void](Cmd-Key @{ keys = 'del' }) }
45
+ else { [void](Cmd-TypeText @{ text = [string]$value }) }
46
+ return 'pixel-type'
47
+ }
48
+ 'text_append' {
49
+ [void](Cmd-PointerClick @{ x = $cx; y = $cy; button = 'left'; expect = $exp })
50
+ [void](Cmd-TypeText @{ text = [string]$value }); return 'pixel-type'
51
+ }
52
+ 'scroll_up' { [void](Cmd-PointerScroll @{ x = $cx; y = $cy; direction = 'up'; clicks = 3; expect = $exp }); return 'pixel-scroll' }
53
+ 'scroll_down' { [void](Cmd-PointerScroll @{ x = $cx; y = $cy; direction = 'down'; clicks = 3; expect = $exp }); return 'pixel-scroll' }
54
+ default { Err 'unknown_action' ("no pixel path for action '" + $action + "'") }
55
+ }
56
+ }
57
+
58
+ function Do-Action($re, $action, $value, $step) {
59
+ $mode = [string]$script:W.pointerMode
60
+ $s = Dict $step
61
+ if ($null -ne $s -and (BoolArg $s 'foreground' $false)) { $mode = 'foreground' }
62
+ if ($mode -eq 'foreground') { return Invoke-PixelAction $re $action $value }
63
+ if ($mode -eq 'auto') {
64
+ try { return Invoke-UiaAction $re $action $value $step }
65
+ catch {
66
+ $msg = $_.Exception.Message
67
+ if ($msg -like 'ERR|pattern_unsupported|*' -or $msg -like 'ERR|read_only|*' -or $msg -like 'ERR|action_failed|*') {
68
+ return Invoke-PixelAction $re $action $value
69
+ }
70
+ throw
71
+ }
72
+ }
73
+ return Invoke-UiaAction $re $action $value $step
74
+ }
75
+
76
+ function Invoke-UiaAction($re, $action, $value, $step) {
77
+ $el = $re.ae
78
+ $patterns = $re.patterns
79
+ $s = Dict $step
80
+ $forceFg = $false
81
+ if ($null -ne $s) { $forceFg = BoolArg $s 'foreground' $false }
82
+ $via = 'uia'
83
+ switch ($action) {
84
+ 'invoke' {
85
+ # No LegacyIAccessible fallback: the managed client has no LegacyIAccessiblePattern type, so
86
+ # GetPattern($el, 'LegacyIAccessible') always returned $null and this branch was unreachable.
87
+ # Patterns come from the ref's cache (Get-RefPattern): a set_value/focus reads the same
88
+ # pattern in before→action→after, which used to be three GetCurrentPattern round-trips.
89
+ $p = Get-RefPattern $re 'Invoke'
90
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " '" + (TruncStr $re.name 40) + "' does not support Invoke") }
91
+ $p.Invoke(); return $via
92
+ }
93
+ 'set_value' {
94
+ $p = Get-RefPattern $re 'Value'
95
+ if ($null -eq $p) {
96
+ Err 'pattern_unsupported' ("element " + $re.controlType + " '" + (TruncStr $re.name 40) + "' does not support Value")
97
+ }
98
+ try { if ($p.Current.IsReadOnly) { Err 'read_only' ("element " + $re.controlType + " '" + (TruncStr $re.name 40) + "' is read only") } } catch { }
99
+ $p.SetValue([string]$value); return $via
100
+ }
101
+ 'text_append' {
102
+ $p = Get-RefPattern $re 'Value'
103
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " '" + (TruncStr $re.name 40) + "' does not support Value, cannot append") }
104
+ try { if ($p.Current.IsReadOnly) { Err 'read_only' 'element is read only' } } catch { }
105
+ $cur = ''
106
+ try { $cur = [string]$p.Current.Value } catch { $cur = '' }
107
+ $p.SetValue($cur + [string]$value); return $via
108
+ }
109
+ 'toggle' {
110
+ $p = Get-RefPattern $re 'Toggle'
111
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support Toggle") }
112
+ $p.Toggle(); return $via
113
+ }
114
+ 'select' {
115
+ $p = Get-RefPattern $re 'SelectionItem'
116
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support SelectionItem") }
117
+ $p.Select(); return $via
118
+ }
119
+ 'deselect' {
120
+ $p = Get-RefPattern $re 'SelectionItem'
121
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support SelectionItem") }
122
+ $p.RemoveFromSelection(); return $via
123
+ }
124
+ 'add_to_selection' {
125
+ $p = Get-RefPattern $re 'SelectionItem'
126
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support SelectionItem") }
127
+ $p.AddToSelection(); return $via
128
+ }
129
+ 'scroll_to' {
130
+ # ScrollItem.ScrollIntoView is the provider's own "bring me into the viewport", which is
131
+ # cheaper and more correct than the wheel: it moves the exact container that owns the item and
132
+ # never overshoots. Ensure-InView (the wheel path used before every act) stays as the fallback
133
+ # for items whose provider has no ScrollItem.
134
+ $p = Get-RefPattern $re 'ScrollItem'
135
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support ScrollItem") }
136
+ try { $p.ScrollIntoView() } catch { Err 'action_failed' ('ScrollIntoView threw: ' + $_.Exception.Message) }
137
+ return $via
138
+ }
139
+ 'set_range' {
140
+ # A slider/spinner was a pointer target only: the model had to drag it and hope. RangeValue has
141
+ # SetValue(value); clamp to the provider's own Min/Max instead of letting it throw.
142
+ $p = Get-RefPattern $re 'RangeValue'
143
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support RangeValue") }
144
+ $num = 0.0
145
+ $txt = [string]$value
146
+ if (-not [double]::TryParse($txt, [System.Globalization.NumberStyles]::Float, [System.Globalization.CultureInfo]::InvariantCulture, [ref]$num)) {
147
+ Err 'bad_arg' ('set_range needs a numeric value, got "' + (TruncStr $txt 40) + '"')
148
+ }
149
+ try {
150
+ $cur = $p.Current
151
+ if ($num -lt $cur.Minimum) { $num = $cur.Minimum }
152
+ if ($num -gt $cur.Maximum) { $num = $cur.Maximum }
153
+ } catch { }
154
+ try { $p.SetValue($num) } catch { Err 'action_failed' ('RangeValue.SetValue threw: ' + $_.Exception.Message) }
155
+ return $via
156
+ }
157
+ 'expand' {
158
+ $p = Get-RefPattern $re 'ExpandCollapse'
159
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support ExpandCollapse") }
160
+ $p.Expand(); return $via
161
+ }
162
+ 'collapse' {
163
+ $p = Get-RefPattern $re 'ExpandCollapse'
164
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support ExpandCollapse") }
165
+ $p.Collapse(); return $via
166
+ }
167
+ 'scroll_up' {
168
+ $p = Get-RefPattern $re 'Scroll'
169
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support Scroll") }
170
+ try { if (-not [bool]$p.Current.VerticallyScrollable) { Err 'action_failed' ("element " + $re.controlType + " is not vertically scrollable") } } catch { }
171
+ try { $p.ScrollVertical([System.Windows.Automation.ScrollAmount]::SmallDecrement) } catch { Err 'action_failed' ('ScrollVertical threw: ' + $_.Exception.Message) }
172
+ return $via
173
+ }
174
+ 'scroll_down' {
175
+ $p = Get-RefPattern $re 'Scroll'
176
+ if ($null -eq $p) { Err 'pattern_unsupported' ("element " + $re.controlType + " does not support Scroll") }
177
+ try { if (-not [bool]$p.Current.VerticallyScrollable) { Err 'action_failed' ("element " + $re.controlType + " is not vertically scrollable") } } catch { }
178
+ try { $p.ScrollVertical([System.Windows.Automation.ScrollAmount]::SmallIncrement) } catch { Err 'action_failed' ('ScrollVertical threw: ' + $_.Exception.Message) }
179
+ return $via
180
+ }
181
+ 'focus' {
182
+ if (-not $forceFg -and -not (Is-ForegroundHwnd $re.hwnd)) {
183
+ $script:W.stats.refused += 1
184
+ Err 'needs_foreground' ('action focus on window ' + $re.windowId + ' would raise it; pass {"foreground":true} on the step to allow it')
185
+ }
186
+ if ($forceFg) { try { [void][Native]::SetForegroundWindow([IntPtr]$re.hwnd) } catch { } }
187
+ try { $el.SetFocus() } catch { Err 'action_failed' ('SetFocus threw: ' + $_.Exception.Message) }
188
+ return $via
189
+ }
190
+ default {
191
+ Err 'unknown_action' ("unsupported action '" + $action + "' (invoke|set_value|toggle|select|deselect|add_to_selection|expand|collapse|scroll_up|scroll_down|scroll_to|set_range|focus|text_append)")
192
+ }
193
+ }
194
+ }
195
+
196
+ function Raise-Foreground($entry) {
197
+ $h = [IntPtr]$entry.hwnd
198
+ # A minimized window has no usable screen rect, so pixel coordinates taken from the last
199
+ # snapshot point at nothing. Restore it before raising so the raise is actually clickable.
200
+ try { if ([Native]::IsIconic($h)) { [void][Native]::ShowWindow($h, 9); Start-Sleep -Milliseconds 80 } } catch { }
201
+ try { [void][Native]::SetForegroundWindow($h) } catch { }
202
+ }
203
+
204
+ # ---------------------------------------------------------------- conditions
205
+ function Target-Element($entry, $cond, $refMap) {
206
+ $c = Dict $cond
207
+ $ref = $c.Item('ref')
208
+ if ($null -ne $ref -and [string]$ref -ne '') {
209
+ $re = Resolve-Ref $ref $refMap
210
+ return $re
211
+ }
212
+ $by = Dict $c.Item('by')
213
+ if ($null -eq $by) { $by = @{} }
214
+ if ($c.ContainsKey('windowId')) { $entry = Window-Entry ([string]$c.Item('windowId')) }
215
+ $el = Find-ByDescriptor $entry $by
216
+ if ($null -eq $el) { return $null }
217
+ return @{ synthetic = $true; ae = $el; windowId = $entry.id; hwnd = $entry.hwnd; patterns = (PatternsOf $el); name = [string]$el.Current.Name; controlType = (ControlTypeName $el.Current.ControlType); className = [string]$el.Current.ClassName; automationId = [string]$el.Current.AutomationId; ref = $null }
218
+ }
219
+
220
+ # Vertical scroll percent for the scroll_percent_changed verify. $null means "no usable Scroll
221
+ # reading" (pattern unsupported, element unreadable, or not scrollable - UIA reports -1 for the
222
+ # percent then), so the caller can tell that apart from a genuine 0%. Routes through the ref's
223
+ # pattern cache (Get-RefPattern) like every other before→action→after read.
224
+ function Read-ScrollPercent($re) {
225
+ if ($null -eq $re) { return $null }
226
+ $sp = Get-RefPattern $re 'Scroll'
227
+ if ($null -eq $sp) { return $null }
228
+ try {
229
+ $pct = [double]$sp.Current.VerticalScrollPercent
230
+ if ($pct -lt 0) { return $null }
231
+ return $pct
232
+ } catch { return $null }
233
+ }
234
+
235
+ function Eval-Once($type, $entry, $cond, $before, $refMap, $scrollBefore) {
236
+ $c = Dict $cond
237
+ $expect = $c.Item('expect')
238
+ switch ($type) {
239
+ 'element_appears' {
240
+ $t = Target-Element $entry $cond $refMap
241
+ $obs = 'absent'
242
+ if ($null -ne $t) { $obs = 'present' }
243
+ return @{ matched = ($null -ne $t); observed = $obs }
244
+ }
245
+ 'element_gone' {
246
+ $t = Target-Element $entry $cond $refMap
247
+ $obs = 'absent'
248
+ if ($null -ne $t) { $obs = 'present' }
249
+ return @{ matched = ($null -eq $t); observed = $obs }
250
+ }
251
+ }
252
+ $t = Target-Element $entry $cond $refMap
253
+ if ($null -eq $t) { return @{ matched = $false; observed = 'target missing'; error = 'element_gone' } }
254
+ $cp = $null
255
+ try { $cp = $t.ae.Current } catch { }
256
+ if ($null -eq $cp) { return @{ matched = $false; observed = 'unreadable'; error = 'element_gone' } }
257
+ $pats = $t.patterns
258
+ if ($null -eq $pats -or $pats.Count -eq 0) { $pats = PatternsOf $t.ae }
259
+ switch ($type) {
260
+ 'value_equals' {
261
+ $v = Read-Value $t.ae $pats
262
+ return @{ matched = ($v -eq [string]$expect); observed = (TruncStr $v 200) }
263
+ }
264
+ 'value_changed' {
265
+ $v = Read-Value $t.ae $pats
266
+ return @{ matched = ($v -ne [string]$before); observed = (TruncStr $v 200); before = (TruncStr ([string]$before) 200) }
267
+ }
268
+ 'no_change' {
269
+ $v = Read-Value $t.ae $pats
270
+ $n = [string]$cp.Name
271
+ return @{ matched = ($v -eq [string]$before); observed = (TruncStr $v 200); name = (TruncStr $n 80) }
272
+ }
273
+ 'name_contains' {
274
+ $n = [string]$cp.Name
275
+ return @{ matched = ($n.IndexOf([string]$expect, [StringComparison]::OrdinalIgnoreCase) -ge 0); observed = (TruncStr $n 120) }
276
+ }
277
+ 'enabled_is' {
278
+ $want = BoolArg (@{ v = $expect }) 'v' $true
279
+ $e = [bool]$cp.IsEnabled
280
+ return @{ matched = ($e -eq $want); observed = $e }
281
+ }
282
+ 'scroll_percent_changed' {
283
+ # 防循环的确定性判据:"滚了但列表没动"(动作缓存不记结果的无限滚动)在这里有读数可依:
284
+ # ScrollPattern.VerticalScrollPercent 与动作前读数($scrollBefore,动作步开始时读一次)
285
+ # 的差值 ≥ expect(默认 1)才命中。轮询复用上面的同一 Target-Element 管道。
286
+ $sp = Get-RefPattern $t 'Scroll'
287
+ if ($null -eq $sp) { return @{ matched = $false; observed = 'no Scroll pattern'; error = 'pattern_unsupported' } }
288
+ $pct = $null
289
+ try { $pct = [double]$sp.Current.VerticalScrollPercent } catch { $pct = $null }
290
+ if ($null -eq $pct -or $pct -lt 0) { return @{ matched = $false; observed = 'not scrollable'; error = 'pattern_unsupported' } }
291
+ $base = $scrollBefore
292
+ if ($null -eq $base) { $base = $pct } # 无动作前基线(独立 wait):首轮读数即基线
293
+ $want = 1.0
294
+ if ($null -ne $expect -and [string]$expect -ne '') { try { $want = [Math]::Abs([double]$expect) } catch { $want = 1.0 } }
295
+ $delta = [Math]::Abs($pct - [double]$base)
296
+ return @{ matched = ($delta -ge $want); observed = ('percent=' + $pct); before = ('percent=' + $base) }
297
+ }
298
+ default {
299
+ Err 'unknown_condition' ("unsupported condition type '" + $type + "'")
300
+ }
301
+ }
302
+ }
303
+
304
+ function Wait-Condition($entry, $cond, $timeoutMs, $before, $refMap, $scrollBefore) {
305
+ $c = Dict $cond
306
+ $type = [string]$c.Item('type')
307
+ if ($type -eq '') { Err 'missing_arg' 'condition needs a type' }
308
+ if ($null -eq $timeoutMs -or $timeoutMs -le 0) { $timeoutMs = 750 }
309
+ $budget = $timeoutMs
310
+ $left = BudgetLeftMs
311
+ if ($left -lt $budget) { $budget = [int]$left }
312
+ if ($budget -lt 0) { $budget = 0 }
313
+ $st = TickNow
314
+ $deadline = $st + ([Int64]$budget * 10000)
315
+ $last = $null
316
+ while ($true) {
317
+ $last = Eval-Once $type $entry $cond $before $refMap $scrollBefore
318
+ if ($last.matched) { break }
319
+ if ((TickNow) -ge $deadline) { break }
320
+ Start-Sleep -Milliseconds 80
321
+ }
322
+ $r = [ordered]@{ type = $type; matched = [bool]$last.matched; ms = (MsFrom $st) }
323
+ if ($last.ContainsKey('observed')) { $r['observed'] = $last.observed }
324
+ if ($last.ContainsKey('before')) { $r['beforeObserved'] = $last.before }
325
+ if ($last.ContainsKey('name')) { $r['observedName'] = $last.name }
326
+ if ($last.ContainsKey('error')) { $r['error'] = $last.error }
327
+ return $r
328
+ }
329
+