From bb2049411bb93aa68bc01e5fcccdd6d311d7e370 Mon Sep 17 00:00:00 2001 From: Arjun Balgovind <32061677+arjunbalgovind@users.noreply.github.com> Date: Fri, 10 Jul 2020 17:07:28 -0700 Subject: [PATCH] [Keyboard Manager] Add JSON support for App Specific shortcuts (#4840) * Enable app specific shortcut remapping * Fixed lowercase function call * Add test file * Moved GetForegroundProcess to II and added tests * Fixed runtime error while testing due to heap allocation across dll boundary * Renamed function * Changed shortcutBuffer type * Linked App specific UI to backend * Added shortcut validation logic on TextBox LostFocus handler * Moved Validate function and changed default text * Changed to case insensitive warning check * Changed to case insensitive warning check at OnClickAccept * Fixed alignment and spacing issues * Added app-specific JSON support in backend * Updated landing page * Make listview horizontally scrollable * Added tests * Consider all case variants of All Apps in textbox to be global shortcuts --- .../AppSpecificKeysDataModel.cs | 32 +++++ .../KeysDataModel.cs | 2 +- .../RemapKeysDataModel.cs | 2 +- .../ShortcutsKeyDataModel.cs | 4 + .../ViewModels/KeyboardManagerViewModel.cs | 12 +- .../Views/KeyboardManagerPage.xaml | 94 ++++++++++++- ...crosoft.PowerToys.Settings.UnitTest.csproj | 1 + .../ViewModelTests/KeyboardManager.cs | 132 ++++++++++++++++++ .../common/KeyboardManagerConstants.h | 6 + .../common/KeyboardManagerState.cpp | 19 +++ src/modules/keyboardmanager/dll/dllmain.cpp | 102 ++++++++++---- .../keyboardmanager/ui/KeyDropDownControl.cpp | 19 ++- .../keyboardmanager/ui/ShortcutControl.cpp | 13 +- 13 files changed, 404 insertions(+), 34 deletions(-) create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/AppSpecificKeysDataModel.cs create mode 100644 src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/KeyboardManager.cs diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/AppSpecificKeysDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/AppSpecificKeysDataModel.cs new file mode 100644 index 0000000000..1aa1230e58 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/AppSpecificKeysDataModel.cs @@ -0,0 +1,32 @@ +// 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.Collections.Generic; +using System.Linq; +using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Utilities; + +namespace Microsoft.PowerToys.Settings.UI.Lib +{ + public class AppSpecificKeysDataModel : KeysDataModel + { + [JsonPropertyName("targetApp")] + public string TargetApp { get; set; } + + public new List GetOriginalKeys() + { + return base.GetOriginalKeys(); + } + + public new List GetNewRemapKeys() + { + return base.GetNewRemapKeys(); + } + + public bool Compare(AppSpecificKeysDataModel arg) + { + return OriginalKeys.Equals(arg.OriginalKeys) && NewRemapKeys.Equals(arg.NewRemapKeys) && TargetApp.Equals(arg.TargetApp); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs index d9140f7da8..d33a020e15 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/KeysDataModel.cs @@ -2,10 +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 Microsoft.PowerToys.Settings.UI.Lib.Utilities; using System.Collections.Generic; using System.Linq; using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Lib.Utilities; namespace Microsoft.PowerToys.Settings.UI.Lib { diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs index 67477c4214..4712721b47 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/RemapKeysDataModel.cs @@ -17,4 +17,4 @@ namespace Microsoft.PowerToys.Settings.UI.Lib InProcessRemapKeys = new List(); } } -} \ No newline at end of file +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs index ad245f4808..944904dec4 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/ShortcutsKeyDataModel.cs @@ -12,9 +12,13 @@ namespace Microsoft.PowerToys.Settings.UI.Lib [JsonPropertyName("global")] public List GlobalRemapShortcuts { get; set; } + [JsonPropertyName("appSpecific")] + public List AppSpecificRemapShortcuts { get; set; } + public ShortcutsKeyDataModel() { GlobalRemapShortcuts = new List(); + AppSpecificRemapShortcuts = new List(); } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs index 7fe6ab3c77..858d11ebe0 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs @@ -74,7 +74,6 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels PowerToyName, settings.Properties.ActiveConfiguration.Value + JsonFileType, OnConfigFileUpdate); - } public bool Enabled @@ -113,17 +112,22 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } - public List RemapShortcuts + public static List CombineShortcutLists(List globalShortcutList, List appSpecificShortcutList) + { + return globalShortcutList.ConvertAll(x => new AppSpecificKeysDataModel { OriginalKeys = x.OriginalKeys, NewRemapKeys = x.NewRemapKeys, TargetApp = "All Apps" }).Concat(appSpecificShortcutList).ToList(); + } + + public List RemapShortcuts { get { if (profile != null) { - return profile.RemapShortcuts.GlobalRemapShortcuts; + return CombineShortcutLists(profile.RemapShortcuts.GlobalRemapShortcuts, profile.RemapShortcuts.AppSpecificRemapShortcuts); } else { - return new List(); + return new List(); } } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml index 926b7caa7a..d509eea86f 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml @@ -81,6 +81,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -192,7 +281,7 @@