Skip to content

Commit db4456c

Browse files
authored
Merge pull request #9 from levnikmyskin/main
Add wlrlui -m to automatically match profiles
2 parents fd34db7 + 3652660 commit db4456c

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/wlr_layout_ui/__init__.py

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@
1717
pass
1818

1919

20+
def apply_profile(profile):
21+
load()
22+
p_by_id = {p["uid"]: p for p in profile}
23+
rects = [
24+
(
25+
Rect(-i["x"], i["y"], i["height"], i["width"])
26+
if i.get("transform", 0) in (1, 3, 5, 7)
27+
else Rect(-i["x"], i["y"], i["width"], i["height"])
28+
)
29+
for i in profile
30+
]
31+
32+
for i, di in enumerate(displayInfo):
33+
di.active = p_by_id[di.uid]["active"]
34+
di.transform = p_by_id[di.uid].get("transform", 0)
35+
di.scale = p_by_id[di.uid].get("scale", 1.0)
36+
if di.transform in (1, 3, 5, 7):
37+
rects[i].width, rects[i].height = rects[i].height, rects[i].width
38+
39+
cmd = make_command(displayInfo, rects, not LEGACY)
40+
time.sleep(0.5)
41+
if os.system(cmd):
42+
print("Failed applying the layout")
43+
44+
2045
def main():
2146
if len(sys.argv) > 1:
2247
from .profiles import load_profiles
@@ -26,46 +51,39 @@ def main():
2651
print("")
2752
for p in profiles:
2853
print(f" - {p}")
54+
elif sys.argv[1] == "-m":
55+
load()
56+
current_uids = set(di.uid for di in displayInfo)
57+
for key in sorted(profiles):
58+
prof_uids = {p["uid"] for p in profiles[key]}
59+
# check that the two sets have the same elements
60+
if prof_uids == current_uids:
61+
print(f"Matched profile {key}. Applying it...")
62+
apply_profile(profiles[key])
63+
sys.exit(0)
64+
print(f"No profile found: {sys.argv[1]}")
65+
sys.exit(1)
66+
2967
elif sys.argv[1][0] == "-":
3068
load()
3169
print(
3270
"""With no options, launches the GUI
3371
Options:
3472
-l : list profiles
73+
-m : find a profile that matches the currently plugged display set, and apply it.
74+
No-op if not found; will apply first in alphabetical order if multiple found.
3575
<profile name> : loads a profile
3676
"""
3777
)
38-
3978
else:
4079
reload_pre_commands()
4180
try:
4281
profile = profiles[sys.argv[1]]
4382
except KeyError:
4483
print(f"No such profile: {sys.argv[1]}")
4584
raise SystemExit(1)
46-
load()
47-
p_by_id = {p["uid"]: p for p in profile}
48-
rects = [
49-
(
50-
Rect(-i["x"], i["y"], i["height"], i["width"])
51-
if i.get("transform", 0) in (1, 3, 5, 7)
52-
else Rect(-i["x"], i["y"], i["width"], i["height"])
53-
)
54-
for i in profile
55-
]
56-
57-
for i, di in enumerate(displayInfo):
58-
di.active = p_by_id[di.uid]["active"]
59-
di.transform = p_by_id[di.uid].get("transform", 0)
60-
di.scale = p_by_id[di.uid].get("scale", 1.0)
61-
if di.transform in (1, 3, 5, 7):
62-
rects[i].width, rects[i].height = rects[i].height, rects[i].width
63-
64-
cmd = make_command(displayInfo, rects, not LEGACY)
65-
time.sleep(0.5)
66-
if os.system(cmd):
67-
print("Failed applying the layout")
68-
sys.exit(0)
85+
apply_profile(profile)
86+
return
6987
load()
7088
max_width = int(sum(max(screen.available, key=lambda mode: mode.width).width for screen in displayInfo) // UI_RATIO)
7189
max_height = int(sum(max(screen.available, key=lambda mode: mode.height).height for screen in displayInfo) // UI_RATIO)

0 commit comments

Comments
 (0)