mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 22:50:05 +00:00
Add hotkey manager
This commit is contained in:
parent
3da1d15470
commit
2c63b4e387
3 changed files with 150 additions and 0 deletions
|
|
@ -88,6 +88,7 @@ set(SRC_GL_DRAW source/scwx/qt/gl/draw/draw_item.cpp
|
|||
set(HDR_MANAGER source/scwx/qt/manager/alert_manager.hpp
|
||||
source/scwx/qt/manager/download_manager.hpp
|
||||
source/scwx/qt/manager/font_manager.hpp
|
||||
source/scwx/qt/manager/hotkey_manager.hpp
|
||||
source/scwx/qt/manager/media_manager.hpp
|
||||
source/scwx/qt/manager/placefile_manager.hpp
|
||||
source/scwx/qt/manager/position_manager.hpp
|
||||
|
|
@ -101,6 +102,7 @@ set(HDR_MANAGER source/scwx/qt/manager/alert_manager.hpp
|
|||
set(SRC_MANAGER source/scwx/qt/manager/alert_manager.cpp
|
||||
source/scwx/qt/manager/download_manager.cpp
|
||||
source/scwx/qt/manager/font_manager.cpp
|
||||
source/scwx/qt/manager/hotkey_manager.cpp
|
||||
source/scwx/qt/manager/media_manager.cpp
|
||||
source/scwx/qt/manager/placefile_manager.cpp
|
||||
source/scwx/qt/manager/position_manager.cpp
|
||||
|
|
|
|||
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
|
||||
43
scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp
Normal file
43
scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#include <scwx/qt/types/hotkey_types.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QKeyEvent;
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace manager
|
||||
{
|
||||
|
||||
class HotkeyManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY_MOVE(HotkeyManager)
|
||||
|
||||
public:
|
||||
explicit HotkeyManager();
|
||||
~HotkeyManager();
|
||||
|
||||
void HandleKeyPress(QKeyEvent* event);
|
||||
void HandleKeyRelease(QKeyEvent* event);
|
||||
|
||||
static std::shared_ptr<HotkeyManager> Instance();
|
||||
|
||||
signals:
|
||||
void HotkeyPressed(scwx::qt::types::Hotkey hotkey);
|
||||
void HotkeyReleased(scwx::qt::types::Hotkey hotkey);
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> p;
|
||||
};
|
||||
|
||||
} // namespace manager
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
Loading…
Add table
Add a link
Reference in a new issue