Skip to content

Commit

Permalink
Merge pull request #4 from alexfigueroa-solutions/feature/enhance-per…
Browse files Browse the repository at this point in the history
…formance

Feature/enhance performance
  • Loading branch information
cybrdelic authored Nov 11, 2023
2 parents 8461f8a + 28545cf commit 19b7eba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion utils/process_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
63 changes: 44 additions & 19 deletions utils/window_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.2) # 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.2) # Reduced sleep time

# 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.2) # Reduced sleep time
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.

0 comments on commit 19b7eba

Please sign in to comment.