Skip to content

Instantly share code, notes, and snippets.

@Hona
Created October 30, 2026 06:06
Show Gist options
  • Select an option

  • Save Hona/d21d6d622bc2bba77d5a873229641cdf to your computer and use it in GitHub Desktop.

Select an option

Save Hona/d21d6d622bc2bba77d5a873229641cdf to your computer and use it in GitHub Desktop.
anomalyco/opencode#59229 glass-watch.ps1: logs when a window takes clicks but is not drawn
# Logs when a window takes clicks but is not drawn (anomalyco/opencode#59229).
# Every 250 ms it finds the top-level window whose title starts with -Title, then checks four
# points inside it: WindowFromPoint must return that window, and the screen pixel must be the
# test page's magenta. A point that hits the window but is not magenta is logged as GLASS.
# Coordinates are physical pixels. Run: powershell -ExecutionPolicy Bypass -File glass-watch.ps1
param(
[string]$Title = "repro-59229",
[int]$Seconds = 86400,
[string]$Log = (Join-Path $PSScriptRoot "glass-watch.jsonl")
)
Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.Text;
public static class GlassWatch {
[StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left, Top, Right, Bottom; }
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X, Y; }
public delegate bool EnumProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")] public static extern IntPtr SetThreadDpiAwarenessContext(IntPtr context);
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc proc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowText(IntPtr hwnd, StringBuilder text, int max);
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hwnd, out RECT rect);
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hwnd);
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hwnd);
[DllImport("user32.dll")] public static extern bool IsZoomed(IntPtr hwnd);
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(POINT point);
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hwnd, uint flags);
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] public static extern uint GetDpiForWindow(IntPtr hwnd);
[DllImport("user32.dll")] public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint flags);
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hwnd, IntPtr dc);
[DllImport("gdi32.dll")] public static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attribute, out RECT value, int size);
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attribute, out int value, int size);
public static IntPtr Find(string prefix) {
IntPtr found = IntPtr.Zero;
EnumWindows((hwnd, lParam) => {
var text = new StringBuilder(512);
GetWindowText(hwnd, text, text.Capacity);
if (IsWindowVisible(hwnd) && text.ToString().StartsWith(prefix)) { found = hwnd; return false; }
return true;
}, IntPtr.Zero);
return found;
}
public static IntPtr Hit(int x, int y) {
var point = new POINT { X = x, Y = y };
return GetAncestor(WindowFromPoint(point), 2);
}
public static uint Pixel(int x, int y) {
IntPtr dc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(dc, x, y);
ReleaseDC(IntPtr.Zero, dc);
return pixel;
}
}
"@
# DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, so rects, points and pixels are all physical.
[void][GlassWatch]::SetThreadDpiAwarenessContext([IntPtr](-4))
$magenta = 0x00ff00ff
$end = (Get-Date).AddSeconds($Seconds)
$last = ""
"watching windows titled '$Title*', logging to $Log"
while ((Get-Date) -lt $end) {
$hwnd = [GlassWatch]::Find($Title)
if ($hwnd -ne [IntPtr]::Zero) {
$rect = New-Object GlassWatch+RECT
[void][GlassWatch]::GetWindowRect($hwnd, [ref]$rect)
$frame = New-Object GlassWatch+RECT
[void][GlassWatch]::DwmGetWindowAttribute($hwnd, 9, [ref]$frame, 16)
$cloaked = 0
[void][GlassWatch]::DwmGetWindowAttribute($hwnd, 14, [ref]$cloaked, 4)
$samples = @(@(0.25, 0.75), @(0.75, 0.75), @(0.25, 0.3), @(0.75, 0.3)) | ForEach-Object {
$x = [int]($frame.Left + ($frame.Right - $frame.Left) * $_[0])
$y = [int]($frame.Top + ($frame.Bottom - $frame.Top) * $_[1])
$pixel = [GlassWatch]::Pixel($x, $y)
[ordered]@{ x = $x; y = $y; hit = ([GlassWatch]::Hit($x, $y) -eq $hwnd); drawn = ($pixel -eq $magenta); pixel = $pixel.ToString("x6") }
}
$state = [ordered]@{
rect = @($rect.Left, $rect.Top, $rect.Right, $rect.Bottom)
frame = @($frame.Left, $frame.Top, $frame.Right, $frame.Bottom)
visible = [GlassWatch]::IsWindowVisible($hwnd)
minimized = [GlassWatch]::IsIconic($hwnd)
maximized = [GlassWatch]::IsZoomed($hwnd)
cloaked = $cloaked
dpi = [GlassWatch]::GetDpiForWindow($hwnd)
monitor = [GlassWatch]::MonitorFromWindow($hwnd, 2).ToString("x")
foreground = ([GlassWatch]::GetForegroundWindow() -eq $hwnd)
glass = [bool]($samples | Where-Object { $_.hit -and -not $_.drawn })
}
$key = ($state | ConvertTo-Json -Compress) + (($samples | ForEach-Object { "$($_.hit)$($_.drawn)" }) -join "")
if ($key -ne $last) {
$last = $key
$state.at = (Get-Date).ToString("o")
$state.samples = $samples
$line = $state | ConvertTo-Json -Compress -Depth 4
Add-Content -Path $Log -Value $line
if ($state.glass) { "GLASS $line" } else { $line }
}
}
Start-Sleep -Milliseconds 250
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment