From a6b761433e1aa1abb189a8a19aedd5dc6e88e65e Mon Sep 17 00:00:00 2001 From: Aaron Junker Date: Wed, 18 Jun 2025 21:05:34 +0200 Subject: [PATCH] Refactoring --- .../IndexYmlGenerator.cs | 2 +- .../Exceptions/InvalidYamlFileException.cs | 10 -- .../ShortcutGuide.Ui/Helpers/DisplayHelper.cs | 19 ++- .../ShortcutGuide.Ui/Helpers/DpiHelper.cs | 2 +- .../Helpers/ManifestInterpreter.cs | 34 ++--- .../Helpers/PowerToysShortcutsPopulator.cs | 5 +- .../Helpers/ResourceLoaderInstance.cs | 18 +++ .../Helpers/TasklistPositions.cs | 5 +- .../ShortcutGuide.Ui/Models/ShortcutEntry.cs | 123 ++++++++-------- .../ShortcutGuide.Ui/NativeMethods.cs | 10 +- .../ShortcutGuide.Ui/Program.cs | 8 +- .../Properties/Resource.Designer.cs | 135 ------------------ .../ShortcutGuide.Ui/ShortcutGuide.Ui.csproj | 13 -- .../ShortcutGuideXAML/App.xaml | 3 +- .../ShortcutGuideXAML/App.xaml.cs | 4 +- .../ShortcutGuideXAML/MainWindow.xaml | 1 - .../ShortcutGuideXAML/MainWindow.xaml.cs | 23 ++- .../ShortcutGuideXAML/ShortcutView.xaml | 10 +- .../ShortcutGuideXAML/ShortcutView.xaml.cs | 57 ++++---- .../en-us/Resources.resw} | 2 +- 20 files changed, 164 insertions(+), 320 deletions(-) delete mode 100644 src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Exceptions/InvalidYamlFileException.cs create mode 100644 src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ResourceLoaderInstance.cs delete mode 100644 src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Properties/Resource.Designer.cs rename src/modules/ShortcutGuideV2/ShortcutGuide.Ui/{Properties/Resource.resx => Strings/en-us/Resources.resw} (99%) diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.IndexYmlGenerator/ShortcutGuide.IndexYmlGenerator/IndexYmlGenerator.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.IndexYmlGenerator/ShortcutGuide.IndexYmlGenerator/IndexYmlGenerator.cs index 185401729a..d8a1179733 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.IndexYmlGenerator/ShortcutGuide.IndexYmlGenerator/IndexYmlGenerator.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.IndexYmlGenerator/ShortcutGuide.IndexYmlGenerator/IndexYmlGenerator.cs @@ -21,7 +21,7 @@ namespace ShortcutGuide.IndexYmlGenerator // Todo: Exception handling public static void CreateIndexYmlFile() { - string path = ManifestInterpreter.GetPathOfIntepretations(); + string path = ManifestInterpreter.GetPathOfInterpretations(); if (File.Exists(Path.Combine(path, "index.yml"))) { File.Delete(Path.Combine(path, "index.yml")); diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Exceptions/InvalidYamlFileException.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Exceptions/InvalidYamlFileException.cs deleted file mode 100644 index 01f75a5a87..0000000000 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Exceptions/InvalidYamlFileException.cs +++ /dev/null @@ -1,10 +0,0 @@ -// 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 ShortcutGuide.Exceptions -{ - internal sealed class InvalidYamlFileException : System.Exception - { - } -} diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DisplayHelper.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DisplayHelper.cs index 97facff029..cc968333cb 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DisplayHelper.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DisplayHelper.cs @@ -2,30 +2,29 @@ // 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 Windows.Foundation; using WinUIEx; namespace ShortcutGuide.Helpers { - public static partial class DisplayHelper + public static class DisplayHelper { - private enum MonitorFromWindowDwFlags : int + private enum MonitorFromWindowDwFlags { MONITOR_DEFAULTTONEAREST = 2, } public static Rect GetWorkAreaForDisplayWithWindow(nint hwnd) { - foundMonitorIndex = -1; - monitorIndex = 0; + _foundMonitorIndex = -1; + _monitorIndex = 0; var monitor = NativeMethods.MonitorFromWindow(hwnd, (int)MonitorFromWindowDwFlags.MONITOR_DEFAULTTONEAREST); NativeMethods.EnumDisplayMonitors(nint.Zero, nint.Zero, MonitorEnumProc, new NativeMethods.LPARAM(monitor)); - return MonitorInfo.GetDisplayMonitors()[foundMonitorIndex].RectWork; + return MonitorInfo.GetDisplayMonitors()[_foundMonitorIndex].RectWork; } - private static int foundMonitorIndex = -1; - private static int monitorIndex; + private static int _foundMonitorIndex = -1; + private static int _monitorIndex; private static bool MonitorEnumProc(nint hMonitor, nint hdcMonitor, ref NativeMethods.RECT lprcMonitor, nint dwData) { @@ -33,11 +32,11 @@ namespace ShortcutGuide.Helpers if (hMonitor == targetMonitor) { - foundMonitorIndex = monitorIndex; + _foundMonitorIndex = _monitorIndex; return false; } - monitorIndex++; + _monitorIndex++; return true; } } diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DpiHelper.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DpiHelper.cs index bfdde35051..c63f449af1 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DpiHelper.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DpiHelper.cs @@ -5,7 +5,7 @@ namespace ShortcutGuide.Helpers { // This class is rewritten from C++ to C# from the measure tool project - internal static partial class DpiHelper + internal static class DpiHelper { #pragma warning disable SA1310 // Field names should not contain underscore private const int DEFAULT_DPI = 96; diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ManifestInterpreter.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ManifestInterpreter.cs index eddcbc79fe..70497c6b07 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ManifestInterpreter.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ManifestInterpreter.cs @@ -22,14 +22,15 @@ namespace ShortcutGuide.Helpers public static ShortcutFile GetShortcutsOfApplication(string applicationName) { - string path = GetPathOfIntepretations(); + string path = GetPathOfInterpretations(); IEnumerable files = Directory.EnumerateFiles(path, applicationName + ".*.yml") ?? throw new FileNotFoundException($"The file for the application '{applicationName}' was not found in '{path}'."); - return files.Any(f => f.EndsWith($".{Language}.yml", StringComparison.InvariantCulture)) + IEnumerable filesEnumerable = files as string[] ?? files.ToArray(); + return filesEnumerable.Any(f => f.EndsWith($".{Language}.yml", StringComparison.InvariantCulture)) ? YamlToShortcutList(File.ReadAllText(Path.Combine(path, applicationName + $".{Language}.yml"))) - : files.Any(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture)) - ? YamlToShortcutList(File.ReadAllText(files.First(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture)))) + : filesEnumerable.Any(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture)) + ? YamlToShortcutList(File.ReadAllText(filesEnumerable.First(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture)))) : throw new FileNotFoundException($"The file for the application '{applicationName}' was not found in '{path}' with the language '{Language}' or 'en-US'."); } @@ -39,14 +40,14 @@ namespace ShortcutGuide.Helpers return deserializer.Deserialize(content); } - public static string GetPathOfIntepretations() + public static string GetPathOfInterpretations() { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "WinGet", "KeyboardShortcuts"); } public static IndexFile GetIndexYamlFile() { - string path = GetPathOfIntepretations(); + string path = GetPathOfInterpretations(); string content = File.ReadAllText(Path.Combine(path, "index.yml")); Deserializer deserializer = new(); return deserializer.Deserialize(content); @@ -58,15 +59,7 @@ namespace ShortcutGuide.Helpers List applicationIds = []; - static bool IsMatch(string input, string filter) - { - input = input.ToLower(CultureInfo.InvariantCulture); - filter = filter.ToLower(CultureInfo.InvariantCulture); - string regexPattern = "^" + Regex.Escape(filter).Replace("\\*", ".*") + "$"; - return Regex.IsMatch(input, regexPattern); - } - - var processes = Process.GetProcesses(); + Process[] processes = Process.GetProcesses(); if (NativeMethods.GetWindowThreadProcessId(handle, out uint processId) > 0) { @@ -115,11 +108,14 @@ namespace ShortcutGuide.Helpers } return [.. applicationIds]; - } - public static ShortcutFile GetShortcutsOfDefaultShell() - { - return GetShortcutsOfApplication(GetIndexYamlFile().DefaultShellName); + static bool IsMatch(string input, string filter) + { + input = input.ToLower(CultureInfo.InvariantCulture); + filter = filter.ToLower(CultureInfo.InvariantCulture); + string regexPattern = "^" + Regex.Escape(filter).Replace("\\*", ".*") + "$"; + return Regex.IsMatch(input, regexPattern); + } } } } diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/PowerToysShortcutsPopulator.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/PowerToysShortcutsPopulator.cs index b2b39a1dac..af777f1b31 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/PowerToysShortcutsPopulator.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/PowerToysShortcutsPopulator.cs @@ -7,15 +7,14 @@ using System.Globalization; using System.IO; using System.Text.RegularExpressions; using Microsoft.PowerToys.Settings.UI.Library; -using ShortcutGuide.Helpers; -namespace ShortcutGuide +namespace ShortcutGuide.Helpers { internal sealed partial class PowerToysShortcutsPopulator { public static void Populate() { - string path = Path.Combine(ManifestInterpreter.GetPathOfIntepretations(), $"Microsoft.PowerToys.{ManifestInterpreter.Language}.yml"); + string path = Path.Combine(ManifestInterpreter.GetPathOfInterpretations(), $"Microsoft.PowerToys.{ManifestInterpreter.Language}.yml"); string content = File.ReadAllText(path); diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ResourceLoaderInstance.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ResourceLoaderInstance.cs new file mode 100644 index 0000000000..549bbb0b56 --- /dev/null +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ResourceLoaderInstance.cs @@ -0,0 +1,18 @@ +// 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 Microsoft.Windows.ApplicationModel.Resources; + +namespace ShortcutGuide.Helpers +{ + internal static class ResourceLoaderInstance + { + internal static ResourceLoader ResourceLoader { get; private set; } + + static ResourceLoaderInstance() + { + ResourceLoader = new ResourceLoader("PowerToys.ShortcutGuide.pri"); + } + } +} diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/TasklistPositions.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/TasklistPositions.cs index 296933434e..3056a0b28c 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/TasklistPositions.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/TasklistPositions.cs @@ -2,13 +2,12 @@ // 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.Runtime.InteropServices; -using TasklistButton = NativeMethods.TasklistButton; +using TasklistButton = ShortcutGuide.NativeMethods.TasklistButton; namespace ShortcutGuide.Helpers { - internal sealed partial class TasklistPositions + internal sealed class TasklistPositions { public static TasklistButton[] GetButtons() { diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/ShortcutEntry.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/ShortcutEntry.cs index 66528c019d..bd7ce34f3c 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/ShortcutEntry.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/ShortcutEntry.cs @@ -75,6 +75,7 @@ namespace ShortcutGuide.Models } catch { + // ignored } } @@ -86,7 +87,7 @@ namespace ShortcutGuide.Models shortcutStackPanel.Orientation = Orientation.Horizontal; // If any entry is blank, we skip the whole shortcut - if (!shortcutEntry.Ctrl && !shortcutEntry.Alt && !shortcutEntry.Shift && !shortcutEntry.Win && shortcutEntry.Keys.Length == 0) + if (shortcutEntry is { Ctrl: false, Alt: false, Shift: false, Win: false, Keys.Length: 0 }) { return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, shortcutStackPanel, shortcut); } @@ -103,21 +104,18 @@ namespace ShortcutGuide.Models shortcutStackPanel.Children.Add(shortcutIndexTextBlock); } - void AddNewTextToStackPanel(string text) - { - shortcutStackPanel.Children.Add(new TextBlock { Text = text, Margin = new Thickness(3), VerticalAlignment = VerticalAlignment.Center }); - } - if (shortcutEntry.Win) { PathIcon winIcon = (XamlReader.Load(@"") as PathIcon)!; - Viewbox winIconContainer = new(); - winIconContainer.Child = winIcon; - winIconContainer.HorizontalAlignment = HorizontalAlignment.Center; - winIconContainer.VerticalAlignment = VerticalAlignment.Center; - winIconContainer.Height = 24; - winIconContainer.Width = 24; - winIconContainer.Margin = new Thickness(3); + Viewbox winIconContainer = new() + { + Child = winIcon, + HorizontalAlignment = HorizontalAlignment.Center, + VerticalAlignment = VerticalAlignment.Center, + Height = 24, + Width = 24, + Margin = new Thickness(3), + }; shortcutStackPanel.Children.Add(winIconContainer); } @@ -136,7 +134,7 @@ namespace ShortcutGuide.Models AddNewTextToStackPanel("Shift"); } - foreach (object key in shortcutEntry.Keys) + foreach (string key in shortcutEntry.Keys) { switch (key) { @@ -208,18 +206,10 @@ namespace ShortcutGuide.Models shortcutStackPanel.Children.Add(arrowUDTextBlock); AnimateTextBlock(arrowUDTextBlock, "↑↓", 1000); break; - case string name when name.StartsWith('<') && name.EndsWith('>'): + case { } name when name.StartsWith('<') && name.EndsWith('>'): AddNewTextToStackPanel(name[1..^1]); break; - case int num: - if (num == 0) - { - break; - } - - AddNewTextToStackPanel(Helper.GetKeyName((uint)num)); - break; - case string num when int.TryParse(num, out int parsedNum): + case { } num when int.TryParse(num, out int parsedNum): if (parsedNum == 0) { break; @@ -228,55 +218,76 @@ namespace ShortcutGuide.Models AddNewTextToStackPanel(Helper.GetKeyName((uint)parsedNum)); break; default: - AddNewTextToStackPanel((string)key); + AddNewTextToStackPanel(key); break; } } + + continue; + + void AddNewTextToStackPanel(string text) + { + shortcutStackPanel.Children.Add(new TextBlock { Text = text, Margin = new Thickness(3), VerticalAlignment = VerticalAlignment.Center }); + } } StackPanel stackPanelToReturn = shortcutStackPanels[0]; - if (shortcutStackPanels.Count == 0) + switch (shortcutStackPanels.Count) { - return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, new StackPanel(), shortcut); - } - else if (shortcutStackPanels.Count > 1) - { - stackPanelToReturn = new StackPanel(); - - stackPanelToReturn.Orientation = Orientation.Vertical; - foreach (StackPanel panel in shortcutStackPanels) + case 0: + return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, new StackPanel(), shortcut); + case <= 1: + return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, stackPanelToReturn, shortcut); + default: { - panel.Visibility = Visibility.Collapsed; - stackPanelToReturn.Children.Add(panel); - } - - shortcutStackPanels[0].Visibility = Visibility.Visible; - for (int i = 1; i < shortcutStackPanels.Count; i++) - { - shortcutStackPanels[i].Visibility = Visibility.Collapsed; - } - - async void AnimateStackPanels(StackPanel[] panels, int delay = 2000) - { - int index = 0; - while (!ShortcutView.AnimationCancellationTokenSource.Token.IsCancellationRequested) { - foreach (StackPanel panel in panels) + stackPanelToReturn = new StackPanel + { + Orientation = Orientation.Vertical, + }; + + foreach (StackPanel panel in shortcutStackPanels) { panel.Visibility = Visibility.Collapsed; + stackPanelToReturn.Children.Add(panel); } - panels[index].Visibility = Visibility.Visible; - index = (index + 1) % panels.Length; - await Task.Delay(delay); + shortcutStackPanels[0].Visibility = Visibility.Visible; + for (int i = 1; i < shortcutStackPanels.Count; i++) + { + shortcutStackPanels[i].Visibility = Visibility.Collapsed; + } + + async void AnimateStackPanels(StackPanel[] panels, int delay = 2000) + { + try + { + int index = 0; + while (!ShortcutView.AnimationCancellationTokenSource.Token.IsCancellationRequested) + { + foreach (StackPanel panel in panels) + { + panel.Visibility = Visibility.Collapsed; + } + + panels[index].Visibility = Visibility.Visible; + index = (index + 1) % panels.Length; + await Task.Delay(delay); + } + } + catch + { + // ignored + } + } + + AnimateStackPanels([.. shortcutStackPanels]); } + + return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, stackPanelToReturn, shortcut); } - - AnimateStackPanels([.. shortcutStackPanels]); } - - return new ShortcutTemplateDataObject(shortcut.Name, shortcut.Description ?? string.Empty, stackPanelToReturn, shortcut); } } } diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/NativeMethods.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/NativeMethods.cs index c263045bce..3ba85fc270 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/NativeMethods.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/NativeMethods.cs @@ -6,13 +6,10 @@ using System; using System.Runtime.InteropServices; using Windows.Graphics; +namespace ShortcutGuide; + internal static partial class NativeMethods { - internal static readonly IntPtr HWND_TOPMOST = new(-1); - internal const uint SWP_NOSIZE = 0x0001; - internal const uint SWP_NOMOVE = 0x0002; - internal const uint SWP_NOACTIVATE = 0x0010; - internal const uint SWP_SHOWWINDOW = 0x0040; internal const int GWL_STYLE = -16; internal const int WS_CAPTION = 0x00C00000; @@ -29,9 +26,6 @@ internal static partial class NativeMethods [LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)] internal static partial IntPtr FindWindowA(in string lpClassName, in string? lpWindowName); - [LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)] - internal static partial IntPtr FindWindowExA(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); - [LibraryImport("User32.dll")] internal static partial IntPtr MonitorFromWindow(IntPtr hwnd, int dwFlags); diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Program.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Program.cs index 7670fc8145..3010737113 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Program.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Program.cs @@ -32,16 +32,16 @@ namespace ShortcutGuide return; } - if (!Directory.Exists(ManifestInterpreter.GetPathOfIntepretations())) + if (!Directory.Exists(ManifestInterpreter.GetPathOfInterpretations())) { - Directory.CreateDirectory(ManifestInterpreter.GetPathOfIntepretations()); + Directory.CreateDirectory(ManifestInterpreter.GetPathOfInterpretations()); } // Todo: Only copy files after an update. // Todo: Handle error foreach (var file in InbuiltManifestFiles) { - File.Copy(Path.GetDirectoryName(Environment.ProcessPath) + "\\Assets\\ShortcutGuide\\" + file, ManifestInterpreter.GetPathOfIntepretations() + "\\" + file, true); + File.Copy(Path.GetDirectoryName(Environment.ProcessPath) + "\\Assets\\ShortcutGuide\\" + file, ManifestInterpreter.GetPathOfInterpretations() + "\\" + file, true); } Process.Start(Path.GetDirectoryName(Environment.ProcessPath) + "\\PowerToys.ShortcutGuide.IndexYmlGenerator.exe"); @@ -68,8 +68,6 @@ namespace ShortcutGuide // Something prevents the process from exiting, so we need to kill it manually. Process.GetCurrentProcess().Kill(); - - return; } } } diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Properties/Resource.Designer.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Properties/Resource.Designer.cs deleted file mode 100644 index 2fc8285ae7..0000000000 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Properties/Resource.Designer.cs +++ /dev/null @@ -1,135 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace ShortcutGuide.Properties { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resource { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resource() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ShortcutGuide.Properties.Resource", typeof(Resource).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to Error displaying the application's shortcuts. - /// - internal static string ErrorInAppParsing { - get { - return ResourceManager.GetString("ErrorInAppParsing", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to There has been an error displaying this category. - /// - internal static string ErrorInCategoryParsing { - get { - return ResourceManager.GetString("ErrorInCategoryParsing", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to No shortcuts pinned or recommended. - /// - internal static string NoShortcutsInOverview { - get { - return ResourceManager.GetString("NoShortcutsInOverview", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Overview. - /// - internal static string Overview { - get { - return ResourceManager.GetString("Overview", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Pin. - /// - internal static string PinShortcut { - get { - return ResourceManager.GetString("PinShortcut", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to No results found. - /// - internal static string SearchBlank { - get { - return ResourceManager.GetString("SearchBlank", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Shortcut Guide. - /// - internal static string Title { - get { - return ResourceManager.GetString("Title", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Unpin. - /// - internal static string UnpinShortcut { - get { - return ResourceManager.GetString("UnpinShortcut", resourceCulture); - } - } - } -} diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuide.Ui.csproj b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuide.Ui.csproj index 0fd7e74638..baaa25dd33 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuide.Ui.csproj +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuide.Ui.csproj @@ -100,19 +100,6 @@ - - - True - True - Resource.resx - - - - - ResXFileCodeGenerator - Resource.Designer.cs - - MSBuild:Compile diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml index 96acc70087..ecf79cfe49 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml @@ -2,8 +2,7 @@ + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml.cs index 84a79446eb..923d8da426 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/App.xaml.cs @@ -6,7 +6,7 @@ using Microsoft.UI.Xaml; namespace ShortcutGuide { - public partial class App : Application + public partial class App { public App() { @@ -17,7 +17,7 @@ namespace ShortcutGuide { _window = new MainWindow(); _window.Activate(); - _window.Closed += (s, e) => + _window.Closed += (_, _) => { _window = null; Current.Exit(); diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml index 4a6b398f56..a42615b474 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml @@ -4,7 +4,6 @@ x:Class="ShortcutGuide.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="using:ShortcutGuide" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml.cs index fe7b930c26..cfe427260a 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml.cs @@ -2,12 +2,10 @@ // 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.Globalization; using System.IO; using System.Text.Json; -using System.Threading.Tasks; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.UI; using Microsoft.UI.Windowing; @@ -16,18 +14,17 @@ using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; using ShortcutGuide.Helpers; using ShortcutGuide.Models; -using ShortcutGuide.Properties; using Windows.Foundation; using Windows.Graphics; using WinUIEx; -using static NativeMethods; +using static ShortcutGuide.NativeMethods; namespace ShortcutGuide { /// /// An empty window that can be used on its own or navigated to within a Frame. /// - public sealed partial class MainWindow : WindowEx + public sealed partial class MainWindow { private readonly string[] _currentApplicationIds; @@ -41,7 +38,7 @@ namespace ShortcutGuide InitializeComponent(); - Title = Resource.ResourceManager.GetString("Title", CultureInfo.InvariantCulture)!; + Title = ResourceLoaderInstance.ResourceLoader.GetString("Title")!; var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this); WindowHwnd = hwnd; @@ -92,7 +89,7 @@ namespace ShortcutGuide _appWindow = AppWindow.GetFromWindowId(windowId); GetCursorPos(out POINT lpPoint); - _appWindow.Move(lpPoint with { Y = lpPoint.Y - ((int)Height / 2), X = lpPoint.X - ((int)Width / 2) }); + _appWindow.Move(new POINT { Y = lpPoint.Y - ((int)Height / 2), X = lpPoint.X - ((int)Width / 2) }); float dpiScale = DpiHelper.GetDPIScaleForWindow((int)hwnd); @@ -104,13 +101,15 @@ namespace ShortcutGuide _setPosition = true; AppWindow.Changed += (_, a) => { - if (a.DidPresenterChange) + if (!a.DidPresenterChange) { - Rect monitorRect = DisplayHelper.GetWorkAreaForDisplayWithWindow(hwnd); - float dpiScale = DpiHelper.GetDPIScaleForWindow((int)hwnd); - this.SetWindowSize(monitorRect.Width / dpiScale, monitorRect.Height / dpiScale / 2); - _appWindow.Move(new PointInt32((int)monitorRect.X, (int)(monitorRect.Y + (int)(monitorRect.Height / 2)))); + return; } + + Rect monitorRect = DisplayHelper.GetWorkAreaForDisplayWithWindow(hwnd); + float dpiScale = DpiHelper.GetDPIScaleForWindow((int)hwnd); + this.SetWindowSize(monitorRect.Width / dpiScale, monitorRect.Height / dpiScale / 2); + _appWindow.Move(new PointInt32((int)monitorRect.X, (int)(monitorRect.Y + (int)(monitorRect.Height / 2)))); }; } diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml index 971809d29b..9b4d749e61 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml @@ -44,7 +44,7 @@ - + @@ -54,7 +54,7 @@ - + @@ -64,16 +64,16 @@ - + - + - + diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml.cs b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml.cs index ade3bb62b0..6bb32c65ad 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml.cs +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml.cs @@ -16,17 +16,16 @@ using Microsoft.UI.Xaml.Media.Animation; using Microsoft.UI.Xaml.Shapes; using ShortcutGuide.Helpers; using ShortcutGuide.Models; -using ShortcutGuide.Properties; using Windows.Foundation; namespace ShortcutGuide { - public sealed partial class ShortcutView : Page, INotifyPropertyChanged + public sealed partial class ShortcutView : INotifyPropertyChanged { private readonly DispatcherTimer _taskbarUpdateTimer = new() { Interval = TimeSpan.FromMilliseconds(500) }; private readonly bool _showTaskbarShortcuts; public static readonly CancellationTokenSource AnimationCancellationTokenSource = new(); - private ShortcutFile shortcutList = ManifestInterpreter.GetShortcutsOfApplication(ShortcutPageParameters.CurrentPageName); + private readonly ShortcutFile _shortcutList = ManifestInterpreter.GetShortcutsOfApplication(ShortcutPageParameters.CurrentPageName); public ShortcutView() { @@ -40,20 +39,20 @@ namespace ShortcutGuide { CategorySelector.Items.Add(new SelectorBarItem() { - Text = Resource.ResourceManager.GetString("Overview", CultureInfo.CurrentUICulture), + Text = ResourceLoaderInstance.ResourceLoader.GetString("Overview"), Name = i.ToString(CultureInfo.InvariantCulture), }); i++; - foreach (var category in shortcutList.Shortcuts) + foreach (var category in _shortcutList.Shortcuts) { switch (category.SectionName) { - case string name when name.StartsWith("", StringComparison.Ordinal): + case { } name when name.StartsWith("", StringComparison.Ordinal): _showTaskbarShortcuts = true; break; - case string name when name.StartsWith('<') && name.EndsWith('>'): + case { } name when name.StartsWith('<') && name.EndsWith('>'): break; default: CategorySelector.Items.Add(new SelectorBarItem() { Text = category.SectionName, Name = i.ToString(CultureInfo.InvariantCulture) }); @@ -66,7 +65,7 @@ namespace ShortcutGuide CategorySelector.SelectedItem = CategorySelector.Items[0]; CategorySelector.SelectionChanged += CategorySelector_SelectionChanged; - foreach (var shortcut in shortcutList.Shortcuts[0].Properties) + foreach (var shortcut in _shortcutList.Shortcuts[0].Properties) { ShortcutListElement.Items.Add((ShortcutTemplateDataObject)shortcut); } @@ -92,10 +91,10 @@ namespace ShortcutGuide { OverviewStackPanel.Visibility = Visibility.Collapsed; ErrorMessage.Visibility = Visibility.Visible; - ErrorMessage.Text = Resource.ResourceManager.GetString("ErrorInAppParsing", CultureInfo.CurrentUICulture); + ErrorMessage.Text = ResourceLoaderInstance.ResourceLoader.GetString("ErrorInAppParsing"); } - Unloaded += (s, e) => + Unloaded += (_, _) => { AnimationCancellationTokenSource.Cancel(); _taskbarUpdateTimer.Tick -= UpdateTaskbarIndicators; @@ -105,7 +104,7 @@ namespace ShortcutGuide private void UpdateTaskbarIndicators(object? sender, object? e) { - var buttons = TasklistPositions.GetButtons(); + NativeMethods.TasklistButton[] buttons = TasklistPositions.GetButtons(); Canvas[] canvases = [ TaskbarIndicator1, TaskbarIndicator2, @@ -171,7 +170,7 @@ namespace ShortcutGuide set { _contentHeight = value; - OnPropertyChanged(nameof(ContentHeight)); + OnPropertyChanged(); } } @@ -191,13 +190,8 @@ namespace ShortcutGuide PinnedListTitle.Visibility = Visibility.Visible; ShortcutListElement.Visibility = Visibility.Collapsed; - foreach (var list in shortcutList.Shortcuts) + foreach (var list in _shortcutList.Shortcuts) { - if (list.Properties == null) - { - continue; - } - foreach (var shortcut in list.Properties) { if (shortcut.Recommended) @@ -228,7 +222,7 @@ namespace ShortcutGuide { OverviewStackPanel.Visibility = Visibility.Collapsed; ErrorMessage.Visibility = Visibility.Visible; - ErrorMessage.Text = Resource.ResourceManager.GetString("NoShortcutsInOverview", CultureInfo.CurrentUICulture); + ErrorMessage.Text = ResourceLoaderInstance.ResourceLoader.GetString("NoShortcutsInOverview"); } } @@ -256,13 +250,8 @@ namespace ShortcutGuide OverviewStackPanel.Visibility = Visibility.Collapsed; - foreach (var list in shortcutList.Shortcuts) + foreach (var list in _shortcutList.Shortcuts) { - if (list.Properties == null) - { - continue; - } - foreach (var shortcut in list.Properties) { if (shortcut.Name.Contains(filter, StringComparison.InvariantCultureIgnoreCase)) @@ -274,7 +263,7 @@ namespace ShortcutGuide } else { - foreach (var shortcut in shortcutList.Shortcuts[int.Parse(CategorySelector.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties) + foreach (var shortcut in _shortcutList.Shortcuts[int.Parse(CategorySelector.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties) { if (shortcut.Name.Contains(filter, StringComparison.InvariantCultureIgnoreCase)) { @@ -283,12 +272,14 @@ namespace ShortcutGuide } } - if (ShortcutListElement.Items.Count == 0) + if (ShortcutListElement.Items.Count != 0) { - ShortcutListElement.Visibility = Visibility.Collapsed; - ErrorMessage.Visibility = Visibility.Visible; - ErrorMessage.Text = Resource.ResourceManager.GetString("SearchBlank", CultureInfo.CurrentUICulture); + return; } + + ShortcutListElement.Visibility = Visibility.Collapsed; + ErrorMessage.Visibility = Visibility.Visible; + ErrorMessage.Text = ResourceLoaderInstance.ResourceLoader.GetString("SearchBlank"); } public void CategorySelector_SelectionChanged(SelectorBar sender, SelectorBarSelectionChangedEventArgs e) @@ -311,7 +302,7 @@ namespace ShortcutGuide return; } - foreach (var shortcut in shortcutList.Shortcuts[int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties) + foreach (var shortcut in _shortcutList.Shortcuts[int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties) { ShortcutListElement.Items.Add((ShortcutTemplateDataObject)shortcut); } @@ -319,7 +310,7 @@ namespace ShortcutGuide catch (NullReferenceException) { ErrorMessage.Visibility = Visibility.Visible; - ErrorMessage.Text = Resource.ResourceManager.GetString("ErrorInCategoryParsing", CultureInfo.CurrentUICulture); + ErrorMessage.Text = ResourceLoaderInstance.ResourceLoader.GetString("ErrorInCategoryParsing"); } FilterBy(_searchFilter); @@ -362,7 +353,7 @@ namespace ShortcutGuide bool isItemPinned = ShortcutPageParameters.PinnedShortcuts[ShortcutPageParameters.CurrentPageName].Contains(originalObject); - pinItem.Text = isItemPinned ? Resource.ResourceManager.GetString("UnpinShortcut", CultureInfo.CurrentUICulture) : Resource.ResourceManager.GetString("PinShortcut", CultureInfo.CurrentUICulture); + pinItem.Text = isItemPinned ? ResourceLoaderInstance.ResourceLoader.GetString("UnpinShortcut") : ResourceLoaderInstance.ResourceLoader.GetString("PinShortcut"); pinItem.Icon = new SymbolIcon(isItemPinned ? Symbol.UnPin : Symbol.Pin); } } diff --git a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Properties/Resource.resx b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Strings/en-us/Resources.resw similarity index 99% rename from src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Properties/Resource.resx rename to src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Strings/en-us/Resources.resw index a91a80368d..de6af34d87 100644 --- a/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Properties/Resource.resx +++ b/src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Strings/en-us/Resources.resw @@ -1,4 +1,4 @@ - +