Skip to content

Commit a6b7614

Browse files
committed
Refactoring
1 parent da77396 commit a6b7614

File tree

20 files changed

+163
-319
lines changed

20 files changed

+163
-319
lines changed

src/modules/ShortcutGuideV2/ShortcutGuide.IndexYmlGenerator/ShortcutGuide.IndexYmlGenerator/IndexYmlGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void Main()
2121
// Todo: Exception handling
2222
public static void CreateIndexYmlFile()
2323
{
24-
string path = ManifestInterpreter.GetPathOfIntepretations();
24+
string path = ManifestInterpreter.GetPathOfInterpretations();
2525
if (File.Exists(Path.Combine(path, "index.yml")))
2626
{
2727
File.Delete(Path.Combine(path, "index.yml"));

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Exceptions/InvalidYamlFileException.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DisplayHelper.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,41 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using Windows.Foundation;
76
using WinUIEx;
87

98
namespace ShortcutGuide.Helpers
109
{
11-
public static partial class DisplayHelper
10+
public static class DisplayHelper
1211
{
13-
private enum MonitorFromWindowDwFlags : int
12+
private enum MonitorFromWindowDwFlags
1413
{
1514
MONITOR_DEFAULTTONEAREST = 2,
1615
}
1716

1817
public static Rect GetWorkAreaForDisplayWithWindow(nint hwnd)
1918
{
20-
foundMonitorIndex = -1;
21-
monitorIndex = 0;
19+
_foundMonitorIndex = -1;
20+
_monitorIndex = 0;
2221
var monitor = NativeMethods.MonitorFromWindow(hwnd, (int)MonitorFromWindowDwFlags.MONITOR_DEFAULTTONEAREST);
2322
NativeMethods.EnumDisplayMonitors(nint.Zero, nint.Zero, MonitorEnumProc, new NativeMethods.LPARAM(monitor));
24-
return MonitorInfo.GetDisplayMonitors()[foundMonitorIndex].RectWork;
23+
return MonitorInfo.GetDisplayMonitors()[_foundMonitorIndex].RectWork;
2524
}
2625

27-
private static int foundMonitorIndex = -1;
28-
private static int monitorIndex;
26+
private static int _foundMonitorIndex = -1;
27+
private static int _monitorIndex;
2928

3029
private static bool MonitorEnumProc(nint hMonitor, nint hdcMonitor, ref NativeMethods.RECT lprcMonitor, nint dwData)
3130
{
3231
nint targetMonitor = dwData;
3332

3433
if (hMonitor == targetMonitor)
3534
{
36-
foundMonitorIndex = monitorIndex;
35+
_foundMonitorIndex = _monitorIndex;
3736
return false;
3837
}
3938

40-
monitorIndex++;
39+
_monitorIndex++;
4140
return true;
4241
}
4342
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DpiHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace ShortcutGuide.Helpers
66
{
77
// This class is rewritten from C++ to C# from the measure tool project
8-
internal static partial class DpiHelper
8+
internal static class DpiHelper
99
{
1010
#pragma warning disable SA1310 // Field names should not contain underscore
1111
private const int DEFAULT_DPI = 96;

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ManifestInterpreter.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ public class ManifestInterpreter
2222

2323
public static ShortcutFile GetShortcutsOfApplication(string applicationName)
2424
{
25-
string path = GetPathOfIntepretations();
25+
string path = GetPathOfInterpretations();
2626
IEnumerable<string> files = Directory.EnumerateFiles(path, applicationName + ".*.yml") ??
2727
throw new FileNotFoundException($"The file for the application '{applicationName}' was not found in '{path}'.");
2828

29-
return files.Any(f => f.EndsWith($".{Language}.yml", StringComparison.InvariantCulture))
29+
IEnumerable<string> filesEnumerable = files as string[] ?? files.ToArray();
30+
return filesEnumerable.Any(f => f.EndsWith($".{Language}.yml", StringComparison.InvariantCulture))
3031
? YamlToShortcutList(File.ReadAllText(Path.Combine(path, applicationName + $".{Language}.yml")))
31-
: files.Any(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture))
32-
? YamlToShortcutList(File.ReadAllText(files.First(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture))))
32+
: filesEnumerable.Any(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture))
33+
? YamlToShortcutList(File.ReadAllText(filesEnumerable.First(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture))))
3334
: throw new FileNotFoundException($"The file for the application '{applicationName}' was not found in '{path}' with the language '{Language}' or 'en-US'.");
3435
}
3536

@@ -39,14 +40,14 @@ public static ShortcutFile YamlToShortcutList(string content)
3940
return deserializer.Deserialize<ShortcutFile>(content);
4041
}
4142

42-
public static string GetPathOfIntepretations()
43+
public static string GetPathOfInterpretations()
4344
{
4445
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "WinGet", "KeyboardShortcuts");
4546
}
4647

