mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 21:20:06 +00:00
Add hotkey manager
This commit is contained in:
parent
3da1d15470
commit
2c63b4e387
3 changed files with 150 additions and 0 deletions
105
scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp
Normal file
105
scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#include <scwx/qt/manager/hotkey_manager.hpp>
|
||||
#include <scwx/qt/settings/hotkey_settings.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <QKeyEvent>
|
||||
#include <QKeySequence>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace manager
|
||||
{
|
||||
|
||||
static const std::string logPrefix_ = "scwx::qt::manager::hotkey_manager";
|
||||
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
|
||||
|
||||
class HotkeyManager::Impl
|
||||
{
|
||||
public:
|
||||
explicit Impl()
|
||||
{
|
||||
auto& hotkeySettings = settings::HotkeySettings::Instance();
|
||||
|
||||
for (auto hotkey : types::HotkeyIterator())
|
||||
{
|
||||
auto& hotkeyVariable = hotkeySettings.hotkey(hotkey);
|
||||
|
||||
UpdateHotkey(hotkey, hotkeyVariable.GetValue());
|
||||
|
||||
callbacks_.emplace_back(hotkeyVariable,
|
||||
hotkeyVariable.RegisterValueChangedCallback(
|
||||
[this, hotkey](const std::string& value)
|
||||
{ UpdateHotkey(hotkey, value); }));
|
||||
}
|
||||
}
|
||||
|
||||
~Impl()
|
||||
{
|
||||
for (auto& callback : callbacks_)
|
||||
{
|
||||
callback.first.UnregisterValueChangedCallback(callback.second);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateHotkey(types::Hotkey hotkey, const std::string& value);
|
||||
|
||||
std::vector<
|
||||
std::pair<settings::SettingsVariable<std::string>&, boost::uuids::uuid>>
|
||||
callbacks_ {};
|
||||
boost::container::flat_map<types::Hotkey, QKeySequence> hotkeys_ {};
|
||||
};
|
||||
|
||||
HotkeyManager::HotkeyManager() : p(std::make_unique<Impl>()) {}
|
||||
HotkeyManager::~HotkeyManager() = default;
|
||||
|
||||
void HotkeyManager::Impl::UpdateHotkey(types::Hotkey hotkey,
|
||||
const std::string& value)
|
||||
{
|
||||
hotkeys_.insert_or_assign(hotkey,
|
||||
QKeySequence {QString::fromStdString(value)});
|
||||
}
|
||||
|
||||
void HotkeyManager::HandleKeyPress(QKeyEvent* ev)
|
||||
{
|
||||
for (auto& hotkey : p->hotkeys_)
|
||||
{
|
||||
if (hotkey.second.count() == 1 &&
|
||||
hotkey.second[0] == ev->keyCombination())
|
||||
{
|
||||
Q_EMIT HotkeyPressed(hotkey.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HotkeyManager::HandleKeyRelease(QKeyEvent* ev)
|
||||
{
|
||||
Q_UNUSED(ev);
|
||||
}
|
||||
|
||||
std::shared_ptr<HotkeyManager> HotkeyManager::Instance()
|
||||
{
|
||||
static std::weak_ptr<HotkeyManager> hotkeyManagerReference_ {};
|
||||
static std::mutex instanceMutex_ {};
|
||||
|
||||
std::unique_lock lock(instanceMutex_);
|
||||
|
||||
std::shared_ptr<HotkeyManager> hotkeyManager =
|
||||
hotkeyManagerReference_.lock();
|
||||
|
||||
if (hotkeyManager == nullptr)
|
||||
{
|
||||
hotkeyManager = std::make_shared<HotkeyManager>();
|
||||
hotkeyManagerReference_ = hotkeyManager;
|
||||
}
|
||||
|
||||
return hotkeyManager;
|
||||
}
|
||||
|
||||
} // namespace manager
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
Loading…
Add table
Add a link
Reference in a new issue