-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathpreferences.py
243 lines (197 loc) · 9.95 KB
/
preferences.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
# If you want a different preferences folder - Comment the lines marked #default and uncomment the lines marked with # custom
import os
from talon import ui
import copy
from .configuration import hud_get_configuration
user_preferences_file_dir = hud_get_configuration("user_preferences_folder")
widget_settings_file_ending = "widget_settings.csv"
user_preferences_file_location = os.path.join(user_preferences_file_dir, widget_settings_file_ending)
# Loads and persists all the data based on the users preferences
# To keep the display state consistent across sessions
class HeadUpDisplayUserPreferences:
persisting_enabled = False
monitor_file_path = None
default_prefs = {
"version": "0.6",
"show_animations": True,
"enabled": False,
"auto_focus": False,
"theme_name": "light",
}
# Keep the base preferences available as well
# To make sure we can keep HUD environments as specific as possible
base_prefs = {}
prefs = {}
content_prefs = {}
monitor_related_pref_endings = ("_x", "_y", "_width", "_height",
"_limit_x", "_limit_y", "_limit_width", "_limit_height",
"_font_size", "_alignment", "_expand_direction")
boolean_keys = ["enabled", "show_animations", "auto_focus"]
hud_environment = ""
def __init__(self, hud_environment = "", hud_version = 5):
self.hud_environment = hud_environment
self.load_preferences(self.get_screen_preferences_filepath(ui.screens()))
self.hud_version = hud_version
def set_hud_environment(self, hud_environment):
self.hud_environment = hud_environment
def enable(self):
self.persisting_enabled = True
def disable(self):
self.persisting_enabled = False
def get_watch_directories(self):
screens = ui.screens()
watch_list = [self.get_main_preferences_filename(), self.get_screen_preferences_filepath(screens)]
if self.hud_environment:
temp_environment = self.hud_environment
self.hud_environment = ""
watch_list.extend([self.get_main_preferences_filename(), self.get_screen_preferences_filepath(screens)])
self.hud_environment = temp_environment
return watch_list
# Get the preferences filename for the current monitor dimensions
def get_screen_preferences_filepath(self, screens):
hud_environment = self.hud_environment
talon_hud_environment = "" if hud_environment == None or hud_environment == "" else hud_environment + "_"
preferences_title = talon_hud_environment + "monitor"
for screen in screens:
preferences_postfix = []
preferences_postfix.append(str(int(screen.x)))
preferences_postfix.append(str(int(screen.y)))
preferences_postfix.append(str(int(screen.width)))
preferences_postfix.append(str(int(screen.height)))
preferences_title += "(" + "_".join(preferences_postfix) + ")"
preferences_title += ".csv"
return os.path.join(user_preferences_file_dir, preferences_title)
def load_default_preferences(self):
preferences = {}
for key, value in self.default_prefs.items():
preferences[key] = value
if self.hud_environment != "":
lines = []
real_hud_environment = self.hud_environment
self.hud_environment = ""
monitor_file_path = self.get_screen_preferences_filepath(ui.screens())
file_path = self.get_main_preferences_filename()
if os.path.exists(file_path):
fh = open(file_path, "r")
lines.extend(fh.readlines())
fh.close()
if monitor_file_path is not None:
if os.path.exists(monitor_file_path):
fh = open(monitor_file_path, "r")
lines.extend(fh.readlines())
fh.close()
preferences = {}
for key, value in self.default_prefs.items():
preferences[key] = value
# Clear context_menu and walk_through stuff so the new sizes can be applied
migrate_v05 = False
# Override defaults with file values
for index,line in enumerate(lines):
split_line = line.strip("\n").split(",", 1)
key = split_line[0]
if not migrate_v05 and key.startswith("walk_through"):
migrate_v05 = True
value = split_line[1]
if (key in self.boolean_keys):
preferences[key] = True if int(value) > 0 else False
elif value is not None:
preferences[key] = value
self.hud_environment = real_hud_environment
if migrate_v05:
keys = list(preferences.keys())
for key in keys:
if key.startswith("context_menu") or key.startswith("walk_through"):
del preferences[key]
self.base_prefs = preferences
return copy.copy(preferences)
def load_preferences(self, monitor_file_path=None):
# Copy over defaults first
preferences = self.load_default_preferences()
file_path = self.get_main_preferences_filename()
lines = []
if os.path.exists(file_path):
fh = open(file_path, "r")
lines.extend(fh.readlines())
fh.close()
if monitor_file_path is not None:
self.monitor_file_path = monitor_file_path
if os.path.exists(monitor_file_path):
fh = open(monitor_file_path, "r")
lines.extend(fh.readlines())
fh.close()
# Clear context_menu and walk_through stuff so the new sizes can be applied
migrate_v05 = False
# Override defaults with file values
for index,line in enumerate(lines):
split_line = line.strip("\n").split(",", 1)
key = split_line[0]
if not migrate_v05 and key.startswith("walk_through"):
migrate_v05 = True
value = split_line[1]
if (key in self.boolean_keys):
preferences[key] = True if int(value) > 0 else False
elif value is not None:
preferences[key] = value
if migrate_v05:
keys = list(preferences.keys())
for key in keys:
if key.startswith("context_menu") or key.startswith("walk_through"):
del preferences[key]
self.prefs = preferences
if self.hud_environment == "":
self.base_prefs = copy.copy(preferences)
# Persist the preferences as a CSV for reloading between Talon sessions later
# But only when the new preferences have changed
def persist_preferences(self, new_preferences, force=False):
if not self.persisting_enabled:
return
preferences_changed = False
monitor_changed = False
for key, value in new_preferences.items():
if (key not in self.prefs or value != self.prefs[key]):
if key.endswith(self.monitor_related_pref_endings):
monitor_changed = True
else:
preferences_changed = True
self.prefs[key] = value
if preferences_changed or force:
self.save_preferences_file(self.get_main_preferences_filename())
if (monitor_changed or force) and self.monitor_file_path is not None:
self.save_preferences_file(self.monitor_file_path)
def get_main_preferences_filename(self, without_hud_environment = False):
hud_environment = self.hud_environment
talon_hud_environment = "" if without_hud_environment or hud_environment == None or hud_environment == "" else hud_environment + "_"
return os.path.join(user_preferences_file_dir, talon_hud_environment + widget_settings_file_ending)
# Save the given preferences file
def save_preferences_file(self, filename):
is_monitor_preference = filename != self.get_main_preferences_filename()
# Transform data before persisting
lines = []
for index, key in enumerate(self.prefs):
if ( is_monitor_preference and key.endswith(self.monitor_related_pref_endings) ) or \
( not is_monitor_preference and not key.endswith(self.monitor_related_pref_endings) ):
# Skip persisting the initial current topics if the file does not exist yet to allow
# The topics to by migrated properly instead of being cleared on the first load
if key.endswith("_current_topics") and not os.path.exists(filename):
continue
# Skip values that are the same in the base environment preferences
# To keep the changes as specific as possible to each environment
# Unless they are content related
if self.hud_environment != "" and not key.endswith("_current_topics") and key in self.base_prefs and self.base_prefs[key] == self.prefs[key]:
continue
value = self.prefs[key]
transformed_value = value
line = key + ","
if (key in self.boolean_keys):
transformed_value = "1" if value else "0"
elif value is None:
continue
line = line + transformed_value
# Only add new lines to non-final rows
if index + 1 != len(self.prefs):
line = line + "\n"
lines.append(line)
if len(lines) > 0:
fh = open(filename, "w")
fh.write("".join(lines))
fh.close()