4748
public static IndexFile GetIndexYamlFile()
4849
{
49-
string path = GetPathOfIntepretations();
50+
string path = GetPathOfInterpretations();
5051
string content = File.ReadAllText(Path.Combine(path, "index.yml"));
5152
Deserializer deserializer = new();
5253
return deserializer.Deserialize<IndexFile>(content);
@@ -58,15 +59,7 @@ public static string[] GetAllCurrentApplicationIds()
5859

5960
List<string> applicationIds = [];
6061

61-
static bool IsMatch(string input, string filter)
62-
{
63-
input = input.ToLower(CultureInfo.InvariantCulture);
64-
filter = filter.ToLower(CultureInfo.InvariantCulture);
65-
string regexPattern = "^" + Regex.Escape(filter).Replace("\\*", ".*") + "$";
66-
return Regex.IsMatch(input, regexPattern);
67-
}
68-
69-
var processes = Process.GetProcesses();
62+
Process[] processes = Process.GetProcesses();
7063

7164
if (NativeMethods.GetWindowThreadProcessId(handle, out uint processId) > 0)
7265
{
@@ -115,11 +108,14 @@ static bool IsMatch(string input, string filter)
115108
}
116109

117110
return [.. applicationIds];
118-
}
119111

120-
public static ShortcutFile GetShortcutsOfDefaultShell()
121-
{
122-
return GetShortcutsOfApplication(GetIndexYamlFile().DefaultShellName);
112+
static bool IsMatch(string input, string filter)
113+
{
114+
input = input.ToLower(CultureInfo.InvariantCulture);
115+
filter = filter.ToLower(CultureInfo.InvariantCulture);
116+
string regexPattern = "^" + Regex.Escape(filter).Replace("\\*", ".*") + "$";
117+
return Regex.IsMatch(input, regexPattern);
118+
}
123119
}
124120
}
125121
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/PowerToysShortcutsPopulator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
using System.IO;
88
using System.Text.RegularExpressions;
99
using Microsoft.PowerToys.Settings.UI.Library;
10-
using ShortcutGuide.Helpers;
1110

