-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwindow_manager.py
More file actions
58 lines (48 loc) · 1.99 KB
/
window_manager.py
File metadata and controls
58 lines (48 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
import logging
logger = logging.getLogger(__name__)
WINDOWS_PLATFORMS = ["windows", "win32", "cygwin"]
MAC_PLATFORMS = ["mac", "darwin"]
POE_WINDOW_NAME = "Path of Exile"
class WindowManager:
def set_foreground(self) -> None:
import win32gui
# import win32con
import win32com.client
self._handle = win32gui.FindWindowEx(0, 0, 0, POE_WINDOW_NAME)
if not self._handle:
logger.warn(f"Window not found: {self._handle}")
return
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("%")
# win32gui.ShowWindow(self._handle, win32con.SW_RESTORE)
win32gui.SetForegroundWindow(self._handle)
def is_poe_window_focused(self) -> bool:
platf = sys.platform.lower()
if platf in WINDOWS_PLATFORMS:
import win32gui
window = win32gui.GetForegroundWindow()
active_window_name = win32gui.GetWindowText(window)
if active_window_name:
return active_window_name == POE_WINDOW_NAME
return False
elif platf in MAC_PLATFORMS:
# Source: https://stackoverflow.com/questions/5286274/front-most-window-using-cgwindowlistcopywindowinfo
import Quartz # type: ignore
windows = Quartz.CGWindowListCopyWindowInfo(
Quartz.kCGWindowListExcludeDesktopElements
| Quartz.kCGWindowListOptionOnScreenOnly,
Quartz.kCGNullWindowID,
)
for win in windows:
if win["kCGWindowLayer"] == 0:
window_name = "%s %s" % (
win[Quartz.kCGWindowOwnerName],
win.get(Quartz.kCGWindowName, ""),
)
return window_name == POE_WINDOW_NAME
return False
else:
raise Exception(
f"sys.platform={platf} is not supported. Available platforms: {WINDOWS_PLATFORMS}, {MAC_PLATFORMS}"
)