blob: 341e76da76c21069a265a7a9103efa128cdf3186 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#pragma once
#include "../Base.h"
#include "../events/KeyEventArgs.h"
#include "cru/base/Base.h"
#include "cru/base/Event.h"
#include "cru/platform/gui/Keyboard.h"
#include <cstddef>
#include <functional>
#include <optional>
#include <unordered_map>
#include <vector>
namespace cru::ui::helper {
class ShortcutKeyBind {
public:
ShortcutKeyBind(
platform::gui::KeyCode key,
platform::gui::KeyModifier modifier = platform::gui::KeyModifiers::none)
: key_(key), modifier_(modifier) {}
CRU_DEFAULT_COPY(ShortcutKeyBind)
CRU_DEFAULT_MOVE(ShortcutKeyBind)
~ShortcutKeyBind() = default;
platform::gui::KeyCode GetKey() const { return key_; }
platform::gui::KeyModifier GetModifier() const { return modifier_; }
ShortcutKeyBind AddModifier(platform::gui::KeyModifier modifier) const {
return ShortcutKeyBind(key_, modifier_ | modifier);
}
bool Is(platform::gui::KeyCode key,
platform::gui::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);
}
String ToString() const {
String result = u"(";
result += platform::gui::ToString(modifier_);
result += u")";
result += platform::gui::ToString(key_);
return result;
}
private:
platform::gui::KeyCode key_;
platform::gui::KeyModifier modifier_;
};
inline String ToString(const ShortcutKeyBind& key_bind) {
return key_bind.ToString();
}
} // namespace cru::ui::helper
namespace std {
template <>
struct hash<cru::ui::helper::ShortcutKeyBind> {
std::size_t operator()(const cru::ui::helper::ShortcutKeyBind& value) const {
std::size_t result = 0;
cru::hash_combine(result, static_cast<int>(value.GetKey()));
cru::hash_combine(result, value.GetModifier());
return result;
}
};
} // namespace std
namespace cru::ui::helper {
struct Shortcut {
// Just for debug.
String name;
ShortcutKeyBind key_bind;
// Return true if it consumes the shortcut. Or return false if it does not
// handle the shortcut.
std::function<bool()> handler;
};
struct ShortcutInfo {
int id;
String name;
ShortcutKeyBind key_bind;
std::function<bool()> handler;
};
class CRU_UI_API ShortcutHub : public Object {
CRU_DEFINE_CLASS_LOG_TAG(u"ShortcutHub")
public:
ShortcutHub() = default;
CRU_DELETE_COPY(ShortcutHub)
CRU_DELETE_MOVE(ShortcutHub)
~ShortcutHub() override = default;
int RegisterShortcut(String name, ShortcutKeyBind bind,
std::function<bool()> handler) {
return RegisterShortcut({std::move(name), bind, std::move(handler)});
}
// Return an id used for unregistering.
int RegisterShortcut(Shortcut shortcut);
void UnregisterShortcut(int id);
std::vector<ShortcutInfo> GetAllShortcuts() const;
std::optional<ShortcutInfo> GetShortcut(int id) const;
const std::vector<ShortcutInfo>& GetShortcutByKeyBind(
const ShortcutKeyBind& key_bind) const;
IEvent<events::KeyEventArgs&>* FallbackKeyEvent() { return &fallback_event_; }
void Install(controls::Control* control);
void Uninstall();
private:
void OnKeyDown(events::KeyEventArgs& event);
private:
std::unordered_map<ShortcutKeyBind, std::vector<ShortcutInfo>> map_;
std::vector<ShortcutInfo> empty_list_;
int current_id_ = 1;
Event<events::KeyEventArgs&> fallback_event_;
EventRevokerListGuard event_guard_;
};
} // namespace cru::ui::helper
|