115 lines
3.2 KiB
Python
Executable File
115 lines
3.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import asyncio
|
|
import json
|
|
import os
|
|
from i3ipc.aio import Connection
|
|
from i3ipc import Event
|
|
from typing import List
|
|
|
|
class WorkspaceConfig:
|
|
|
|
i3_index: str
|
|
sorting_index: int
|
|
display_index: str
|
|
display_name: str
|
|
display_groups: List[str]
|
|
|
|
def __init__(self, i3_index: str, dictionary: dict):
|
|
self.i3_index = i3_index
|
|
self.sorting_index = dictionary.get("sort", int(i3_index))
|
|
self.display_index = dictionary["name"]
|
|
self.display_name = dictionary.get("use", "???")
|
|
self.display_groups = dictionary.get("groups", ['default'])
|
|
|
|
@classmethod
|
|
def parse_file(cls: type, filename: str):
|
|
result = []
|
|
dictionary: dict = None
|
|
|
|
with open(filename, 'r') as file:
|
|
dictionary = json.load(file)
|
|
|
|
for index, data in dictionary.items():
|
|
result.append(cls(index, data))
|
|
|
|
result.sort(key=lambda config: config.sorting_index)
|
|
|
|
return result
|
|
|
|
class WorkspaceStatus:
|
|
|
|
config: WorkspaceConfig
|
|
expanded: bool
|
|
state: str
|
|
active: bool
|
|
|
|
def __init__(self, config, state = None):
|
|
self.config = config
|
|
self.state = state or ""
|
|
self.active = state != None
|
|
self.expanded = state == "focused" or state == "visible"
|
|
|
|
def dict_dump(self):
|
|
return {
|
|
"i3_index": self.config.i3_index,
|
|
"name": self.config.display_index,
|
|
"purpose": self.config.display_name,
|
|
"expand": self.expanded,
|
|
"state": self.state,
|
|
"active": self.active
|
|
}
|
|
|
|
config = WorkspaceConfig.parse_file(f"{os.environ['HOME']}/.config/eww/modules/i3.json")
|
|
|
|
groups = []
|
|
|
|
for ws in config:
|
|
for group in ws.display_groups:
|
|
if not group in groups:
|
|
groups.append(group)
|
|
|
|
data = {
|
|
"ws": {},
|
|
"mode": "default"
|
|
}
|
|
|
|
def write_data():
|
|
global data
|
|
print(json.dumps(data), flush=True)
|
|
|
|
async def workspace_event(self: Connection, _):
|
|
global config, data
|
|
|
|
ws_i3_dict = { ws.name: ws for ws in await self.get_workspaces()}
|
|
status = []
|
|
for ws_def in config:
|
|
ws_i3 = ws_i3_dict.get(ws_def.i3_index, None)
|
|
if ws_i3:
|
|
state = ws_i3.focused and "focused" or ws_i3.visible and "visible" or ws_i3.urgent and "urgent" or "unfocused"
|
|
status.append(WorkspaceStatus(ws_def, state))
|
|
else:
|
|
status.append(WorkspaceStatus(ws_def))
|
|
|
|
for group in groups:
|
|
data['ws'][group] = [ ws.dict_dump() for ws in filter(lambda ws: group in ws.config.display_groups, status) ]
|
|
# data['ws']['left'] = [ ws.dict_dump() for ws in filter(lambda ws: ws.config.sorting_index in range(1, 11), status) ]
|
|
# data['ws']['right'] = [ ws.dict_dump() for ws in filter(lambda ws: ws.config.sorting_index in range(11, 21), status) ]
|
|
write_data()
|
|
|
|
async def mode_event(self: Connection, event):
|
|
global data
|
|
data['mode'] = event.change;
|
|
write_data()
|
|
|
|
async def main():
|
|
i3 = await Connection(auto_reconnect = True).connect()
|
|
|
|
i3.on(Event.WORKSPACE_FOCUS, workspace_event)
|
|
i3.on(Event.WORKSPACE_URGENT, workspace_event)
|
|
i3.on(Event.MODE, mode_event)
|
|
await workspace_event(i3, None)
|
|
await i3.main()
|
|
|
|
asyncio.run(main())
|