From 37e43bbff36dd7f21d0a483eda62509b9bd7aebf Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 22:52:34 +0800 Subject: ... --- include/cru/ui/ShortcutHub.hpp | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 include/cru/ui/ShortcutHub.hpp (limited to 'include/cru/ui/ShortcutHub.hpp') diff --git a/include/cru/ui/ShortcutHub.hpp b/include/cru/ui/ShortcutHub.hpp new file mode 100644 index 00000000..9995a4e1 --- /dev/null +++ b/include/cru/ui/ShortcutHub.hpp @@ -0,0 +1,78 @@ +#pragma once +#include "Base.hpp" + +#include "cru/platform/native/Keyboard.hpp" + +#include +#include +#include +#include +#include + +namespace cru::ui { + +class ShortcutKeyBind { + public: + ShortcutKeyBind(platform::native::KeyCode key, + platform::native::KeyModifier modifier) + : key_(key), modifier_(modifier) {} + + CRU_DEFAULT_COPY(ShortcutKeyBind) + CRU_DEFAULT_MOVE(ShortcutKeyBind) + + ~ShortcutKeyBind() = default; + + platform::native::KeyCode GetKey() const { return key_; } + platform::native::KeyModifier GetModifier() const { return modifier_; } + + bool Is(platform::native::KeyCode key, + platform::native::KeyModifier modifier) const { + return key == key_ && modifier == modifier_; + } + + bool operator==(const ShortcutKeyBind& other) const { + return this->key_ == other.key_ && this->modifier_ == other.modifier_; + } + + bool operator!=(const ShortcutKeyBind& other) const { + return !this->operator==(other); + } + + private: + platform::native::KeyCode key_; + platform::native::KeyModifier modifier_; +}; + +struct ShortcutInfo { + std::u16string name; + ShortcutKeyBind key_bind; + std::function handler; +}; + +class ShortcutHub : public Object { + public: + ShortcutHub(); + + CRU_DELETE_COPY(ShortcutHub) + CRU_DELETE_MOVE(ShortcutHub) + + ~ShortcutHub() override; + + // Handler return true if it consumes the shortcut. Or return false if it does + // not handle the shortcut. Name is just for debug. + int RegisterShortcut(std::u16string name, ShortcutKeyBind bind, + std::function handler); + + void UnregisterShortcut(int id); + + std::vector GetAllShortcuts() const; + std::optional GetShortcut(int id) const; + std::vector GetShortcutByKeyBind( + const ShortcutKeyBind& key_bind) const; + + void Install(Control* control); + void Uninstall(); + + private: +}; +} // namespace cru::ui -- cgit v1.2.3