import ctypes, win32gui, win32process

user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
    user32.SetThreadDesktop(hdesk)

log_path = r"c:\Projects\JX\logs\inspect_all_visible_popups.txt"
lines = []

def enum_cb(hwnd, _):
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd):
        title = win32gui.GetWindowText(hwnd)
        cls = win32gui.GetClassName(hwnd)
        rect = win32gui.GetWindowRect(hwnd)
        tid, pid = win32process.GetWindowThreadProcessId(hwnd)
        # Check if rect is non-empty
        if rect[2] > rect[0] and rect[3] > rect[1]:
            lines.append(f"HWND: {hex(hwnd)} | PID: {pid} | Class: '{cls}' | Rect: {rect} | Title: '{title}'")
            # Enumerate children
            def enum_child(chwnd, _):
                ctitle = win32gui.GetWindowText(chwnd)
                ccls = win32gui.GetClassName(chwnd)
                lines.append(f"   Child {hex(chwnd)} [{ccls}]: '{ctitle}'")
            win32gui.EnumChildWindows(hwnd, enum_child, None)

win32gui.EnumWindows(enum_cb, None)

with open(log_path, "w", encoding="utf-8") as f:
    f.write("\n".join(lines))
print("Wrote inspect_all_visible_popups.txt")