12-
namespace ShortcutGuide
11+
namespace ShortcutGuide.Helpers
1312
{
1413
internal sealed partial class PowerToysShortcutsPopulator
1514
{
1615
public static void Populate()
1716
{
18-
string path = Path.Combine(ManifestInterpreter.GetPathOfIntepretations(), $"Microsoft.PowerToys.{ManifestInterpreter.Language}.yml");
17+
string path = Path.Combine(ManifestInterpreter.GetPathOfInterpretations(), $"Microsoft.PowerToys.{ManifestInterpreter.Language}.yml");
1918

2019
string content = File.ReadAllText(path);
2120

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Windows.ApplicationModel.Resources;
6+
7+
namespace ShortcutGuide.Helpers
8+
{
9+
internal static class ResourceLoaderInstance
10+
{
11+
internal static ResourceLoader ResourceLoader { get; private set; }
12+
13+
static ResourceLoaderInstance()
14+
{
15+
ResourceLoader = new ResourceLoader("PowerToys.ShortcutGuide.pri");
16+
}
17+
}
18+
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/TasklistPositions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using System.Runtime.InteropServices;
7-
using TasklistButton = NativeMethods.TasklistButton;
6+
using TasklistButton = ShortcutGuide.NativeMethods.TasklistButton;
87

98
namespace ShortcutGuide.Helpers
109
{
11-
internal sealed partial class TasklistPositions
10+
internal sealed class TasklistPositions
1211
{
1312
public static TasklistButton[] GetButtons()
1413
{

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/ShortcutEntry.cs

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ async void AnimateTextBlock(TextBlock animatedTextBlock, string text, int delay
7575
}
7676
catch
7777
{
78+
// ignored
7879
}
7980
}
8081

@@ -86,7 +87,7 @@ async void AnimateTextBlock(TextBlock animatedTextBlock, string text, int delay
8687
shortcutStackPanel.Orientation = Orientation.Horizontal;
8788

8889
// If any entry is blank, we skip the whole shortcut
89-
if (!shortcutEntry.Ctrl && !shortcutEntry.Alt && !shortcutEntry.Shift && !shortcutEntry.Win && shortcutEntry.Keys.Length == 0)
90+
if (shortcutEntry is { Ctrl: false, Alt: false, Shift: false, Win: false, Keys.Length: 0 })
9091
{
9192
return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, shortcutStackPanel, shortcut);
9293
}
@@ -103,21 +104,18 @@ async void AnimateTextBlock(TextBlock animatedTextBlock, string text, int delay
103104
shortcutStackPanel.Children.Add(shortcutIndexTextBlock);
104105
}
105106

106-
void AddNewTextToStackPanel(string text)
107-
{
108-
shortcutStackPanel.Children.Add(new TextBlock { Text = text, Margin = new Thickness(3), VerticalAlignment = VerticalAlignment.Center });
109-
}
110-
111107
if (shortcutEntry.Win)
112108
{
113109
PathIcon winIcon = (XamlReader.Load(@"<PathIcon xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" Data=""M683 1229H0V546h683v683zm819 0H819V546h683v683zm-819 819H0v-683h683v683zm819 0H819v-683h683v683z"" />") as PathIcon)!;
114-
Viewbox winIconContainer = new();
115-
winIconContainer.Child = winIcon;
116-
winIconContainer.HorizontalAlignment = HorizontalAlignment.Center;
117-
winIconContainer.VerticalAlignment = VerticalAlignment.Center;
118-
winIconContainer.Height = 24;
119-
winIconContainer.Width = 24;
120-
winIconContainer.Margin = new Thickness(3);
110+
Viewbox winIconContainer = new()
111+
{
112+
Child = winIcon,
113+
HorizontalAlignment = HorizontalAlignment.Center,
114+
VerticalAlignment = VerticalAlignment.Center,
115+
Height = 24,
116+
Width = 24,
117+
Margin = new Thickness(3),
118+
};
121119
shortcutStackPanel.Children.Add(winIconContainer);
122120
}
123121

@@ -136,7 +134,7 @@ void AddNewTextToStackPanel(string text)
136134
AddNewTextToStackPanel("Shift");
137135
}
138136

139-
foreach (object key in shortcutEntry.Keys)
137+
foreach (string key in shortcutEntry.Keys)
140138
{
141139
switch (key)
142140
{
@@ -208,18 +206,10 @@ void AddNewTextToStackPanel(string text)
208206
shortcutStackPanel.Children.Add(arrowUDTextBlock);
209207
AnimateTextBlock(arrowUDTextBlock, "↑↓", 1000);
210208
break;
211-
case string name when name.StartsWith('<') && name.EndsWith('>'):
209+
case { } name when name.StartsWith('<') && name.EndsWith('>'):
212210
AddNewTextToStackPanel(name[1..^1]);
213211
break;
214-
case int num:
215-
if (num == 0)
216-
{
217-
break;
218-
}
219-
220-
AddNewTextToStackPanel(Helper.GetKeyName((uint)num));
221-
break;
222-
case string num when int.TryParse(num, out int parsedNum):
212+
case { } num when int.TryParse(num, out int parsedNum):
223213
if (parsedNum == 0)
224214
{
225215
break;
@@ -228,55 +218,76 @@ void AddNewTextToStackPanel(string text)
228218
AddNewTextToStackPanel(Helper.GetKeyName((uint)parsedNum));
229219
break;
230220
default:
231-
AddNewTextToStackPanel((string)key);
221+
AddNewTextToStackPanel(key);
232222
break;
233223
}
234224
}
235-
}
236225

237-
StackPanel stackPanelToReturn = shortcutStackPanels[0];
226+
continue;
238227

239-
if (shortcutStackPanels.Count == 0)
240-
{
241-
return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, new StackPanel(), shortcut);
242-
}
243-
else if (shortcutStackPanels.Count > 1)
244-
{
245-
stackPanelToReturn = new StackPanel();
246-
247-
stackPanelToReturn.Orientation = Orientation.Vertical;
248-
foreach (StackPanel panel in shortcutStackPanels)
228+
void AddNewTextToStackPanel(string text)
249229
{
250-
panel.Visibility = Visibility.Collapsed;
251-
stackPanelToReturn.Children.Add(panel);
230+
shortcutStackPanel.Children.Add(new TextBlock { Text = text, Margin = new Thickness(3), VerticalAlignment = VerticalAlignment.Center });
252231
}
232+
}
253233

254-
shortcutStackPanels[0].Visibility = Visibility.Visible;
255-
for (int i = 1; i < shortcutStackPanels.Count; i++)
256-
{
257-
shortcutStackPanels[i].Visibility = Visibility.Collapsed;
258-
}
234+
StackPanel stackPanelToReturn = shortcutStackPanels[0];
259235

260-
async void AnimateStackPanels(StackPanel[] panels, int delay = 2000)
236+
switch (shortcutStackPanels.Count)
237+
{
238+
case 0:
239+
return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, new StackPanel(), shortcut);
240+
case <= 1:
241+
return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, stackPanelToReturn, shortcut);
242+
default:
261243
{
262-
int index = 0;
263-
while (!ShortcutView.AnimationCancellationTokenSource.Token.IsCancellationRequested)
264244
{
265-
foreach (StackPanel panel in panels)
245+
stackPanelToReturn = new StackPanel
246+
{
247+
Orientation = Orientation.Vertical,
248+
};
249+
250+
foreach (StackPanel panel in shortcutStackPanels)
266251
{
267252
panel.Visibility = Visibility.Collapsed;
253+
stackPanelToReturn.Children.Add(panel);
268254
}
269255

270-
panels[index].Visibility = Visibility.Visible;
271-
index = (index + 1) % panels.Length;
272-
await Task.Delay(delay);
256+
shortcutStackPanels[0].Visibility = Visibility.Visible;
257+
for (int i = 1; i < shortcutStackPanels.Count; i++)
258+
{
259+
shortcutStackPanels[i].Visibility = Visibility.Collapsed;
260+
}
261+
262+
async void AnimateStackPanels(StackPanel[] panels, int delay = 2000)
263+
{
264+
try
265+
{
266+
int index = 0;
267+
while (!ShortcutView.AnimationCancellationTokenSource.Token.IsCancellationRequested)
268+
{
269+
foreach (StackPanel panel in panels)
270+
{
271+
panel.Visibility = Visibility.Collapsed;
272+
}
273+
274+
panels[index].Visibility = Visibility.Visible;
275+
index = (index + 1) % panels.Length;
276+
await Task.Delay(delay);
277+
}
278+
}
279+
catch
280+
{
281+
// ignored
282+
}
283+
}
284+
285+
AnimateStackPanels([.. shortcutStackPanels]);
273286
}
274-
}
275287

276-
AnimateStackPanels([.. shortcutStackPanels]);
288+
return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, stackPanelToReturn, shortcut);
289+
}
277290
}
278-
279-
return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, stackPanelToReturn, shortcut);
280291
}
281292
}
282293
}

0 commit comments

Comments
 (0)