toggle: improvements

Closes #40
This commit is contained in:
2 * r + 2 * t
2025-08-16 17:41:23 +10:00
parent e24656da0e
commit 0df89887a0
2 changed files with 19 additions and 12 deletions
+1 -3
View File
@@ -28,9 +28,7 @@ def parse_args() -> (argparse.ArgumentParser, argparse.Namespace):
# Create parser for toggle opts # Create parser for toggle opts
toggle_parser = command_parser.add_parser("toggle", help="toggle a special workspace") toggle_parser = command_parser.add_parser("toggle", help="toggle a special workspace")
toggle_parser.set_defaults(cls=toggle.Command) toggle_parser.set_defaults(cls=toggle.Command)
toggle_parser.add_argument( toggle_parser.add_argument("workspace", help="the workspace to toggle")
"workspace", choices=["communication", "music", "sysmon", "specialws", "todo"], help="the workspace to toggle"
)
# Create parser for scheme opts # Create parser for scheme opts
scheme_parser = command_parser.add_parser("scheme", help="manage the colour scheme") scheme_parser = command_parser.add_parser("scheme", help="manage the colour scheme")
+16 -7
View File
@@ -1,6 +1,6 @@
import json import json
import shlex
import shutil import shutil
import subprocess
from argparse import Namespace from argparse import Namespace
from collections import ChainMap from collections import ChainMap
@@ -111,9 +111,13 @@ class Command:
self.specialws() self.specialws()
return return
spawned = False
if self.args.workspace in self.cfg:
for client in self.cfg[self.args.workspace].values(): for client in self.cfg[self.args.workspace].values():
if "enable" in client and client["enable"]: if "enable" in client and client["enable"] and self.handle_client_config(client):
self.handle_client_config(client) spawned = True
if not spawned:
hypr.dispatch("togglespecialworkspace", self.args.workspace) hypr.dispatch("togglespecialworkspace", self.args.workspace)
def get_clients(self) -> list[dict[str, any]]: def get_clients(self) -> list[dict[str, any]]:
@@ -127,13 +131,15 @@ class Command:
if selector(client) and client["workspace"]["name"] != f"special:{workspace}": if selector(client) and client["workspace"]["name"] != f"special:{workspace}":
hypr.dispatch("movetoworkspacesilent", f"special:{workspace},address:{client['address']}") hypr.dispatch("movetoworkspacesilent", f"special:{workspace},address:{client['address']}")
def spawn_client(self, selector: callable, spawn: list[str]) -> None: def spawn_client(self, selector: callable, spawn: list[str]) -> bool:
if (spawn[0].endswith(".desktop") or shutil.which(spawn[0])) and not any( if (spawn[0].endswith(".desktop") or shutil.which(spawn[0])) and not any(
selector(client) for client in self.get_clients() selector(client) for client in self.get_clients()
): ):
subprocess.Popen(["app2unit", "--", *spawn], start_new_session=True) hypr.dispatch("exec", f"[workspace special:{self.args.workspace}] app2unit -- {shlex.join(spawn)}")
return True
return False
def handle_client_config(self, client: dict[str, any]) -> None: def handle_client_config(self, client: dict[str, any]) -> bool:
def selector(c: dict[str, any]) -> bool: def selector(c: dict[str, any]) -> bool:
# Each match is or, inside matches is and # Each match is or, inside matches is and
for match in client["match"]: for match in client["match"]:
@@ -141,11 +147,14 @@ class Command:
return True return True
return False return False
spawned = False
if "command" in client and client["command"]: if "command" in client and client["command"]:
self.spawn_client(selector, client["command"]) spawned = self.spawn_client(selector, client["command"])
if "move" in client and client["move"]: if "move" in client and client["move"]:
self.move_client(selector, self.args.workspace) self.move_client(selector, self.args.workspace)
return spawned
def specialws(self) -> None: def specialws(self) -> None:
special = next(m for m in hypr.message("monitors") if m["focused"])["specialWorkspace"]["name"] special = next(m for m in hypr.message("monitors") if m["focused"])["specialWorkspace"]["name"]
hypr.dispatch("togglespecialworkspace", special[8:] or "special") hypr.dispatch("togglespecialworkspace", special[8:] or "special")