yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
shortcut_manager.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SYSTEM_SHORTCUT_MANAGER_H
2#define YAZE_APP_EDITOR_SYSTEM_SHORTCUT_MANAGER_H
3
4#include <functional>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9#include "absl/strings/string_view.h"
10
11// Must define before including imgui.h
12#ifndef IMGUI_DEFINE_MATH_OPERATORS
13#define IMGUI_DEFINE_MATH_OPERATORS
14#endif
15
16#include "imgui/imgui.h"
17
18namespace yaze {
19namespace editor {
20
21struct Shortcut {
22 enum class Scope { kGlobal, kEditor, kPanel };
23 std::string name;
25 std::vector<ImGuiKey> keys;
26 std::function<void()> callback;
27};
28
29std::vector<ImGuiKey> ParseShortcut(const std::string& shortcut);
30
31std::string PrintShortcut(const std::vector<ImGuiKey>& keys);
32
39std::string InferShortcutGroup(absl::string_view name);
40
42 public:
43 void RegisterShortcut(const std::string& name,
44 const std::vector<ImGuiKey>& keys,
46 shortcuts_[name] = {name, scope, keys};
47 }
48 void RegisterShortcut(const std::string& name,
49 const std::vector<ImGuiKey>& keys,
50 std::function<void()> callback,
52 shortcuts_[name] = {name, scope, keys, callback};
53 }
54
55 void RegisterShortcut(const std::string& name, ImGuiKey key,
56 std::function<void()> callback,
58 shortcuts_[name] = {name, scope, {key}, callback};
59 }
60
67 void RegisterCommand(const std::string& name, std::function<void()> callback,
69 shortcuts_[name] = {name, scope, {}, callback}; // Empty key vector
70 }
71
72 void ExecuteShortcut(const std::string& name) const {
73 shortcuts_.at(name).callback();
74 }
75
76 // Access the shortcut and print the readable name of the shortcut for menus
77 const Shortcut& GetShortcut(const std::string& name) const {
78 return shortcuts_.at(name);
79 }
80
81 const Shortcut* FindShortcut(const std::string& name) const {
82 auto it = shortcuts_.find(name);
83 return it != shortcuts_.end() ? &it->second : nullptr;
84 }
85
86 // Get shortcut callback function
87 std::function<void()> GetCallback(const std::string& name) const {
88 return shortcuts_.at(name).callback;
89 }
90
91 const std::string GetKeys(const std::string& name) const {
92 return PrintShortcut(shortcuts_.at(name).keys);
93 }
94
95 const std::unordered_map<std::string, Shortcut>& GetShortcuts() const {
96 return shortcuts_;
97 }
98 bool UpdateShortcutKeys(const std::string& name,
99 const std::vector<ImGuiKey>& keys);
100 std::vector<Shortcut> GetShortcutsByScope(Shortcut::Scope scope) const {
101 std::vector<Shortcut> result;
102 result.reserve(shortcuts_.size());
103 for (const auto& [_, sc] : shortcuts_) {
104 if (sc.scope == scope)
105 result.push_back(sc);
106 }
107 return result;
108 }
109
110 // Convenience methods for registering common shortcuts
111 void RegisterStandardShortcuts(std::function<void()> save_callback,
112 std::function<void()> open_callback,
113 std::function<void()> close_callback,
114 std::function<void()> find_callback,
115 std::function<void()> settings_callback);
116
117 void RegisterWindowNavigationShortcuts(std::function<void()> focus_left,
118 std::function<void()> focus_right,
119 std::function<void()> focus_up,
120 std::function<void()> focus_down,
121 std::function<void()> close_window,
122 std::function<void()> split_horizontal,
123 std::function<void()> split_vertical);
124
125 private:
126 std::unordered_map<std::string, Shortcut> shortcuts_;
127};
128
129void ExecuteShortcuts(const ShortcutManager& shortcut_manager);
130
131} // namespace editor
132} // namespace yaze
133
134#endif // YAZE_APP_EDITOR_SYSTEM_SHORTCUT_MANAGER_H
const Shortcut & GetShortcut(const std::string &name) const
const Shortcut * FindShortcut(const std::string &name) const
const std::string GetKeys(const std::string &name) const
void RegisterCommand(const std::string &name, std::function< void()> callback, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
Register a command without keyboard shortcut (command palette only)
void RegisterShortcut(const std::string &name, const std::vector< ImGuiKey > &keys, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
void RegisterShortcut(const std::string &name, ImGuiKey key, std::function< void()> callback, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
std::function< void()> GetCallback(const std::string &name) const
std::vector< Shortcut > GetShortcutsByScope(Shortcut::Scope scope) const
void RegisterShortcut(const std::string &name, const std::vector< ImGuiKey > &keys, std::function< void()> callback, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
void RegisterStandardShortcuts(std::function< void()> save_callback, std::function< void()> open_callback, std::function< void()> close_callback, std::function< void()> find_callback, std::function< void()> settings_callback)
void RegisterWindowNavigationShortcuts(std::function< void()> focus_left, std::function< void()> focus_right, std::function< void()> focus_up, std::function< void()> focus_down, std::function< void()> close_window, std::function< void()> split_horizontal, std::function< void()> split_vertical)
void ExecuteShortcut(const std::string &name) const
std::unordered_map< std::string, Shortcut > shortcuts_
const std::unordered_map< std::string, Shortcut > & GetShortcuts() const
bool UpdateShortcutKeys(const std::string &name, const std::vector< ImGuiKey > &keys)
std::string InferShortcutGroup(absl::string_view name)
Menu-IA group for a shortcut/command name (File, View, Drawers, …).
std::vector< ImGuiKey > ParseShortcut(const std::string &shortcut)
std::string PrintShortcut(const std::vector< ImGuiKey > &keys)
void ExecuteShortcuts(const ShortcutManager &shortcut_manager)
std::function< void()> callback
std::vector< ImGuiKey > keys