game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
context_menu_manager.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// ui/qt/context_menu_manager.cpp
4// ==============================================================================
5// Extracted from MainWindow's customContextMenuRequested lambda on
6// 2026-04-19. The GameActionFactory landed earlier in the same pass,
7// enabling this extraction without duplicating the shared per-game
8// actions across two managers.
9// ==============================================================================
10
12
15#include "integrations/steam_integration.h" // SteamLibraryScanner
16#include "mainwindow.h"
19#include "models/game.h"
21#include "theme_manager.h"
22#include "widgets/game_list_delegate.h" // for GameIdRole
23
24#include <QAction>
25#include <QClipboard>
26#include <QGuiApplication>
27#include <QKeySequence>
28#include <QListView>
29#include <QMainWindow>
30#include <QMenu>
31#include <QMessageBox>
32#include <QMetaObject>
33#include <QModelIndex>
34#include <QModelIndexList>
35#include <QPointer>
36#include <QString>
37#include <QStringLiteral>
38#include <QThreadPool>
39
40// ==============================================================================
41// Impl
42// ==============================================================================
43
45{
46 public:
47 Impl(QMainWindow* window, core::GameLibraryController* libCtrl, DetailsPanelManager* detailsMgr,
48 GameActionFactory* factory, std::shared_ptr<QtNotificationService> notifSvc,
49 ThemeManager* themeMgr)
50 : mainWindow(window), libraryController(libCtrl), detailsPanelManager(detailsMgr),
51 actionFactory(factory), notificationService(std::move(notifSvc)), themeManager(themeMgr)
52 {
53 }
54
55 QMainWindow* mainWindow = nullptr;
59 std::shared_ptr<QtNotificationService> notificationService;
61 QListView* gameList = nullptr;
62};
63
64// ==============================================================================
65// Ctor / dtor
66// ==============================================================================
67
69 core::GameLibraryController* libraryController,
70 DetailsPanelManager* detailsPanelManager,
71 GameActionFactory* actionFactory,
72 std::shared_ptr<QtNotificationService> notificationService,
73 ThemeManager* themeManager, QObject* qparent)
74 : QObject(qparent),
75 impl_(std::make_unique<Impl>(mainWindow, libraryController, detailsPanelManager,
76 actionFactory, std::move(notificationService), themeManager))
77{
78}
79
81
82// ==============================================================================
83// install
84// ==============================================================================
85
86void ContextMenuManager::install(QListView* gameList)
87{
88 auto& I = *impl_;
89 I.gameList = gameList;
90 if (!I.gameList)
91 return;
92
93 connect(
94 I.gameList, &QListView::customContextMenuRequested, this,
95 [this](const QPoint& pos)
96 {
97 auto& II = *impl_;
98 QModelIndexList selectedItems = II.gameList->selectionModel()->selectedIndexes();
99 if (selectedItems.isEmpty())
100 return;
101
102 QMenu contextMenu(II.mainWindow);
103 impl_->themeManager->styleMenu(&contextMenu);
104
105 // ----------------------------------------------------------------
106 // Multi-selection path.
107 // ----------------------------------------------------------------
108 if (selectedItems.size() > 1)
109 {
110 QAction* selectAllAction = contextMenu.addAction(tr("Select All"));
111 connect(selectAllAction, &QAction::triggered, this,
112 [this]() { impl_->gameList->selectAll(); });
113
114 contextMenu.addSeparator();
115
116 QAction* deleteAction = contextMenu.addAction(
117 tr("Remove %1 Games from Library").arg(selectedItems.size()));
118 connect(deleteAction, &QAction::triggered, this,
119 [this, selectedItems]()
120 {
121 auto reply =
122 QMessageBox::question(impl_->mainWindow, tr("Confirm Removal"),
123 tr("Are you sure you want to remove %1 "
124 "games from the library?")
125 .arg(selectedItems.size()),
126 QMessageBox::Yes | QMessageBox::No);
127 if (reply != QMessageBox::Yes)
128 return;
129
130 std::vector<std::string> ids;
131 ids.reserve(static_cast<size_t>(selectedItems.size()));
132 for (const QModelIndex& idx : selectedItems)
133 {
134 ids.push_back(idx.data(GameIdRole).toString().toStdString());
135 }
136 emit gamesBatchRemoveRequested(ids);
137 });
138
139 contextMenu.exec(II.gameList->mapToGlobal(pos));
140 return;
141 }
142
143 // ----------------------------------------------------------------
144 // Single-selection path — shared actions via GameActionFactory,
145 // context-menu-unique actions inline.
146 // ----------------------------------------------------------------
147 const QModelIndex item = selectedItems.first();
148 const std::string gameId = item.data(GameIdRole).toString().toStdString();
149 const Game* game = II.libraryController->getGame(gameId);
150 if (!game)
151 return;
152
153 const bool isSteamGame =
154 game->source == GameSource::Steam && game->metadata.steamAppId > 0;
155 const bool isInstalled = game->isInstalled();
156
157 GameActionFactory* factory = II.actionFactory;
158 if (!factory)
159 return;
160
161 factory->addLaunchActions(&contextMenu, gameId, game);
162 contextMenu.addSeparator();
163 factory->addConfigureAction(&contextMenu, gameId);
164 factory->addQuickSettingsSubmenu(&contextMenu, gameId, game,
165 /*includeMoreSettings=*/true);
166 contextMenu.addSeparator();
167
168 // Uninstall (Steam + installed) — context-menu-specific
169 // because it spawns a QThreadPool worker + drives a
170 // post-uninstall Steam URL open on completion.
171 if (isSteamGame && isInstalled)
172 {
173 const int steamAppId = game->metadata.steamAppId;
174 const std::string gameName = game->name;
175 QAction* uninstallAction =
176 contextMenu.addAction(QStringLiteral("\u2715 Uninstall"));
177 connect(uninstallAction, &QAction::triggered, this,
178 [this, gameId, gameName, steamAppId]()
179 {
180 auto& III = *impl_;
181 auto reply =
182 QMessageBox::question(III.mainWindow, tr("Confirm Uninstall"),
183 tr("Are you sure you want to uninstall %1?"
184 "\n\nThis will delete the game files "
185 "and free up disk space.\nYou can "
186 "reinstall the game from Steam at any "
187 "time.")
188 .arg(QString::fromStdString(gameName)),
189 QMessageBox::Yes | QMessageBox::No);
190 if (reply != QMessageBox::Yes)
191 return;
192
193 const std::uint32_t appId = static_cast<std::uint32_t>(steamAppId);
194 if (III.notificationService)
195 {
196 III.notificationService->info("Uninstalling " + gameName + "...");
197 }
198
199 // Use QPointer so if the manager is
200 // destroyed while the worker is still
201 // running, the UI-thread callback safely
202 // no-ops. MainWindow drives the post-
203 // success refresh through
204 // steamUninstallCompleted.
205 QPointer<ContextMenuManager> guard(this);
206 QThreadPool::globalInstance()->start(
207 [guard, appId, gameId, gameName]()
208 {
209 bool success = SteamLibraryScanner::forceUninstallGame(appId);
210 QMetaObject::invokeMethod(
211 guard,
212 [guard, success, gameName, gameId, appId]()
213 {
214 if (!guard)
215 return;
216 emit guard->steamUninstallCompleted(gameId, success,
217 gameName, appId);
218 },
219 Qt::QueuedConnection);
220 });
221 });
222 }
223
224 // Steam Manage / Store (shared).
225 factory->addSteamActions(&contextMenu, game);
226 if (isSteamGame)
227 contextMenu.addSeparator();
228
229 QAction* selectAllAction2 = contextMenu.addAction(tr("Select All"));
230 connect(selectAllAction2, &QAction::triggered, this,
231 [this]() { impl_->gameList->selectAll(); });
232
233 contextMenu.addSeparator();
234
235 factory->addFavoriteAction(&contextMenu, gameId, game);
236 factory->addCollectionsSubmenu(&contextMenu, gameId, game);
237
238 contextMenu.addSeparator();
239
240 factory->addOpenFolderAction(&contextMenu, game);
241 factory->addProtonDBAction(&contextMenu, game);
242 factory->addChangeArtworkAction(&contextMenu, gameId, game);
243
244 // Verify Game Files — context-menu-only.
245 if (isInstalled)
246 {
247 const QString gameName = QString::fromStdString(game->name);
248 const QString gamePath = QString::fromStdString(game->runnerConfig.executablePath);
249 const int steamAppId = game->metadata.steamAppId;
250 QAction* verifyAction =
251 contextMenu.addAction(QStringLiteral("\u2713 Verify Game Files..."));
252 connect(verifyAction, &QAction::triggered, this,
253 [this, gameId, gameName, gamePath, steamAppId]()
254 {
255 GameVerificationDialog dialog(impl_->mainWindow);
256 dialog.setGame(QString::fromStdString(gameId), gameName, gamePath,
257 steamAppId);
258 dialog.exec();
259 });
260 }
261
262 // Copy to Clipboard submenu — context-menu-only.
263 QMenu* copyMenu = contextMenu.addMenu(tr("Copy to Clipboard"));
264 impl_->themeManager->styleMenu(copyMenu);
265 QAction* copyNameAction = copyMenu->addAction(tr("Game Name"));
266 connect(copyNameAction, &QAction::triggered, this, [game]()
267 { QGuiApplication::clipboard()->setText(QString::fromStdString(game->name)); });
268 if (!game->runnerConfig.executablePath.empty())
269 {
270 QAction* copyPathAction = copyMenu->addAction(tr("Executable Path"));
271 connect(copyPathAction, &QAction::triggered, this,
272 [game]()
273 {
274 QGuiApplication::clipboard()->setText(
275 QString::fromStdString(game->runnerConfig.executablePath));
276 });
277 }
278 if (game->metadata.steamAppId > 0)
279 {
280 QAction* copyAppIdAction = copyMenu->addAction(tr("Steam App ID"));
281 connect(copyAppIdAction, &QAction::triggered, this,
282 [game]()
283 {
284 QGuiApplication::clipboard()->setText(
285 QString::number(game->metadata.steamAppId));
286 });
287 }
288
289 contextMenu.addSeparator();
290
291 factory->addRemoveAction(&contextMenu, gameId);
292
293 contextMenu.exec(II.gameList->mapToGlobal(pos));
294 });
295}
DetailsPanelManager * detailsPanelManager
std::shared_ptr< QtNotificationService > notificationService
GameActionFactory * actionFactory
Impl(QMainWindow *window, core::GameLibraryController *libCtrl, DetailsPanelManager *detailsMgr, GameActionFactory *factory, std::shared_ptr< QtNotificationService > notifSvc, ThemeManager *themeMgr)
core::GameLibraryController * libraryController
void install(QListView *gameList)
Hook the manager's right-click handler onto gameList.
ContextMenuManager(QMainWindow *mainWindow, core::GameLibraryController *libraryController, DetailsPanelManager *detailsPanelManager, GameActionFactory *actionFactory, std::shared_ptr< QtNotificationService > notificationService, ThemeManager *themeManager, QObject *qparent=nullptr)
~ContextMenuManager() override
void addQuickSettingsSubmenu(QMenu *menu, const std::string &gameId, const Game *game, bool includeMoreSettings)
"Quick Settings" submenu with MangoHud / GameMode / Gamescope / VkBasalt toggles.
void addCollectionsSubmenu(QMenu *menu, const std::string &gameId, const Game *game)
"Collections" submenu listing every collection as a checkmarked action, plus "Create New....
void addChangeArtworkAction(QMenu *menu, const std::string &gameId, const Game *game)
"Change Artwork..." — opens SteamGridDBSelectorDialog if the game has a Steam App ID.
void addFavoriteAction(QMenu *menu, const std::string &gameId, const Game *game)
Favorite toggle (text flips between Add/Remove based on current state).
void addConfigureAction(QMenu *menu, const std::string &gameId)
Configure... — opens GameConfigDialog.
void addRemoveAction(QMenu *menu, const std::string &gameId)
"Remove from Library" with confirmation QMessageBox.
void addProtonDBAction(QMenu *menu, const Game *game)
"View on ProtonDB" — opens https://www.protondb.com/app/{appId} in the default browser.
void addOpenFolderAction(QMenu *menu, const Game *game)
"Open Game Folder" — opens the executable's parent directory in the file manager.
void addSteamActions(QMenu *menu, const Game *game)
Steam actions — "Manage in Steam..." (if installed) and "View on Steam Store" (always for Steam games...
void addLaunchActions(QMenu *menu, const std::string &gameId, const Game *game)
Launch (if installed + not running), Stop (if running), or Install via Steam (if Steam game + not ins...
void setGame(const QString &gameId, const QString &gameName, const QString &gamePath, int steamAppId=0)
Complete representation of a game in the library.
Definition game.h:767
GameSource source
Where the game was acquired from (Steam, GOG, Local, etc.).
Definition game.h:775
static bool forceUninstallGame(uint32_t appId)
Force uninstall a Steam game by directly removing files.
@ Steam
Steam store/platform.
Definition game.h:41
@ GameIdRole