Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/pywinctl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ def restart(self, isAliveCB=None, isActiveCB=None, isVisibleCB=None, isMinimized
getAppsWithName,
getAllAppsWindowsTitles,
getWindowsAt,
getTopWindowAt,
getAllScreens,
getMousePos,
getScreenSize,
Expand All @@ -689,6 +690,7 @@ def restart(self, isAliveCB=None, isActiveCB=None, isVisibleCB=None, isMinimized
getAppsWithName,
getAllAppsWindowsTitles,
getWindowsAt,
getTopWindowAt,
getAllScreens,
getMousePos,
getScreenSize,
Expand All @@ -710,6 +712,7 @@ def restart(self, isAliveCB=None, isActiveCB=None, isVisibleCB=None, isMinimized
getAppsWithName,
getAllAppsWindowsTitles,
getWindowsAt,
getTopWindowAt,
getAllScreens,
getMousePos,
getScreenSize,
Expand Down
44 changes: 35 additions & 9 deletions src/pywinctl/_pywinctl_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down
35 changes: 26 additions & 9 deletions src/pywinctl/_pywinctl_macos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import annotations

import ast
import difflib
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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):
Expand Down
36 changes: 25 additions & 11 deletions src/pywinctl/_pywinctl_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down