mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01: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>
471 lines
15 KiB
C++
471 lines
15 KiB
C++
#include "pch.h"
|
|
#include "hotkey_conflict_detector.h"
|
|
#include <common/SettingsAPI/settings_helpers.h>
|
|
#include <windows.h>
|
|
#include <unordered_map>
|
|
#include <cwchar>
|
|
|
|
namespace HotkeyConflictDetector
|
|
{
|
|
Hotkey ShortcutToHotkey(const CentralizedHotkeys::Shortcut& shortcut)
|
|
{
|
|
Hotkey hotkey;
|
|
|
|
hotkey.win = (shortcut.modifiersMask & MOD_WIN) != 0;
|
|
hotkey.ctrl = (shortcut.modifiersMask & MOD_CONTROL) != 0;
|
|
hotkey.shift = (shortcut.modifiersMask & MOD_SHIFT) != 0;
|
|
hotkey.alt = (shortcut.modifiersMask & MOD_ALT) != 0;
|
|
|
|
hotkey.key = shortcut.vkCode > 255 ? 0 : static_cast<unsigned char>(shortcut.vkCode);
|
|
|
|
return hotkey;
|
|
}
|
|
|
|
HotkeyConflictManager* HotkeyConflictManager::instance = nullptr;
|
|
std::mutex HotkeyConflictManager::instanceMutex;
|
|
|
|
HotkeyConflictManager& HotkeyConflictManager::GetInstance()
|
|
{
|
|
std::lock_guard<std::mutex> lock(instanceMutex);
|
|
if (instance == nullptr)
|
|
{
|
|
instance = new HotkeyConflictManager();
|
|
}
|
|
return *instance;
|
|
}
|
|
|
|
HotkeyConflictType HotkeyConflictManager::HasConflict(Hotkey const& _hotkey, const wchar_t* _moduleName, const int _hotkeyID)
|
|
{
|
|
if (disabledHotkeys.find(_moduleName) != disabledHotkeys.end())
|
|
{
|
|
return HotkeyConflictType::NoConflict;
|
|
}
|
|
|
|
uint16_t handle = GetHotkeyHandle(_hotkey);
|
|
|
|
if (handle == 0)
|
|
{
|
|
return HotkeyConflictType::NoConflict;
|
|
}
|
|
|
|
// The order is important, first to check sys conflict and then inapp conflict
|
|
if (sysConflictHotkeyMap.find(handle) != sysConflictHotkeyMap.end())
|
|
{
|
|
return HotkeyConflictType::SystemConflict;
|
|
}
|
|
|
|
if (inAppConflictHotkeyMap.find(handle) != inAppConflictHotkeyMap.end())
|
|
{
|
|
return HotkeyConflictType::InAppConflict;
|
|
}
|
|
|
|
auto it = hotkeyMap.find(handle);
|
|
|
|
if (it == hotkeyMap.end())
|
|
{
|
|
return HasConflictWithSystemHotkey(_hotkey) ?
|
|
HotkeyConflictType::SystemConflict :
|
|
HotkeyConflictType::NoConflict;
|
|
}
|
|
|
|
if (wcscmp(it->second.moduleName.c_str(), _moduleName) == 0 && it->second.hotkeyID == _hotkeyID)
|
|
{
|
|
// A shortcut matching its own assignment is not considered a conflict.
|
|
return HotkeyConflictType::NoConflict;
|
|
}
|
|
|
|
return HotkeyConflictType::InAppConflict;
|
|
}
|
|
|
|
HotkeyConflictType HotkeyConflictManager::HasConflict(Hotkey const& _hotkey)
|
|
{
|
|
uint16_t handle = GetHotkeyHandle(_hotkey);
|
|
|
|
if (handle == 0)
|
|
{
|
|
return HotkeyConflictType::NoConflict;
|
|
}
|
|
|
|
// The order is important, first to check sys conflict and then inapp conflict
|
|
if (sysConflictHotkeyMap.find(handle) != sysConflictHotkeyMap.end())
|
|
{
|
|
return HotkeyConflictType::SystemConflict;
|
|
}
|
|
|
|
if (inAppConflictHotkeyMap.find(handle) != inAppConflictHotkeyMap.end())
|
|
{
|
|
return HotkeyConflictType::InAppConflict;
|
|
}
|
|
|
|
auto it = hotkeyMap.find(handle);
|
|
|
|
if (it == hotkeyMap.end())
|
|
{
|
|
return HasConflictWithSystemHotkey(_hotkey) ?
|
|
HotkeyConflictType::SystemConflict :
|
|
HotkeyConflictType::NoConflict;
|
|
}
|
|
|
|
return HotkeyConflictType::InAppConflict;
|
|
}
|
|
|
|
// This function should only be called when a conflict has already been identified.
|
|
// It returns a list of all conflicting shortcuts.
|
|
std::vector<HotkeyConflictInfo> HotkeyConflictManager::GetAllConflicts(Hotkey const& _hotkey)
|
|
{
|
|
std::vector<HotkeyConflictInfo> conflicts;
|
|
uint16_t handle = GetHotkeyHandle(_hotkey);
|
|
|
|
// Check in-app conflicts first
|
|
auto inAppIt = inAppConflictHotkeyMap.find(handle);
|
|
if (inAppIt != inAppConflictHotkeyMap.end())
|
|
{
|
|
// Add all in-app conflicts
|
|
for (const auto& conflict : inAppIt->second)
|
|
{
|
|
conflicts.push_back(conflict);
|
|
}
|
|
|
|
return conflicts;
|
|
}
|
|
|
|
// Check system conflicts
|
|
auto sysIt = sysConflictHotkeyMap.find(handle);
|
|
if (sysIt != sysConflictHotkeyMap.end())
|
|
{
|
|
HotkeyConflictInfo systemConflict;
|
|
systemConflict.hotkey = _hotkey;
|
|
systemConflict.moduleName = L"System";
|
|
systemConflict.hotkeyID = 0;
|
|
|
|
conflicts.push_back(systemConflict);
|
|
|
|
return conflicts;
|
|
}
|
|
|
|
// Check if there's a successfully registered hotkey that would conflict
|
|
auto registeredIt = hotkeyMap.find(handle);
|
|
if (registeredIt != hotkeyMap.end())
|
|
{
|
|
conflicts.push_back(registeredIt->second);
|
|
|
|
return conflicts;
|
|
}
|
|
|
|
// If all the above conditions are ruled out, a system-level conflict is the only remaining explanation.
|
|
HotkeyConflictInfo systemConflict;
|
|
systemConflict.hotkey = _hotkey;
|
|
systemConflict.moduleName = L"System";
|
|
systemConflict.hotkeyID = 0;
|
|
conflicts.push_back(systemConflict);
|
|
|
|
return conflicts;
|
|
}
|
|
|
|
bool HotkeyConflictManager::AddHotkey(Hotkey const& _hotkey, const wchar_t* _moduleName, const int _hotkeyID, bool isEnabled)
|
|
{
|
|
if (!isEnabled)
|
|
{
|
|
disabledHotkeys[_moduleName].push_back({ _hotkey, _moduleName, _hotkeyID });
|
|
return true;
|
|
}
|
|
|
|
uint16_t handle = GetHotkeyHandle(_hotkey);
|
|
|
|
if (handle == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
HotkeyConflictType conflictType = HasConflict(_hotkey, _moduleName, _hotkeyID);
|
|
if (conflictType != HotkeyConflictType::NoConflict)
|
|
{
|
|
if (conflictType == HotkeyConflictType::InAppConflict)
|
|
{
|
|
auto hotkeyFound = hotkeyMap.find(handle);
|
|
inAppConflictHotkeyMap[handle].insert({ _hotkey, _moduleName, _hotkeyID });
|
|
|
|
if (hotkeyFound != hotkeyMap.end())
|
|
{
|
|
inAppConflictHotkeyMap[handle].insert(hotkeyFound->second);
|
|
hotkeyMap.erase(hotkeyFound);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sysConflictHotkeyMap[handle].insert({ _hotkey, _moduleName, _hotkeyID });
|
|
}
|
|
return false;
|
|
}
|
|
|
|
HotkeyConflictInfo hotkeyInfo;
|
|
hotkeyInfo.moduleName = _moduleName;
|
|
hotkeyInfo.hotkeyID = _hotkeyID;
|
|
hotkeyInfo.hotkey = _hotkey;
|
|
hotkeyMap[handle] = hotkeyInfo;
|
|
|
|
return true;
|
|
}
|
|
|
|
std::vector<HotkeyConflictInfo> HotkeyConflictManager::RemoveHotkeyByModule(const std::wstring& moduleName)
|
|
{
|
|
std::vector<HotkeyConflictInfo> removedHotkeys;
|
|
|
|
if (disabledHotkeys.find(moduleName) != disabledHotkeys.end())
|
|
{
|
|
disabledHotkeys.erase(moduleName);
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(hotkeyMutex);
|
|
bool foundRecord = false;
|
|
|
|
for (auto it = sysConflictHotkeyMap.begin(); it != sysConflictHotkeyMap.end();)
|
|
{
|
|
auto& conflictSet = it->second;
|
|
for (auto setIt = conflictSet.begin(); setIt != conflictSet.end();)
|
|
{
|
|
if (setIt->moduleName == moduleName)
|
|
{
|
|
removedHotkeys.push_back(*setIt);
|
|
setIt = conflictSet.erase(setIt);
|
|
foundRecord = true;
|
|
}
|
|
else
|
|
{
|
|
++setIt;
|
|
}
|
|
}
|
|
if (conflictSet.empty())
|
|
{
|
|
it = sysConflictHotkeyMap.erase(it);
|
|
}
|
|
else
|
|
{
|
|
++it;
|
|
}
|
|
}
|
|
|
|
for (auto it = inAppConflictHotkeyMap.begin(); it != inAppConflictHotkeyMap.end();)
|
|
{
|
|
auto& conflictSet = it->second;
|
|
uint16_t handle = it->first;
|
|
|
|
for (auto setIt = conflictSet.begin(); setIt != conflictSet.end();)
|
|
{
|
|
if (setIt->moduleName == moduleName)
|
|
{
|
|
removedHotkeys.push_back(*setIt);
|
|
setIt = conflictSet.erase(setIt);
|
|
foundRecord = true;
|
|
}
|
|
else
|
|
{
|
|
++setIt;
|
|
}
|
|
}
|
|
|
|
if (conflictSet.empty())
|
|
{
|
|
it = inAppConflictHotkeyMap.erase(it);
|
|
}
|
|
else if (conflictSet.size() == 1)
|
|
{
|
|
// Move the only remaining conflict to main map
|
|
const auto& onlyConflict = *conflictSet.begin();
|
|
hotkeyMap[handle] = onlyConflict;
|
|
it = inAppConflictHotkeyMap.erase(it);
|
|
}
|
|
else
|
|
{
|
|
++it;
|
|
}
|
|
}
|
|
|
|
for (auto it = hotkeyMap.begin(); it != hotkeyMap.end();)
|
|
{
|
|
if (it->second.moduleName == moduleName)
|
|
{
|
|
uint16_t handle = it->first;
|
|
removedHotkeys.push_back(it->second);
|
|
it = hotkeyMap.erase(it);
|
|
foundRecord = true;
|
|
|
|
auto inAppIt = inAppConflictHotkeyMap.find(handle);
|
|
if (inAppIt != inAppConflictHotkeyMap.end() && inAppIt->second.size() == 1)
|
|
{
|
|
// Move the only in-app conflict to main map
|
|
const auto& onlyConflict = *inAppIt->second.begin();
|
|
hotkeyMap[handle] = onlyConflict;
|
|
inAppConflictHotkeyMap.erase(inAppIt);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
++it;
|
|
}
|
|
}
|
|
|
|
return removedHotkeys;
|
|
}
|
|
|
|
void HotkeyConflictManager::EnableHotkeyByModule(const std::wstring& moduleName)
|
|
{
|
|
if (disabledHotkeys.find(moduleName) == disabledHotkeys.end())
|
|
{
|
|
return; // No disabled hotkeys for this module
|
|
}
|
|
|
|
auto hotkeys = disabledHotkeys[moduleName];
|
|
disabledHotkeys.erase(moduleName);
|
|
|
|
for (const auto& hotkeyInfo : hotkeys)
|
|
{
|
|
// Re-add the hotkey as enabled
|
|
AddHotkey(hotkeyInfo.hotkey, moduleName.c_str(), hotkeyInfo.hotkeyID, true);
|
|
}
|
|
}
|
|
|
|
void HotkeyConflictManager::DisableHotkeyByModule(const std::wstring& moduleName)
|
|
{
|
|
auto hotkeys = RemoveHotkeyByModule(moduleName);
|
|
disabledHotkeys[moduleName] = hotkeys;
|
|
}
|
|
|
|
bool HotkeyConflictManager::HasConflictWithSystemHotkey(const Hotkey& hotkey)
|
|
{
|
|
// Convert PowerToys Hotkey format to Win32 RegisterHotKey format
|
|
UINT modifiers = 0;
|
|
if (hotkey.win)
|
|
{
|
|
modifiers |= MOD_WIN;
|
|
}
|
|
if (hotkey.ctrl)
|
|
{
|
|
modifiers |= MOD_CONTROL;
|
|
}
|
|
if (hotkey.alt)
|
|
{
|
|
modifiers |= MOD_ALT;
|
|
}
|
|
if (hotkey.shift)
|
|
{
|
|
modifiers |= MOD_SHIFT;
|
|
}
|
|
|
|
// No modifiers or no key is not a valid hotkey
|
|
if (modifiers == 0 || hotkey.key == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Use a unique ID for this test registration
|
|
const int hotkeyId = 0x0FFF; // Arbitrary ID for temporary registration
|
|
|
|
// Try to register the hotkey with Windows, using nullptr instead of a window handle
|
|
if (!RegisterHotKey(nullptr, hotkeyId, modifiers, hotkey.key))
|
|
{
|
|
// If registration fails with ERROR_HOTKEY_ALREADY_REGISTERED, it means the hotkey
|
|
// is already in use by the system or another application
|
|
if (GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// If registration succeeds, unregister it immediately
|
|
UnregisterHotKey(nullptr, hotkeyId);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
json::JsonObject HotkeyConflictManager::GetHotkeyConflictsAsJson()
|
|
{
|
|
std::lock_guard<std::mutex> lock(hotkeyMutex);
|
|
|
|
using namespace json;
|
|
JsonObject root;
|
|
|
|
// Serialize hotkey to a unique string format for grouping
|
|
auto serializeHotkey = [](const Hotkey& hotkey) -> JsonObject {
|
|
JsonObject obj;
|
|
obj.Insert(L"win", value(hotkey.win));
|
|
obj.Insert(L"ctrl", value(hotkey.ctrl));
|
|
obj.Insert(L"shift", value(hotkey.shift));
|
|
obj.Insert(L"alt", value(hotkey.alt));
|
|
obj.Insert(L"key", value(static_cast<int>(hotkey.key)));
|
|
return obj;
|
|
};
|
|
|
|
// New format: Group conflicts by hotkey
|
|
JsonArray inAppConflictsArray;
|
|
JsonArray sysConflictsArray;
|
|
|
|
// Process in-app conflicts - only include hotkeys that are actually in conflict
|
|
for (const auto& [handle, conflicts] : inAppConflictHotkeyMap)
|
|
{
|
|
if (!conflicts.empty())
|
|
{
|
|
JsonObject conflictGroup;
|
|
|
|
// All entries have the same hotkey, so use the first one for the key
|
|
conflictGroup.Insert(L"hotkey", serializeHotkey(conflicts.begin()->hotkey));
|
|
|
|
// Create an array of module info without repeating the hotkey
|
|
JsonArray modules;
|
|
for (const auto& info : conflicts)
|
|
{
|
|
JsonObject moduleInfo;
|
|
moduleInfo.Insert(L"moduleName", value(info.moduleName));
|
|
moduleInfo.Insert(L"hotkeyID", value(info.hotkeyID));
|
|
modules.Append(moduleInfo);
|
|
}
|
|
|
|
conflictGroup.Insert(L"modules", modules);
|
|
inAppConflictsArray.Append(conflictGroup);
|
|
}
|
|
}
|
|
|
|
// Process system conflicts - only include hotkeys that are actually in conflict
|
|
for (const auto& [handle, conflicts] : sysConflictHotkeyMap)
|
|
{
|
|
if (!conflicts.empty())
|
|
{
|
|
JsonObject conflictGroup;
|
|
|
|
// All entries have the same hotkey, so use the first one for the key
|
|
conflictGroup.Insert(L"hotkey", serializeHotkey(conflicts.begin()->hotkey));
|
|
|
|
// Create an array of module info without repeating the hotkey
|
|
JsonArray modules;
|
|
for (const auto& info : conflicts)
|
|
{
|
|
JsonObject moduleInfo;
|
|
moduleInfo.Insert(L"moduleName", value(info.moduleName));
|
|
moduleInfo.Insert(L"hotkeyID", value(info.hotkeyID));
|
|
modules.Append(moduleInfo);
|
|
}
|
|
|
|
conflictGroup.Insert(L"modules", modules);
|
|
sysConflictsArray.Append(conflictGroup);
|
|
}
|
|
}
|
|
|
|
// Add the grouped conflicts to the root object
|
|
root.Insert(L"inAppConflicts", inAppConflictsArray);
|
|
root.Insert(L"sysConflicts", sysConflictsArray);
|
|
|
|
return root;
|
|
}
|
|
|
|
uint16_t HotkeyConflictManager::GetHotkeyHandle(const Hotkey& hotkey)
|
|
{
|
|
uint16_t handle = hotkey.key;
|
|
handle |= hotkey.win << 8;
|
|
handle |= hotkey.ctrl << 9;
|
|
handle |= hotkey.shift << 10;
|
|
handle |= hotkey.alt << 11;
|
|
return handle;
|
|
}
|
|
} |