44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
"""
|
|
Windows window utilities using win32gui.
|
|
Enumerate and manage application windows.
|
|
"""
|
|
try:
|
|
import win32gui
|
|
import win32con
|
|
|
|
def get_active_window() -> dict:
|
|
hwnd = win32gui.GetForegroundWindow()
|
|
return {
|
|
'hwnd': hwnd,
|
|
'title': win32gui.GetWindowText(hwnd)
|
|
}
|
|
|
|
def list_windows() -> list[dict]:
|
|
windows = []
|
|
def _enum(hwnd, _):
|
|
if win32gui.IsWindowVisible(hwnd):
|
|
title = win32gui.GetWindowText(hwnd)
|
|
if title:
|
|
windows.append({'hwnd': hwnd, 'title': title})
|
|
win32gui.EnumWindows(_enum, None)
|
|
return windows
|
|
|
|
def focus_window(title_substring: str) -> bool:
|
|
"""Bring a window to foreground by partial title match."""
|
|
def _enum(hwnd, _):
|
|
if title_substring.lower() in win32gui.GetWindowText(hwnd).lower():
|
|
win32gui.SetForegroundWindow(hwnd)
|
|
return False
|
|
win32gui.EnumWindows(_enum, None)
|
|
return True
|
|
|
|
except ImportError:
|
|
def get_active_window():
|
|
return {'hwnd': None, 'title': 'Unknown (pywin32 not installed)'}
|
|
|
|
def list_windows():
|
|
return []
|
|
|
|
def focus_window(title_substring: str):
|
|
return False
|