mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
[FancyZones] Split zones-settings: layout hotkeys (#15514)
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
#include "pch.h"
|
||||
#include "LayoutHotkeys.h"
|
||||
|
||||
#include <common/logger/logger.h>
|
||||
|
||||
#include <FancyZonesLib/FancyZonesWinHookEventIDs.h>
|
||||
#include <FancyZonesLib/JsonHelpers.h>
|
||||
#include <FancyZonesLib/util.h>
|
||||
|
||||
namespace JsonUtils
|
||||
{
|
||||
struct LayoutHotkeysJSON
|
||||
{
|
||||
GUID uuid;
|
||||
int key;
|
||||
|
||||
static std::optional<LayoutHotkeysJSON> FromJson(const json::JsonObject& json)
|
||||
{
|
||||
try
|
||||
{
|
||||
LayoutHotkeysJSON result;
|
||||
|
||||
std::wstring uuidStr = json.GetNamedString(NonLocalizable::LayoutHotkeysIds::LayoutUuidID).c_str();
|
||||
auto uuidOpt = FancyZonesUtils::GuidFromString(uuidStr);
|
||||
if (!uuidOpt)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
result.uuid = uuidOpt.value();
|
||||
result.key = static_cast<int>(json.GetNamedNumber(NonLocalizable::LayoutHotkeysIds::KeyID));
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (const winrt::hresult_error&)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
LayoutHotkeys::THotkeyMap ParseJson(const json::JsonObject& json)
|
||||
{
|
||||
LayoutHotkeys::THotkeyMap map{};
|
||||
auto layoutHotkeys = json.GetNamedArray(NonLocalizable::LayoutHotkeysIds::LayoutHotkeysArrayID);
|
||||
|
||||
for (uint32_t i = 0; i < layoutHotkeys.Size(); ++i)
|
||||
{
|
||||
if (auto obj = LayoutHotkeysJSON::FromJson(layoutHotkeys.GetObjectAt(i)); obj.has_value())
|
||||
{
|
||||
map[obj->key] = obj->uuid;
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(map);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LayoutHotkeys::LayoutHotkeys()
|
||||
{
|
||||
const std::wstring& settingsFileName = LayoutHotkeysFileName();
|
||||
m_fileWatcher = std::make_unique<FileWatcher>(settingsFileName, [&]() {
|
||||
PostMessageW(HWND_BROADCAST, WM_PRIV_LAYOUT_HOTKEYS_FILE_UPDATE, NULL, NULL);
|
||||
});
|
||||
}
|
||||
|
||||
LayoutHotkeys& LayoutHotkeys::instance()
|
||||
{
|
||||
static LayoutHotkeys self;
|
||||
return self;
|
||||
}
|
||||
|
||||
void LayoutHotkeys::LoadData()
|
||||
{
|
||||
auto data = json::from_file(LayoutHotkeysFileName());
|
||||
|
||||
try
|
||||
{
|
||||
if (data)
|
||||
{
|
||||
m_hotkeyMap = JsonUtils::ParseJson(data.value());
|
||||
}
|
||||
else
|
||||
{
|
||||
m_hotkeyMap.clear();
|
||||
Logger::info(L"layout-hotkeys.json file is missing or malformed");
|
||||
}
|
||||
}
|
||||
catch (const winrt::hresult_error& e)
|
||||
{
|
||||
Logger::error(L"Parsing layout-hotkeys error: {}", e.message());
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<GUID> LayoutHotkeys::GetLayoutId(int key) const noexcept
|
||||
{
|
||||
auto iter = m_hotkeyMap.find(key);
|
||||
if (iter != m_hotkeyMap.end())
|
||||
{
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
size_t LayoutHotkeys::GetHotkeysCount() const noexcept
|
||||
{
|
||||
return m_hotkeyMap.size();
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <guiddef.h>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include <FancyZonesLib/ModuleConstants.h>
|
||||
|
||||
#include <common/SettingsAPI/FileWatcher.h>
|
||||
#include <common/SettingsAPI/settings_helpers.h>
|
||||
|
||||
namespace NonLocalizable
|
||||
{
|
||||
namespace LayoutHotkeysIds
|
||||
{
|
||||
const static wchar_t* LayoutHotkeysArrayID = L"layout-hotkeys";
|
||||
const static wchar_t* LayoutUuidID = L"layout-id";
|
||||
const static wchar_t* KeyID = L"key";
|
||||
}
|
||||
}
|
||||
|
||||
class LayoutHotkeys
|
||||
{
|
||||
public:
|
||||
using THotkeyMap = std::map<int, GUID>;
|
||||
|
||||
static LayoutHotkeys& instance();
|
||||
|
||||
inline static std::wstring LayoutHotkeysFileName()
|
||||
{
|
||||
std::wstring saveFolderPath = PTSettingsHelper::get_module_save_folder_location(NonLocalizable::ModuleKey);
|
||||
#if defined(UNIT_TESTS)
|
||||
return saveFolderPath + L"\\test-layout-hotkeys.json";
|
||||
#endif
|
||||
return saveFolderPath + L"\\layout-hotkeys.json";
|
||||
}
|
||||
|
||||
void LoadData();
|
||||
|
||||
std::optional<GUID> GetLayoutId(int key) const noexcept;
|
||||
size_t GetHotkeysCount() const noexcept;
|
||||
|
||||
private:
|
||||
LayoutHotkeys();
|
||||
~LayoutHotkeys() = default;
|
||||
|
||||
THotkeyMap m_hotkeyMap;
|
||||
std::unique_ptr<FileWatcher> m_fileWatcher;
|
||||
};
|
||||
Reference in New Issue
Block a user