/
SettingsService.cs
180 lines (160 loc) · 5.57 KB
/
SettingsService.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
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace PowerAccent.Core.Services;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Enumerations;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using System.IO.Abstractions;
using System.Text.Json;
public class SettingsService
{
private const string PowerAccentModuleName = "QuickAccent";
private readonly ISettingsUtils _settingsUtils;
private readonly IFileSystemWatcher _watcher;
private readonly object _loadingSettingsLock = new object();
public SettingsService()
{
_settingsUtils = new SettingsUtils();
ReadSettings();
_watcher = Helper.GetFileWatcher(PowerAccentModuleName, "settings.json", () => { ReadSettings(); });
}
private void ReadSettings()
{
// TODO this IO call should by Async, update GetFileWatcher helper to support async
lock (_loadingSettingsLock)
{
{
try
{
if (!_settingsUtils.SettingsExists(PowerAccentModuleName))
{
Logger.LogInfo("QuickAccent settings.json was missing, creating a new one");
var defaultSettings = new PowerAccentSettings();
var options = new JsonSerializerOptions
{
WriteIndented = true,
};
_settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), PowerAccentModuleName);
}
var settings = _settingsUtils.GetSettingsOrDefault<PowerAccentSettings>(PowerAccentModuleName);
if (settings != null)
{
ActivationKey = settings.Properties.ActivationKey;
switch (settings.Properties.ToolbarPosition.Value)
{
case "Top center":
Position = Position.Top;
break;
case "Bottom center":
Position = Position.Bottom;
break;
case "Left":
Position = Position.Left;
break;
case "Right":
Position = Position.Right;
break;
case "Top right corner":
Position = Position.TopRight;
break;
case "Top left corner":
Position = Position.TopLeft;
break;
case "Bottom right corner":
Position = Position.BottomRight;
break;
case "Bottom left corner":
Position = Position.BottomLeft;
break;
case "Center":
Position = Position.Center;
break;
}
}
}
catch (Exception ex)
{
Logger.LogError("Failed to read changed settings", ex);
}
}
}
}
private PowerAccentActivationKey _activationKey = PowerAccentActivationKey.Both;
public PowerAccentActivationKey ActivationKey
{
get
{
return _activationKey;
}
set
{
_activationKey = value;
}
}
private Position _position = Position.Top;
public Position Position
{
get
{
return _position;
}
set
{
_position = value;
}
}
private int _inputTime = 200;
public int InputTime
{
get
{
return _inputTime;
}
set
{
_inputTime = value;
}
}
public char[] GetLetterKey(LetterKey letter)
{
return GetDefaultLetterKey(letter);
}
public static char[] GetDefaultLetterKey(LetterKey letter)
{
switch (letter)
{
case LetterKey.A:
return new char[] { 'à', 'â', 'á', 'ä', 'ã', 'å', 'æ' };
case LetterKey.C:
return new char[] { 'ć', 'ĉ', 'č', 'ċ', 'ç', 'ḉ' };
case LetterKey.E:
return new char[] { 'é', 'è', 'ê', 'ë', 'ē', 'ė', '€' };
case LetterKey.I:
return new char[] { 'î', 'ï', 'í', 'ì', 'ī' };
case LetterKey.N:
return new char[] { 'ñ', 'ń' };
case LetterKey.O:
return new char[] { 'ô', 'ö', 'ó', 'ò', 'õ', 'ø', 'œ' };
case LetterKey.S:
return new char[] { 'š', 'ß', 'ś' };
case LetterKey.U:
return new char[] { 'û', 'ù', 'ü', 'ú', 'ū' };
case LetterKey.Y:
return new char[] { 'ÿ', 'ý' };
}
throw new ArgumentException("Letter {0} is missing", letter.ToString());
}
}
public enum Position
{
Top,
Bottom,
Left,
Right,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Center,
}