aboutsummaryrefslogtreecommitdiff
path: root/src/ui/ShortcutHub.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-10-28 16:17:20 +0800
committercrupest <crupest@outlook.com>2020-10-28 16:17:20 +0800
commitfde24556042b76863bdec34dcc213cb7298f68f9 (patch)
treea6ed8de1a5044c24b72a1b71dad21bb977b6b1be /src/ui/ShortcutHub.cpp
parent9d4a37acbe66983654aa90dbd3aa3700038697b9 (diff)
downloadcru-fde24556042b76863bdec34dcc213cb7298f68f9.tar.gz
cru-fde24556042b76863bdec34dcc213cb7298f68f9.tar.bz2
cru-fde24556042b76863bdec34dcc213cb7298f68f9.zip
...
Diffstat (limited to 'src/ui/ShortcutHub.cpp')
-rw-r--r--src/ui/ShortcutHub.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/ui/ShortcutHub.cpp b/src/ui/ShortcutHub.cpp
index e5847f8c..145cfa87 100644
--- a/src/ui/ShortcutHub.cpp
+++ b/src/ui/ShortcutHub.cpp
@@ -1,5 +1,11 @@
#include "cru/ui/ShortcutHub.hpp"
+
+#include "cru/common/Logger.hpp"
+#include "cru/ui/Control.hpp"
+#include "cru/ui/DebugFlags.hpp"
+
#include <algorithm>
+#include <functional>
#include <iterator>
#include <optional>
@@ -55,4 +61,60 @@ const std::vector<ShortcutInfo>& ShortcutHub::GetShortcutByKeyBind(
if (result != map_.cend()) return result->second;
return empty_list_;
}
+
+void ShortcutHub::Install(Control* control) {
+ if (!event_guard_.IsEmpty()) {
+ log::Error(u"Shortcut hub is already installed. Failed to install.");
+ return;
+ }
+
+ event_guard_ += control->KeyDownEvent()->Bubble()->AddHandler(
+ std::bind(&ShortcutHub::OnKeyDown, this, std::placeholders::_1));
+}
+
+void ShortcutHub::Uninstall() {
+ if (event_guard_.IsEmpty()) {
+ log::Warn(u"Shortcut hub is not installed. Failed to uninstall.");
+ return;
+ }
+
+ event_guard_.Clear();
+}
+
+void ShortcutHub::OnKeyDown(event::KeyEventArgs& event) {
+ ShortcutKeyBind key_bind(event.GetKeyCode(), event.GetKeyModifier());
+ const auto& shortcut_list = this->GetShortcutByKeyBind(key_bind);
+
+ if constexpr (debug_flags::shortcut) {
+ if (shortcut_list.empty()) {
+ log::Debug(u"No shortcut for key bind {}.", key_bind.ToString());
+ }
+ log::Debug(u"Begin to handle shortcut for key bind {}.",
+ key_bind.ToString());
+ }
+
+ for (const auto& shortcut : shortcut_list) {
+ auto is_handled = shortcut.handler();
+ if (is_handled) {
+ if constexpr (debug_flags::shortcut) {
+ log::Debug(u"Handle {} handled it.", shortcut.name);
+ }
+
+ event.SetHandled();
+
+ break;
+ } else {
+ if constexpr (debug_flags::shortcut) {
+ log::Debug(u"Handle {} disdn't handle it.", shortcut.name);
+ }
+ }
+ }
+
+ if constexpr (debug_flags::shortcut) {
+ if (!shortcut_list.empty()) {
+ log::Debug(u"End handling shortcut for key bind {}.",
+ key_bind.ToString());
+ }
+ }
+}
} // namespace cru::ui