From 72c9602ba1eb3c71b4ed32618103507a5194cb75 Mon Sep 17 00:00:00 2001 From: cybrvybe Date: Sat, 11 Nov 2023 08:29:00 -0800 Subject: [PATCH 1/3] enhanced logging --- utils/process_launcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/process_launcher.py b/utils/process_launcher.py index 44c79cb..9cf4e56 100644 --- a/utils/process_launcher.py +++ b/utils/process_launcher.py @@ -8,5 +8,5 @@ def launch(self, app): command = [app["command"]] arguments = app.get("arguments", []) - print(f"Launching {app['command']}...") # Debug print + print(f"Launching {app['name']} with command: {command + arguments}") # Enhanced logging subprocess.Popen(command + arguments, shell=True) From f35747b4f6d029a081eeacb96003fad0561952c5 Mon Sep 17 00:00:00 2001 From: cybrvybe Date: Sat, 11 Nov 2023 08:29:52 -0800 Subject: [PATCH 2/3] removes default wait times and instead wait for windows handle to appear to increase speed by about 3x --- utils/window_manager.py | 63 ++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/utils/window_manager.py b/utils/window_manager.py index 0a357c9..f28ee42 100644 --- a/utils/window_manager.py +++ b/utils/window_manager.py @@ -4,46 +4,71 @@ import win32gui import win32con import pygetwindow as gw - - class WindowManager: def __init__(self, process_launcher): self.process_launcher = process_launcher - self.default_wait_time = 2 + self.timeout = 10 # Maximum time to wait for the window to appear in seconds + self.handled_hwnds = set() # Track HWNDs of handled windows def position_window(self, app): - print(f"Positioning {app['name']}...") # Debug print + print(f"Positioning {app['name']}...") self.process_launcher.launch(app) - time.sleep(self.default_wait_time) - hwnd = self._get_window_handle(app["name"]) + hwnd = self._wait_for_window_handle(app["name"]) if hwnd: self._move_window_to_monitor(hwnd, app["monitor"]) - self._maximize_window(hwnd) + if app.get("maximize", True): + self._maximize_window(hwnd) + else: + print(f"Failed to find window for {app['name']} within timeout.") + + def _wait_for_window_handle(self, title): + start_time = time.time() + while time.time() - start_time < self.timeout: + hwnd = self._get_window_handle(title) + if hwnd: + return hwnd + time.sleep(0.5) # Check every 0.5 seconds + return None def _get_window_handle(self, title): - try: - window = gw.getWindowsWithTitle(title)[0] - return window._hWnd - except IndexError: - return None + windows = gw.getWindowsWithTitle(title) + for window in windows: + hwnd = window._hWnd + if hwnd not in self.handled_hwnds: + self.handled_hwnds.add(hwnd) # Add to tracked HWNDs + return hwnd + return None def _move_window_to_monitor(self, hwnd, monitor_number): - # Move the window off-screen to ensure it isn't maximized on the wrong monitor - win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, -32000, -32000, 0, 0, win32con.SWP_SHOWWINDOW) - time.sleep(0.5) # Short delay for the window to move off-screen + print(f"Preparing to move window (HWND: {hwnd}) to monitor {monitor_number}") + + # Check if the window is maximized using GetWindowPlacement + window_placement = win32gui.GetWindowPlacement(hwnd) + is_maximized = window_placement[1] == win32con.SW_SHOWMAXIMIZED + print(f"Window is {'maximized' if is_maximized else 'not maximized'}") + # Move the window off-screen (if not maximized) + if not is_maximized: + win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, -32000, -32000, 0, 0, win32con.SWP_SHOWWINDOW) + print("Window moved off-screen") + time.sleep(0.5) + + # Enumerate monitors and move the window to the desired monitor monitors = win32api.EnumDisplayMonitors() if monitor_number - 1 < len(monitors): monitor_info = win32api.GetMonitorInfo(monitors[monitor_number - 1][0]) monitor_area = monitor_info['Monitor'] win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, monitor_area[0], monitor_area[1], 0, 0, win32con.SWP_SHOWWINDOW) + print(f"Window moved to monitor {monitor_number}") + time.sleep(0.5) else: print("Monitor number out of range") + # Restore the window to its original state (if it was maximized) + if is_maximized: + win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE) + print("Window restored to maximized state") + def _maximize_window(self, hwnd): win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE) - - -# Main execution logic (not shown here) would create an instance of WorkspaceAutomator -# and call its run method, similar to what you have in your existing script. From 28545cf162836c363ecba1ee5925cfb4944fa77c Mon Sep 17 00:00:00 2001 From: cybrvybe Date: Sat, 11 Nov 2023 08:32:26 -0800 Subject: [PATCH 3/3] improves speed by 3x --- utils/window_manager.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/window_manager.py b/utils/window_manager.py index f28ee42..5584dc7 100644 --- a/utils/window_manager.py +++ b/utils/window_manager.py @@ -28,7 +28,7 @@ def _wait_for_window_handle(self, title): hwnd = self._get_window_handle(title) if hwnd: return hwnd - time.sleep(0.5) # Check every 0.5 seconds + time.sleep(0.2) # Check every 0.5 seconds return None def _get_window_handle(self, title): @@ -52,7 +52,7 @@ def _move_window_to_monitor(self, hwnd, monitor_number): if not is_maximized: win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, -32000, -32000, 0, 0, win32con.SWP_SHOWWINDOW) print("Window moved off-screen") - time.sleep(0.5) + time.sleep(0.2) # Reduced sleep time # Enumerate monitors and move the window to the desired monitor monitors = win32api.EnumDisplayMonitors() @@ -61,7 +61,7 @@ def _move_window_to_monitor(self, hwnd, monitor_number): monitor_area = monitor_info['Monitor'] win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, monitor_area[0], monitor_area[1], 0, 0, win32con.SWP_SHOWWINDOW) print(f"Window moved to monitor {monitor_number}") - time.sleep(0.5) + time.sleep(0.2) # Reduced sleep time else: print("Monitor number out of range")