diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomActionDataModel.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomActionDataModel.cs new file mode 100644 index 000000000..5b26ed202 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomActionDataModel.cs @@ -0,0 +1,17 @@ +// 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.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib.CustomAction +{ + public class CustomActionDataModel + { + [JsonPropertyName("action_name")] + public string Name { get; set; } + + [JsonPropertyName("value")] + public string Value { get; set; } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomNamePolicy.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomNamePolicy.cs new file mode 100644 index 000000000..e0db7c114 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomNamePolicy.cs @@ -0,0 +1,24 @@ +// 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.Text.Json; + +namespace Microsoft.PowerToys.Settings.UI.Lib.CustomAction +{ + public class CustomNamePolicy : JsonNamingPolicy + { + private Func convertDelegate; + + public CustomNamePolicy(Func convertDelegate) + { + this.convertDelegate = convertDelegate; + } + + public override string ConvertName(string name) + { + return convertDelegate(name); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/ModuleCustomAction.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/ModuleCustomAction.cs new file mode 100644 index 000000000..052aa7883 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/ModuleCustomAction.cs @@ -0,0 +1,11 @@ +// 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 Microsoft.PowerToys.Settings.UI.Lib.CustomAction +{ + public class ModuleCustomAction + { + public CustomActionDataModel ModuleAction { get; set; } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/SendCustomAction.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/SendCustomAction.cs new file mode 100644 index 000000000..3dab58e83 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/SendCustomAction.cs @@ -0,0 +1,35 @@ +// 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.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.PowerToys.Settings.UI.Lib.CustomAction +{ + public class SendCustomAction + { + private readonly string moduleName; + + public SendCustomAction(string moduleName) + { + this.moduleName = moduleName; + } + + [JsonPropertyName("action")] + public ModuleCustomAction Action { get; set; } + + public string ToJsonString() + { + var jsonSerializerOptions = new JsonSerializerOptions + { + PropertyNamingPolicy = new CustomNamePolicy((propertyName) => + { + return propertyName.Equals("ModuleAction") ? moduleName : propertyName; + }), + }; + + return JsonSerializer.Serialize(this, jsonSerializerOptions); + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs new file mode 100644 index 000000000..8211463d6 --- /dev/null +++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs @@ -0,0 +1,47 @@ +// 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.Diagnostics; +using System.Runtime.InteropServices; +using Microsoft.PowerToys.Settings.UI.Lib.CustomAction; + +namespace Microsoft.PowerToys.Settings.UI.Lib.Utilities +{ + public class Helper + { + public static bool AllowRunnerToForeground() + { + var result = false; + var processes = Process.GetProcessesByName("PowerToys"); + if (processes.Length > 0) + { + var pid = processes[0].Id; + result = AllowSetForegroundWindow(pid); + } + + return result; + } + + public static string GetSerializedCustomAction(string moduleName, string actionName, string actionValue) + { + var customAction = new CustomActionDataModel + { + Name = actionName, + Value = actionValue, + }; + + var moduleCustomAction = new ModuleCustomAction + { + ModuleAction = customAction, + }; + + var sendCustomAction = new SendCustomAction(moduleName); + sendCustomAction.Action = moduleCustomAction; + return sendCustomAction.ToJsonString(); + } + + [DllImport("user32.dll")] + private static extern bool AllowSetForegroundWindow(int dwProcessId); + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs index 75232a2b9..dc3435ba8 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs @@ -1,15 +1,50 @@ // 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.PowerToys.Settings.UI.Helpers; - -namespace Microsoft.PowerToys.Settings.UI.ViewModels -{ - public class KeyboardManagerViewModel : Observable - { - public KeyboardManagerViewModel() - { - } - } -} + +using System.Threading.Tasks; +using System.Windows.Input; +using Microsoft.PowerToys.Settings.UI.Helpers; +using Microsoft.PowerToys.Settings.UI.Lib.Utilities; +using Microsoft.PowerToys.Settings.UI.Views; + +namespace Microsoft.PowerToys.Settings.UI.ViewModels +{ + public class KeyboardManagerViewModel : Observable + { + private ICommand remapKeyboardCommand; + private ICommand editShortcutCommand; + + public ICommand RemapKeyboardCommand => remapKeyboardCommand ?? (remapKeyboardCommand = new RelayCommand(OnRemapKeyboard)); + + public ICommand EditShortcutCommand => editShortcutCommand ?? (editShortcutCommand = new RelayCommand(OnEditShortcut)); + + public KeyboardManagerViewModel() + { + } + + private async void OnRemapKeyboard() + { + await Task.Run(() => OnRemapKeyboardBackground()); + } + + private async void OnEditShortcut() + { + await Task.Run(() => OnEditShortcutBackground()); + } + + private async Task OnRemapKeyboardBackground() + { + Helper.AllowRunnerToForeground(); + ShellPage.DefaultSndMSGCallback(Helper.GetSerializedCustomAction("Keyboard Manager", "RemapKeyboard", "Create Remap Keyboard Window")); + await Task.CompletedTask; + } + + private async Task OnEditShortcutBackground() + { + Helper.AllowRunnerToForeground(); + ShellPage.DefaultSndMSGCallback(Helper.GetSerializedCustomAction("Keyboard Manager", "EditShortcut", "Create Edit Shortcut Window")); + await Task.CompletedTask; + } + } +} diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml index 95f7d3a13..2f1eb3bb4 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml +++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/KeyboardManagerPage.xaml @@ -122,7 +122,8 @@