mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 02:06:36 +02:00
<!-- 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>
297 lines
10 KiB
C#
297 lines
10 KiB
C#
// 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.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.Helpers;
|
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
|
using Microsoft.PowerToys.Settings.UI.SerializationContext;
|
|
using Windows.Globalization;
|
|
using Windows.Media.Ocr;
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|
{
|
|
public partial class PowerOcrViewModel : PageViewModelBase
|
|
{
|
|
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;
|
|
|
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
|
|
|
private readonly ISettingsUtils _settingsUtils;
|
|
private readonly System.Threading.Lock _delayedActionLock = new System.Threading.Lock();
|
|
|
|
private readonly PowerOcrSettings _powerOcrSettings;
|
|
private Timer _delayedTimer;
|
|
|
|
private GpoRuleConfigured _enabledGpoRuleConfiguration;
|
|
private bool _enabledStateIsGPOConfigured;
|
|
private bool _isEnabled;
|
|
private int _languageIndex;
|
|
private List<Language> possibleOcrLanguages;
|
|
|
|
public ObservableCollection<string> AvailableLanguages { get; } = new ObservableCollection<string>();
|
|
|
|
public int LanguageIndex
|
|
{
|
|
get
|
|
{
|
|
return _languageIndex;
|
|
}
|
|
|
|
set
|
|
{
|
|
if (value != _languageIndex)
|
|
{
|
|
_languageIndex = value;
|
|
if (_powerOcrSettings != null && _languageIndex < possibleOcrLanguages.Count && _languageIndex >= 0)
|
|
{
|
|
_powerOcrSettings.Properties.PreferredLanguage = possibleOcrLanguages[_languageIndex].NativeName;
|
|
NotifySettingsChanged();
|
|
}
|
|
|
|
OnPropertyChanged(nameof(LanguageIndex));
|
|
}
|
|
}
|
|
}
|
|
|
|
private Func<string, int> SendConfigMSG { get; }
|
|
|
|
public PowerOcrViewModel(
|
|
ISettingsUtils settingsUtils,
|
|
ISettingsRepository<GeneralSettings> settingsRepository,
|
|
ISettingsRepository<PowerOcrSettings> powerOcrsettingsRepository,
|
|
Func<string, int> ipcMSGCallBackFunc)
|
|
{
|
|
// To obtain the general settings configurations of PowerToys Settings.
|
|
ArgumentNullException.ThrowIfNull(settingsRepository);
|
|
|
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
|
|
|
// To obtain the settings configurations of Fancy zones.
|
|
ArgumentNullException.ThrowIfNull(settingsRepository);
|
|
|
|
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
|
|
|
ArgumentNullException.ThrowIfNull(powerOcrsettingsRepository);
|
|
|
|
_powerOcrSettings = powerOcrsettingsRepository.SettingsConfig;
|
|
|
|
InitializeEnabledValue();
|
|
|
|
// set the callback functions value to handle outgoing IPC message.
|
|
SendConfigMSG = ipcMSGCallBackFunc;
|
|
|
|
_delayedTimer = new Timer();
|
|
_delayedTimer.Interval = SaveSettingsDelayInMs;
|
|
_delayedTimer.Elapsed += DelayedTimer_Tick;
|
|
_delayedTimer.AutoReset = false;
|
|
}
|
|
|
|
private void InitializeEnabledValue()
|
|
{
|
|
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredTextExtractorEnabledValue();
|
|
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
|
|
{
|
|
// Get the enabled state from GPO.
|
|
_enabledStateIsGPOConfigured = true;
|
|
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
|
|
}
|
|
else
|
|
{
|
|
_isEnabled = GeneralSettingsConfig.Enabled.PowerOcr;
|
|
}
|
|
}
|
|
|
|
public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
|
|
{
|
|
var hotkeysDict = new Dictionary<string, HotkeySettings[]>
|
|
{
|
|
[ModuleName] = [ActivationShortcut],
|
|
};
|
|
|
|
return hotkeysDict;
|
|
}
|
|
|
|
public bool IsEnabled
|
|
{
|
|
get => _isEnabled;
|
|
set
|
|
{
|
|
if (_enabledStateIsGPOConfigured)
|
|
{
|
|
// If it's GPO configured, shouldn't be able to change this state.
|
|
return;
|
|
}
|
|
|
|
if (_isEnabled != value)
|
|
{
|
|
_isEnabled = value;
|
|
OnPropertyChanged(nameof(IsEnabled));
|
|
|
|
// Set the status of PowerOcr in the general settings
|
|
GeneralSettingsConfig.Enabled.PowerOcr = value;
|
|
var outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
|
|
|
|
SendConfigMSG(outgoing.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsWin11OrGreater
|
|
{
|
|
get => OSVersionHelper.IsWindows11();
|
|
}
|
|
|
|
public bool IsEnabledGpoConfigured
|
|
{
|
|
get => _enabledStateIsGPOConfigured;
|
|
}
|
|
|
|
public HotkeySettings ActivationShortcut
|
|
{
|
|
get => _powerOcrSettings.Properties.ActivationShortcut;
|
|
set
|
|
{
|
|
if (_powerOcrSettings.Properties.ActivationShortcut != value)
|
|
{
|
|
_powerOcrSettings.Properties.ActivationShortcut = value ?? _powerOcrSettings.Properties.DefaultActivationShortcut;
|
|
OnPropertyChanged(nameof(ActivationShortcut));
|
|
|
|
_settingsUtils.SaveSettings(_powerOcrSettings.ToJsonString(), PowerOcrSettings.ModuleName);
|
|
NotifySettingsChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
internal void UpdateLanguages()
|
|
{
|
|
int preferredLanguageIndex = -1;
|
|
int systemLanguageIndex = -1;
|
|
CultureInfo systemCulture = CultureInfo.CurrentUICulture;
|
|
|
|
// get the list of all installed OCR languages. While processing them, search for the previously preferred language and also for the current ui language
|
|
possibleOcrLanguages = OcrEngine.AvailableRecognizerLanguages.OrderBy(x => x.NativeName).ToList();
|
|
AvailableLanguages.Clear();
|
|
foreach (Language language in possibleOcrLanguages)
|
|
{
|
|
if (_powerOcrSettings.Properties.PreferredLanguage?.Equals(language.DisplayName, StringComparison.Ordinal) == true)
|
|
{
|
|
preferredLanguageIndex = AvailableLanguages.Count;
|
|
}
|
|
|
|
if (systemCulture.DisplayName.Equals(language.DisplayName, StringComparison.Ordinal) || systemCulture.Parent.DisplayName.Equals(language.DisplayName, StringComparison.Ordinal))
|
|
{
|
|
systemLanguageIndex = AvailableLanguages.Count;
|
|
}
|
|
|
|
AvailableLanguages.Add(EnsureStartUpper(language.NativeName));
|
|
}
|
|
|
|
// if the previously stored preferred language is not available (has been deleted or this is the first run with language preference)
|
|
if (preferredLanguageIndex == -1)
|
|
{
|
|
// try to use the current ui language. If it is also not available, set the first language as preferred (to have any selected language)
|
|
if (systemLanguageIndex >= 0)
|
|
{
|
|
preferredLanguageIndex = systemLanguageIndex;
|
|
}
|
|
else
|
|
{
|
|
preferredLanguageIndex = 0;
|
|
}
|
|
}
|
|
|
|
// set the language index -> the preferred language gets selected in the combo box
|
|
LanguageIndex = preferredLanguageIndex;
|
|
}
|
|
|
|
private void ScheduleSavingOfSettings()
|
|
{
|
|
lock (_delayedActionLock)
|
|
{
|
|
if (_delayedTimer.Enabled)
|
|
{
|
|
_delayedTimer.Stop();
|
|
}
|
|
|
|
_delayedTimer.Start();
|
|
}
|
|
}
|
|
|
|
private void DelayedTimer_Tick(object sender, EventArgs e)
|
|
{
|
|
lock (_delayedActionLock)
|
|
{
|
|
_delayedTimer.Stop();
|
|
NotifySettingsChanged();
|
|
}
|
|
}
|
|
|
|
private void NotifySettingsChanged()
|
|
{
|
|
// Using InvariantCulture as this is an IPC message
|
|
SendConfigMSG(
|
|
string.Format(
|
|
CultureInfo.InvariantCulture,
|
|
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
|
|
PowerOcrSettings.ModuleName,
|
|
JsonSerializer.Serialize(_powerOcrSettings, SourceGenerationContextContext.Default.PowerOcrSettings)));
|
|
}
|
|
|
|
public void RefreshEnabledState()
|
|
{
|
|
InitializeEnabledValue();
|
|
OnPropertyChanged(nameof(IsEnabled));
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
if (disposing)
|
|
{
|
|
_delayedTimer?.Dispose();
|
|
_delayedTimer = null;
|
|
}
|
|
|
|
_disposed = true;
|
|
}
|
|
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
public string SnippingToolInfoBarMargin
|
|
{
|
|
// Workaround for wrong StackPanel behavior: On hidden controls the margin is still reserved.
|
|
get => IsWin11OrGreater ? "0,0,0,25" : "0,0,0,0";
|
|
}
|
|
|
|
private string EnsureStartUpper(string input)
|
|
{
|
|
if (string.IsNullOrEmpty(input))
|
|
{
|
|
return input;
|
|
}
|
|
|
|
var inputArray = input.ToCharArray();
|
|
inputArray[0] = char.ToUpper(inputArray[0], CultureInfo.CurrentCulture);
|
|
return new string(inputArray);
|
|
}
|
|
}
|
|
}
|