forked from funkypitt/Tinta4Plus-Universal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeManager.py
More file actions
215 lines (187 loc) · 8.18 KB
/
Copy pathThemeManager.py
File metadata and controls
215 lines (187 loc) · 8.18 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
Copyright (c) 2025 Jon Cox (joncox123). All rights reserved.
WARNING: This software is provided "AS IS", without any warranty of any kind. It may contain bugs or other defects
that result in data loss, corruption, hardware damage or other issues. Use at your own risk.
It may temporarily or permanently render your hardware inoperable.
It may corrupt or damage the Embedded Controller or eInk T-CON controller in your laptop.
The author is not responsible for any damage, data loss or lost productivity caused by use of this software.
By downloading and using this software you agree to these terms and acknowledge the risks involved.
"""
import subprocess
import os
class ThemeManager:
"""Manage GTK theme switching across desktop environments.
Supports GNOME (gsettings) and XFCE (xfconf-query).
Auto-detects the desktop environment at startup.
"""
# Mapping for GNOME color-scheme preference
_GNOME_COLOR_SCHEME = {
'HighContrast': 'default',
'Adwaita-dark': 'prefer-dark',
}
def __init__(self, logger):
self.logger = logger
self.desktop_env = self._detect_desktop_environment()
self.logger.info(f"ThemeManager: desktop={self.desktop_env}")
def _detect_desktop_environment(self):
"""Detect the running desktop environment.
Returns 'gnome', 'cinnamon', 'xfce', 'kde', or 'unknown'.
"""
desktop = os.environ.get('XDG_CURRENT_DESKTOP', '').lower()
if 'cinnamon' in desktop:
return 'cinnamon'
if 'gnome' in desktop or 'ubuntu' in desktop:
return 'gnome'
if 'kde' in desktop:
return 'kde'
if 'xfce' in desktop:
return 'xfce'
return 'unknown'
# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------
def set_theme(self, theme_name):
"""Set the GTK theme.
Args:
theme_name: Theme name (e.g., 'HighContrast', 'Adwaita-dark')
For KDE, mapped to Plasma color schemes.
Returns:
bool: True if successful, False otherwise
"""
if self.desktop_env in ('gnome', 'cinnamon'):
return self._set_gnome_theme(theme_name)
elif self.desktop_env == 'kde':
return self._set_kde_theme(theme_name)
elif self.desktop_env == 'xfce':
return self._set_xfce_theme(theme_name)
else:
self.logger.warning(f"Unknown desktop environment, trying gsettings then xfconf-query")
if self._set_gnome_theme(theme_name):
return True
return self._set_xfce_theme(theme_name)
def get_current_theme(self):
"""Get the current GTK theme name.
Returns:
str: Current theme name, or None if detection failed
"""
if self.desktop_env in ('gnome', 'cinnamon'):
return self._get_gnome_theme()
elif self.desktop_env == 'kde':
return self._get_kde_theme()
elif self.desktop_env == 'xfce':
return self._get_xfce_theme()
else:
theme = self._get_gnome_theme()
if theme:
return theme
return self._get_xfce_theme()
# ------------------------------------------------------------------
# GNOME backend (gsettings)
# ------------------------------------------------------------------
def _set_gnome_theme(self, theme_name):
"""Set theme via gsettings (GNOME / GTK)."""
try:
subprocess.run([
'gsettings', 'set',
'org.gnome.desktop.interface', 'gtk-theme',
theme_name
], check=True, capture_output=True, timeout=5)
# Also set color-scheme if we know the mapping
color_scheme = self._GNOME_COLOR_SCHEME.get(theme_name)
if color_scheme:
subprocess.run([
'gsettings', 'set',
'org.gnome.desktop.interface', 'color-scheme',
color_scheme
], check=True, capture_output=True, timeout=5)
self.logger.info(f"GNOME: switched to {theme_name} theme")
return True
except subprocess.CalledProcessError as e:
self.logger.error(f"Failed to set GNOME theme: {e}")
return False
except FileNotFoundError:
self.logger.error("gsettings not found (not running GNOME?)")
return False
def _get_gnome_theme(self):
"""Get current theme via gsettings."""
try:
result = subprocess.run([
'gsettings', 'get',
'org.gnome.desktop.interface', 'gtk-theme'
], capture_output=True, text=True, check=True, timeout=5)
# gsettings returns value with quotes, e.g. "'Adwaita-dark'"
return result.stdout.strip().strip("'")
except Exception:
return None
# ------------------------------------------------------------------
# KDE backend (plasma-apply-colorscheme)
# ------------------------------------------------------------------
# Map GTK theme names used by the app to Plasma color schemes
_KDE_THEME_MAP = {
'HighContrast': 'BreezeHighContrast',
'Adwaita-dark': 'BreezeDark',
'Adwaita': 'BreezeLight',
}
_KDE_THEME_REVERSE = {v: k for k, v in _KDE_THEME_MAP.items()}
def _set_kde_theme(self, theme_name):
"""Set color scheme via plasma-apply-colorscheme (KDE Plasma)."""
plasma_scheme = self._KDE_THEME_MAP.get(theme_name, theme_name)
try:
subprocess.run([
'plasma-apply-colorscheme', plasma_scheme
], check=True, capture_output=True, timeout=5)
self.logger.info(f"KDE: switched to {plasma_scheme} color scheme")
return True
except subprocess.CalledProcessError as e:
self.logger.error(f"Failed to set KDE color scheme: {e}")
return False
except FileNotFoundError:
self.logger.error("plasma-apply-colorscheme not found (not running KDE Plasma?)")
return False
def _get_kde_theme(self):
"""Get current color scheme via plasma-apply-colorscheme."""
try:
result = subprocess.run([
'plasma-apply-colorscheme', '--list-schemes'
], capture_output=True, text=True, timeout=5)
# Lines with " (current)" suffix indicate the active scheme
for line in result.stdout.splitlines():
line = line.strip()
if line.endswith('(current color scheme)') or line.endswith('(current)'):
scheme = line.rsplit('(', 1)[0].strip().lstrip('* ')
# Map back to GTK name if possible
return self._KDE_THEME_REVERSE.get(scheme, scheme)
return None
except Exception:
return None
# ------------------------------------------------------------------
# XFCE backend (xfconf-query)
# ------------------------------------------------------------------
def _set_xfce_theme(self, theme_name):
"""Set theme via xfconf-query (XFCE)."""
try:
subprocess.run([
'xfconf-query',
'-c', 'xsettings',
'-p', '/Net/ThemeName',
'-s', theme_name
], check=True, capture_output=True, timeout=5)
self.logger.info(f"XFCE: switched to {theme_name} theme")
return True
except subprocess.CalledProcessError as e:
self.logger.error(f"Failed to set XFCE theme: {e}")
return False
except FileNotFoundError:
self.logger.error("xfconf-query not found (not running XFCE?)")
return False
def _get_xfce_theme(self):
"""Get current theme via xfconf-query."""
try:
result = subprocess.run([
'xfconf-query',
'-c', 'xsettings',
'-p', '/Net/ThemeName'
], capture_output=True, text=True, check=True, timeout=5)
return result.stdout.strip()
except Exception:
return None