Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
command_registry.hpp
Go to the documentation of this file.
1#pragma once
2
54
56
57#include <cstdint>
58#include <functional>
59#include <optional>
60#include <string>
61#include <string_view>
62#include <unordered_map>
63#include <vector>
64
65namespace pulp::state { class PropertiesFile; }
66
67namespace pulp::view {
68
69class View;
70
74using CommandID = std::uint32_t;
75
76inline constexpr CommandID kInvalidCommandID = 0;
77
81inline constexpr KeyCode kKeyComma = static_cast<KeyCode>(',');
82
88inline constexpr CommandID kCommandOpenSettings = 0x504C5354; // 'PLST'
89
94#ifdef __APPLE__
95inline constexpr std::uint16_t kOpenSettingsModifiers = kModCmd;
96#else
97inline constexpr std::uint16_t kOpenSettingsModifiers = kModCtrl;
98#endif
99
104 std::string name;
105 std::string category;
107 std::uint16_t default_modifiers = 0;
108 bool enabled = true;
109};
110
123 info.name = "Settings…";
124 info.category = "App";
125 info.default_key = kKeyComma;
126 info.default_modifiers = kOpenSettingsModifiers;
127 return info;
128}
129
138public:
139 virtual ~CommandHandler() = default;
140
146 virtual std::vector<CommandID> commands() const = 0;
147
150 virtual bool perform_command(CommandID id) = 0;
151};
152
156public:
159 void bind(KeyCode key, std::uint16_t modifiers, CommandID id);
160
162 void unbind(KeyCode key, std::uint16_t modifiers);
163
166
168 CommandID find(KeyCode key, std::uint16_t modifiers) const;
169
172 struct Chord {
174 std::uint16_t modifiers = 0;
175 };
176 std::vector<Chord> chords_for(CommandID id) const;
177
179 std::size_t size() const { return bindings_.size(); }
180
182 void clear() { bindings_.clear(); }
183
190 std::string_view key_prefix = "shortcuts") const;
192 std::string_view key_prefix = "shortcuts");
193
194private:
195 // Composite key: key code in low 32 bits, modifiers in high 16 bits.
196 using ChordKey = std::uint64_t;
197 static ChordKey make_key(KeyCode k, std::uint16_t m) {
198 return (static_cast<ChordKey>(m) << 32) |
199 static_cast<ChordKey>(static_cast<std::uint32_t>(k));
200 }
201 std::unordered_map<ChordKey, CommandID> bindings_;
202};
203
208public:
209 CommandRegistry() = default;
210 ~CommandRegistry() = default;
211
212 // Non-copyable, non-movable — handlers register by raw pointer.
215
216 // ── Command metadata ────────────────────────────────────────────
217
224
226 std::optional<CommandInfo> command_info(CommandID id) const;
227
229 const std::vector<CommandInfo>& commands() const { return commands_; }
230
233 void set_enabled(CommandID id, bool enabled);
234
235 // ── Handler chain ───────────────────────────────────────────────
236
241
245
247 std::size_t handler_count() const { return handlers_.size(); }
248
249 // ── Dispatch ────────────────────────────────────────────────────
250
255
259 bool dispatch_key_event(const KeyEvent& event);
260
261 // ── ShortcutMap access ──────────────────────────────────────────
262
263 ShortcutMap& shortcuts() { return shortcuts_; }
264 const ShortcutMap& shortcuts() const { return shortcuts_; }
265
266private:
267 std::vector<CommandInfo> commands_;
268 std::unordered_map<CommandID, std::size_t> command_index_;
269 std::vector<CommandHandler*> handlers_;
270 ShortcutMap shortcuts_;
271};
272
283
284} // namespace pulp::view
JSON-backed persistent settings file.
Definition properties_file.hpp:16
Interface implemented by any object that can perform commands.
Definition command_registry.hpp:137
virtual ~CommandHandler()=default
virtual bool perform_command(CommandID id)=0
Perform a command.
virtual std::vector< CommandID > commands() const =0
Report which commands this handler can perform.
Central command dispatcher.
Definition command_registry.hpp:207
void set_enabled(CommandID id, bool enabled)
Mutate a command's enabled flag (e.g.
CommandRegistry(const CommandRegistry &)=delete
void remove_handler(CommandHandler *handler)
Remove a handler from the chain.
void register_command(const CommandInfo &info)
Register a command.
bool dispatch(CommandID id)
Dispatch a command id through the handler chain.
bool dispatch_key_event(const KeyEvent &event)
Dispatch a key event: look up the chord in the ShortcutMap, then dispatch the resulting command (if a...
std::size_t handler_count() const
Number of handlers currently in the chain.
Definition command_registry.hpp:247
CommandRegistry & operator=(const CommandRegistry &)=delete
void add_handler(CommandHandler *handler)
Add a handler to the front of the dispatch chain (highest priority).
ShortcutMap & shortcuts()
Definition command_registry.hpp:263
const std::vector< CommandInfo > & commands() const
All registered commands, in registration order.
Definition command_registry.hpp:229
const ShortcutMap & shortcuts() const
Definition command_registry.hpp:264
std::optional< CommandInfo > command_info(CommandID id) const
Look up a command by id. Returns nullopt if unknown.
(KeyCode, modifiers) → CommandID lookup table.
Definition command_registry.hpp:155
void unbind_command(CommandID id)
Remove every chord that points to the given command.
bool load_from(const pulp::state::PropertiesFile &props, std::string_view key_prefix="shortcuts")
void save_to(pulp::state::PropertiesFile &props, std::string_view key_prefix="shortcuts") const
Serialize / restore through a pulp::state::PropertiesFile.
void unbind(KeyCode key, std::uint16_t modifiers)
Remove any binding for the given chord. No-op if unbound.
void clear()
Clear all bindings.
Definition command_registry.hpp:182
std::size_t size() const
Number of bound chords.
Definition command_registry.hpp:179
CommandID find(KeyCode key, std::uint16_t modifiers) const
Look up a chord. Returns kInvalidCommandID if unbound.
std::vector< Chord > chords_for(CommandID id) const
void bind(KeyCode key, std::uint16_t modifiers, CommandID id)
Associate a (key, modifier) chord with a command.
Definition view.hpp:73
Input event types with modifier keys and pointer IDs.
SequencerStateChannel is the audio-safe transport for a plugin's structured NON-parameter state: the ...
Definition audio_device_manager.hpp:48
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ kModCmd
Command (macOS) — alias for platform primary modifier.
Definition input_events.hpp:21
@ kModCtrl
Control key (Ctrl on Windows/Linux).
Definition input_events.hpp:18
constexpr std::uint16_t kOpenSettingsModifiers
The platform-standard open-settings chord: Cmd+, on macOS, Ctrl+, elsewhere.
Definition command_registry.hpp:95
uint32_t CommandID
A unique command identifier.
Definition app_framework.hpp:20
CommandInfo make_open_settings_command_info()
CommandInfo for kCommandOpenSettings with the platform-standard chord as its default binding.
Definition command_registry.hpp:120
void route_global_keys(View &root, CommandRegistry &registry)
Install registry as the root view's single global-key dispatcher: assigns root.on_global_key to forwa...
constexpr CommandID kCommandOpenSettings
Framework-reserved CommandID for "open the app's settings / preferences" (ASCII 'PLST').
Definition command_registry.hpp:88
KeyCode
Definition input_events.hpp:35
@ m
Definition input_events.hpp:40
@ k
Definition input_events.hpp:40
@ unknown
Definition input_events.hpp:36
constexpr CommandID kInvalidCommandID
Definition command_registry.hpp:76
@ info
Definition design_ir.hpp:695
constexpr KeyCode kKeyComma
ASCII comma as a KeyCode.
Definition command_registry.hpp:81
Metadata for one command.
Definition command_registry.hpp:102
std::uint16_t default_modifiers
Definition command_registry.hpp:107
KeyCode default_key
Definition command_registry.hpp:106
bool enabled
False = greyed out / not dispatched.
Definition command_registry.hpp:108
std::string name
User-facing display name ("Open File…").
Definition command_registry.hpp:104
std::string category
Group ("File", "Edit", "View") for the editor.
Definition command_registry.hpp:105
CommandID id
Definition command_registry.hpp:103
Rich key event with key code, modifiers, and repeat state.
Definition input_events.hpp:193
All chords currently bound to the given command.
Definition command_registry.hpp:172
KeyCode key
Definition command_registry.hpp:173
std::uint16_t modifiers
Definition command_registry.hpp:174