small code refactoring

This commit is contained in:
fdev31 2023-04-28 00:41:12 +02:00
parent 65950cc2ad
commit 78c04872fd
3 changed files with 57 additions and 48 deletions

View file

@ -65,7 +65,7 @@ class Pyprland:
print(f"EVT {full_name}({params.strip()})") print(f"EVT {full_name}({params.strip()})")
await self._callHandler(full_name, params) await self._callHandler(full_name, params)
async def read_command(self, reader, writer): async def read_command(self, reader, writer) -> None:
data = (await reader.readline()).decode() data = (await reader.readline()).decode()
if not data: if not data:
print("Server starved") print("Server starved")

View file

@ -16,6 +16,7 @@ async def get_event_stream():
async def hyprctlJSON(command) -> list[dict[str, Any]] | dict[str, Any]: async def hyprctlJSON(command) -> list[dict[str, Any]] | dict[str, Any]:
"""Run an IPC command and return the JSON output."""
if DEBUG: if DEBUG:
print("(JS)>>>", command) print("(JS)>>>", command)
ctl_reader, ctl_writer = await asyncio.open_unix_connection(HYPRCTL) ctl_reader, ctl_writer = await asyncio.open_unix_connection(HYPRCTL)
@ -29,7 +30,8 @@ async def hyprctlJSON(command) -> list[dict[str, Any]] | dict[str, Any]:
return ret return ret
async def hyprctl(command, base_command="dispatch"): async def hyprctl(command, base_command="dispatch") -> bool:
"""Run an IPC command. Returns success value."""
if DEBUG: if DEBUG:
print(">>>", command) print(">>>", command)
ctl_reader, ctl_writer = await asyncio.open_unix_connection(HYPRCTL) ctl_reader, ctl_writer = await asyncio.open_unix_connection(HYPRCTL)
@ -45,13 +47,13 @@ async def hyprctl(command, base_command="dispatch"):
await ctl_writer.wait_closed() await ctl_writer.wait_closed()
if DEBUG: if DEBUG:
print("<<<", resp) print("<<<", resp)
r = resp == b"ok" * (len(resp) // 2) r: bool = resp == b"ok" * (len(resp) // 2)
if DEBUG and not r: if DEBUG and not r:
print(f"FAILED {resp}") print(f"FAILED {resp}")
return r return r
async def get_focused_monitor_props(): async def get_focused_monitor_props() -> dict[str, Any]:
for monitor in await hyprctlJSON("monitors"): for monitor in await hyprctlJSON("monitors"):
assert isinstance(monitor, dict) assert isinstance(monitor, dict)
if monitor.get("focused") == True: if monitor.get("focused") == True:

View file

@ -14,10 +14,60 @@ DEFAULT_MARGIN = 60
async def get_client_props_by_pid(pid: int): async def get_client_props_by_pid(pid: int):
for client in await hyprctlJSON("clients"): for client in await hyprctlJSON("clients"):
assert isinstance(client, dict)
if client.get("pid") == pid: if client.get("pid") == pid:
return client return client
class Animations:
@classmethod
async def fromtop(cls, monitor, client, client_uid, margin):
mon_x = monitor["x"]
mon_y = monitor["y"]
mon_width = monitor["width"]
client_width = client["size"][0]
margin_x = int((mon_width - client_width) / 2) + mon_x
await hyprctl(f"movewindowpixel exact {margin_x} {mon_y + margin},{client_uid}")
@classmethod
async def frombottom(cls, monitor, client, client_uid, margin):
mon_x = monitor["x"]
mon_y = monitor["y"]
mon_width = monitor["width"]
mon_height = monitor["height"]
client_width = client["size"][0]
client_height = client["size"][1]
margin_x = int((mon_width - client_width) / 2) + mon_x
await hyprctl(
f"movewindowpixel exact {margin_x} {mon_y + mon_height - client_height - margin},{client_uid}"
)
@classmethod
async def fromleft(cls, monitor, client, client_uid, margin):
mon_y = monitor["y"]
mon_height = monitor["height"]
client_height = client["size"][1]
margin_y = int((mon_height - client_height) / 2) + mon_y
await hyprctl(f"movewindowpixel exact {margin} {margin_y},{client_uid}")
@classmethod
async def fromright(cls, monitor, client, client_uid, margin):
mon_y = monitor["y"]
mon_width = monitor["width"]
mon_height = monitor["height"]
client_width = client["size"][0]
client_height = client["size"][1]
margin_y = int((mon_height - client_height) / 2) + mon_y
await hyprctl(
f"movewindowpixel exact {mon_width - client_width - margin} {margin_y},{client_uid}"
)
class Scratch: class Scratch:
def __init__(self, uid, opts): def __init__(self, uid, opts):
self.uid = uid self.uid = uid
@ -210,49 +260,6 @@ class Extension(Plugin):
if uid not in self.transitioning_scratches: if uid not in self.transitioning_scratches:
await hyprctl(f"movetoworkspacesilent special:scratch,{pid}") await hyprctl(f"movetoworkspacesilent special:scratch,{pid}")
async def _animation_fromtop(self, monitor, client, client_uid, margin):
mon_x = monitor["x"]
mon_y = monitor["y"]
mon_width = monitor["width"]
client_width = client["size"][0]
margin_x = int((mon_width - client_width) / 2) + mon_x
await hyprctl(f"movewindowpixel exact {margin_x} {mon_y + margin},{client_uid}")
async def _animation_frombottom(self, monitor, client, client_uid, margin):
mon_x = monitor["x"]
mon_y = monitor["y"]
mon_width = monitor["width"]
mon_height = monitor["height"]
client_width = client["size"][0]
client_height = client["size"][1]
margin_x = int((mon_width - client_width) / 2) + mon_x
await hyprctl(
f"movewindowpixel exact {margin_x} {mon_y + mon_height - client_height - margin},{client_uid}"
)
async def _animation_fromleft(self, monitor, client, client_uid, margin):
mon_y = monitor["y"]
mon_height = monitor["height"]
client_height = client["size"][1]
margin_y = int((mon_height - client_height) / 2) + mon_y
await hyprctl(f"movewindowpixel exact {margin} {margin_y},{client_uid}")
async def _animation_fromright(self, monitor, client, client_uid, margin):
mon_y = monitor["y"]
mon_width = monitor["width"]
mon_height = monitor["height"]
client_width = client["size"][0]
client_height = client["size"][1]
margin_y = int((mon_height - client_height) / 2) + mon_y
await hyprctl(
f"movewindowpixel exact {mon_width - client_width - margin} {margin_y},{client_uid}"
)
async def run_show(self, uid, force=False): async def run_show(self, uid, force=False):
uid = uid.strip() uid = uid.strip()
item = self.scratches.get(uid) item = self.scratches.get(uid)
@ -287,7 +294,7 @@ class Extension(Plugin):
await hyprctl(f"movetoworkspacesilent {wrkspc},{pid}") await hyprctl(f"movetoworkspacesilent {wrkspc},{pid}")
if animation_type: if animation_type:
margin = item.conf.get("margin", DEFAULT_MARGIN) margin = item.conf.get("margin", DEFAULT_MARGIN)
fn = getattr(self, "_animation_%s" % animation_type) fn = getattr(Animations, animation_type)
await fn(monitor, item.clientInfo, pid, margin) await fn(monitor, item.clientInfo, pid, margin)
await hyprctl(f"focuswindow {pid}") await hyprctl(f"focuswindow {pid}")