-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes windows manager and adds test script
- Loading branch information
Showing
3 changed files
with
42 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python3 main.py profiles/test_profiles.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |