[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

![image](https://github.com/user-attachments/assets/3915174e-d1e7-4f86-8835-2a1bafcc85c9)

<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:
Shawn Yuan
2025-08-20 09:31:52 +08:00
committed by GitHub
parent ce4d8dc11e
commit 75526b9580
104 changed files with 4578 additions and 366 deletions

View File

@@ -13,8 +13,8 @@ using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Timers;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -24,15 +24,16 @@ using Windows.Security.Credentials;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class AdvancedPasteViewModel : Observable, IDisposable
public partial class AdvancedPasteViewModel : PageViewModelBase
{
private static readonly HashSet<string> WarnHotkeys = ["Ctrl + V", "Ctrl + Shift + V"];
private bool disposedValue;
private bool _disposed;
// Delay saving of settings in order to avoid calling save multiple times and hitting file in use exception. If there is no other request to save settings in given interval, we proceed to save it; otherwise, we schedule saving it after this interval
private const int SaveSettingsDelayInMs = 500;
protected override string ModuleName => AdvancedPasteSettings.ModuleName;
private GeneralSettings GeneralSettingsConfig { get; set; }
private readonly ISettingsUtils _settingsUtils;
@@ -98,6 +99,36 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
UpdateCustomActionsCanMoveUpDown();
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeySettings = new List<HotkeySettings>
{
PasteAsPlainTextShortcut,
AdvancedPasteUIShortcut,
PasteAsMarkdownShortcut,
PasteAsJsonShortcut,
};
foreach (var action in _additionalActions.GetAllActions())
{
if (action is AdvancedPasteAdditionalAction additionalAction)
{
hotkeySettings.Add(additionalAction.Shortcut);
}
}
// Custom actions do not have localization header, just use the action name.
foreach (var customAction in _customActions)
{
hotkeySettings.Add(customAction.Shortcut);
}
return new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = hotkeySettings.ToArray(),
};
}
private void InitializeEnabledValue()
{
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredAdvancedPasteEnabledValue();
@@ -264,9 +295,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (_advancedPasteSettings.Properties.AdvancedPasteUIShortcut != value)
{
_advancedPasteSettings.Properties.AdvancedPasteUIShortcut = value ?? AdvancedPasteProperties.DefaultAdvancedPasteUIShortcut;
OnPropertyChanged(nameof(AdvancedPasteUIShortcut));
OnPropertyChanged(nameof(IsConflictingCopyShortcut));
OnPropertyChanged(nameof(AdvancedPasteUIShortcut));
SaveAndNotifySettings();
}
}
@@ -280,9 +310,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (_advancedPasteSettings.Properties.PasteAsPlainTextShortcut != value)
{
_advancedPasteSettings.Properties.PasteAsPlainTextShortcut = value ?? AdvancedPasteProperties.DefaultPasteAsPlainTextShortcut;
OnPropertyChanged(nameof(PasteAsPlainTextShortcut));
OnPropertyChanged(nameof(IsConflictingCopyShortcut));
OnPropertyChanged(nameof(PasteAsPlainTextShortcut));
SaveAndNotifySettings();
}
}
@@ -296,9 +325,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (_advancedPasteSettings.Properties.PasteAsMarkdownShortcut != value)
{
_advancedPasteSettings.Properties.PasteAsMarkdownShortcut = value ?? new HotkeySettings();
OnPropertyChanged(nameof(PasteAsMarkdownShortcut));
OnPropertyChanged(nameof(IsConflictingCopyShortcut));
OnPropertyChanged(nameof(PasteAsMarkdownShortcut));
SaveAndNotifySettings();
}
}
@@ -312,9 +340,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (_advancedPasteSettings.Properties.PasteAsJsonShortcut != value)
{
_advancedPasteSettings.Properties.PasteAsJsonShortcut = value ?? new HotkeySettings();
OnPropertyChanged(nameof(PasteAsJsonShortcut));
OnPropertyChanged(nameof(IsConflictingCopyShortcut));
OnPropertyChanged(nameof(PasteAsJsonShortcut));
SaveAndNotifySettings();
}
}
@@ -399,23 +426,31 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
OnPropertyChanged(nameof(ShowClipboardHistoryIsGpoConfiguredInfoBar));
}
protected virtual void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposed)
{
if (disposing)
{
_delayedTimer.Dispose();
_delayedTimer?.Dispose();
foreach (var action in _additionalActions.GetAllActions())
{
action.PropertyChanged -= OnAdditionalActionPropertyChanged;
}
foreach (var customAction in _customActions)
{
customAction.PropertyChanged -= OnCustomActionPropertyChanged;
}
_customActions.CollectionChanged -= OnCustomActionsCollectionChanged;
}
disposedValue = true;
_disposed = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
internal void DisableAI()

View File

@@ -3,11 +3,13 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -16,8 +18,10 @@ using Microsoft.PowerToys.Settings.UI.SerializationContext;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class AlwaysOnTopViewModel : Observable
public partial class AlwaysOnTopViewModel : PageViewModelBase
{
protected override string ModuleName => AlwaysOnTopSettings.ModuleName;
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
@@ -75,6 +79,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [Hotkey],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
@@ -11,6 +12,7 @@ using System.Text.Json;
using System.Text.RegularExpressions;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -21,8 +23,10 @@ using Windows.Management.Deployment;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class CmdPalViewModel : Observable
public class CmdPalViewModel : PageViewModelBase
{
protected override string ModuleName => "CmdPal";
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _isEnabled;
private HotkeySettings _hotkey;
@@ -88,6 +92,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [Hotkey],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;

View File

@@ -9,9 +9,9 @@ using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Timers;
using global::PowerToys.GPOWrapper;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Enumerations;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
@@ -20,9 +20,11 @@ using Microsoft.PowerToys.Settings.UI.SerializationContext;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class ColorPickerViewModel : Observable, IDisposable
public partial class ColorPickerViewModel : PageViewModelBase
{
private bool disposedValue;
protected override string ModuleName => ColorPickerSettings.ModuleName;
private bool _disposed;
// Delay saving of settings in order to avoid calling save multiple times and hitting file in use exception. If there is no other request to save settings in given interval, we proceed to save it; otherwise, we schedule saving it after this interval
private const int SaveSettingsDelayInMs = 500;
@@ -87,6 +89,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [ActivationShortcut],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;
@@ -409,23 +421,25 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
OnPropertyChanged(nameof(IsEnabled));
}
protected virtual void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposed)
{
if (disposing)
{
_delayedTimer.Dispose();
_delayedTimer?.Dispose();
foreach (var colorFormat in ColorFormats)
{
colorFormat.PropertyChanged -= ColorFormat_PropertyChanged;
}
ColorFormats.CollectionChanged -= ColorFormats_CollectionChanged;
}
disposedValue = true;
_disposed = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
internal ColorFormatModel GetNewColorFormatModel()

View File

@@ -3,11 +3,12 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -16,8 +17,10 @@ using Microsoft.PowerToys.Settings.UI.SerializationContext;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class CropAndLockViewModel : Observable
public partial class CropAndLockViewModel : PageViewModelBase
{
protected override string ModuleName => CropAndLockSettings.ModuleName;
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
@@ -66,6 +69,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [ReparentActivationShortcut, ThumbnailActivationShortcut],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;

View File

@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Abstractions;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Threading;
using CommunityToolkit.WinUI.Controls;
using global::PowerToys.GPOWrapper;
@@ -14,6 +15,7 @@ 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.Library.Utilities;
using Microsoft.PowerToys.Settings.UI.Services;
@@ -23,8 +25,10 @@ using Microsoft.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class DashboardViewModel : Observable
public partial class DashboardViewModel : PageViewModelBase
{
protected override string ModuleName => "Dashboard";
private const string JsonFileType = ".json";
private Dispatcher dispatcher;
@@ -36,6 +40,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public ObservableCollection<DashboardListItem> ActionModules { get; set; } = new ObservableCollection<DashboardListItem>();
private AllHotkeyConflictsData _allHotkeyConflictsData = new AllHotkeyConflictsData();
public AllHotkeyConflictsData AllHotkeyConflictsData
{
get => _allHotkeyConflictsData;
set
{
if (Set(ref _allHotkeyConflictsData, value))
{
OnPropertyChanged();
}
}
}
public string PowerToysVersion
{
get
@@ -66,6 +84,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
GetShortcutModules();
}
protected override void OnConflictsUpdated(object sender, AllHotkeyConflictsEventArgs e)
{
dispatcher.BeginInvoke(() =>
{
AllHotkeyConflictsData = e.Conflicts ?? new AllHotkeyConflictsData();
});
}
private void RequestConflictData()
{
// Request current conflicts data
GlobalHotkeyConflictManager.Instance?.RequestAllConflicts();
}
private void AddDashboardListItem(ModuleType moduleType)
{
GpoRuleConfigured gpo = ModuleHelper.GetModuleGpoConfiguration(moduleType);
@@ -93,6 +125,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
var settings = NewPlusViewModel.LoadSettings(settingsUtils);
NewPlusViewModel.CopyTemplateExamples(settings.Properties.TemplateLocation.Value);
}
// Request updated conflicts after module state change
RequestConflictData();
}
public void ModuleEnabledChangedOnSettingsPage()
@@ -102,6 +137,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
GetShortcutModules();
OnPropertyChanged(nameof(ShortcutModules));
// Request updated conflicts after module state change
RequestConflictData();
}
catch (Exception ex)
{

View File

@@ -3,9 +3,11 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -13,14 +15,14 @@ using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class FancyZonesViewModel : Observable
public partial class FancyZonesViewModel : PageViewModelBase
{
private SettingsUtils SettingsUtils { get; set; }
protected override string ModuleName => FancyZonesSettings.ModuleName;
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
private const string ModuleName = FancyZonesSettings.ModuleName;
public ButtonClickCommand LaunchEditorEventHandler { get; set; }
private FancyZonesSettings Settings { get; set; }
@@ -44,7 +46,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Positional = 2,
}
public FancyZonesViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<FancyZonesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
public FancyZonesViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<FancyZonesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
{
ArgumentNullException.ThrowIfNull(settingsUtils);
@@ -88,8 +90,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_excludedApps = Settings.Properties.FancyzonesExcludedApps.Value;
_systemTheme = Settings.Properties.FancyzonesSystemTheme.Value;
_showZoneNumber = Settings.Properties.FancyzonesShowZoneNumber.Value;
EditorHotkey = Settings.Properties.FancyzonesEditorHotkey.Value;
_windowSwitching = Settings.Properties.FancyzonesWindowSwitching.Value;
EditorHotkey = Settings.Properties.FancyzonesEditorHotkey.Value;
NextTabHotkey = Settings.Properties.FancyzonesNextTabHotkey.Value;
PrevTabHotkey = Settings.Properties.FancyzonesPrevTabHotkey.Value;
@@ -134,6 +137,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [EditorHotkey, NextTabHotkey, PrevTabHotkey],
};
return hotkeysDict;
}
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
private bool _isEnabled;

View File

@@ -3,11 +3,12 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -15,8 +16,10 @@ using Microsoft.PowerToys.Settings.UI.SerializationContext;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class MeasureToolViewModel : Observable
public partial class MeasureToolViewModel : PageViewModelBase
{
protected override string ModuleName => MeasureToolSettings.ModuleName;
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
@@ -59,6 +62,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [ActivationShortcut],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;

View File

@@ -3,10 +3,11 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -14,8 +15,10 @@ using Microsoft.PowerToys.Settings.Utilities;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class MouseUtilsViewModel : Observable
public partial class MouseUtilsViewModel : PageViewModelBase
{
protected override string ModuleName => "MouseUtils";
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
@@ -101,7 +104,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_mousePointerCrosshairsAutoActivate = MousePointerCrosshairsSettingsConfig.Properties.AutoActivate.Value;
int isEnabled = 0;
NativeMethods.SystemParametersInfo(NativeMethods.SPI_GETCLIENTAREAANIMATION, 0, ref isEnabled, 0);
Utilities.NativeMethods.SystemParametersInfo(Utilities.NativeMethods.SPI_GETCLIENTAREAANIMATION, 0, ref isEnabled, 0);
_isAnimationEnabledBySystem = isEnabled != 0;
// set the callback functions value to handle outgoing IPC message.
@@ -149,6 +153,19 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[FindMyMouseSettings.ModuleName] = [FindMyMouseActivationShortcut],
[MouseHighlighterSettings.ModuleName] = [MouseHighlighterActivationShortcut],
[MousePointerCrosshairsSettings.ModuleName] = [MousePointerCrosshairsActivationShortcut],
[MouseJumpSettings.ModuleName] = [MouseJumpActivationShortcut],
};
return hotkeysDict;
}
public bool IsFindMyMouseEnabled
{
get => _isFindMyMouseEnabled;

View File

@@ -25,7 +25,7 @@ using MouseJump.Common.Models.Styles;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class MouseUtilsViewModel : Observable
public partial class MouseUtilsViewModel : PageViewModelBase
{
private GpoRuleConfigured _jumpEnabledGpoRuleConfiguration;
private bool _jumpEnabledStateIsGPOConfigured;
@@ -37,6 +37,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
ArgumentNullException.ThrowIfNull(mouseJumpSettingsRepository);
this.MouseJumpSettingsConfig = mouseJumpSettingsRepository.SettingsConfig;
this.MouseJumpSettingsConfig.Properties.ThumbnailSize.PropertyChanged += this.MouseJumpThumbnailSizePropertyChanged;
}

View File

@@ -13,7 +13,6 @@ using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using global::PowerToys.GPOWrapper;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers;
@@ -30,8 +29,10 @@ using Windows.ApplicationModel.DataTransfer;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class MouseWithoutBordersViewModel : Observable, IDisposable
public partial class MouseWithoutBordersViewModel : PageViewModelBase, IDisposable
{
protected override string ModuleName => MouseWithoutBordersSettings.ModuleName;
// These should be in the same order as the ComboBoxItems in MouseWithoutBordersPage.xaml switch machine shortcut options
private readonly int[] _switchBetweenMachineShortcutOptions =
{
@@ -43,18 +44,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private readonly Lock _machineMatrixStringLock = new();
private static readonly Dictionary<SocketStatus, Brush> StatusColors = new Dictionary<SocketStatus, Brush>()
{
{ SocketStatus.NA, new SolidColorBrush(ColorHelper.FromArgb(0, 0x71, 0x71, 0x71)) },
{ SocketStatus.Resolving, new SolidColorBrush(Colors.Yellow) },
{ SocketStatus.Connecting, new SolidColorBrush(Colors.Orange) },
{ SocketStatus.Handshaking, new SolidColorBrush(Colors.Blue) },
{ SocketStatus.Error, new SolidColorBrush(Colors.Red) },
{ SocketStatus.ForceClosed, new SolidColorBrush(Colors.Purple) },
{ SocketStatus.InvalidKey, new SolidColorBrush(Colors.Brown) },
{ SocketStatus.Timeout, new SolidColorBrush(Colors.Pink) },
{ SocketStatus.SendError, new SolidColorBrush(Colors.Maroon) },
{ SocketStatus.Connected, new SolidColorBrush(Colors.Green) },
};
{
{ SocketStatus.NA, new SolidColorBrush(ColorHelper.FromArgb(0, 0x71, 0x71, 0x71)) },
{ SocketStatus.Resolving, new SolidColorBrush(Colors.Yellow) },
{ SocketStatus.Connecting, new SolidColorBrush(Colors.Orange) },
{ SocketStatus.Handshaking, new SolidColorBrush(Colors.Blue) },
{ SocketStatus.Error, new SolidColorBrush(Colors.Red) },
{ SocketStatus.ForceClosed, new SolidColorBrush(Colors.Purple) },
{ SocketStatus.InvalidKey, new SolidColorBrush(Colors.Brown) },
{ SocketStatus.Timeout, new SolidColorBrush(Colors.Pink) },
{ SocketStatus.SendError, new SolidColorBrush(Colors.Maroon) },
{ SocketStatus.Connected, new SolidColorBrush(Colors.Green) },
};
private bool _connectFieldsVisible;
@@ -545,6 +546,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_policyDefinedIpMappingRulesIsGPOConfigured = !string.IsNullOrWhiteSpace(_policyDefinedIpMappingRulesGPOData);
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [
ToggleEasyMouseShortcut,
LockMachinesShortcut,
HotKeySwitch2AllPC,
ReconnectShortcut],
};
return hotkeysDict;
}
private void LoadViewModelFromSettings(MouseWithoutBordersSettings moduleSettings)
{
ArgumentNullException.ThrowIfNull(moduleSettings);
@@ -998,6 +1013,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
Settings.Properties.ToggleEasyMouseShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeyToggleEasyMouse;
NotifyPropertyChanged();
NotifyModuleUpdatedSettings();
}
}
}
@@ -1013,6 +1029,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Settings.Properties.LockMachineShortcut = value;
Settings.Properties.LockMachineShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeyLockMachine;
NotifyPropertyChanged();
NotifyModuleUpdatedSettings();
}
}
}
@@ -1028,6 +1045,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Settings.Properties.ReconnectShortcut = value;
Settings.Properties.ReconnectShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeyReconnect;
NotifyPropertyChanged();
NotifyModuleUpdatedSettings();
}
}
}
@@ -1043,6 +1061,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Settings.Properties.Switch2AllPCShortcut = value;
Settings.Properties.Switch2AllPCShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeySwitch2AllPC;
NotifyPropertyChanged();
NotifyModuleUpdatedSettings();
}
}
}
@@ -1201,11 +1220,11 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private void NotifyModuleUpdatedSettings()
{
SendConfigMSG(
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
MouseWithoutBordersSettings.ModuleName,
JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.MouseWithoutBordersSettings)));
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
MouseWithoutBordersSettings.ModuleName,
JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.MouseWithoutBordersSettings)));
}
public void NotifyUpdatedSettings()
@@ -1241,9 +1260,43 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
Clipboard.SetContent(data);
}
public void Dispose()
protected override void Dispose(bool disposing)
{
GC.SuppressFinalize(this);
if (disposing)
{
// Cancel the cancellation token source
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
// Wait for the machine polling task to complete
try
{
_machinePollingThreadTask?.Wait(TimeSpan.FromSeconds(1));
}
catch (AggregateException)
{
// Task was cancelled, which is expected
}
// Dispose the named pipe stream
try
{
syncHelperStream?.Dispose();
}
catch (Exception ex)
{
Logger.LogError($"Error disposing sync helper stream: {ex}");
}
finally
{
syncHelperStream = null;
}
// Dispose the semaphore
_ipcSemaphore?.Dispose();
}
base.Dispose(disposing);
}
internal void UninstallService()

View File

@@ -0,0 +1,251 @@
// 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.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
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.Services;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public abstract class PageViewModelBase : Observable, IDisposable
{
private readonly Dictionary<string, bool> _hotkeyConflictStatus = new Dictionary<string, bool>();
private readonly Dictionary<string, string> _hotkeyConflictTooltips = new Dictionary<string, string>();
private bool _disposed;
protected abstract string ModuleName { get; }
protected PageViewModelBase()
{
if (GlobalHotkeyConflictManager.Instance != null)
{
GlobalHotkeyConflictManager.Instance.ConflictsUpdated += OnConflictsUpdated;
}
}
public virtual void OnPageLoaded()
{
Debug.WriteLine($"=== PAGE LOADED: {ModuleName} ===");
GlobalHotkeyConflictManager.Instance?.RequestAllConflicts();
}
/// <summary>
/// Handles updates to hotkey conflicts for the module. This method is called when the
/// <see cref="GlobalHotkeyConflictManager"/> raises the <c>ConflictsUpdated</c> event.
/// </summary>
/// <param name="sender">The source of the event, typically the <see cref="GlobalHotkeyConflictManager"/> instance.</param>
/// <param name="e">An <see cref="AllHotkeyConflictsEventArgs"/> object containing details about the hotkey conflicts.</param>
/// <remarks>
/// Derived classes can override this method to provide custom handling for hotkey conflicts.
/// Ensure that the overridden method maintains the expected behavior of processing and logging conflict data.
/// </remarks>
protected virtual void OnConflictsUpdated(object sender, AllHotkeyConflictsEventArgs e)
{
UpdateHotkeyConflictStatus(e.Conflicts);
var allHotkeySettings = GetAllHotkeySettings();
void UpdateConflictProperties()
{
if (allHotkeySettings != null)
{
foreach (KeyValuePair<string, HotkeySettings[]> kvp in allHotkeySettings)
{
var module = kvp.Key;
var hotkeySettingsList = kvp.Value;
for (int i = 0; i < hotkeySettingsList.Length; i++)
{
var key = $"{module.ToLowerInvariant()}_{i}";
hotkeySettingsList[i].HasConflict = GetHotkeyConflictStatus(key);
hotkeySettingsList[i].ConflictDescription = GetHotkeyConflictTooltip(key);
}
}
}
}
_ = Task.Run(() =>
{
try
{
var settingsWindow = App.GetSettingsWindow();
settingsWindow.DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, UpdateConflictProperties);
}
catch
{
UpdateConflictProperties();
}
});
}
public virtual Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
return null;
}
protected ModuleConflictsData GetModuleRelatedConflicts(AllHotkeyConflictsData allConflicts)
{
var moduleConflicts = new ModuleConflictsData();
if (allConflicts.InAppConflicts != null)
{
foreach (var conflict in allConflicts.InAppConflicts)
{
if (IsModuleInvolved(conflict))
{
moduleConflicts.InAppConflicts.Add(conflict);
}
}
}
if (allConflicts.SystemConflicts != null)
{
foreach (var conflict in allConflicts.SystemConflicts)
{
if (IsModuleInvolved(conflict))
{
moduleConflicts.SystemConflicts.Add(conflict);
}
}
}
return moduleConflicts;
}
private void ProcessMouseUtilsConflictGroup(HotkeyConflictGroupData conflict, HashSet<string> mouseUtilsModules, bool isSysConflict)
{
// Check if any of the modules in this conflict are MouseUtils submodules
var involvedMouseUtilsModules = conflict.Modules
.Where(module => mouseUtilsModules.Contains(module.ModuleName))
.ToList();
if (involvedMouseUtilsModules.Count != 0)
{
// For each involved MouseUtils module, mark the hotkey as having a conflict
foreach (var module in involvedMouseUtilsModules)
{
string hotkeyKey = $"{module.ModuleName.ToLowerInvariant()}_{module.HotkeyID}";
_hotkeyConflictStatus[hotkeyKey] = true;
_hotkeyConflictTooltips[hotkeyKey] = isSysConflict
? ResourceLoaderInstance.ResourceLoader.GetString("SysHotkeyConflictTooltipText")
: ResourceLoaderInstance.ResourceLoader.GetString("InAppHotkeyConflictTooltipText");
}
}
}
protected virtual void UpdateHotkeyConflictStatus(AllHotkeyConflictsData allConflicts)
{
_hotkeyConflictStatus.Clear();
_hotkeyConflictTooltips.Clear();
// Since MouseUtils in Settings consolidates four modules: Find My Mouse, Mouse Highlighter, Mouse Pointer Crosshairs, and Mouse Jump
// We need to handle this case separately here.
if (string.Equals(ModuleName, "MouseUtils", StringComparison.OrdinalIgnoreCase))
{
var mouseUtilsModules = new HashSet<string>
{
FindMyMouseSettings.ModuleName,
MouseHighlighterSettings.ModuleName,
MousePointerCrosshairsSettings.ModuleName,
MouseJumpSettings.ModuleName,
};
// Process in-app conflicts
foreach (var conflict in allConflicts.InAppConflicts)
{
ProcessMouseUtilsConflictGroup(conflict, mouseUtilsModules, false);
}
// Process system conflicts
foreach (var conflict in allConflicts.SystemConflicts)
{
ProcessMouseUtilsConflictGroup(conflict, mouseUtilsModules, true);
}
}
else
{
if (allConflicts.InAppConflicts.Count > 0)
{
foreach (var conflictGroup in allConflicts.InAppConflicts)
{
foreach (var conflict in conflictGroup.Modules)
{
if (string.Equals(conflict.ModuleName, ModuleName, StringComparison.OrdinalIgnoreCase))
{
var keyName = $"{conflict.ModuleName.ToLowerInvariant()}_{conflict.HotkeyID}";
_hotkeyConflictStatus[keyName] = true;
_hotkeyConflictTooltips[keyName] = ResourceLoaderInstance.ResourceLoader.GetString("InAppHotkeyConflictTooltipText");
}
}
}
}
if (allConflicts.SystemConflicts.Count > 0)
{
foreach (var conflictGroup in allConflicts.SystemConflicts)
{
foreach (var conflict in conflictGroup.Modules)
{
if (string.Equals(conflict.ModuleName, ModuleName, StringComparison.OrdinalIgnoreCase))
{
var keyName = $"{conflict.ModuleName.ToLowerInvariant()}_{conflict.HotkeyID}";
_hotkeyConflictStatus[keyName] = true;
_hotkeyConflictTooltips[keyName] = ResourceLoaderInstance.ResourceLoader.GetString("SysHotkeyConflictTooltipText");
}
}
}
}
}
}
protected virtual bool GetHotkeyConflictStatus(string key)
{
return _hotkeyConflictStatus.ContainsKey(key) && _hotkeyConflictStatus[key];
}
protected virtual string GetHotkeyConflictTooltip(string key)
{
return _hotkeyConflictTooltips.TryGetValue(key, out string value) ? value : null;
}
private bool IsModuleInvolved(HotkeyConflictGroupData conflict)
{
if (conflict.Modules == null)
{
return false;
}
return conflict.Modules.Any(module =>
string.Equals(module.ModuleName, ModuleName, StringComparison.OrdinalIgnoreCase));
}
public virtual void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (GlobalHotkeyConflictManager.Instance != null)
{
GlobalHotkeyConflictManager.Instance.ConflictsUpdated -= OnConflictsUpdated;
}
}
_disposed = true;
}
}
}
}

View File

@@ -3,13 +3,14 @@
// 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.IO.Abstractions;
using System.Text.Json;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -20,10 +21,14 @@ using Settings.UI.Library;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class PeekViewModel : Observable, IDisposable
public class PeekViewModel : PageViewModelBase
{
protected override string ModuleName => PeekSettings.ModuleName;
private bool _isEnabled;
private bool _disposed;
private bool _settingsUpdating;
private GeneralSettings GeneralSettingsConfig { get; set; }
@@ -59,6 +64,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
// Load the application-specific settings, including preview items.
_peekSettings = _settingsUtils.GetSettingsOrDefault<PeekSettings>(PeekSettings.ModuleName);
_peekPreviewSettings = _settingsUtils.GetSettingsOrDefault<PeekPreviewSettings>(PeekSettings.ModuleName, PeekPreviewSettings.FileName);
SetupSettingsFileWatcher();
InitializeEnabledValue();
@@ -118,6 +124,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [ActivationShortcut],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;
@@ -302,11 +318,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
OnPropertyChanged(nameof(IsEnabled));
}
public void Dispose()
protected override void Dispose(bool disposing)
{
_watcher?.Dispose();
if (!_disposed)
{
if (disposing)
{
_watcher?.Dispose();
_watcher = null;
}
GC.SuppressFinalize(this);
_disposed = true;
}
base.Dispose(disposing);
}
}
}

View File

@@ -3,6 +3,7 @@
// 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;
@@ -10,9 +11,9 @@ using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Windows.Input;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -21,7 +22,7 @@ using Microsoft.PowerToys.Settings.UI.SerializationContext;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class PowerLauncherViewModel : Observable
public partial class PowerLauncherViewModel : PageViewModelBase, IDisposable
{
private int _themeIndex;
private int _monitorPositionIndex;
@@ -37,6 +38,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public delegate void SendCallback(PowerLauncherSettings settings);
protected override string ModuleName => PowerLauncherSettings.ModuleName;
private readonly SendCallback callback;
private readonly Func<bool> isDark;
@@ -122,6 +125,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [OpenPowerLauncher],
};
return hotkeysDict;
}
private void OnPluginInfoChange(object sender, PropertyChangedEventArgs e)
{
if (

View File

@@ -11,6 +11,7 @@ using System.Text.Json;
using System.Timers;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -20,9 +21,11 @@ using Windows.Media.Ocr;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class PowerOcrViewModel : Observable, IDisposable
public partial class PowerOcrViewModel : PageViewModelBase
{
private bool disposedValue;
protected override string ModuleName => PowerOcrSettings.ModuleName;
private bool _disposed;
// Delay saving of settings in order to avoid calling save multiple times and hitting file in use exception. If there is no other request to save settings in given interval, we proceed to save it; otherwise, we schedule saving it after this interval
private const int SaveSettingsDelayInMs = 500;
@@ -114,6 +117,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [ActivationShortcut],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;
@@ -246,23 +259,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
OnPropertyChanged(nameof(IsEnabled));
}
protected virtual void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposed)
{
if (disposing)
{
_delayedTimer.Dispose();
_delayedTimer?.Dispose();
_delayedTimer = null;
}
disposedValue = true;
_disposed = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
base.Dispose(disposing);
}
public string SnippingToolInfoBarMargin

View File

@@ -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}");
}
}
}
}

View File

@@ -3,25 +3,29 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json;
using global::PowerToys.GPOWrapper;
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.Interfaces;
using Microsoft.PowerToys.Settings.UI.SerializationContext;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class ShortcutGuideViewModel : Observable
public partial class ShortcutGuideViewModel : PageViewModelBase
{
protected override string ModuleName => ShortcutGuideSettings.ModuleName;
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
private ShortcutGuideSettings Settings { get; set; }
private const string ModuleName = ShortcutGuideSettings.ModuleName;
private Func<string, int> SendConfigMSG { get; }
private string _settingsConfigFileFolder = string.Empty;
@@ -79,6 +83,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [OpenShortcutGuide],
};
return hotkeysDict;
}
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
private bool _isEnabled;

View File

@@ -3,11 +3,12 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Json;
using global::PowerToys.GPOWrapper;
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.Interfaces;
@@ -16,8 +17,10 @@ using Microsoft.PowerToys.Settings.UI.SerializationContext;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class WorkspacesViewModel : Observable
public partial class WorkspacesViewModel : PageViewModelBase
{
protected override string ModuleName => WorkspacesSettings.ModuleName;
private ISettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
@@ -75,6 +78,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
{
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
{
[ModuleName] = [Hotkey],
};
return hotkeysDict;
}
public bool IsEnabled
{
get => _isEnabled;