From 77f6d9376ae64f9a64af5981d0280c4799ddaa16 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 24 Jul 2022 16:15:04 -0400 Subject: [PATCH] Implemented getWindowAt Closes #20 Fixes #18 --- src/pywinctl/__init__.py | 3 +++ src/pywinctl/_pywinctl_linux.py | 44 ++++++++++++++++++++++++++------- src/pywinctl/_pywinctl_macos.py | 35 +++++++++++++++++++------- src/pywinctl/_pywinctl_win.py | 36 ++++++++++++++++++--------- 4 files changed, 89 insertions(+), 29 deletions(-) diff --git a/src/pywinctl/__init__.py b/src/pywinctl/__init__.py index b9a1b27..0e83229 100644 --- a/src/pywinctl/__init__.py +++ b/src/pywinctl/__init__.py @@ -667,6 +667,7 @@ def restart(self, isAliveCB=None, isActiveCB=None, isVisibleCB=None, isMinimized getAppsWithName, getAllAppsWindowsTitles, getWindowsAt, + getTopWindowAt, getAllScreens, getMousePos, getScreenSize, @@ -689,6 +690,7 @@ def restart(self, isAliveCB=None, isActiveCB=None, isVisibleCB=None, isMinimized getAppsWithName, getAllAppsWindowsTitles, getWindowsAt, + getTopWindowAt, getAllScreens, getMousePos, getScreenSize, @@ -710,6 +712,7 @@ def restart(self, isAliveCB=None, isActiveCB=None, isVisibleCB=None, isMinimized getAppsWithName, getAllAppsWindowsTitles, getWindowsAt, + getTopWindowAt, getAllScreens, getMousePos, getScreenSize, diff --git a/src/pywinctl/_pywinctl_linux.py b/src/pywinctl/_pywinctl_linux.py index d0fe774..94b9cd7 100644 --- a/src/pywinctl/_pywinctl_linux.py +++ b/src/pywinctl/_pywinctl_linux.py @@ -10,7 +10,7 @@ import threading import time import traceback -from typing import Union, List, Tuple +from typing import Iterable, Union, List, Tuple, cast import Xlib.X import Xlib.display @@ -108,14 +108,25 @@ def getActiveWindowTitle() -> str: return "" +def __remove_bad_windows(windows: Iterable[Window]): + """ + :param clients: Xlib Windows + :return: A generator of LinuxWindow that filters out BadWindows + """ + for window in windows: + try: + yield LinuxWindow(window) + except Xlib.error.XResourceError: + pass + + def getAllWindows(): """ Get the list of Window objects for all visible windows - :return: list of Window objects """ - windows = EWMH.getClientList() - return [LinuxWindow(window) for window in windows] + windows = cast(list[Window], EWMH.getClientListStacking()) + return [window for window in __remove_bad_windows(windows)] def getAllTitles() -> List[str]: @@ -249,11 +260,26 @@ def getWindowsAt(x: int, y: int): :param y: Y screen coordinate of the window(s) :return: list of Window objects """ - windowsAtXY = [] - for win in getAllWindows(): - if pointInRect(x, y, win.left, win.top, win.width, win.height): - windowsAtXY.append(win) - return windowsAtXY + return [ + window for window + in getAllWindows() + if pointInRect(x, y, window.left, window.top, window.width, window.height)] + + +def getTopWindowAt(x: int, y: int): + """ + Get the Window object at the top of the stack at the point ``(x, y)`` on screen + + :param x: X screen coordinate of the window + :param y: Y screen coordinate of the window + :return: Window object or None + """ + windows = cast(list[Window], EWMH.getClientListStacking()) + for window in __remove_bad_windows(reversed(windows)): + if pointInRect(x, y, window.left, window.top, window.width, window.height): + return window + else: + return None def _xlibGetAllWindows(parent: int = None, title: str = "") -> List[int]: diff --git a/src/pywinctl/_pywinctl_macos.py b/src/pywinctl/_pywinctl_macos.py index ef3f692..171c2cf 100644 --- a/src/pywinctl/_pywinctl_macos.py +++ b/src/pywinctl/_pywinctl_macos.py @@ -1,5 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +from __future__ import annotations import ast import difflib @@ -10,7 +11,7 @@ import time import traceback import threading -from typing import List, Tuple, Union +from typing import List, Optional, Tuple, Union import AppKit import Quartz @@ -121,7 +122,7 @@ def getAllWindows(app: AppKit.NSApplication = None): :param app: (optional) NSApp() object. If passed, returns the Window objects of all windows of given app :return: list of Window objects """ - windows = [] + windows: list[Union[MacOSNSWindow, MacOSWindow]] = [] if not app: activeApps = _getAllApps() titleList = _getWindowTitles() @@ -319,7 +320,7 @@ def getAllAppsWindowsTitles() -> dict: return result -def getWindowsAt(x: int, y: int, app: AppKit.NSApplication = None, allWindows=None): +def getWindowsAt(x: int, y: int, app: AppKit.NSApplication = None, allWindows: Optional[list[MacOSNSWindow | MacOSWindow]] = None): """ Get the list of Window objects whose windows contain the point ``(x, y)`` on screen @@ -329,14 +330,30 @@ def getWindowsAt(x: int, y: int, app: AppKit.NSApplication = None, allWindows=No :param allWindows: (optional) list of window objects (required to improve performance in Apple Script version) :return: list of Window objects """ - matches = [] if not allWindows: allWindows = getAllWindows(app) - for win in allWindows: - box = win.box - if pointInRect(x, y, box.left, box.top, box.width, box.height): - matches.append(win) - return matches + windowBoxGenerator = ((window, window.box) for window in allWindows) + return [ + window for (window, box) + in windowBoxGenerator + if pointInRect(x, y, box.left, box.top, box.width, box.height)] + + +def getTopWindowAt(x: int, y: int, app: AppKit.NSApplication = None, allWindows: Optional[list[MacOSNSWindow | MacOSWindow]] = None): + """ + Get *a* Window object at the point ``(x, y)`` on screen. + Which window is not garranteed. See https://github.com/Kalmat/PyWinCtl/issues/20#issuecomment-1193348238 + + :param x: X screen coordinate of the window + :param y: Y screen coordinate of the window + :return: Window object or None + """ + # TODO: Once we've figured out why getWindowsAt may not always return all windows + # (see https://github.com/Kalmat/PyWinCtl/issues/21) + # we can look into a more efficient implementation that only gets a single window + windows = getWindowsAt(x, y, app, allWindows) + return None if len(windows) == 0 else windows[-1] + def _getAllApps(userOnly: bool = True): diff --git a/src/pywinctl/_pywinctl_win.py b/src/pywinctl/_pywinctl_win.py index 73396e9..831a365 100644 --- a/src/pywinctl/_pywinctl_win.py +++ b/src/pywinctl/_pywinctl_win.py @@ -69,12 +69,10 @@ def getAllWindows(): :return: list of Window objects """ - matches = [] - windowObjs = _findWindowHandles(onlyVisible=True) - for win in windowObjs: - if win32gui.IsWindowVisible(win): - matches.append(Win32Window(win)) - return matches + return [ + Win32Window(hwnd) for hwnd + in _findWindowHandles(onlyVisible=True) + if win32gui.IsWindowVisible(hwnd)] def getAllTitles() -> List[str]: @@ -215,11 +213,27 @@ def getWindowsAt(x: int, y: int): :param y: Y screen coordinate of the window(s) :return: list of Window objects """ - windowsAtXY = [] - for win in getAllWindows(): - if pointInRect(x, y, win.left, win.top, win.width, win.height): - windowsAtXY.append(win) - return windowsAtXY + return [ + window for window + in getAllWindows() + if pointInRect(x, y, window.left, window.top, window.width, window.height)] + + +def getTopWindowAt(x: int, y: int): + """ + Get the Window object at the top of the stack at the point ``(x, y)`` on screen + + :param x: X screen coordinate of the window + :param y: Y screen coordinate of the window + :return: Window object or None + """ + hwnd: int = win32gui.WindowFromPoint((x, y)) + + # Want to pull the parent window from the window handle + # By using GetAncestor we are able to get the parent window instead of the owner window. + while win32gui.IsChild(win32gui.GetParent(hwnd), hwnd): + hwnd = ctypes.windll.user32.GetAncestor(hwnd, win32con.GA_ROOT) + return Win32Window(hwnd) if hwnd else None def _findWindowHandles(parent: int = None, window_class: str = None, title: str = None, onlyVisible: bool = False) -> List[int]: