From c7c9c62fd3813a6230a2af7fc8c9882baa426a7f Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Oct 2020 00:01:30 +0800 Subject: ... --- include/cru/ui/ShortcutHub.hpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'include/cru/ui') 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 #include +#include #include #include #include +#include #include 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 { + 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 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 handler); @@ -67,12 +88,19 @@ class ShortcutHub : public Object { std::vector GetAllShortcuts() const; std::optional GetShortcut(int id) const; - std::vector GetShortcutByKeyBind( + const std::vector& GetShortcutByKeyBind( const ShortcutKeyBind& key_bind) const; void Install(Control* control); void Uninstall(); private: + std::unordered_map> map_; + + const std::vector empty_list_; + + int current_id_ = 1; + + EventRevokerListGuard event_guard_; }; } // namespace cru::ui -- cgit v1.2.3