Files
cbb-refresh-verify-795/tools/Watch-MentorUi.ps1
阿白叔 11a2b2e46e Import CBB refresh verify r4 stability baseline
Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
2026-09-29 15:05:20 +08:00

41 lines
4.0 KiB
PowerShell

[CmdletBinding()]
param([string]$Output = (Join-Path $PSScriptRoot '..\build\mentor-ui-watch.jsonl'), [int]$IntervalMs = 250)
$ErrorActionPreference='Stop'
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes
$typeDef=@'
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public static class MentorWindowProbe {
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid);
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
public static IntPtr[] ForProcess(int processId) { var r=new List<IntPtr>(); EnumWindows((h,p)=>{uint id;GetWindowThreadProcessId(h,out id);if(id==(uint)processId && IsWindowVisible(h))r.Add(h);return true;},IntPtr.Zero);return r.ToArray(); }
}
'@
if($null -eq ('MentorWindowProbe' -as [type])){Add-Type -TypeDefinition $typeDef}
$parent=Split-Path -Parent ([IO.Path]::GetFullPath($Output)); New-Item -ItemType Directory -Path $parent -Force | Out-Null
$last=@{}
function Snapshot($p) {
$proc=Get-Process -Id $p.Id -ErrorAction SilentlyContinue
if($null -eq $proc){return @()}
$all=[Collections.Generic.List[object]]::new()
foreach($hwnd in [MentorWindowProbe]::ForProcess($p.Id)){try{$window=[System.Windows.Automation.AutomationElement]::FromHandle($hwnd);if($null -ne $window){$all.Add($window);foreach($child in $window.FindAll([System.Windows.Automation.TreeScope]::Descendants,[System.Windows.Automation.Condition]::TrueCondition)){$all.Add($child)}}}catch{}}
$rows=@()
foreach($e in $all){try{if([int]$e.Current.ProcessId -ne $p.Id){continue};$r=$e.Current;$row=[ordered]@{name=[string]$r.Name;type=[string]$r.ControlType.ProgrammaticName;id=[string]$r.AutomationId;class=[string]$r.ClassName;enabled=[bool]$r.IsEnabled;hwnd=[int]$r.NativeWindowHandle;help=[string]$r.HelpText;focused=[bool]$r.HasKeyboardFocus;patterns=@($e.GetSupportedPatterns() | ForEach-Object {$_.ProgrammaticName})};$pattern=$null;if($e.TryGetCurrentPattern([System.Windows.Automation.SelectionItemPattern]::Pattern,[ref]$pattern)){$row.selected=$pattern.Current.IsSelected};$pattern=$null;if($e.TryGetCurrentPattern([System.Windows.Automation.ExpandCollapsePattern]::Pattern,[ref]$pattern)){$row.expansion=[string]$pattern.Current.ExpandCollapseState};$rows += $row}catch{}}
return $rows
}
[ordered]@{event='listener_started';listenerPid=$PID;timestampUtc=[datetime]::UtcNow.ToString('o')}|ConvertTo-Json -Compress|Add-Content -LiteralPath $Output -Encoding UTF8
$heartbeat=[datetime]::MinValue
while(-not (Test-Path -LiteralPath ($Output + '.stop'))){
try {
$procs=@(Get-Process LibraryManager,ExpeditionPCB,pcbui -ErrorAction SilentlyContinue)
foreach($p in $procs){$rows=@(Snapshot $p);$key="$($p.ProcessName):$($p.Id)";$json=ConvertTo-Json -InputObject $rows -Compress -Depth 5;$hash=([string]$json).GetHashCode();if(-not $last.ContainsKey($key) -or $last[$key] -ne $hash){$last[$key]=$hash;$line=([ordered]@{timestampUtc=[datetime]::UtcNow.ToString('o');process=$p.ProcessName;pid=$p.Id;windowTitle=$p.MainWindowTitle;elements=$rows}|ConvertTo-Json -Compress -Depth 6)+[Environment]::NewLine;[IO.File]::AppendAllText($Output,$line,(New-Object Text.UTF8Encoding($false)))}}
if(([datetime]::UtcNow-$heartbeat).TotalSeconds -ge 5){$heartbeat=[datetime]::UtcNow;$line=([ordered]@{event='heartbeat';listenerPid=$PID;timestampUtc=$heartbeat.ToString('o');targetPids=@($procs | ForEach-Object {$_.Id})}|ConvertTo-Json -Compress)+[Environment]::NewLine;[IO.File]::AppendAllText($Output,$line,(New-Object Text.UTF8Encoding($false)))}
} catch {$line=([ordered]@{event='listener_error';timestampUtc=[datetime]::UtcNow.ToString('o');message=$_.Exception.Message}|ConvertTo-Json -Compress)+[Environment]::NewLine;[IO.File]::AppendAllText($Output,$line,(New-Object Text.UTF8Encoding($false)))}
Start-Sleep -Milliseconds $IntervalMs
}