forked from facelessuser/Aprosopo
/
set_theme.py
executable file
·461 lines (361 loc) · 14.6 KB
/
set_theme.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""
Module for setting theme options.
Copyright (c) 2014 - 2015 Isaac Muse <isaacmuse@gmail.com>
License: MIT
"""
import sublime
import sublime_plugin
import os
# General settings
STV = int(sublime.version())
ST3 = int(STV) >= 3000
ST2 = 3000 < int(STV) >= 2000
PREFERENCES = "Preferences.sublime-settings"
# Theme specific settings
PLUGIN_SETTINGS = "Theme - Aprosopo.sublime-settings"
COMMON_FEATURES = [
"aprosopo_active_bar",
"aprosopo_solid_tab",
"aprosopo_active_text",
"aprosopo_dirty_bar",
"aprosopo_dirty_button",
"aprosopo_no_file_icons",
"aprosopo_show_tab_close_buttons",
"aprosopo_show_tab_close_on_hover",
"aprosopo_hide_folder_expand_icon",
"aprosopo_hide_open_file_icons",
"aprosopo_combined_dirty_active_bar"
]
SIDEBAR_SIZES = ["xsmall", "small", "medium", "large", "xlarge"]
SIDEBAR_FONT_SIZES = ["xsmall", "small", "medium", "large", "xlarge"]
SIDEBAR_COMMON_FEATURE = "aprosopo_sidebar_tree_%s"
SIDEBAR_FONT_COMMON_FEATURE = "aprosopo_sidebar_font_%s"
def get_theme(obj, default=None):
"""
Get the theme for the theme object.
See if ST2 or ST3 variant is avaliable,
if not use the standard theme format.
"""
special = "@st3" if ST3 else "@st2"
theme = obj.get("theme", None)
if theme is None:
theme = default
elif ST3:
parts = os.path.splitext(theme)
special_theme = parts[0] + special + parts[1]
resources = sublime.find_resources(special_theme)
for r in resources:
if r == "Packages/Theme - Aprosopo/%s" % special_theme:
theme = special_theme
break
else:
parts = os.path.splitext(theme)
special_theme = parts[0] + special + parts[1]
pkgs = sublime.packages_path()
resource = os.path.join(pkgs, "Theme - Aprosopo", special_theme)
if os.path.exists(resource):
theme = special_theme
return theme
def is_valid_theme(theme, theme_file, check_all=False):
"""Check if the theme is a valid variant (st2 or st3 specfic, and default format)."""
valid = False
if theme is not None and theme_file is not None:
special = "@st3" if ST3 else "@st2"
parts = os.path.splitext(theme_file)
valid_themes = [theme_file, parts[0] + special + parts[1]]
if check_all:
valid_themes.append(parts[0] + ("@st3" if not ST3 else "@st2") + parts[1])
if theme in valid_themes:
valid = True
return valid
def clear_all_theme_colors(pref, themes, color_list_key, color_key):
"""Clear theme color variant settings."""
for v in themes.values():
for c in v.get(color_list_key, []):
key = v.get(color_key, None)
if key is not None:
pref.erase(key % c)
def clear_all_themes(pref, themes):
"""Clear theme from settings."""
for v in themes.values():
theme = v.get("theme")
if theme is not None:
if is_valid_theme(pref.get("theme", None), theme):
pref.erase("theme")
def detect_current_theme(pref, themes):
"""Detect the current theme."""
detected = None
for k, v in themes.items():
theme = v.get("theme")
if theme is not None:
theme_name = pref.get("theme", None)
if is_valid_theme(theme_name, theme):
detected = k
break
return detected
def clear_all_sizes(pref, sizes, feature):
"""Clear all sizes for feature."""
for s in sizes:
pref.erase(feature % s)
def clear_all_widgets(themes):
"""Remove theme widget settings."""
widget_path = os.path.join(sublime.packages_path(), "User")
for v in themes.values():
widget = v.get("widget_settings")
if widget is not None:
special = "@st3" if ST3 else "@st2"
parts = os.path.splitext(widget)
widget_names = [widget, parts[0] + special + parts[1]]
for w in widget_names:
widget_file = os.path.join(widget_path, w)
if os.path.exists(widget_file):
os.remove(widget_file)
def clear_all_features(pref, themes):
"""Clear other theme feature settings."""
for feat in COMMON_FEATURES:
pref.erase(feat)
for v in themes.values():
for feat in v.get("theme_specific_keys", []):
pref.erase(feat)
class ClearAprosopoThemeCommand(sublime_plugin.ApplicationCommand):
"""Clear Aprosopo theme settings."""
def run(self):
"""Clear all settings."""
pref = sublime.load_settings(PREFERENCES)
plug = sublime.load_settings(PLUGIN_SETTINGS)
themes = plug.get("themes", {})
clear_all_themes(pref, themes)
clear_all_theme_colors(pref, themes, "colors", "color_key")
clear_all_theme_colors(pref, themes, "dirty_colors", "dirty_color_key")
clear_all_sizes(pref, SIDEBAR_SIZES, SIDEBAR_COMMON_FEATURE)
clear_all_sizes(pref, SIDEBAR_FONT_SIZES, SIDEBAR_FONT_COMMON_FEATURE)
clear_all_features(pref, themes)
clear_all_widgets(themes)
sublime.save_settings(PREFERENCES)
class SetAprosopoThemeCommand(sublime_plugin.ApplicationCommand):
"""Set various Aprosopo theme settings."""
def run(self, color, theme):
"""Setup and set the specified theme."""
# Get needed theme attributes etc.
pref = sublime.load_settings(PREFERENCES)
plug = sublime.load_settings(PLUGIN_SETTINGS)
themes = plug.get("themes", {})
# Inherit color from alternate theme if possible
current = detect_current_theme(pref, themes)
colors = themes.get(theme, {}).get("colors", [])
theme_file = get_theme(themes.get(theme, {}), None)
widget_settings = themes.get(theme, {}).get("widget_settings", None)
widget_scheme = themes.get(theme, {}).get("widget_scheme", None)
if theme_file.replace(".sublime-theme", '').endswith(("@st3", "@st2")):
special = "@st3" if ST3 else "@st2"
parts = os.path.splitext(widget_settings)
widget_settings = parts[0] + special + parts[1]
color_key = themes.get(theme, {}).get("color_key", None)
# See if it is okay to continue
if (
color not in colors or color_key is None or widget_scheme is None or
theme_file is None or widget_settings is None
):
return
# Setup theme
pref.set("theme", theme_file)
clear_all_theme_colors(pref, themes, "colors", "color_key")
pref.set(color_key % color, True)
sublime.save_settings(PREFERENCES)
# Setup theme widget
widget = sublime.load_settings(widget_settings)
widget.set("color_scheme", widget_scheme % color)
widget.set("draw_shadows", False)
sublime.save_settings(widget_settings)
self.set_theme_color(current, theme)
def set_theme_color(self, current_theme, new_theme):
"""Set the theme color."""
if current_theme is not None and current_theme != new_theme:
sublime.run_command(
"inherhit_aprosopo_dirty_color",
{"old_theme": current_theme, "new_theme": new_theme}
)
elif current_theme is None:
sublime.run_command(
"set_aprosopo_theme_dirty",
{"color": "red", "theme": new_theme}
)
def is_checked(self, color, theme):
"""Should menu option be check marked?."""
plug = sublime.load_settings(PLUGIN_SETTINGS)
themes = plug.get("themes", {})
colors = themes.get(theme, {}).get("colors", [])
color_key = themes.get(theme, {}).get("color_key", None)
pref = sublime.load_settings(PREFERENCES)
if color_key is None or color not in colors:
return False
return pref.get(color_key % color, False) is True
class InherhitAprosopoDirtyColorCommand(sublime_plugin.ApplicationCommand):
"""Inherit the dirty color from light or dark when switching between them."""
def run(self, old_theme, new_theme):
"""Run command."""
# Get needed theme attributes etc.
pref = sublime.load_settings(PREFERENCES)
plug = sublime.load_settings(PLUGIN_SETTINGS)
themes = plug.get("themes", {})
old_colors = themes.get(old_theme, {}).get("dirty_colors", [])
new_colors = themes.get(new_theme, {}).get("dirty_colors", [])
old_dirty_key = themes.get(old_theme, {}).get("dirty_color_key", None)
new_dirty_key = themes.get(new_theme, {}).get("dirty_color_key", None)
for color in old_colors:
if pref.get(old_dirty_key % color, False):
if color in new_colors:
pref.erase(old_dirty_key % color)
pref.set(new_dirty_key % color, True)
sublime.save_settings(PREFERENCES)
break
class SetAprosopoThemeDirtyCommand(sublime_plugin.ApplicationCommand):
"""Set the dirty theme setting."""
def run(self, color, theme):
"""Run command."""
# Get needed theme attributes etc.
pref = sublime.load_settings(PREFERENCES)
plug = sublime.load_settings(PLUGIN_SETTINGS)
themes = plug.get("themes", {})
colors = themes.get(theme, {}).get("dirty_colors", [])
theme_file = get_theme(themes.get(theme, {}), None)
dirty_key = themes.get(theme, {}).get("dirty_color_key", None)
# See if it is okay to continue
if (
color not in colors or dirty_key is None or
theme_file is None or not (pref.get("theme", None) == theme_file)
):
return
# Set dirty color
clear_all_theme_colors(pref, themes, "dirty_colors", "dirty_color_key")
pref.set(dirty_key % color, True)
sublime.save_settings(PREFERENCES)
def is_checked(self, color, theme):
"""Check if menu option be checkmarked."""
plug = sublime.load_settings(PLUGIN_SETTINGS)
themes = plug.get("themes", {})
colors = themes.get(theme, {}).get("dirty_colors", [])
theme_file = get_theme(themes.get(theme, {}), None)
dirty_key = themes.get(theme, {}).get("dirty_color_key", None)
pref = sublime.load_settings(PREFERENCES)
if dirty_key is None or (pref.get("theme", None) != theme_file):
return False
if color not in colors:
return False
pref = sublime.load_settings(PREFERENCES)
return pref.get(dirty_key % color, False) is True
class _SetAprosopoSizeFeature(sublime_plugin.ApplicationCommand):
"""Set Arposopo size."""
sizes = []
size_key = "%s"
def run(self, size):
"""Set size."""
pref = sublime.load_settings(PREFERENCES)
if size not in self.sizes:
return
for s in self.sizes:
pref.erase(self.size_key % s)
pref.set(self.size_key % size, True)
sublime.save_settings(PREFERENCES)
def is_checked(self, size):
"""Check if menu option be checkmarked."""
if size not in self.sizes:
return False
pref = sublime.load_settings(PREFERENCES)
return pref.get(self.size_key % size, False) is True
class SetAprosopoThemeSidebarSizeCommand(_SetAprosopoSizeFeature):
"""Configure sidebar size."""
sizes = SIDEBAR_SIZES
size_key = SIDEBAR_COMMON_FEATURE
class SetAprosopoThemeSidebarFontSizeCommand(_SetAprosopoSizeFeature):
"""Configure sidebar font size."""
sizes = SIDEBAR_FONT_SIZES
size_key = SIDEBAR_FONT_COMMON_FEATURE
class ToggleAprosopoThemeFeatureCommand(sublime_plugin.ApplicationCommand):
"""Toggle various Aprosopo theme features."""
def run(
self, feature,
set_when_true=[], set_when_false=[],
unset_when_true=[], unset_when_false=[],
st_version=0
):
"""Toggle feature true or false (when false, the setting is erased)."""
self.pref = sublime.load_settings(PREFERENCES)
self.modified = False
if feature is not None:
state = self.pref.get(feature, None)
self.handle_dependants(
set_when_true, set_when_false,
unset_when_true, unset_when_false, state
)
self.toggle_feature(feature, state)
if self.modified:
sublime.save_settings(PREFERENCES)
def toggle_feature(self, feature, state):
"""Toggle a feature."""
if state is None or state is False:
self.pref.set(feature, True)
self.modified = True
elif state is not None:
self.pref.erase(feature)
self.modified = True
def set_feature(self, feature, value):
"""Set the feature."""
state = self.pref.get(feature, None)
if value:
if state is None or state is False:
self.pref.set(feature, True)
self.modified = True
elif state is not None:
self.pref.erase(feature)
self.modified = True
def handle_dependants(
self, set_when_true, set_when_false,
unset_when_true, unset_when_false, state
):
"""Handle dependants."""
if state is None or state is False:
for feature in set_when_true:
self.set_feature(feature, True)
for feature in unset_when_true:
self.set_feature(feature, False)
elif state is not None:
for feature in set_when_false:
self.set_feature(feature, True)
for feature in unset_when_false:
self.set_feature(feature, False)
def is_visible(
self, feature,
set_when_true=[], set_when_false=[],
unset_when_true=[], unset_when_false=[],
st_version=0
):
"""Show option if ST version matches."""
if isinstance(st_version, list):
size = len(st_version)
if size > 2:
st_version = st_version[:2]
elif size < 2:
st_version += [None] * (2 - size)
minimum, maximum = st_version
else:
minimum = st_version
maximum = None
return (
STV == 0 or
(
minimum <= STV and
(
maximum is None or STV <= maximum
)
)
)
def is_checked(
self, feature,
set_when_true=[], set_when_false=[],
unset_when_true=[], unset_when_false=[],
st_version=0
):
"""Determine if the menu option be checkmarked."""
pref = sublime.load_settings(PREFERENCES)
return pref.get(feature, False) is True