aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/ShortcutHub.hpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-10-28 00:01:30 +0800
committercrupest <crupest@outlook.com>2020-10-28 00:01:30 +0800
commitc7c9c62fd3813a6230a2af7fc8c9882baa426a7f (patch)
tree4528e624b896907d7b87d12a7bd26be4556ddc06 /include/cru/ui/ShortcutHub.hpp
parent37e43bbff36dd7f21d0a483eda62509b9bd7aebf (diff)
downloadcru-c7c9c62fd3813a6230a2af7fc8c9882baa426a7f.tar.gz
cru-c7c9c62fd3813a6230a2af7fc8c9882baa426a7f.tar.bz2
cru-c7c9c62fd3813a6230a2af7fc8c9882baa426a7f.zip
...
Diffstat (limited to 'include/cru/ui/ShortcutHub.hpp')
-rw-r--r--include/cru/ui/ShortcutHub.hpp36
1 files changed, 32 insertions, 4 deletions
diff --git a/include/cru/ui/ShortcutHub.hpp b/include/cru/ui/ShortcutHub.hpp
index 9995a4e1..7a75e4a1 100644
--- a/include/cru/ui/ShortcutHub.hpp
+++ b/include/cru/ui/ShortcutHub.hpp
@@ -1,12 +1,17 @@
#pragma once
#include "Base.hpp"
+#include "cru/common/Base.hpp"
+#include "cru/common/Event.hpp"
#include "cru/platform/native/Keyboard.hpp"
+#include <cstddef>
#include <functional>
+#include <memory>
#include <optional>
#include <string>
#include <string_view>
+#include <unordered_map>
#include <vector>
namespace cru::ui {
@@ -42,8 +47,23 @@ class ShortcutKeyBind {
platform::native::KeyCode key_;
platform::native::KeyModifier modifier_;
};
+} // namespace cru::ui
+
+namespace std {
+template <>
+struct hash<cru::ui::ShortcutKeyBind> {
+ std::size_t operator()(const cru::ui::ShortcutKeyBind& value) const {
+ std::size_t result = 0;
+ cru::hash_combine(result, value.GetKey());
+ cru::hash_combine(result, value.GetModifier());
+ return result;
+ }
+};
+} // namespace std
+namespace cru::ui {
struct ShortcutInfo {
+ int id;
std::u16string name;
ShortcutKeyBind key_bind;
std::function<bool()> handler;
@@ -51,15 +71,16 @@ struct ShortcutInfo {
class ShortcutHub : public Object {
public:
- ShortcutHub();
+ ShortcutHub() = default;
CRU_DELETE_COPY(ShortcutHub)
CRU_DELETE_MOVE(ShortcutHub)
- ~ShortcutHub() override;
+ ~ShortcutHub() override = default;
// Handler return true if it consumes the shortcut. Or return false if it does
- // not handle the shortcut. Name is just for debug.
+ // not handle the shortcut. Name is just for debug. Return an id used for
+ // unregistering.
int RegisterShortcut(std::u16string name, ShortcutKeyBind bind,
std::function<bool()> handler);
@@ -67,12 +88,19 @@ class ShortcutHub : public Object {
std::vector<ShortcutInfo> GetAllShortcuts() const;
std::optional<ShortcutInfo> GetShortcut(int id) const;
- std::vector<ShortcutInfo> GetShortcutByKeyBind(
+ const std::vector<ShortcutInfo>& GetShortcutByKeyBind(
const ShortcutKeyBind& key_bind) const;
void Install(Control* control);
void Uninstall();
private:
+ std::unordered_map<ShortcutKeyBind, std::vector<ShortcutInfo>> map_;
+
+ const std::vector<ShortcutInfo> empty_list_;
+
+ int current_id_ = 1;
+
+ EventRevokerListGuard event_guard_;
};
} // namespace cru::ui