-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Copy pathKeysDataModel.cs
319 lines (271 loc) · 10 KB
/
KeysDataModel.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
// 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.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Windows.Input;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
namespace Microsoft.PowerToys.Settings.UI.Library
{
public class KeysDataModel : INotifyPropertyChanged
{
[JsonPropertyName("originalKeys")]
public string OriginalKeys { get; set; }
[JsonPropertyName("secondKeyOfChord")]
public uint SecondKeyOfChord { get; set; }
[JsonPropertyName("newRemapKeys")]
public string NewRemapKeys { get; set; }
[JsonPropertyName("unicodeText")]
public string NewRemapString { get; set; }
[JsonPropertyName("runProgramFilePath")]
public string RunProgramFilePath { get; set; }
[JsonPropertyName("runProgramArgs")]
public string RunProgramArgs { get; set; }
[JsonPropertyName("openUri")]
public string OpenUri { get; set; }
[JsonPropertyName("operationType")]
public int OperationType { get; set; }
private enum KeyboardManagerEditorType
{
KeyEditor = 0,
ShortcutEditor,
}
public const string CommaSeparator = "<comma>";
private static Process editor;
private ICommand _editShortcutItemCommand;
public ICommand EditShortcutItem => _editShortcutItemCommand ?? (_editShortcutItemCommand = new RelayCommand<object>(OnEditShortcutItem));
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void OnEditShortcutItem(object parameter)
{
OpenEditor((int)KeyboardManagerEditorType.ShortcutEditor);
}
private async void OpenEditor(int type)
{
if (editor != null)
{
BringProcessToFront(editor);
return;
}
const string PowerToyName = KeyboardManagerSettings.ModuleName;
const string KeyboardManagerEditorPath = "KeyboardManagerEditor\\PowerToys.KeyboardManagerEditor.exe";
try
{
if (editor != null && editor.HasExited)
{
Logger.LogInfo($"Previous instance of {PowerToyName} editor exited");
editor = null;
}
if (editor != null)
{
Logger.LogInfo($"The {PowerToyName} editor instance {editor.Id} exists. Bringing the process to the front");
BringProcessToFront(editor);
return;
}
string path = Path.Combine(Environment.CurrentDirectory, KeyboardManagerEditorPath);
Logger.LogInfo($"Starting {PowerToyName} editor from {path}");
// InvariantCulture: type represents the KeyboardManagerEditorType enum value
editor = Process.Start(path, $"{type.ToString(CultureInfo.InvariantCulture)} {Environment.ProcessId}");
await editor.WaitForExitAsync();
editor = null;
}
catch (Exception e)
{
editor = null;
Logger.LogError($"Exception encountered when opening an {PowerToyName} editor", e);
}
}
private static void BringProcessToFront(Process process)
{
if (process == null)
{
return;
}
IntPtr handle = process.MainWindowHandle;
if (NativeMethods.IsIconic(handle))
{
NativeMethods.ShowWindow(handle, NativeMethods.SWRESTORE);
}
NativeMethods.SetForegroundWindow(handle);
}
private static List<string> MapKeysOnlyChord(uint secondKeyOfChord)
{
var result = new List<string>();
if (secondKeyOfChord <= 0)
{
return result;
}
result.Add(Helper.GetKeyName(secondKeyOfChord));
return result;
}
private static List<string> MapKeys(string stringOfKeys, uint secondKeyOfChord, bool splitChordsWithComma = false)
{
if (stringOfKeys == null)
{
return new List<string>();
}
if (secondKeyOfChord > 0)
{
var keys = stringOfKeys.Split(';');
return keys.Take(keys.Length - 1)
.Select(uint.Parse)
.Select(Helper.GetKeyName)
.ToList();
}
else
{
if (splitChordsWithComma)
{
var keys = stringOfKeys.Split(';')
.Select(uint.Parse)
.Select(Helper.GetKeyName)
.ToList();
keys.Insert(keys.Count - 1, CommaSeparator);
return keys;
}
else
{
return stringOfKeys
.Split(';')
.Select(uint.Parse)
.Select(Helper.GetKeyName)
.ToList();
}
}
}
private static List<string> MapKeys(string stringOfKeys)
{
return MapKeys(stringOfKeys, 0);
}
public List<string> GetMappedOriginalKeys(bool ignoreSecondKeyInChord, bool splitChordsWithComma = false)
{
if (ignoreSecondKeyInChord && SecondKeyOfChord > 0)
{
return MapKeys(OriginalKeys, SecondKeyOfChord);
}
else
{
return MapKeys(OriginalKeys, 0, splitChordsWithComma);
}
}
public List<string> GetMappedOriginalKeysOnlyChord()
{
return MapKeysOnlyChord(SecondKeyOfChord);
}
public List<string> GetMappedOriginalKeys()
{
return GetMappedOriginalKeys(false);
}
public List<string> GetMappedOriginalKeysWithSplitChord()
{
return GetMappedOriginalKeys(false, true);
}
public bool IsRunProgram
{
get
{
return OperationType == 1;
}
}
public bool IsOpenURI
{
get
{
return OperationType == 2;
}
}
public bool IsOpenUriOrIsRunProgram
{
get
{
return IsOpenURI || IsRunProgram;
}
}
public bool HasChord
{
get
{
return SecondKeyOfChord > 0;
}
}
public List<string> GetMappedNewRemapKeys(int runProgramMaxLength)
{
if (IsRunProgram)
{
// we're going to just pretend this is a "key" if we have a RunProgramFilePath
if (string.IsNullOrEmpty(RunProgramFilePath))
{
return new List<string>();
}
else
{
return new List<string> { FormatFakeKeyForDisplay(runProgramMaxLength) };
}
}
else if (IsOpenURI)
{
// we're going to just pretend this is a "key" if we have a RunProgramFilePath
if (string.IsNullOrEmpty(OpenUri))
{
return new List<string>();
}
else
{
if (OpenUri.Length > runProgramMaxLength)
{
return new List<string> { $"{OpenUri.Substring(0, runProgramMaxLength - 3)}..." };
}
else
{
return new List<string> { OpenUri };
}
}
}
return (string.IsNullOrEmpty(NewRemapString) || NewRemapString == "*Unsupported*") ? MapKeys(NewRemapKeys) : new List<string> { NewRemapString };
}
// Instead of doing something fancy pants, we'll just display the RunProgramFilePath data when it's IsRunProgram
// It truncates the start of the program to run, if it's long and truncates the end of the args if it's long
// e.g.: c:\MyCool\PathIs\Long\software.exe myArg1 myArg2 myArg3 -> (something like) "...ng\software.exe myArg1..."
// the idea is you get the most important part of the program to run and some of the args in case that the only thing thats different,
// e.g: "...path\software.exe cool1.txt" and "...path\software.exe cool3.txt"
private string FormatFakeKeyForDisplay(int runProgramMaxLength)
{
// was going to use this:
var fakeKey = Environment.ExpandEnvironmentVariables(RunProgramFilePath);
try
{
if (File.Exists(fakeKey))
{
fakeKey = Path.GetFileName(Environment.ExpandEnvironmentVariables(RunProgramFilePath));
}
}
catch
{
}
fakeKey = $"{fakeKey} {RunProgramArgs}".Trim();
if (fakeKey.Length > runProgramMaxLength)
{
fakeKey = $"{fakeKey.Substring(0, runProgramMaxLength - 3)}...";
}
return fakeKey;
}
public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}