Skip to content

Commit 8f09ab3

Browse files
committed
ALL: Added getAllWindowsDict() general function. Added getPID() method.
LINUX: Added bad window filter to check for window.id == 0
1 parent 3a04edf commit 8f09ab3

4 files changed

Lines changed: 43 additions & 33 deletions

File tree

src/pywinctl/_main.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import time
1010
from abc import ABC, abstractmethod
1111
from collections.abc import Callable
12-
from typing import Any, cast, List, Tuple, Union
12+
from typing import Any, cast, List, Tuple, Union, TypedDict
1313

1414
from pymonctl import findMonitorsAtPoint, getAllMonitors, getAllMonitorsDict, getMousePos as getMouse
1515
from pywinbox import PyWinBox, Box, Rect, Point, Size
@@ -992,6 +992,19 @@ def displayWindowsUnderMouse(xOffset: int = 0, yOffset: int = 0) -> None:
992992
sys.stdout.flush()
993993

994994

995+
class _WINDATA(TypedDict):
996+
id: Union[int, tuple[str, str]]
997+
display: list[str]
998+
position: tuple[int, int]
999+
size: tuple[int, int]
1000+
status: int
1001+
1002+
1003+
class _WINDICT(TypedDict):
1004+
pid: int
1005+
windows: dict[str, _WINDATA]
1006+
1007+
9951008
if sys.platform == "darwin":
9961009
from ._pywinctl_macos import (MacOSWindow as Window, checkPermissions, getActiveWindow,
9971010
getActiveWindowTitle, getAllAppsNames, getAllAppsWindowsTitles,

src/pywinctl/_pywinctl_linux.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import Xlib.ext
2323
from Xlib.xobject.drawable import Window as XWindow
2424

25-
from ._main import BaseWindow, Re, _WatchDog, _findMonitorName
25+
from ._main import BaseWindow, Re, _WatchDog, _findMonitorName, _WINDATA, _WINDICT
2626
from ewmhlib import EwmhWindow, EwmhRoot, defaultEwmhRoot, Props
2727
from ewmhlib._ewmhlib import _xlibGetAllWindows
2828

@@ -264,7 +264,7 @@ def getAllAppsWindowsTitles():
264264
return result
265265

266266

267-
def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, int | dict[str, str | dict[str, int | Point | Size | str]]]]:
267+
def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, _WINDICT]:
268268
"""
269269
Get all visible apps and windows info
270270
@@ -284,7 +284,7 @@ def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, in
284284
:param tryToFilter: Windows ONLY. Set to ''True'' to try to get User (non-system) apps only (may skip real user apps)
285285
:return: python dictionary
286286
"""
287-
result: dict[str, int | dict[str, int | dict[str, str | dict[str, int | Point | Size | str]]]] = {}
287+
result: dict[str, _WINDICT] = {}
288288
for win in getAllWindows():
289289
winId = win.getHandle()
290290
appName = win.getAppName()
@@ -294,18 +294,17 @@ def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, in
294294
status = 1
295295
elif win.isMaximized:
296296
status = 2
297-
winDict = {
297+
pos = win.position
298+
size = win.size
299+
winDict: _WINDATA = {
298300
"id": winId,
299301
"display": win.getDisplay(),
300-
"position": win.position,
301-
"size": win.size,
302+
"position": (pos.x, pos.y),
303+
"size": (size.width, size.height),
302304
"status": status
303305
}
304306
if appName not in result.keys():
305-
result[appName] = {}
306-
result[appName]["pìd"] = appPID
307-
if "windows" not in result[appName].keys():
308-
result[appName]["windows"] = {}
307+
result[appName] = {"pid": appPID, "windows": {}}
309308
result[appName]["windows"][win.title] = winDict
310309
return result
311310

src/pywinctl/_pywinctl_macos.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import AppKit
2222
import Quartz
2323

24-
from ._main import BaseWindow, Re, _WatchDog, _findMonitorName
24+
from ._main import BaseWindow, Re, _WatchDog, _findMonitorName, _WINDATA, _WINDICT
2525
from pywinbox import Size, Point, Rect, pointInBox
2626

2727

@@ -311,7 +311,7 @@ def getAllAppsWindowsTitles():
311311
return result
312312

313313

314-
def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, int | dict[str, str | dict[str, int | Point | Size | str]]]]:
314+
def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, _WINDICT]:
315315
"""
316316
Get all visible apps and windows info
317317
@@ -343,7 +343,7 @@ def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, in
343343
.replace('missing value', '"missing value"') \
344344
.replace("{", "[").replace("}", "]")
345345
res = ast.literal_eval(ret)
346-
result: dict[str, int | dict[str, int | dict[str, str | dict[str, int | Point | Size | str]]]] = {}
346+
result: dict[str, _WINDICT] = {}
347347
if len(res) > 0:
348348
pids = res[0]
349349
apps = res[1]
@@ -362,19 +362,16 @@ def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, in
362362
status = 1
363363
elif win.isMaximized:
364364
status = 2
365-
display = win.getDisplay()
366-
if appName not in result.keys():
367-
result[appName] = {}
368-
result[appName]["pid"] = pID
369-
if "windows" not in result[appName].keys():
370-
result[appName]["windows"] = {}
371-
result[appName]["windows"][title] = {
365+
winDict: _WINDATA = {
372366
"id": winId,
373-
"display": display,
367+
"display": win.getDisplay(),
374368
"position": pos[j],
375369
"size": sizes[j],
376370
"status": status
377371
}
372+
if appName not in result.keys():
373+
result[appName] = {"pid": pID, "windows": {}}
374+
result[appName]["windows"][title] = winDict
378375
break
379376
return result
380377

src/pywinctl/_pywinctl_win.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import win32api
2525
import win32gui
2626

27-
from ._main import BaseWindow, Re, _WatchDog, _findMonitorName
27+
from ._main import BaseWindow, Re, _WatchDog, _findMonitorName, _WINDATA, _WINDICT
2828
from pywinbox import Size, Point, Rect, pointInBox
2929

3030
# WARNING: Changes are not immediately applied, specially for hide/show (unmap/map)
@@ -231,7 +231,7 @@ def getAllAppsWindowsTitles() -> dict[str, List[str]]:
231231
return result
232232

233233

234-
def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, int | dict[str, str | dict[str, int | Point | Size | str]]]]:
234+
def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, _WINDICT]:
235235
"""
236236
Get all visible apps and windows info
237237
@@ -241,7 +241,9 @@ def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, in
241241
Values:
242242
"pid": app PID
243243
"windows": subdictionary of all app windows
244-
"title": subdictionary of window info
244+
Key: window title
245+
246+
Values:
245247
"id": window handle
246248
"display": display in which window is mostly visible
247249
"position": window position (x, y) within display
@@ -252,7 +254,7 @@ def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, in
252254
:return: python dictionary
253255
"""
254256
process_list = _getAllApps(tryToFilter)
255-
result: dict[str, int | dict[str, int | dict[str, str | dict[str, int | Point | Size | str]]]] = {}
257+
result: dict[str, _WINDICT] = {}
256258
for win in getAllWindows():
257259
winId = win.getHandle()
258260
pID = win32process.GetWindowThreadProcessId(winId)
@@ -265,18 +267,17 @@ def getAllWindowsDict(tryToFilter: bool = False) -> dict[str, int | dict[str, in
265267
status = 1
266268
elif win.isMaximized:
267269
status = 2
268-
winDict = {
270+
pos = win.position
271+
size = win.size
272+
winDict: _WINDATA = {
269273
"id": winId,
270274
"display": win.getDisplay(),
271-
"position": win.position,
272-
"size": win.size,
275+
"position": (pos.x, pos.y),
276+
"size": (size.width, size.height),
273277
"status": status
274278
}
275279
if appName not in result.keys():
276-
result[appName] = {}
277-
result[appName]["pìd"] = appPID
278-
if "windows" not in result[appName].keys():
279-
result[appName]["windows"] = {}
280+
result[appName] = {"pid": appPID, "windows": {}}
280281
result[appName]["windows"][win.title] = winDict
281282
break
282283
return result

0 commit comments

Comments
 (0)