diff options
Diffstat (limited to 'deckit/modules')
| -rw-r--r-- | deckit/modules/windows.py | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/deckit/modules/windows.py b/deckit/modules/windows.py index 1907b59..a15f44c 100644 --- a/deckit/modules/windows.py +++ b/deckit/modules/windows.py @@ -24,6 +24,7 @@ from pathlib import Path from ..module import Module, ModuleError ADDR = re.compile(r"0x[0-9a-f]{1,16}") +STABLE = re.compile(r"[0-9a-f]{1,16}") # Hyprland's stableId: grim -T's toplevel identifier # The user's own entries first, so they win over the system's, as in a launcher. DESKTOP_BASES = (Path.home() / ".local/share/applications", Path("/usr/share/applications")) @@ -39,6 +40,16 @@ def _popen(cmd): subprocess.Popen(cmd, start_new_session=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) +def _shot(stable_id): + """A window's preview, captured even on a hidden workspace (60 ms, ~20 KB).""" + try: + out = subprocess.run(["grim", "-T", stable_id, "-s", "0.3", "-t", "jpeg", "-q", "70", "-"], + capture_output=True, timeout=3).stdout + except (OSError, subprocess.TimeoutExpired): + return None + return (out, "image/jpeg") if out else None + + def _dispatch(cmd): """Run a hyprctl dispatch and wait: the two-step move depends on the order.""" try: @@ -91,6 +102,17 @@ def _desktop_icons(entries: dict) -> dict: return mapping +def workspace_icons(path) -> dict: + """Workspace number -> icon name, from keystone's Workspaces.qml map.""" + # ponytail: a regex over the QML's `1: "web-browser",` lines, read once at + # start; a parser if that file ever stops being a plain map. + try: + text = Path(path).expanduser().read_text() + except OSError: + return {} + return {int(n): name for n, name in re.findall(r'^\s*(\d+):\s*"([A-Za-z0-9._-]+)"', text, re.M)} + + def hypr_lua(cfg: dict) -> bool: mode = cfg.get("hyprland", "auto") if mode == "auto": @@ -119,7 +141,7 @@ def _state(run): except json.JSONDecodeError: return {"windows": [], "workspaces": [], "activeClass": None} wins = sorted( - ({"address": c["address"], "class": c["class"], "title": c["title"], + ({"address": c["address"], "id": c.get("stableId", ""), "class": c["class"], "title": c["title"], "workspace": c["workspace"]["id"], "active": c["address"] == active} for c in clients if c.get("mapped", True) and c["workspace"]["id"] > 0), key=lambda w: (w["workspace"], w["class"])) @@ -134,8 +156,11 @@ def make(cfg: dict) -> Module: lua = hypr_lua(cfg) apps = [str(a) for a in cfg.get("launch", {}).get("apps", [])] workspaces = cfg.get("windows", {}).get("workspaces", list(range(1, 11))) + ws_icons = workspace_icons(cfg.get("windows", {}).get("workspace_icons", "")) entries = _desktop_entries() icons = _desktop_icons(entries) + shot = cfg.get("shot", _shot) + shown = set() # stableIds of the windows the last state listed def workspace(body): n = body.get("n") @@ -176,13 +201,20 @@ def make(cfg: dict) -> Module: def state(): st = _state(run) + shown.clear() + shown.update(w["id"] for w in st["windows"]) for win in st["windows"]: win["icons"] = icons.get(win["class"].lower(), [win["class"].lower()]) known = sorted(set(int(n) for n in workspaces) | set(st["workspaces"])) launches = [{"key": a, "icon": entries[a]["icon"], "label": entries[a]["name"] or a} for a in apps if a in entries] - return {**st, "workspaces": known, "launches": launches} + return {**st, "workspaces": known, "launches": launches, + "wsIcons": {str(n): ws_icons[n] for n in known if n in ws_icons}} + + def image(key): + # Only a window the tablet was shown; never an arbitrary identifier. + return shot(key) if STABLE.fullmatch(key) and key in shown else None - return Module(name="windows", title="Windows", state=state, + return Module(name="windows", title="Windows", state=state, image=image, actions={"workspace": workspace, "focus": focus, "close": close, "move": move, "launch": launch_key}) |
