-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathPowerAccentViewModel.cs
More file actions
389 lines (338 loc) · 16.1 KB
/
PowerAccentViewModel.cs
File metadata and controls
389 lines (338 loc) · 16.1 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
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
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Enumerations;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class PowerAccentViewModel : Observable
{
private GeneralSettings GeneralSettingsConfig { get; set; }
private readonly PowerAccentSettings _powerAccentSettings;
private readonly SettingsUtils _settingsUtils;
private const string SpecialGroup = "QuickAccent_Group_Special";
private const string LanguageGroup = "QuickAccent_Group_Language";
public List<PowerAccentLanguageModel> Languages { get; } = [
new PowerAccentLanguageModel("SPECIAL", "QuickAccent_SelectedLanguage_Special", SpecialGroup),
new PowerAccentLanguageModel("BG", "QuickAccent_SelectedLanguage_Bulgarian", LanguageGroup),
new PowerAccentLanguageModel("CA", "QuickAccent_SelectedLanguage_Catalan", LanguageGroup),
new PowerAccentLanguageModel("CRH", "QuickAccent_SelectedLanguage_Crimean", LanguageGroup),
new PowerAccentLanguageModel("CUR", "QuickAccent_SelectedLanguage_Currency", SpecialGroup),
new PowerAccentLanguageModel("HR", "QuickAccent_SelectedLanguage_Croatian", LanguageGroup),
new PowerAccentLanguageModel("CZ", "QuickAccent_SelectedLanguage_Czech", LanguageGroup),
new PowerAccentLanguageModel("DK", "QuickAccent_SelectedLanguage_Danish", LanguageGroup),
new PowerAccentLanguageModel("GA", "QuickAccent_SelectedLanguage_Gaeilge", LanguageGroup),
new PowerAccentLanguageModel("GD", "QuickAccent_SelectedLanguage_Gaidhlig", LanguageGroup),
new PowerAccentLanguageModel("NL", "QuickAccent_SelectedLanguage_Dutch", LanguageGroup),
new PowerAccentLanguageModel("EL", "QuickAccent_SelectedLanguage_Greek", LanguageGroup),
new PowerAccentLanguageModel("GRC", "QuickAccent_SelectedLanguage_Greek_Polytonic", LanguageGroup),
new PowerAccentLanguageModel("EST", "QuickAccent_SelectedLanguage_Estonian", LanguageGroup),
new PowerAccentLanguageModel("EPO", "QuickAccent_SelectedLanguage_Esperanto", LanguageGroup),
new PowerAccentLanguageModel("FI", "QuickAccent_SelectedLanguage_Finnish", LanguageGroup),
new PowerAccentLanguageModel("FR", "QuickAccent_SelectedLanguage_French", LanguageGroup),
new PowerAccentLanguageModel("DE", "QuickAccent_SelectedLanguage_German", LanguageGroup),
new PowerAccentLanguageModel("HE", "QuickAccent_SelectedLanguage_Hebrew", LanguageGroup),
new PowerAccentLanguageModel("HU", "QuickAccent_SelectedLanguage_Hungarian", LanguageGroup),
new PowerAccentLanguageModel("IS", "QuickAccent_SelectedLanguage_Icelandic", LanguageGroup),
new PowerAccentLanguageModel("IPA", "QuickAccent_SelectedLanguage_IPA", SpecialGroup),
new PowerAccentLanguageModel("IT", "QuickAccent_SelectedLanguage_Italian", LanguageGroup),
new PowerAccentLanguageModel("KU", "QuickAccent_SelectedLanguage_Kurdish", LanguageGroup),
new PowerAccentLanguageModel("LT", "QuickAccent_SelectedLanguage_Lithuanian", LanguageGroup),
new PowerAccentLanguageModel("MK", "QuickAccent_SelectedLanguage_Macedonian", LanguageGroup),
new PowerAccentLanguageModel("MT", "QuickAccent_SelectedLanguage_Maltese", LanguageGroup),
new PowerAccentLanguageModel("MI", "QuickAccent_SelectedLanguage_Maori", LanguageGroup),
new PowerAccentLanguageModel("NO", "QuickAccent_SelectedLanguage_Norwegian", LanguageGroup),
new PowerAccentLanguageModel("PI", "QuickAccent_SelectedLanguage_Pinyin", LanguageGroup),
new PowerAccentLanguageModel("PIE", "QuickAccent_SelectedLanguage_Proto_Indo_European", LanguageGroup),
new PowerAccentLanguageModel("PL", "QuickAccent_SelectedLanguage_Polish", LanguageGroup),
new PowerAccentLanguageModel("PT", "QuickAccent_SelectedLanguage_Portuguese", LanguageGroup),
new PowerAccentLanguageModel("RO", "QuickAccent_SelectedLanguage_Romanian", LanguageGroup),
new PowerAccentLanguageModel("ROM", "QuickAccent_SelectedLanguage_Romanization", SpecialGroup),
new PowerAccentLanguageModel("SK", "QuickAccent_SelectedLanguage_Slovak", LanguageGroup),
new PowerAccentLanguageModel("SL", "QuickAccent_SelectedLanguage_Slovenian", LanguageGroup),
new PowerAccentLanguageModel("SP", "QuickAccent_SelectedLanguage_Spanish", LanguageGroup),
new PowerAccentLanguageModel("SR", "QuickAccent_SelectedLanguage_Serbian", LanguageGroup),
new PowerAccentLanguageModel("SR_CYRL", "QuickAccent_SelectedLanguage_Serbian_Cyrillic", LanguageGroup),
new PowerAccentLanguageModel("SV", "QuickAccent_SelectedLanguage_Swedish", LanguageGroup),
new PowerAccentLanguageModel("TK", "QuickAccent_SelectedLanguage_Turkish", LanguageGroup),
new PowerAccentLanguageModel("VI", "QuickAccent_SelectedLanguage_Vietnamese", LanguageGroup),
new PowerAccentLanguageModel("CY", "QuickAccent_SelectedLanguage_Welsh", LanguageGroup),
];
public PowerAccentLanguageGroupModel[] LanguageGroups { get; private set; }
private readonly string[] _toolbarOptions =
{
"Top center",
"Bottom center",
"Left",
"Right",
"Top right corner",
"Top left corner",
"Bottom right corner",
"Bottom left corner",
"Center",
};
private Func<string, int> SendConfigMSG { get; }
public PowerAccentViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc)
{
// To obtain the general settings configurations of PowerToys Settings.
ArgumentNullException.ThrowIfNull(settingsRepository);
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
GeneralSettingsConfig = settingsRepository.SettingsConfig;
InitializeEnabledValue();
InitializeLanguages();
if (_settingsUtils.SettingsExists(PowerAccentSettings.ModuleName))
{
_powerAccentSettings = _settingsUtils.GetSettingsOrDefault<PowerAccentSettings>(PowerAccentSettings.ModuleName);
}
else
{
_powerAccentSettings = new PowerAccentSettings();
}
_inputTimeMs = _powerAccentSettings.Properties.InputTime.Value;
_excludedApps = _powerAccentSettings.Properties.ExcludedApps.Value;
if (!string.IsNullOrWhiteSpace(_powerAccentSettings.Properties.SelectedLang.Value) && !_powerAccentSettings.Properties.SelectedLang.Value.Contains("ALL"))
{
SelectedLanguageOptions = _powerAccentSettings.Properties.SelectedLang.Value.Split(',')
.Select(l => Languages.Find(lang => lang.LanguageCode == l))
.Where(l => l != null) // Wrongly typed languages will appear as null after find. We want to remove those to avoid crashes.
.ToArray();
}
else if (_powerAccentSettings.Properties.SelectedLang.Value.Contains("ALL"))
{
SelectedLanguageOptions = Languages.ToArray();
}
else
{
SelectedLanguageOptions = Array.Empty<PowerAccentLanguageModel>();
}
_toolbarPositionIndex = Array.IndexOf(_toolbarOptions, _powerAccentSettings.Properties.ToolbarPosition.Value);
// set the callback functions value to handle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
}
private void InitializeEnabledValue()
{
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredQuickAccentEnabledValue();
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true;
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
}
else
{
_isEnabled = GeneralSettingsConfig.Enabled.PowerAccent;
}
}
/// <summary>
/// Adds Localized Language Name, sorts by it and splits languages into two groups.
/// </summary>
private void InitializeLanguages()
{
foreach (var item in Languages)
{
item.Language = ResourceLoaderInstance.ResourceLoader.GetString(item.LanguageResourceID);
}
Languages.Sort((x, y) => string.Compare(x.Language, y.Language, StringComparison.Ordinal));
LanguageGroups = Languages
.GroupBy(language => language.GroupResourceID)
.Select(grp => new PowerAccentLanguageGroupModel(grp.ToList(), ResourceLoaderInstance.ResourceLoader.GetString(grp.Key)))
.ToArray();
}
public bool IsEnabled
{
get => _isEnabled;
set
{
if (_enabledStateIsGPOConfigured)
{
// If it's GPO configured, shouldn't be able to change this state.
return;
}
if (_isEnabled != value)
{
_isEnabled = value;
GeneralSettingsConfig.Enabled.PowerAccent = value;
OnPropertyChanged(nameof(IsEnabled));
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(outgoing.ToString());
}
}
}
public bool IsEnabledGpoConfigured
{
get => _enabledStateIsGPOConfigured;
}
public int ActivationKey
{
get
{
return (int)_powerAccentSettings.Properties.ActivationKey;
}
set
{
if (value != (int)_powerAccentSettings.Properties.ActivationKey)
{
_powerAccentSettings.Properties.ActivationKey = (PowerAccentActivationKey)value;
OnPropertyChanged(nameof(ActivationKey));
RaisePropertyChanged();
}
}
}
public bool DoNotActivateOnGameMode
{
get
{
return _powerAccentSettings.Properties.DoNotActivateOnGameMode;
}
set
{
if (value != _powerAccentSettings.Properties.DoNotActivateOnGameMode)
{
_powerAccentSettings.Properties.DoNotActivateOnGameMode = value;
OnPropertyChanged(nameof(DoNotActivateOnGameMode));
RaisePropertyChanged();
}
}
}
private int _inputTimeMs = PowerAccentSettings.DefaultInputTimeMs;
public int InputTimeMs
{
get
{
return _inputTimeMs;
}
set
{
if (value != _inputTimeMs)
{
_inputTimeMs = value;
_powerAccentSettings.Properties.InputTime.Value = value;
OnPropertyChanged(nameof(InputTimeMs));
RaisePropertyChanged();
}
}
}
private string _excludedApps;
public string ExcludedApps
{
get
{
return _excludedApps;
}
set
{
if (value != _excludedApps)
{
_excludedApps = value;
_powerAccentSettings.Properties.ExcludedApps.Value = value;
OnPropertyChanged(nameof(ExcludedApps));
RaisePropertyChanged();
}
}
}
private int _toolbarPositionIndex;
public int ToolbarPositionIndex
{
get
{
return _toolbarPositionIndex;
}
set
{
if (_toolbarPositionIndex != value)
{
_toolbarPositionIndex = value;
_powerAccentSettings.Properties.ToolbarPosition.Value = _toolbarOptions[value];
RaisePropertyChanged(nameof(ToolbarPositionIndex));
}
}
}
public bool AllSelected => _selectedLanguageOptions.Length == Languages.Count;
private PowerAccentLanguageModel[] _selectedLanguageOptions;
public PowerAccentLanguageModel[] SelectedLanguageOptions
{
get => _selectedLanguageOptions;
set
{
_selectedLanguageOptions = value;
_powerAccentSettings.Properties.SelectedLang.Value = string.Join(',', _selectedLanguageOptions.Select(l => l.LanguageCode));
RaisePropertyChanged(nameof(SelectedLanguageOptions));
}
}
public bool ShowUnicodeDescription
{
get
{
return _powerAccentSettings.Properties.ShowUnicodeDescription;
}
set
{
if (value != _powerAccentSettings.Properties.ShowUnicodeDescription)
{
_powerAccentSettings.Properties.ShowUnicodeDescription = value;
OnPropertyChanged(nameof(ShowUnicodeDescription));
RaisePropertyChanged();
}
}
}
public bool SortByUsageFrequency
{
get
{
return _powerAccentSettings.Properties.SortByUsageFrequency;
}
set
{
if (value != _powerAccentSettings.Properties.SortByUsageFrequency)
{
_powerAccentSettings.Properties.SortByUsageFrequency = value;
OnPropertyChanged(nameof(SortByUsageFrequency));
RaisePropertyChanged();
}
}
}
public bool StartSelectionFromTheLeft
{
get
{
return _powerAccentSettings.Properties.StartSelectionFromTheLeft;
}
set
{
if (value != _powerAccentSettings.Properties.StartSelectionFromTheLeft)
{
_powerAccentSettings.Properties.StartSelectionFromTheLeft = value;
OnPropertyChanged(nameof(StartSelectionFromTheLeft));
RaisePropertyChanged();
}
}
}
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
// Notify UI of property change
OnPropertyChanged(propertyName);
if (SendConfigMSG != null)
{
SndPowerAccentSettings snd = new SndPowerAccentSettings(_powerAccentSettings);
SndModuleSettings<SndPowerAccentSettings> ipcMessage = new SndModuleSettings<SndPowerAccentSettings>(snd);
SendConfigMSG(ipcMessage.ToJsonString());
}
}
public void RefreshEnabledState()
{
InitializeEnabledValue();
OnPropertyChanged(nameof(IsEnabled));
}
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
private bool _isEnabled;
}
}