43 lines
993 B
Python
43 lines
993 B
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import i3ipc
|
|
|
|
|
|
@dataclass
|
|
class Context:
|
|
window_id: int
|
|
app_id: str
|
|
klass: str
|
|
instance: str
|
|
title: str
|
|
|
|
|
|
class I3Provider:
|
|
def __init__(self):
|
|
self._conn = i3ipc.Connection()
|
|
|
|
def _focused(self):
|
|
return self._conn.get_tree().find_focused()
|
|
|
|
def capture(self) -> Context:
|
|
node = self._focused()
|
|
return Context(
|
|
window_id=node.id,
|
|
app_id=node.app_id or "",
|
|
klass=node.window_class or "",
|
|
instance=node.window_instance or "",
|
|
title=node.name or "",
|
|
)
|
|
|
|
def is_same_focus(self, ctx: Context) -> bool:
|
|
node = self._focused()
|
|
return bool(node and node.id == ctx.window_id)
|
|
|
|
def focus_window(self, window_id: int) -> bool:
|
|
try:
|
|
self._conn.command(f"[con_id={window_id}] focus")
|
|
return True
|
|
except Exception:
|
|
return False
|