diff --git a/app/buttons/commands.py b/app/buttons/commands.py index cedccf4..29613a7 100644 --- a/app/buttons/commands.py +++ b/app/buttons/commands.py @@ -158,6 +158,10 @@ def handle_command(message=None): hwnd = window.get_focused() if hwnd: window.close(hwnd) + + if not is_win: + return jsonify({"success": True}) + # Make sure the window is closed by killing the process if necessary (windows only) subprocess.Popen(f"taskkill /f /im {hwnd}", shell=True) subprocess.Popen(f"taskkill /f /im {hwnd}.exe", shell=True) diff --git a/app/buttons/window/close_window.py b/app/buttons/window/close_window.py index e95d998..77364ae 100644 --- a/app/buttons/window/close_window.py +++ b/app/buttons/window/close_window.py @@ -1,21 +1,32 @@ -from app.utils.platform import is_win +from app.utils.platform import is_windows, is_linux -if is_win: +import subprocess +if is_windows: import win32gui import win32con from app.utils.logger import log -def close_window(window_title): - if not is_win: - log.error("This command is only available on Windows") - raise RuntimeError("This command is only available on Windows") +def close_window(window): + if is_windows: + # Find the window + window_name = window + hwnd = win32gui.FindWindow(None, window_name) + if hwnd == 0: + log.warning(f"Window with title '{window_name}' not found or already closed") + raise RuntimeError(f"Window with title '{window_name}' not found or already closed") + + # Close the window + win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) - # Find the window - hwnd = win32gui.FindWindow(None, window_title) - if hwnd == 0: - log.warning(f"Window with title '{window_title}' not found or already closed") - raise RuntimeError(f"Window with title '{window_title}' not found or already closed") + elif is_linux: + # While using Linux, the window name is the window ID + window_id = window + if subprocess.run(["xdotool", "windowclose", window_id]).returncode != 0: + log.warning(f"Window with ID '{window_id}' not found or already closed") + raise RuntimeError(f"Window with ID '{window_id}' not found or already closed") + log.success(f"Window with ID '{window_id}' has been closed") - # Close the window - win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) \ No newline at end of file + else: + log.error("This command is not implemented for this platform.") + raise RuntimeError("This command is not implemented for this platform.") diff --git a/app/buttons/window/get_focused.py b/app/buttons/window/get_focused.py index ba36e83..0a6c2ce 100644 --- a/app/buttons/window/get_focused.py +++ b/app/buttons/window/get_focused.py @@ -1,19 +1,31 @@ -from app.utils.platform import is_win +from app.utils.platform import is_windows, is_linux -if is_win: import win32gui +if is_windows: import win32gui from app.utils.logger import log +import subprocess def get_focused_window(): - if not is_win: - log.error("This command is only available on Windows") - raise RuntimeError("This command is only available on Windows") + if is_windows: + hwnd = win32gui.GetForegroundWindow() + if hwnd == 0: + log.debug("No window has focus") + return None + else: + window_title = win32gui.GetWindowText(hwnd) + log.debug(f"Focused window: {window_title}") + return window_title - hwnd = win32gui.GetForegroundWindow() - if hwnd == 0: - log.debug("No window has focus") - return None + elif is_linux: + try: + window_id = subprocess.check_output(["xdotool", "getwindowfocus"], universal_newlines=True).strip() + window_title = subprocess.check_output(["xdotool", "getwindowname", window_id], universal_newlines=True).strip() + log.debug(f"Focused window: '{window_title}' ; window_id: '{window_id}'") + return window_id + except subprocess.CalledProcessError: + log.debug("No window has focus") + return None + else: - window_title = win32gui.GetWindowText(hwnd) - log.debug(f"Focused window: {window_title}") - return window_title \ No newline at end of file + log.error("This command is not implemented for this platform.") + raise RuntimeError("This command is not implemented for this platform.")