yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command_palette.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SYSTEM_COMMAND_PALETTE_H_
2#define YAZE_APP_EDITOR_SYSTEM_COMMAND_PALETTE_H_
3
4#include <cstdint>
5#include <functional>
6#include <memory>
7#include <string>
8#include <string_view>
9#include <unordered_map>
10#include <vector>
11
12namespace yaze {
13namespace editor {
14
15class CommandPalette;
16class WorkspaceWindowManager;
17class EditorRegistry;
18class RecentProjectsModel;
19
28 public:
29 virtual ~CommandProvider() = default;
31 virtual std::string ProviderId() const = 0;
33 virtual void Provide(CommandPalette* palette) = 0;
34};
35
40 static constexpr const char* kPanel = "Panels";
41 static constexpr const char* kEditor = "Editor";
42 static constexpr const char* kLayout = "Layout";
43 static constexpr const char* kFile = "File";
44 static constexpr const char* kEdit = "Edit";
45 static constexpr const char* kView = "View";
46 static constexpr const char* kNavigation = "Navigation";
47 static constexpr const char* kTools = "Tools";
48 static constexpr const char* kWorkflow = "Workflow";
49 static constexpr const char* kHelp = "Help";
50 static constexpr const char* kDrawer = "Drawers";
51};
52
54 std::string name;
55 std::string category;
56 std::string description;
57 std::string shortcut;
58 std::function<void()> callback;
59 int usage_count = 0;
60 int64_t last_used_ms = 0;
63 std::string provider_id;
66 bool enabled = true;
67 std::string note;
68};
69
75
77 public:
78 void AddCommand(const std::string& name, const std::string& category,
79 const std::string& description, const std::string& shortcut,
80 std::function<void()> callback);
81
85 bool RecordUsage(const std::string& name);
86
90 std::vector<CommandEntry> SearchCommands(const std::string& query);
91 std::vector<CommandMatch> Search(const std::string& query,
92 int64_t now_ms) const;
93 std::vector<CommandMatch> Search(const std::string& query) const;
94
95 std::vector<CommandEntry> GetRecentCommands(int limit = 10);
96
97 std::vector<CommandEntry> GetFrequentCommands(int limit = 10);
98
103 std::vector<CommandEntry> GetAllCommands() const;
104
108 std::vector<CommandEntry> GetVisibleCommands() const;
109
113 size_t GetCommandCount() const { return commands_.size(); }
114
119 void Clear();
120
121 // ============================================================================
122 // Providers
123 // ============================================================================
124
129 void RegisterProvider(std::unique_ptr<CommandProvider> provider);
130
133 void UnregisterProvider(const std::string& provider_id);
134
137 void RefreshProvider(const std::string& provider_id);
138
140 void RefreshProviders();
141
144 void RemoveProviderCommands(const std::string& provider_id);
145
146 // ============================================================================
147 // Bulk Registration Methods (used by built-in providers; kept public so
148 // tests and legacy callers can still drive registration directly).
149 // ============================================================================
150
157 size_t session_id);
158
164 std::function<void(const std::string&)> switch_callback);
165
171 std::function<void(const std::string&)> apply_callback);
172
181 std::function<void(const std::string&)> open_callback);
182
189 void RegisterOverworldMapCommands(size_t session_id);
190
197 void RegisterDungeonRoomCommands(size_t session_id);
198
206 std::function<void(int drawer_type)> toggle_callback,
207 std::function<void()> cycle_next = {},
208 std::function<void()> cycle_prev = {});
209
213 void RegisterWorkflowCommands(WorkspaceWindowManager* window_manager,
214 size_t session_id);
215
240 const RecentProjectsModel* model,
241 const std::vector<std::string>& template_names,
242 std::function<void(const std::string&)> remove_callback,
243 std::function<void(const std::string&)> toggle_pin_callback,
244 std::function<void()> undo_remove_callback,
245 std::function<void()> clear_recents_callback,
246 std::function<void(const std::string&)> create_from_template_callback,
247 std::function<void()> dismiss_welcome_callback,
248 std::function<void()> show_welcome_callback);
249
254 void SaveHistory(const std::string& filepath);
255
260 void LoadHistory(const std::string& filepath);
261
263 static int FuzzyScore(const std::string& text, const std::string& query);
264
270 static int ScoreText(std::string_view text, std::string_view query);
271
275 static int ScoreEntry(const CommandEntry& entry, std::string_view query,
276 int64_t now_ms);
277
282 static std::string NormalizeCommandName(std::string_view name);
283
286 static bool IsInternalCommandId(std::string_view name);
287
290 std::string CanonicalCommandName(const std::string& name) const;
291
292 static int64_t NowMs();
293
296 uint64_t generation() const { return generation_; }
297
298 private:
299 uint64_t generation_ = 0;
300 struct UsageStats {
301 int count = 0;
302 int64_t last_used_ms = 0;
303 };
304
305 std::unordered_map<std::string, CommandEntry> commands_;
307 std::unordered_map<std::string, UsageStats> usage_;
308 std::vector<std::unique_ptr<CommandProvider>> providers_;
312};
313
314} // namespace editor
315} // namespace yaze
316
317#endif // YAZE_APP_EDITOR_SYSTEM_COMMAND_PALETTE_H_
std::vector< CommandEntry > SearchCommands(const std::string &query)
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 SaveHistory(const std::string &filepath)
Save command usage history to disk.
std::string CanonicalCommandName(const std::string &name) const
void Clear()
Clear all commands (and forget every registered provider). Usage history is kept so refreshing provid...
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)
size_t GetCommandCount() const
Get command count.
void LoadHistory(const std::string &filepath)
Load command usage history from disk.
void UnregisterProvider(const std::string &provider_id)
void RegisterPanelCommands(WorkspaceWindowManager *window_manager, size_t session_id)
Register all window toggle commands from WorkspaceWindowManager.
void RemoveProviderCommands(const std::string &provider_id)
std::vector< CommandEntry > GetAllCommands() const
Get all registered commands (raw, not deduplicated)
static std::string NormalizeCommandName(std::string_view name)
std::unordered_map< std::string, CommandEntry > commands_
void RegisterProvider(std::unique_ptr< CommandProvider > provider)
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 int ScoreEntry(const CommandEntry &entry, std::string_view query, int64_t now_ms)
static int ScoreText(std::string_view text, std::string_view query)
std::vector< CommandEntry > GetRecentCommands(int limit=10)
std::vector< CommandEntry > GetVisibleCommands() const
bool RecordUsage(const std::string &name)
void RefreshProviders()
Remove-and-re-run every registered provider.
static bool IsInternalCommandId(std::string_view name)
std::vector< std::unique_ptr< CommandProvider > > providers_
void RegisterRecentFilesCommands(std::function< void(const std::string &)> open_callback)
Register commands to open recent files.
std::vector< CommandMatch > Search(const std::string &query, int64_t now_ms) const
std::unordered_map< std::string, UsageStats > usage_
Usage keyed by command name; survives Clear() and provider refreshes.
std::vector< CommandEntry > GetFrequentCommands(int limit=10)
static int FuzzyScore(const std::string &text, const std::string &query)
Legacy name for ScoreText (kept for the Window Finder).
void RefreshProvider(const std::string &provider_id)
Plug-in command source for the palette.
virtual std::string ProviderId() const =0
Stable identifier. Used for selective refresh; must be unique.
virtual ~CommandProvider()=default
virtual void Provide(CommandPalette *palette)=0
Populate palette with this source's commands.
Central registry for all editor cards with session awareness and dependency injection.
Categories for command palette entries.
static constexpr const char * kDrawer
static constexpr const char * kTools
static constexpr const char * kLayout
static constexpr const char * kWorkflow
static constexpr const char * kFile
static constexpr const char * kView
static constexpr const char * kEditor
static constexpr const char * kHelp
static constexpr const char * kEdit
static constexpr const char * kPanel
static constexpr const char * kNavigation
std::function< void()> callback
A search hit: the (deduplicated) entry plus the score it matched with.