Skip to content

Commit

Permalink
fixes windows manager and adds test script
Browse files Browse the repository at this point in the history
  • Loading branch information
cybrdelic committed Nov 11, 2023
1 parent 41efb58 commit c50fc8b
Showing 3 changed files with 42 additions and 47 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ class WorkspaceAutomator:
def __init__(self, profile_path=DEFAULT_PROFILE_PATH):
self.profile = self._load_profile(profile_path)
self.process_launcher = ProcessLauncher()
self.window_manager = WindowManager()
self.window_manager = WindowManager(self.process_launcher)

def _load_profile(self, profile_path):
with open(profile_path, 'r') as f:
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 main.py profiles/test_profiles.json
86 changes: 40 additions & 46 deletions utils/window_manager.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,47 @@
import pyautogui
import pygetwindow as gw
import subprocess
import time
import win32api
import win32gui
import win32con
import pygetwindow as gw

class WindowManager:
def position_window(self, app):
# Handle monitor selection and switch to the desired monitor
self._switch_monitor(app["monitor"])

# Wait for the application window to appear and get its title
time.sleep(2)
window = self._get_window_by_title(app["name"])
class WindowManager:
def __init__(self, process_launcher):
self.process_launcher = process_launcher
self.default_wait_time = 2

# Handle window positioning
if type(app["position"]) is dict:
self._position_window_by_coordinates(window, app["position"])
def position_window(self, app):
self.process_launcher.launch(app)
time.sleep(self.default_wait_time)
hwnd = self._get_window_handle(app["name"])

if hwnd:
self._move_window_to_monitor(hwnd, app["monitor"])
self._maximize_window(hwnd)

def _get_window_handle(self, title):
try:
window = gw.getWindowsWithTitle(title)[0]
return window._hWnd
except IndexError:
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

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)
else:
self._position_window_by_keyword(window, app["position"])
print("Monitor number out of range")

# Handle workspace shift if provided
if "workspace" in app:
self._switch_workspace(app["workspace"])
def _maximize_window(self, hwnd):
win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)

def _switch_monitor(self, monitor_number):
# For simplicity, we'll just move the mouse to the desired monitor
# You can enhance this logic further
if monitor_number == 1:
pyautogui.moveTo(0, 0)
else:
pyautogui.moveTo(pyautogui.size().width / 2, 0)

def _position_window_by_coordinates(self, window, position):
# Adjust the window size and position
screen_width, screen_height = pyautogui.size()
width = position["width"] if type(position["width"]) is int else int(screen_width * float(position["width"].strip('%')) / 100)
height = position["height"] if type(position["height"]) is int else int(screen_height * float(position["height"].strip('%')) / 100)
window.resizeTo(width, height)
window.moveTo(position["left"], position["top"])

def _position_window_by_keyword(self, window, position_keyword):
if position_keyword == "maximized":
window.maximize()

def _switch_workspace(self, workspace_number):
# Assuming Ctrl + Win + Left/Right switches workspaces
for _ in range(workspace_number - 1):
pyautogui.hotkey('ctrl', 'win', 'right')

def _get_window_by_title(self, title):
for window in gw.getWindowsWithTitle(''):
if title.lower() in window.title.lower():
return window
return None
# 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 c50fc8b

Please sign in to comment.