-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate keyboard manager with settings v2[Part-2] (#2107)
* Added CustomAction Data Models * Updated data model * Updated Button command trigger for keyboard manager * Integerated custom actions in Keyboard manager module backend * Allow Runner to launch windows on foreground * Refactor and moved the execution logic to background thread * Removed non-required unlock calls * Fixed typo * Resolve PR comments
- Loading branch information
Showing
13 changed files
with
292 additions
and
40 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomActionDataModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/CustomNamePolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, string> convertDelegate; | ||
|
||
public CustomNamePolicy(Func<string, string> convertDelegate) | ||
{ | ||
this.convertDelegate = convertDelegate; | ||
} | ||
|
||
public override string ConvertName(string name) | ||
{ | ||
return convertDelegate(name); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/ModuleCustomAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/core/Microsoft.PowerToys.Settings.UI.Lib/CustomAction/SendCustomAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/core/Microsoft.PowerToys.Settings.UI.Lib/Utilities/Helper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
59 changes: 47 additions & 12 deletions
59
src/core/Microsoft.PowerToys.Settings.UI/ViewModels/KeyboardManagerViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.