game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
command_palette.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// command_palette.h
4// ==============================================================================
5// VS Code-style command palette for quick action access
6// ==============================================================================
7// Provides fuzzy search over all available commands with keyboard navigation.
8// Commands are dynamically generated based on current application state.
9// ==============================================================================
10
11#pragma once
12
13#include <QDialog>
14#include <QKeyEvent>
15#include <QLabel>
16#include <QLineEdit>
17#include <QListWidget>
18#include <QTimer>
19#include <QVBoxLayout>
20#include <functional>
21#include <memory>
22#include <vector>
23
24// Forward declarations
25class MainWindow;
26class ThemeManager;
27
28// ==============================================================================
29// Command Structure
30// ==============================================================================
31
32/**
33 * @brief Represents a single command in the palette
34 *
35 * Commands can be static (always available) or dynamic (context-dependent).
36 * Each command has an action callback that executes when selected.
37 */
39{
40 QString id; // Unique identifier (e.g., "game.launch")
41 QString label; // Display text (e.g., "Launch Game")
42 QString category; // Category for grouping (e.g., "Games", "View")
43 QString shortcut; // Keyboard shortcut hint (e.g., "Ctrl+L")
44 QString icon; // Icon/emoji for visual identification
45 QString description; // Optional description for tooltips
46 int priority = 0; // Higher priority = shown first in fuzzy results
47
48 std::function<void()> action; // Action to execute
49 std::function<bool()> isEnabled; // Optional: returns false to disable
50 std::function<bool()> isVisible; // Optional: returns false to hide
51
52 // Helper to check if command matches search query (fuzzy match)
53 int matchScore(const QString& query) const;
54
55 // Check if command should be shown
56 bool shouldShow() const
57 {
58 return !isVisible || isVisible();
59 }
60
61 // Check if command is enabled
62 bool enabled() const
63 {
64 return !isEnabled || isEnabled();
65 }
66};
67
68// ==============================================================================
69// Command Palette Widget
70// ==============================================================================
71
72/**
73 * @class CommandPalette
74 * @brief Modal dialog providing fuzzy-searchable command list
75 *
76 * Features:
77 * - Fuzzy search with scoring (prefix matches score higher)
78 * - Keyboard navigation (Up/Down/Enter/Escape)
79 * - Recently used commands shown first
80 * - Category grouping in results
81 * - Dynamic command state (enabled/disabled based on context)
82 *
83 * Usage:
84 * auto palette = new CommandPalette(mainWindow);
85 * palette->registerCommands(commands);
86 * palette->show(); // Or connect to Ctrl+Shift+P shortcut
87 */
88class CommandPalette : public QDialog
89{
90 Q_OBJECT
91
92 public:
93 explicit CommandPalette(MainWindow* parent, ThemeManager* themeManager = nullptr);
94 ~CommandPalette() override;
95
96 // =========================================================================
97 // Command Registration
98 // =========================================================================
99
100 /**
101 * @brief Register a single command
102 * @param cmd Command to register
103 */
104 void registerCommand(const PaletteCommand& cmd);
105
106 /**
107 * @brief Register multiple commands at once
108 * @param commands Vector of commands to register
109 */
110 void registerCommands(const std::vector<PaletteCommand>& commands);
111
112 /**
113 * @brief Remove a command by ID
114 * @param id Command ID to remove
115 */
116 void unregisterCommand(const QString& id);
117
118 /**
119 * @brief Remove all commands in a category
120 * @param category Category name
121 */
122 void unregisterCategory(const QString& category);
123
124 /**
125 * @brief Clear all registered commands
126 */
127 void clearCommands();
128
129 // =========================================================================
130 // Dynamic Commands
131 // =========================================================================
132
133 /**
134 * @brief Register a provider for dynamic commands
135 * @param provider Function that returns commands based on current state
136 *
137 * Dynamic providers are called each time the palette opens to add
138 * context-sensitive commands (e.g., "Stop [Game Name]" when a game is running).
139 */
140 void registerDynamicProvider(std::function<std::vector<PaletteCommand>()> provider);
141
142 // =========================================================================
143 // Display
144 // =========================================================================
145
146 /**
147 * @brief Show the palette and focus the search field
148 */
149 void showPalette();
150
151 /**
152 * @brief Get the list of recently used command IDs
153 * @param limit Maximum number to return
154 * @return List of command IDs, most recent first
155 */
156 QStringList getRecentCommands(int limit = 5) const;
157
158 protected:
159 void keyPressEvent(QKeyEvent* event) override;
160 void showEvent(QShowEvent* event) override;
161 bool eventFilter(QObject* obj, QEvent* event) override;
162
163 private slots:
164 void onSearchTextChanged(const QString& text);
165 void onItemActivated(QListWidgetItem* item);
166 void onItemDoubleClicked(QListWidgetItem* item);
167
168 private:
169 void setupUI();
170 void populateResults(const QString& query);
171 void executeCommand(const QString& commandId);
172 void addToRecentCommands(const QString& commandId);
173 void selectNextItem();
174 void selectPreviousItem();
175 QListWidgetItem* createCommandItem(const PaletteCommand& cmd);
176 void applyTheme();
177
178 // Command storage
179 std::vector<PaletteCommand> staticCommands_;
180 std::vector<std::function<std::vector<PaletteCommand>()>> dynamicProviders_;
181
182 // Recently used commands (persisted via QSettings)
183 QStringList recentCommands_;
184 static constexpr int MAX_RECENT_COMMANDS = 10;
185
186 // UI Components
187 QLineEdit* searchEdit_ = nullptr;
188 QListWidget* resultsList_ = nullptr;
189 QLabel* hintLabel_ = nullptr;
190
191 // Search debounce
192 QTimer* searchDebounceTimer_ = nullptr;
193 static constexpr int SEARCH_DEBOUNCE_MS = 50;
194
195 // Parent window for callbacks
196 MainWindow* mainWindow_ = nullptr;
197
198 ThemeManager* themeManager_;
199};
200
201// ==============================================================================
202// Command Builder Helper
203// ==============================================================================
204
205/**
206 * @class CommandBuilder
207 * @brief Fluent builder for creating PaletteCommand objects
208 *
209 * Usage:
210 * auto cmd = CommandBuilder("game.launch")
211 * .label("Launch Game")
212 * .category("Games")
213 * .shortcut("Enter")
214 * .icon("...")
215 * .action([this]() { launchSelectedGame(); })
216 * .enabledWhen([this]() { return hasSelectedGame(); })
217 * .build();
218 */
220{
221 public:
222 explicit CommandBuilder(const QString& id)
223 {
224 cmd_.id = id;
225 }
226
227 CommandBuilder& label(const QString& label)
228 {
229 cmd_.label = label;
230 return *this;
231 }
232 CommandBuilder& category(const QString& cat)
233 {
234 cmd_.category = cat;
235 return *this;
236 }
237 CommandBuilder& shortcut(const QString& sc)
238 {
239 cmd_.shortcut = sc;
240 return *this;
241 }
242 CommandBuilder& icon(const QString& icon)
243 {
244 cmd_.icon = icon;
245 return *this;
246 }
247 CommandBuilder& description(const QString& desc)
248 {
249 cmd_.description = desc;
250 return *this;
251 }
253 {
254 cmd_.priority = p;
255 return *this;
256 }
257 CommandBuilder& action(std::function<void()> fn)
258 {
259 cmd_.action = std::move(fn);
260 return *this;
261 }
262 CommandBuilder& enabledWhen(std::function<bool()> fn)
263 {
264 cmd_.isEnabled = std::move(fn);
265 return *this;
266 }
267 CommandBuilder& visibleWhen(std::function<bool()> fn)
268 {
269 cmd_.isVisible = std::move(fn);
270 return *this;
271 }
272
274 {
275 return cmd_;
276 }
277
278 private:
279 PaletteCommand cmd_;
280};
CommandBuilder & visibleWhen(std::function< bool()> fn)
CommandBuilder & priority(int p)
CommandBuilder & icon(const QString &icon)
CommandBuilder & shortcut(const QString &sc)
CommandBuilder & category(const QString &cat)
CommandBuilder & enabledWhen(std::function< bool()> fn)
CommandBuilder & label(const QString &label)
CommandBuilder & description(const QString &desc)
PaletteCommand build() const
CommandBuilder(const QString &id)
CommandBuilder & action(std::function< void()> fn)
void clearCommands()
Clear all registered commands.
void showPalette()
Show the palette and focus the search field.
void unregisterCategory(const QString &category)
Remove all commands in a category.
void registerCommands(const std::vector< PaletteCommand > &commands)
Register multiple commands at once.
void keyPressEvent(QKeyEvent *event) override
void unregisterCommand(const QString &id)
Remove a command by ID.
bool eventFilter(QObject *obj, QEvent *event) override
void registerCommand(const PaletteCommand &cmd)
Register a single command.
void showEvent(QShowEvent *event) override
void registerDynamicProvider(std::function< std::vector< PaletteCommand >()> provider)
Register a provider for dynamic commands.
CommandPalette(MainWindow *parent, ThemeManager *themeManager=nullptr)
~CommandPalette() override
QStringList getRecentCommands(int limit=5) const
Get the list of recently used command IDs.
Represents a single command in the palette.
int matchScore(const QString &query) const
bool enabled() const
std::function< bool()> isEnabled
std::function< bool()> isVisible
std::function< void()> action
bool shouldShow() const