-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathset-dark-theme-variant.py
85 lines (66 loc) · 2.51 KB
/
set-dark-theme-variant.py
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
#!/usr/bin/env python3
import Xlib
import Xlib.display
import time
import subprocess
import os
import sys
disp = Xlib.display.Display()
root = disp.screen().root
NET_CLIENT_LIST = disp.intern_atom('_NET_CLIENT_LIST')
def set_theme_variant_by_window_id(id, variant):
# Use subprocess to call
# xprop and set the variant from id.
try:
s = subprocess.call(['xprop', '-f', '_GTK_THEME_VARIANT', '8u', '-set', '_GTK_THEME_VARIANT', variant, '-id', str(id)],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if s == 0:
return True
return False
except Exception as ex:
return False
def set_theme_variant_from_win_id_collection(win_id_collection, variant):
# Loop though all of the collected
# window ids and set theme variant
for win_id in win_id_collection:
set_theme_variant_by_window_id(win_id, variant)
def collection_win_id_from_wm_class_name(win_class_name):
collect = []
# Loop though all of the windows
# and collect id's those that match
# win_class: prusa-slicer
for win_id in root.get_full_property(NET_CLIENT_LIST, Xlib.X.AnyPropertyType).value:
try:
win = disp.create_resource_object('window', win_id)
if not win.get_wm_transient_for():
win_class = win.get_wm_class()
if win_id and win_class_name in win_class:
collect.append(
win_id) if win_id not in collect else collect
except Xlib.error.BadWindow:
pass
return collect
if __name__ == '__main__':
if os.environ.get('PRUSA_SLICER_DARK_THEME', 'false') != 'true':
sys.exit(0)
# Listen for X Property Change events.
root.change_attributes(event_mask=Xlib.X.PropertyChangeMask)
# the class name of the slicer window
win_class_name = 'prusa-slicer'
# the variant to set
variant = 'dark'
start = time.time()
while True:
# collect all of the window ids
collect = collection_win_id_from_wm_class_name(win_class_name)
# give PrusaSlicer window 2 secs to
# collect the wanted window ids
# set the theme variant and exit
if time.time() - start <= 2:
# disp.next_event() blocks if no events are
# queued. In combination with while True
# it creates a very simple event loop.
disp.next_event()
set_theme_variant_from_win_id_collection(collect, variant)
else:
break