summaryrefslogtreecommitdiff
path: root/src/caelestia/subcommands/toggle.py
blob: e045c4391e5445bd505c3727df9cbc877b779b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import subprocess
from argparse import Namespace

from caelestia.utils import hypr


class Command:
    args: Namespace
    clients: list[dict[str, any]] = None

    def __init__(self, args: Namespace) -> None:
        self.args = args

    def run(self) -> None:
        getattr(self, self.args.workspace)()

    def get_clients(self) -> list[dict[str, any]]:
        if self.clients is None:
            self.clients = hypr.message("clients")

        return self.clients

    def move_client(self, selector: callable, workspace: str) -> None:
        for client in self.get_clients():
            if selector(client):
                hypr.dispatch("movetoworkspacesilent", f"special:{workspace},address:{client['address']}")

    def spawn_client(self, selector: callable, spawn: list[str]) -> bool:
        exists = any(selector(client) for client in self.get_clients())

        if not exists:
            subprocess.Popen(["app2unit", "--", *spawn], start_new_session=True)

        return not exists

    def spawn_or_move(self, selector: callable, spawn: list[str], workspace: str) -> None:
        if not self.spawn_client(selector, spawn):
            self.move_client(selector, workspace)

    def communication(self) -> None:
        self.spawn_or_move(lambda c: c["class"] == "discord", ["discord"], "communication")
        self.move_client(lambda c: c["class"] == "whatsapp", "communication")
        hypr.dispatch("togglespecialworkspace", "communication")

    def music(self) -> None:
        self.spawn_or_move(
            lambda c: c["class"] == "Spotify" or c["initialTitle"] == "Spotify" or c["initialTitle"] == "Spotify Free",
            ["spicetify", "watch", "-s"],
            "music",
        )
        self.move_client(lambda c: c["class"] == "feishin", "music")
        hypr.dispatch("togglespecialworkspace", "music")

    def sysmon(self) -> None:
        self.spawn_client(
            lambda c: c["class"] == "btop" and c["title"] == "btop" and c["workspace"]["name"] == "special:sysmon",
            ["foot", "-a", "btop", "-T", "btop", "fish", "-C", "exec btop"],
        )
        hypr.dispatch("togglespecialworkspace", "sysmon")

    def todo(self) -> None:
        self.spawn_or_move(lambda c: c["class"] == "Todoist", ["todoist"], "todo")
        hypr.dispatch("togglespecialworkspace", "todo")

    def specialws(self) -> None:
        workspaces = hypr.message("workspaces")
        on_special_ws = any(ws["name"] == "special:special" for ws in workspaces)
        toggle_ws = "special"

        if not on_special_ws:
            active_ws = hypr.message("activewindow")["workspace"]["name"]
            if active_ws.startswith("special:"):
                toggle_ws = active_ws[8:]

        hypr.dispatch("togglespecialworkspace", toggle_ws)