yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command_palette_providers.cc
Go to the documentation of this file.
2
3#include <map>
4#include <utility>
5
6#include "absl/strings/match.h"
7#include "absl/strings/numbers.h"
8#include "absl/strings/str_format.h"
9#include "app/editor/editor.h"
14
15namespace yaze {
16namespace editor {
17
19 WorkspaceWindowManager* window_manager, size_t session_id)
20 : window_manager_(window_manager), session_id_(session_id) {}
21
25
27 std::function<void(const std::string&)> switch_callback)
28 : switch_callback_(std::move(switch_callback)) {}
29
33
35 std::function<void(const std::string&)> open_callback)
36 : open_callback_(std::move(open_callback)) {}
37
41
43 : session_id_(session_id) {}
44
48
49std::string LookupShortcutHint(const ShortcutManager* shortcut_manager,
50 const std::string& name) {
51 return shortcut_manager ? shortcut_manager->GetDisplayString(name)
52 : std::string();
53}
54
55std::string PaletteNameForShortcut(const std::string& name) {
57 return name;
58 // "switch.<EditorType>" -> "Switch to <Editor Name>" so it merges with the
59 // palette's "Switch to: <Category> Editor" entry and lends it Ctrl+<n>.
60 if (absl::StartsWith(name, "switch.")) {
61 int type_value = 0;
62 if (absl::SimpleAtoi(name.substr(7), &type_value) && type_value > 0 &&
63 type_value < static_cast<int>(kEditorTypeCount)) {
64 return "Switch to " +
65 EditorRegistry::GetEditorName(static_cast<EditorType>(type_value));
66 }
67 }
68 return {};
69}
70
72 : session_id_(session_id) {}
73
77
79 const ShortcutManager* shortcut_manager)
80 : shortcut_manager_(shortcut_manager) {}
81
83 if (!palette || !shortcut_manager_)
84 return;
85 // Several ids can map to one palette name ("switch.6" and the keyless
86 // "Switch to Overworld Editor" command). Keep the bound one so the hint is
87 // not lost to unordered_map iteration order.
88 struct Pick {
89 std::string source;
90 std::string hint;
91 };
92 std::map<std::string, Pick> picks;
93 for (const auto& [name, shortcut] : shortcut_manager_->GetShortcuts()) {
94 if (!shortcut.callback)
95 continue; // Bindings without an action would be no-op rows.
96 const std::string palette_name = PaletteNameForShortcut(name);
97 if (palette_name.empty())
98 continue;
99 Pick pick{name, LookupShortcutHint(shortcut_manager_, name)};
100 auto it = picks.find(palette_name);
101 if (it == picks.end()) {
102 picks.emplace(palette_name, std::move(pick));
103 } else if (it->second.hint.empty() && !pick.hint.empty()) {
104 it->second = std::move(pick);
105 }
106 }
107 const ShortcutManager* manager = shortcut_manager_;
108 for (const auto& [palette_name, pick] : picks) {
109 palette->AddCommand(palette_name, "Shortcuts",
110 InferShortcutGroup(pick.source), pick.hint,
111 [manager, source = pick.source]() {
112 const Shortcut* live = manager->FindShortcut(source);
113 if (live && live->callback)
114 live->callback();
115 });
116 }
117}
118
120 std::function<void(int drawer_type)> toggle_callback,
121 std::function<void()> cycle_next, std::function<void()> cycle_prev)
122 : toggle_callback_(std::move(toggle_callback)),
123 cycle_next_(std::move(cycle_next)),
124 cycle_prev_(std::move(cycle_prev)) {}
125
129
131 std::function<void(const std::string&)> apply_callback)
132 : apply_callback_(std::move(apply_callback)) {}
133
137
139 WorkspaceWindowManager* window_manager, size_t session_id)
140 : window_manager_(window_manager), session_id_(session_id) {}
141
145
147 WorkspaceWindowManager* window_manager, UserSettings* user_settings,
148 size_t session_id)
149 : window_manager_(window_manager),
150 user_settings_(user_settings),
151 session_id_(session_id) {}
152
154 if (!palette || !user_settings_ || !window_manager_)
155 return;
156
157 auto categories = window_manager_->GetAllCategories(session_id_);
158
159 // Global helpers first — they're callable any time.
160 palette->AddCommand("Sidebar: Reset Order", CommandCategory::kView,
161 "Clear the custom sidebar order and revert to defaults",
162 /*shortcut=*/"", [this]() {
163 if (!user_settings_)
164 return;
165 user_settings_->prefs().sidebar_order.clear();
166 (void)user_settings_->Save();
167 });
168
169 palette->AddCommand("Sidebar: Show All Categories", CommandCategory::kView,
170 "Un-hide every sidebar category",
171 /*shortcut=*/"", [this]() {
172 if (!user_settings_)
173 return;
175 (void)user_settings_->Save();
176 });
177
178 // Per-category toggles. Using static "Toggle" labels avoids the need to
179 // refresh the provider every time state flips.
180 for (const auto& cat : categories) {
182 continue;
183
184 std::string pin_name = absl::StrFormat("Sidebar: Toggle Pin: %s", cat);
185 std::string pin_desc =
186 absl::StrFormat("Pin or unpin %s at the top of the sidebar", cat);
187 std::string captured_cat_pin = cat;
188 palette->AddCommand(pin_name, CommandCategory::kView, pin_desc,
189 /*shortcut=*/"", [this, captured_cat_pin]() {
190 if (!user_settings_)
191 return;
192 auto& pinned = user_settings_->prefs().sidebar_pinned;
193 if (pinned.count(captured_cat_pin)) {
194 pinned.erase(captured_cat_pin);
195 } else {
196 pinned.insert(captured_cat_pin);
197 }
198 (void)user_settings_->Save();
199 });
200
201 std::string hide_name =
202 absl::StrFormat("Sidebar: Toggle Visibility: %s", cat);
203 std::string hide_desc =
204 absl::StrFormat("Show or hide %s in the sidebar", cat);
205 std::string captured_cat_hide = cat;
206 palette->AddCommand(hide_name, CommandCategory::kView, hide_desc,
207 /*shortcut=*/"", [this, captured_cat_hide]() {
208 if (!user_settings_)
209 return;
210 auto& hidden = user_settings_->prefs().sidebar_hidden;
211 if (hidden.count(captured_cat_hide)) {
212 hidden.erase(captured_cat_hide);
213 } else {
214 hidden.insert(captured_cat_hide);
215 }
216 (void)user_settings_->Save();
217 });
218 }
219}
220
222 : callbacks_(std::move(callbacks)) {}
223
231
232} // namespace editor
233} // namespace yaze
void RegisterDrawerCommands(std::function< void(int drawer_type)> toggle_callback, std::function< void()> cycle_next={}, std::function< void()> cycle_prev={})
Register right-drawer toggle commands from GetDrawerCatalog().
void RegisterDungeonRoomCommands(size_t session_id)
Register dungeon room navigation commands.
void RegisterOverworldMapCommands(size_t session_id)
Register overworld map navigation commands (0x00-0x9F).
void RegisterWelcomeCommands(const RecentProjectsModel *model, const std::vector< std::string > &template_names, std::function< void(const std::string &)> remove_callback, std::function< void(const std::string &)> toggle_pin_callback, std::function< void()> undo_remove_callback, std::function< void()> clear_recents_callback, std::function< void(const std::string &)> create_from_template_callback, std::function< void()> dismiss_welcome_callback, std::function< void()> show_welcome_callback)
Expose welcome-screen actions through the command palette.
void RegisterLayoutCommands(std::function< void(const std::string &)> apply_callback)
Register layout preset commands.
void AddCommand(const std::string &name, const std::string &category, const std::string &description, const std::string &shortcut, std::function< void()> callback)
void RegisterPanelCommands(WorkspaceWindowManager *window_manager, size_t session_id)
Register all window toggle commands from WorkspaceWindowManager.
void RegisterWorkflowCommands(WorkspaceWindowManager *window_manager, size_t session_id)
Register hack workflow commands from workflow-aware panels/actions.
void RegisterEditorCommands(std::function< void(const std::string &)> switch_callback)
Register all editor switch commands.
static bool IsInternalCommandId(std::string_view name)
void RegisterRecentFilesCommands(std::function< void(const std::string &)> open_callback)
Register commands to open recent files.
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
DrawerCommandsProvider(std::function< void(int drawer_type)> toggle_callback, std::function< void()> cycle_next={}, std::function< void()> cycle_prev={})
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
EditorCommandsProvider(std::function< void(const std::string &)> switch_callback)
std::function< void(const std::string &) switch_callback_)
static std::string GetEditorName(EditorType type)
LayoutCommandsProvider(std::function< void(const std::string &)> apply_callback)
std::function< void(const std::string &) apply_callback_)
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
PanelCommandsProvider(WorkspaceWindowManager *window_manager, size_t session_id)
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
std::function< void(const std::string &) open_callback_)
RecentFilesCommandsProvider(std::function< void(const std::string &)> open_callback)
ShortcutCommandsProvider(const ShortcutManager *shortcut_manager)
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
const Shortcut * FindShortcut(const std::string &name) const
std::string GetDisplayString(const std::string &name) const
Live, platform-formatted binding for a shortcut name.
const std::unordered_map< std::string, Shortcut > & GetShortcuts() const
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
SidebarCommandsProvider(WorkspaceWindowManager *window_manager, UserSettings *user_settings, size_t session_id)
Manages user preferences and settings persistence.
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
void Provide(CommandPalette *palette) override
Populate palette with this source's commands.
WorkflowCommandsProvider(WorkspaceWindowManager *window_manager, size_t session_id)
Central registry for all editor cards with session awareness and dependency injection.
static constexpr const char * kDashboardCategory
std::vector< std::string > GetAllCategories(size_t session_id) const
std::string InferShortcutGroup(absl::string_view name)
Menu-IA group for a shortcut/command name (File, View, Drawers, …).
std::string PaletteNameForShortcut(const std::string &name)
std::string LookupShortcutHint(const ShortcutManager *shortcut_manager, const std::string &name)
constexpr size_t kEditorTypeCount
Definition editor.h:236
static constexpr const char * kView
std::function< void()> callback
std::unordered_set< std::string > sidebar_hidden
std::function< void(const std::string &) toggle_pin)
std::function< void(const std::string &) remove)
std::function< void(const std::string &) create_from_template)