-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkEnvLauncher.py
More file actions
155 lines (128 loc) · 6.07 KB
/
WorkEnvLauncher.py
File metadata and controls
155 lines (128 loc) · 6.07 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
import sys
import json
import time
import subprocess
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gio', '2.0')
from gi.repository import Gdk, Gio, GLib
class WorkspaceOrchestrator:
def __init__(self, config_path):
try:
with open(config_path, 'r') as f: self.config = json.load(f)
except Exception as e: sys.exit(f"JSON Error: {e}")
self.bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
self.proxy = Gio.DBusProxy.new_sync(
self.bus, Gio.DBusProxyFlags.NONE, None,
'org.gnome.Shell',
'/org/gnome/Shell/Extensions/Windows',
'org.gnome.Shell.Extensions.Windows', None
)
self.monitors = self._detect_monitors()
self.window_map = {}
self.app_config_map = {app['id']: app for app in self.config['apps']}
def _detect_monitors(self):
display = Gdk.Display.get_default()
mons = []
for i in range(display.get_n_monitors()):
m = display.get_monitor(i)
g = m.get_geometry()
area = m.get_width_mm() * m.get_height_mm() or (g.width * g.height)
mons.append({'x': g.x, 'y': g.y, 'w': g.width, 'h': g.height, 'area': area})
mons.sort(key=lambda x: x['area'], reverse=True)
print(f"Detected Monitors (Large -> Small):")
for idx, m in enumerate(mons):
print(f" #{idx}: {m['w']}x{m['h']} at X={m['x']}, Y={m['y']}")
return {'large': mons[0], 'small': mons[1] if len(mons) > 1 else mons[0], 'count': len(mons)}
def get_windows(self):
try:
res = self.proxy.call_sync('List', None, Gio.DBusCallFlags.NONE, -1, None)
return {w['id']: w for w in json.loads(res.unpack()[0])}
except: return {}
def launch_apps(self):
print("\n--- Phase 1: Launching Apps ---")
for app in self.config['apps']:
print(f" > Launching: {app['id']}")
before = self.get_windows()
subprocess.Popen(app['cmd'], shell=True, start_new_session=True,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if not app.get('wait', False): continue
# Lock loop
for i in range(20):
time.sleep(0.5)
now = self.get_windows()
new_ids = set(now.keys()) - set(before.keys())
for nid in new_ids:
w_class = now[nid].get('wm_class', '').lower()
if app.get('match_class', '').lower() in w_class:
self.window_map[app['id']] = nid
print(f" [+] Locked ID: {nid}")
break
if app['id'] in self.window_map: break
def _apply_gaps(self, geo, mon):
gaps = self.config['meta'].get('gaps', {})
top_bar = self.config['meta']['safe_margins'].get('top_bar', 0) if mon['y'] == 0 else 0
x, y, w, h = geo['x'], geo['y'], geo['w'], geo['h']
mx, my, mw, mh = mon['x'], mon['y'], mon['w'], mon['h']
def p(v, ref): return int(float(v.strip('%'))/100*ref) if isinstance(v,str) and '%' in v else int(v)
tx, ty = p(x, mw), p(y, mh)
tw, th = p(w, mw), p(h, mh)
# Prevent off-screen push
if (tx + tw) > mw: tw = mw - tx - gaps.get('outer', 0)
if (ty + th) > mh: th = mh - ty - gaps.get('outer', 0)
outer, inner = gaps.get('outer', 0), gaps.get('inner', 0)
pl = outer if (tx <= 2) else inner//2
pr = outer if (tx+tw >= mw-2) else inner//2
pt = outer if (ty <= 2) else inner//2
pb = outer if (ty+th >= mh-2) else inner//2
return mx+tx+pl, my+ty+pt+(top_bar if ty<=2 else 0), tw-(pl+pr), th-(pt+pb)-(top_bar if ty<=2 else 0)
def _refresh_id(self, ref_id):
# Refresh logic for unstable IDs (Slicer)
app_cfg = self.app_config_map.get(ref_id)
current_windows = self.get_windows()
for wid, info in current_windows.items():
if app_cfg.get('match_class', '').lower() in info.get('wm_class', '').lower():
self.window_map[ref_id] = wid
return wid
return None
def arrange_windows(self):
print("\n--- Phase 2: Arranging Layout ---")
print(" > Waiting 1.75s for apps to settle...")
time.sleep(1.75)
mode = "dual" if self.monitors['count'] > 1 else "single"
print(f" > Layout Mode: {mode.upper()}")
for rule in self.config['layout'].get(mode, []):
ref = rule['ref']
print(f" > Processing rule: {ref}")
# 1. ID Check
if ref not in self.window_map or self.window_map[ref] not in self.get_windows():
if not self._refresh_id(ref):
print(f" [!] Lost window {ref}. Skipping.")
continue
wid = self.window_map[ref]
mon = self.monitors.get(rule.get('monitor', 'large'), self.monitors['large'])
def dbus(method, fmt, *args):
try: self.proxy.call_sync(method, GLib.Variant(f'({fmt})', (wid, *args)), Gio.DBusCallFlags.NONE, -1, None)
except: pass
# 2. Clean State
dbus('Unstick', 'u')
dbus('Unmaximize', 'u')
dbus('Activate', 'u')
time.sleep(0.1)
# 3. Geometry (Only logic remaining)
if 'geo' in rule:
fx, fy, fw, fh = self._apply_gaps(rule['geo'], mon)
print(f" -> Geometry: {fx}x{fy}")
dbus('MoveResize', 'uiiuu', int(fx), int(fy), int(fw), int(fh))
time.sleep(0.1)
dbus('MoveResize', 'uiiuu', int(fx), int(fy), int(fw), int(fh))
# 4. Layer
if rule.get('layer') == 'maximize': dbus('Maximize', 'u')
elif rule.get('layer') == 'top': dbus('Activate', 'u')
if __name__ == "__main__":
if len(sys.argv) < 2: sys.exit(1)
orch = WorkspaceOrchestrator(sys.argv[1])
orch.launch_apps()
orch.arrange_windows()
print("\nDone.")