-
Notifications
You must be signed in to change notification settings - Fork 7
/
Settings.cs
333 lines (291 loc) · 10.6 KB
/
Settings.cs
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
using Microsoft.Win32;
using PaintDotNet;
using PaintDotNet.Effects;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace PdnCodeLab
{
internal static class Settings
{
private static RegistryKey regKey = null;
private static readonly string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
private static readonly string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
internal static bool Bookmarks
{
get => GetRegValue("Bookmarks", false);
set => SetRegValue("Bookmarks", value);
}
internal static bool CheckForUpdates
{
get => GetRegValue("CheckForUpdates", false);
set => SetRegValue("CheckForUpdates", value);
}
internal static bool CodeFolding
{
get => GetRegValue("CodeFolding", false);
set => SetRegValue("CodeFolding", value);
}
internal static bool DisableAutoComplete
{
get => GetRegValue("DisableAutoComplete", false);
set => SetRegValue("DisableAutoComplete", value);
}
internal static Theme EditorTheme
{
get => GetRegValue("EditorTheme", Theme.Auto);
set => SetRegValue("EditorTheme", value);
}
internal static bool ErrorBox
{
get => GetRegValue("ErrorBox", false);
set => SetRegValue("ErrorBox", value);
}
internal static string FontFamily
{
get
{
string fontFamily = GetRegValue("FontFamily", "Cascadia Mono");
if (UIUtil.IsFontInstalled(fontFamily))
{
return fontFamily;
}
string[] fallbackFonts = ["Cascadia Mono", "Consolas", "Courier New"];
return fallbackFonts.FirstOrDefault(font => UIUtil.IsFontInstalled(font), "Verdana");
}
set => SetRegValue("FontFamily", value);
}
internal static bool LargeFonts
{
get => GetRegValue("LargeFonts", false);
set => SetRegValue("LargeFonts", value);
}
internal static string LastSlnDirectory
{
get => GetRegValue("LastSlnDir", documentsPath);
set => SetRegValue("LastSlnDir", value);
}
internal static string LastSourceDirectory
{
get => GetRegValue("LastSourceDir", desktopPath);
set => SetRegValue("LastSourceDir", value);
}
internal static DateTime LatestUpdateCheck
{
get
{
string regValue = GetRegValue("LatestUpdateCheck", string.Empty);
if (DateTime.TryParseExact(regValue, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime))
{
return dateTime;
}
else
{
string newRegValue = DateTime.Now.ToString("yyyy-MM-dd");
SetRegValue("LatestUpdateCheck", newRegValue);
return DateTime.Now - TimeSpan.FromDays(8);
}
}
set
{
string regValue = value.ToString("yyyy-MM-dd");
SetRegValue("LatestUpdateCheck", regValue);
}
}
internal static bool LineNumbers
{
get => GetRegValue("LineNumbers", false);
set => SetRegValue("LineNumbers", value);
}
internal static bool Map
{
get => GetRegValue("Map", false);
set => SetRegValue("Map", value);
}
internal static bool Output
{
get => GetRegValue("Output", false);
set => SetRegValue("Output", value);
}
internal static string RecentDocs
{
get => GetRegValue("RecentDocs", string.Empty);
set => SetRegValue("RecentDocs", value);
}
internal static bool ToolBar
{
get => GetRegValue("ToolBar", true);
set => SetRegValue("ToolBar", value);
}
internal static bool WhiteSpace
{
get => GetRegValue("WhiteSpace", false);
set => SetRegValue("WhiteSpace", value);
}
internal static bool WordWrap
{
get => GetRegValue("WordWrap", false);
set => SetRegValue("WordWrap", value);
}
internal static bool WordWrapPlainText
{
get => GetRegValue("WordWrapPlainText", true);
set => SetRegValue("WordWrapPlainText", value);
}
internal static string Snippets
{
get => GetRegValue("Snippets", string.Empty);
set => SetRegValue("Snippets", value);
}
internal static int WarningLevel
{
get => GetRegValue("WarningLevel", 4);
set => SetRegValue("WarningLevel", value);
}
internal static IEnumerable<string> WarningsToIgnore
{
get => GetRegValue("WarningsToIgnore", "CS0414").Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
set => SetRegValue("WarningsToIgnore", value.Join("|"));
}
internal static int IndentSpaces
{
get => GetRegValue("IndentSpaces", 4);
set => SetRegValue("IndentSpaces", value);
}
internal static bool CaretLineFrame
{
get => GetRegValue("CaretLineFrame", false);
set => SetRegValue("CaretLineFrame", value);
}
internal static bool ExtendedColors
{
get => GetRegValue("ExtendedColors", false);
set => SetRegValue("ExtendedColors", value);
}
internal static bool Spellcheck
{
get => GetRegValue("Spellcheck", false);
set => SetRegValue("Spellcheck", value);
}
internal static string SpellingLang
{
get => GetRegValue("SpellingLang", CultureInfo.CurrentUICulture.Name);
set => SetRegValue("SpellingLang", value);
}
internal static IEnumerable<string> SpellingWordsToIgnore
{
get => GetRegValue("SpellingWordsToIgnore", "Desc").Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
set => SetRegValue("SpellingWordsToIgnore", value.Join("|"));
}
internal static RenderPreset RenderPreset
{
get => GetRegValue(nameof(RenderPreset), RenderPreset.Regular);
set => SetRegValue(nameof(RenderPreset), value);
}
internal static BitmapEffectRenderingFlags RenderingFlags
{
get => GetRegValue(nameof(RenderingFlags), BitmapEffectRenderingFlags.None);
set => SetRegValue(nameof(RenderingFlags), value);
}
internal static BitmapEffectRenderingSchedule RenderingSchedule
{
get => GetRegValue(nameof(RenderingSchedule), BitmapEffectRenderingSchedule.SquareTiles);
set => SetRegValue(nameof(RenderingSchedule), value);
}
internal static DocCommentOptions DocCommentOptions
{
get => GetRegValue(nameof(DocCommentOptions), DocCommentOptions.Default);
set => SetRegValue(nameof(DocCommentOptions), value);
}
private static void OpenRegKey()
{
if (regKey == null)
{
regKey = Registry.CurrentUser.OpenSubKey("Software\\CodeLab", true);
if (regKey == null)
{
Registry.CurrentUser.CreateSubKey("Software\\CodeLab").Flush();
regKey = Registry.CurrentUser.OpenSubKey("Software\\CodeLab", true);
}
}
}
private static T GetRegValue<T>(string valueName, T defaultValue)
{
if (regKey == null)
{
OpenRegKey();
}
if (defaultValue is bool b)
{
int defaultInt = b ? 1 : 0;
int regValue = (int)regKey.GetValue(valueName, defaultInt);
object boolObj = regValue == 1;
return (T)boolObj;
}
else if (defaultValue is int integer)
{
return (T)regKey.GetValue(valueName, integer);
}
else if (defaultValue is string str)
{
return (T)regKey.GetValue(valueName, str);
}
else if (defaultValue is Enum)
{
Type enumType = defaultValue.GetType();
Type underlyingType = Enum.GetUnderlyingType(enumType);
object defaultValueAsNum = Convert.ChangeType(defaultValue, underlyingType);
object regValue = regKey.GetValue(valueName, defaultValueAsNum);
object regValueAsNum = Convert.ChangeType(regValue, underlyingType);
return (T)regValueAsNum;
}
else
{
throw new NotImplementedException();
}
}
private static void SetRegValue<T>(string valueName, T value)
{
if (regKey == null)
{
OpenRegKey();
}
if (value is bool b)
{
int regValue = b ? 1 : 0;
regKey.SetValue(valueName, regValue, RegistryValueKind.DWord);
}
else if (value is int integer)
{
regKey.SetValue(valueName, integer, RegistryValueKind.DWord);
}
else if (value is string str)
{
regKey.SetValue(valueName, str, RegistryValueKind.String);
}
else if (value is Enum)
{
Type enumType = value.GetType();
Type underlyingType = Enum.GetUnderlyingType(enumType);
object valueAsNum = Convert.ChangeType(value, underlyingType);
RegistryValueKind valueKind = (underlyingType == typeof(ulong) || underlyingType == typeof(long)) ? RegistryValueKind.QWord : RegistryValueKind.DWord;
regKey.SetValue(valueName, valueAsNum, valueKind);
}
else
{
throw new NotImplementedException();
}
regKey.Flush();
}
internal static void CloseRegKey()
{
if (regKey != null)
{
regKey.Close();
regKey.Dispose();
regKey = null;
}
}
}
}