mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 19:57:57 +01:00
195 lines
7.0 KiB
C++
195 lines
7.0 KiB
C++
|
|
#include "pch.h"
|
||
|
|
#include "SettingsHelper.h"
|
||
|
|
|
||
|
|
#include <common/SettingsAPI/settings_objects.h>
|
||
|
|
#include <common/SettingsAPI/settings_helpers.h>
|
||
|
|
#include <common/logger/logger.h>
|
||
|
|
|
||
|
|
#include <common/KeyboardManagerConstants.h>
|
||
|
|
|
||
|
|
bool LoadSingleKeyRemaps(KeyboardManagerState& keyboardManagerState, const json::JsonObject& jsonData)
|
||
|
|
{
|
||
|
|
bool result = true;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
auto remapKeysData = jsonData.GetNamedObject(KeyboardManagerConstants::RemapKeysSettingName);
|
||
|
|
keyboardManagerState.ClearSingleKeyRemaps();
|
||
|
|
|
||
|
|
if (remapKeysData)
|
||
|
|
{
|
||
|
|
auto inProcessRemapKeys = remapKeysData.GetNamedArray(KeyboardManagerConstants::InProcessRemapKeysSettingName);
|
||
|
|
for (const auto& it : inProcessRemapKeys)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
auto originalKey = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||
|
|
auto newRemapKey = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||
|
|
|
||
|
|
// If remapped to a shortcut
|
||
|
|
if (std::wstring(newRemapKey).find(L";") != std::string::npos)
|
||
|
|
{
|
||
|
|
keyboardManagerState.AddSingleKeyRemap(std::stoul(originalKey.c_str()), Shortcut(newRemapKey.c_str()));
|
||
|
|
}
|
||
|
|
|
||
|
|
// If remapped to a key
|
||
|
|
else
|
||
|
|
{
|
||
|
|
keyboardManagerState.AddSingleKeyRemap(std::stoul(originalKey.c_str()), std::stoul(newRemapKey.c_str()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"Improper Key Data JSON. Try the next remap.");
|
||
|
|
result = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"Improper JSON format for single key remaps. Skip to next remap type");
|
||
|
|
result = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool LoadAppSpecificShortcutRemaps(KeyboardManagerState& keyboardManagerState, const json::JsonObject& remapShortcutsData)
|
||
|
|
{
|
||
|
|
bool result = true;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
auto appSpecificRemapShortcuts = remapShortcutsData.GetNamedArray(KeyboardManagerConstants::AppSpecificRemapShortcutsSettingName);
|
||
|
|
for (const auto& it : appSpecificRemapShortcuts)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
auto originalKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||
|
|
auto newRemapKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||
|
|
auto targetApp = it.GetObjectW().GetNamedString(KeyboardManagerConstants::TargetAppSettingName);
|
||
|
|
|
||
|
|
// If remapped to a shortcut
|
||
|
|
if (std::wstring(newRemapKeys).find(L";") != std::string::npos)
|
||
|
|
{
|
||
|
|
keyboardManagerState.AddAppSpecificShortcut(targetApp.c_str(), Shortcut(originalKeys.c_str()), Shortcut(newRemapKeys.c_str()));
|
||
|
|
}
|
||
|
|
|
||
|
|
// If remapped to a key
|
||
|
|
else
|
||
|
|
{
|
||
|
|
keyboardManagerState.AddAppSpecificShortcut(targetApp.c_str(), Shortcut(originalKeys.c_str()), std::stoul(newRemapKeys.c_str()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"Improper Key Data JSON. Try the next shortcut.");
|
||
|
|
result = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"Improper JSON format for os level shortcut remaps. Skip to next remap type");
|
||
|
|
result = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool LoadShortcutRemaps(KeyboardManagerState& keyboardManagerState, const json::JsonObject& jsonData)
|
||
|
|
{
|
||
|
|
bool result = true;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
auto remapShortcutsData = jsonData.GetNamedObject(KeyboardManagerConstants::RemapShortcutsSettingName);
|
||
|
|
keyboardManagerState.ClearOSLevelShortcuts();
|
||
|
|
keyboardManagerState.ClearAppSpecificShortcuts();
|
||
|
|
if (remapShortcutsData)
|
||
|
|
{
|
||
|
|
// Load os level shortcut remaps
|
||
|
|
try
|
||
|
|
{
|
||
|
|
auto globalRemapShortcuts = remapShortcutsData.GetNamedArray(KeyboardManagerConstants::GlobalRemapShortcutsSettingName);
|
||
|
|
for (const auto& it : globalRemapShortcuts)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
auto originalKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::OriginalKeysSettingName);
|
||
|
|
auto newRemapKeys = it.GetObjectW().GetNamedString(KeyboardManagerConstants::NewRemapKeysSettingName);
|
||
|
|
|
||
|
|
// If remapped to a shortcut
|
||
|
|
if (std::wstring(newRemapKeys).find(L";") != std::string::npos)
|
||
|
|
{
|
||
|
|
keyboardManagerState.AddOSLevelShortcut(Shortcut(originalKeys.c_str()), Shortcut(newRemapKeys.c_str()));
|
||
|
|
}
|
||
|
|
|
||
|
|
// If remapped to a key
|
||
|
|
else
|
||
|
|
{
|
||
|
|
keyboardManagerState.AddOSLevelShortcut(Shortcut(originalKeys.c_str()), std::stoul(newRemapKeys.c_str()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"Improper Key Data JSON. Try the next shortcut.");
|
||
|
|
result = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"Improper JSON format for os level shortcut remaps. Skip to next remap type");
|
||
|
|
result = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Load app specific shortcut remaps
|
||
|
|
result = result && LoadAppSpecificShortcutRemaps(keyboardManagerState, remapShortcutsData);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"Improper JSON format for shortcut remaps. Skip to next remap type");
|
||
|
|
result = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool SettingsHelper::LoadSettings(KeyboardManagerState& keyboardManagerState)
|
||
|
|
{
|
||
|
|
Logger::trace(L"SettingsHelper::LoadSettings()");
|
||
|
|
try
|
||
|
|
{
|
||
|
|
PowerToysSettings::PowerToyValues settings = PowerToysSettings::PowerToyValues::load_from_settings_file(KeyboardManagerConstants::ModuleName);
|
||
|
|
auto current_config = settings.get_string_value(KeyboardManagerConstants::ActiveConfigurationSettingName);
|
||
|
|
|
||
|
|
if (!current_config)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
keyboardManagerState.SetCurrentConfigName(*current_config);
|
||
|
|
|
||
|
|
// Read the config file and load the remaps.
|
||
|
|
auto configFile = json::from_file(PTSettingsHelper::get_module_save_folder_location(KeyboardManagerConstants::ModuleName) + L"\\" + *current_config + L".json");
|
||
|
|
if (!configFile)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool result = LoadSingleKeyRemaps(keyboardManagerState, *configFile);
|
||
|
|
result = result && LoadShortcutRemaps(keyboardManagerState, *configFile);
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
catch (...)
|
||
|
|
{
|
||
|
|
Logger::error(L"SettingsHelper::LoadSettings() failed");
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|