mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
[Feature] PowerToys hotkey conflict detection (#41029)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request Implements comprehensive hotkey conflict detection and resolution system for PowerToys, providing real-time conflict checking and centralized management interface. ## PR Checklist - [ ] **Closes:** #xxx - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [x] **Localization:** All end-user-facing strings can be localized - [x] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [x] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: [Shortcut conflict detction dev spec](https://github.com/MicrosoftDocs/windows-dev-docs/pull/5519) ## TODO Lists - [x] Add real-time hotkey validation functionality to the hotkey dialog - [x] Immediately detect conflicts and update shortcut conflict status after applying new shortcuts - [x] Return conflict list from runner hotkey conflict detector for conflict checking. - [x] Implement the Tooltip for every shortcut control - [x] Add dialog UI for showing all the shortcut conflicts - [x] Support changing shortcut directly inside the shortcut conflict window/dialog, no need to nav to the settings page. - [x] Redesign the `ShortcutConflictDialogContentControl` to align with the spec - [x] Add navigating and changing hotkey auctionability to the `ShortcutConflictDialogContentControl` - [x] Add telemetry. Impemented in [another PR](https://github.com/shuaiyuanxx/PowerToys/pull/47) ## Shortcut Conflict Support Modules  <details> <summary>Demo videos</summary> https://github.com/user-attachments/assets/476d992c-c6ca-4bcd-a3f2-b26cc612d1b9 https://github.com/user-attachments/assets/1c1a2537-de54-4db2-bdbf-6f1908ff1ce7 https://github.com/user-attachments/assets/9c992254-fc2b-402c-beec-20fceef25e6b https://github.com/user-attachments/assets/d66abc1c-b8bf-45f8-a552-ec989dab310f </details> <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed Manually validation performed. --------- Signed-off-by: Shawn Yuan <shuaiyuan@microsoft.com> Signed-off-by: Shuai Yuan <shuai.yuan.zju@gmail.com> Co-authored-by: Niels Laute <niels.laute@live.nl>
This commit is contained in:
@@ -0,0 +1,384 @@
|
||||
// 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.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
using ManagedCommon;
|
||||
using Microsoft.PowerToys.Settings.UI.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.HotkeyConflicts;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||
using Microsoft.PowerToys.Settings.UI.SerializationContext;
|
||||
using Microsoft.PowerToys.Settings.UI.Services;
|
||||
using Microsoft.Windows.ApplicationModel.Resources;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
{
|
||||
public class ShortcutConflictViewModel : PageViewModelBase
|
||||
{
|
||||
private readonly SettingsFactory _settingsFactory;
|
||||
private readonly Func<string, int> _ipcMSGCallBackFunc;
|
||||
private readonly Dispatcher _dispatcher;
|
||||
|
||||
private bool _disposed;
|
||||
private AllHotkeyConflictsData _conflictsData = new();
|
||||
private ObservableCollection<HotkeyConflictGroupData> _conflictItems = new();
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
public ShortcutConflictViewModel(
|
||||
ISettingsUtils settingsUtils,
|
||||
ISettingsRepository<GeneralSettings> settingsRepository,
|
||||
Func<string, int> ipcMSGCallBackFunc)
|
||||
{
|
||||
_dispatcher = Dispatcher.CurrentDispatcher;
|
||||
_ipcMSGCallBackFunc = ipcMSGCallBackFunc ?? throw new ArgumentNullException(nameof(ipcMSGCallBackFunc));
|
||||
resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
||||
|
||||
// Create SettingsFactory
|
||||
_settingsFactory = new SettingsFactory(settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)));
|
||||
}
|
||||
|
||||
public AllHotkeyConflictsData ConflictsData
|
||||
{
|
||||
get => _conflictsData;
|
||||
set
|
||||
{
|
||||
if (Set(ref _conflictsData, value))
|
||||
{
|
||||
UpdateConflictItems();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<HotkeyConflictGroupData> ConflictItems
|
||||
{
|
||||
get => _conflictItems;
|
||||
private set => Set(ref _conflictItems, value);
|
||||
}
|
||||
|
||||
protected override string ModuleName => "ShortcutConflictsWindow";
|
||||
|
||||
private IHotkeyConfig GetModuleSettings(string moduleKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
// MouseWithoutBorders and Peek settings may be changed by the logic in the utility as machines connect.
|
||||
// We need to get a fresh version every time instead of using a repository.
|
||||
if (string.Equals(moduleKey, MouseWithoutBordersSettings.ModuleName, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(moduleKey, PeekSettings.ModuleName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return _settingsFactory.GetFreshSettings(moduleKey);
|
||||
}
|
||||
|
||||
// For other modules, get the settings from SettingsRepository
|
||||
return _settingsFactory.GetSettings(moduleKey);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error loading settings for {moduleKey}: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnConflictsUpdated(object sender, AllHotkeyConflictsEventArgs e)
|
||||
{
|
||||
_dispatcher.BeginInvoke(() =>
|
||||
{
|
||||
ConflictsData = e.Conflicts ?? new AllHotkeyConflictsData();
|
||||
});
|
||||
}
|
||||
|
||||
private void UpdateConflictItems()
|
||||
{
|
||||
var items = new ObservableCollection<HotkeyConflictGroupData>();
|
||||
|
||||
ProcessConflicts(ConflictsData?.InAppConflicts, false, items);
|
||||
ProcessConflicts(ConflictsData?.SystemConflicts, true, items);
|
||||
|
||||
ConflictItems = items;
|
||||
OnPropertyChanged(nameof(ConflictItems));
|
||||
}
|
||||
|
||||
private void ProcessConflicts(IEnumerable<HotkeyConflictGroupData> conflicts, bool isSystemConflict, ObservableCollection<HotkeyConflictGroupData> items)
|
||||
{
|
||||
if (conflicts == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var conflict in conflicts)
|
||||
{
|
||||
ProcessConflictGroup(conflict, isSystemConflict);
|
||||
items.Add(conflict);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessConflictGroup(HotkeyConflictGroupData conflict, bool isSystemConflict)
|
||||
{
|
||||
foreach (var module in conflict.Modules)
|
||||
{
|
||||
SetupModuleData(module, isSystemConflict);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetupModuleData(ModuleHotkeyData module, bool isSystemConflict)
|
||||
{
|
||||
try
|
||||
{
|
||||
var settings = GetModuleSettings(module.ModuleName);
|
||||
var allHotkeyAccessors = settings.GetAllHotkeyAccessors();
|
||||
var hotkeyAccessor = allHotkeyAccessors[module.HotkeyID];
|
||||
|
||||
if (hotkeyAccessor != null)
|
||||
{
|
||||
// Get current hotkey settings (fresh from file) using the accessor's getter
|
||||
module.HotkeySettings = hotkeyAccessor.Value;
|
||||
|
||||
// Set header using localization key
|
||||
module.Header = GetHotkeyLocalizationHeader(module.ModuleName, module.HotkeyID, hotkeyAccessor.LocalizationHeaderKey);
|
||||
module.IsSystemConflict = isSystemConflict;
|
||||
|
||||
// Set module display info
|
||||
var moduleType = settings.GetModuleType();
|
||||
module.ModuleType = moduleType;
|
||||
var displayName = resourceLoader.GetString(ModuleHelper.GetModuleLabelResourceName(moduleType));
|
||||
module.DisplayName = displayName;
|
||||
module.IconPath = ModuleHelper.GetModuleTypeFluentIconName(moduleType);
|
||||
|
||||
if (module.HotkeySettings != null)
|
||||
{
|
||||
SetConflictProperties(module.HotkeySettings, isSystemConflict);
|
||||
}
|
||||
|
||||
module.PropertyChanged -= OnModuleHotkeyDataPropertyChanged;
|
||||
module.PropertyChanged += OnModuleHotkeyDataPropertyChanged;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Could not find hotkey accessor for {module.ModuleName}.{module.HotkeyID}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error setting up module data for {module.ModuleName}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void SetConflictProperties(HotkeySettings settings, bool isSystemConflict)
|
||||
{
|
||||
settings.HasConflict = true;
|
||||
settings.IsSystemConflict = isSystemConflict;
|
||||
}
|
||||
|
||||
private void OnModuleHotkeyDataPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (sender is ModuleHotkeyData moduleData && e.PropertyName == nameof(ModuleHotkeyData.HotkeySettings))
|
||||
{
|
||||
UpdateModuleHotkeySettings(moduleData.ModuleName, moduleData.HotkeyID, moduleData.HotkeySettings);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateModuleHotkeySettings(string moduleName, int hotkeyID, HotkeySettings newHotkeySettings)
|
||||
{
|
||||
try
|
||||
{
|
||||
var settings = GetModuleSettings(moduleName);
|
||||
var accessors = settings.GetAllHotkeyAccessors();
|
||||
|
||||
var hotkeyAccessor = accessors[hotkeyID];
|
||||
|
||||
// Use the accessor's setter to update the hotkey settings
|
||||
hotkeyAccessor.Value = newHotkeySettings;
|
||||
|
||||
if (settings is ISettingsConfig settingsConfig)
|
||||
{
|
||||
// No need to save settings here, the runner will call module interface to save it
|
||||
// SaveSettingsToFile(settings);
|
||||
|
||||
// Send IPC notification using the same format as other ViewModels
|
||||
SendConfigMSG(settingsConfig, moduleName);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error updating hotkey settings for {moduleName}.{hotkeyID}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveModuleSettingsAndNotify(string moduleName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var settings = GetModuleSettings(moduleName);
|
||||
|
||||
if (settings is ISettingsConfig settingsConfig)
|
||||
{
|
||||
// No need to save settings here, the runner will call module interface to save it
|
||||
// SaveSettingsToFile(settings);
|
||||
|
||||
// Send IPC notification using the same format as other ViewModels
|
||||
SendConfigMSG(settingsConfig, moduleName);
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"Saved settings and sent IPC notification for module: {moduleName}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error saving settings and notifying for {moduleName}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveSettingsToFile(IHotkeyConfig settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get the repository for this settings type using reflection
|
||||
var settingsType = settings.GetType();
|
||||
var repositoryMethod = typeof(SettingsFactory).GetMethod("GetRepository");
|
||||
if (repositoryMethod != null)
|
||||
{
|
||||
var genericMethod = repositoryMethod.MakeGenericMethod(settingsType);
|
||||
var repository = genericMethod.Invoke(_settingsFactory, null);
|
||||
|
||||
if (repository != null)
|
||||
{
|
||||
var saveMethod = repository.GetType().GetMethod("SaveSettingsToFile");
|
||||
saveMethod?.Invoke(repository, null);
|
||||
System.Diagnostics.Debug.WriteLine($"Saved settings to file for type: {settingsType.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error saving settings to file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends IPC notification using the same format as other ViewModels
|
||||
/// </summary>
|
||||
private void SendConfigMSG(ISettingsConfig settingsConfig, string moduleName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jsonTypeInfo = GetJsonTypeInfo(settingsConfig.GetType());
|
||||
var serializedSettings = jsonTypeInfo != null
|
||||
? JsonSerializer.Serialize(settingsConfig, jsonTypeInfo)
|
||||
: JsonSerializer.Serialize(settingsConfig);
|
||||
|
||||
var ipcMessage = string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
|
||||
moduleName,
|
||||
serializedSettings);
|
||||
|
||||
var result = _ipcMSGCallBackFunc(ipcMessage);
|
||||
System.Diagnostics.Debug.WriteLine($"Sent IPC notification for {moduleName}, result: {result}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error sending IPC notification for {moduleName}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private JsonTypeInfo GetJsonTypeInfo(Type settingsType)
|
||||
{
|
||||
try
|
||||
{
|
||||
var contextType = typeof(SourceGenerationContextContext);
|
||||
var defaultProperty = contextType.GetProperty("Default", BindingFlags.Public | BindingFlags.Static);
|
||||
var defaultContext = defaultProperty?.GetValue(null) as JsonSerializerContext;
|
||||
|
||||
if (defaultContext != null)
|
||||
{
|
||||
var typeInfoProperty = contextType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.FirstOrDefault(p => p.PropertyType.IsGenericType &&
|
||||
p.PropertyType.GetGenericTypeDefinition() == typeof(JsonTypeInfo<>) &&
|
||||
p.PropertyType.GetGenericArguments()[0] == settingsType);
|
||||
|
||||
return typeInfoProperty?.GetValue(defaultContext) as JsonTypeInfo;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error getting JsonTypeInfo for {settingsType.Name}: {ex.Message}");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private string GetHotkeyLocalizationHeader(string moduleName, int hotkeyID, string headerKey)
|
||||
{
|
||||
// Handle AdvancedPaste custom actions
|
||||
if (string.Equals(moduleName, AdvancedPasteSettings.ModuleName, StringComparison.OrdinalIgnoreCase)
|
||||
&& hotkeyID > 9)
|
||||
{
|
||||
return headerKey;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return resourceLoader.GetString($"{headerKey}/Header");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error getting hotkey header for {moduleName}.{hotkeyID}: {ex.Message}");
|
||||
return headerKey; // Return the key itself as fallback
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
UnsubscribeFromEvents();
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void UnsubscribeFromEvents()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ConflictItems != null)
|
||||
{
|
||||
foreach (var conflictGroup in ConflictItems)
|
||||
{
|
||||
if (conflictGroup?.Modules != null)
|
||||
{
|
||||
foreach (var module in conflictGroup.Modules)
|
||||
{
|
||||
if (module != null)
|
||||
{
|
||||
module.PropertyChanged -= OnModuleHotkeyDataPropertyChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error unsubscribing from events: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user