game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
status_indicators.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// status_indicators.cpp
4// ==============================================================================
5// Rich status bar indicators implementation
6// ==============================================================================
7
11#include "icon_font.h"
13#include "theme_manager.h"
14
15#include <QDesktopServices>
16#include <QHBoxLayout>
17#include <QMenu>
18#include <QPainter>
19#include <QPainterPath>
20#include <QProcess>
21#include <QUrl>
22
23// ==============================================================================
24// StatusIndicator Base Implementation
25// ==============================================================================
26
27StatusIndicator::StatusIndicator(QWidget* parent, ThemeManager* themeManager)
28 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
29{
30 auto* layout = new QHBoxLayout(this);
31 layout->setContentsMargins(6, 2, 6, 2);
32 layout->setSpacing(4);
33
34 iconLabel_ = new QLabel(this);
35 iconLabel_->setAlignment(Qt::AlignCenter);
36 layout->addWidget(iconLabel_);
37
38 textLabel_ = new QLabel(this);
39 textLabel_->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
40 layout->addWidget(textLabel_);
41
42 setCursor(Qt::PointingHandCursor);
43 setMouseTracking(true);
44
45 // Set initial text colors
47}
48
49void StatusIndicator::setIcon(const QString& icon)
50{
51 iconLabel_->setText(icon);
52}
53
54void StatusIndicator::setText(const QString& text)
55{
56 textLabel_->setText(text);
57}
58
59void StatusIndicator::setTooltipText(const QString& tip)
60{
61 setToolTip(tip);
62}
63
64void StatusIndicator::paintEvent(QPaintEvent* event)
65{
66 Q_UNUSED(event)
67
68 QPainter painter(this);
69 painter.setRenderHint(QPainter::Antialiasing);
70
71 const ThemeColors& c = themeManager_->colors();
72
73 // Draw background on hover/press
74 if (pressed_)
75 {
76 QPainterPath path;
77 path.addRoundedRect(rect().adjusted(1, 1, -1, -1), 4, 4);
78 painter.fillPath(path, c.accent);
79 }
80 else if (hovered_)
81 {
82 QPainterPath path;
83 path.addRoundedRect(rect().adjusted(1, 1, -1, -1), 4, 4);
84 painter.fillPath(path, c.hoverBackground);
85 }
86
87 // NOTE: Do NOT call setStyleSheet() here - it triggers repaint and causes infinite loop!
88 // Text colors are updated in updateTextColors() called from state change handlers.
89}
90
92{
93 const ThemeColors& c = themeManager_->colors();
94 QColor textColor = pressed_ ? c.textOnAccent : c.textPrimary;
95 iconLabel_->setStyleSheet(QString("color: %1; font-family: '%2'; font-size: %3px;")
96 .arg(textColor.name(), IconFont::solidFamily())
97 .arg(themeManager_->scaledFontSize(12)));
98 textLabel_->setStyleSheet(QString("color: %1; font-size: %2px;")
99 .arg(textColor.name())
100 .arg(themeManager_->scaledFontSize(11)));
101}
102
103void StatusIndicator::enterEvent(QEnterEvent* event)
104{
105 Q_UNUSED(event)
106 hovered_ = true;
108 update();
109}
110
112{
113 Q_UNUSED(event)
114 hovered_ = false;
115 pressed_ = false;
117 update();
118}
119
120void StatusIndicator::mousePressEvent(QMouseEvent* event)
121{
122 if (event->button() == Qt::LeftButton)
123 {
124 pressed_ = true;
126 update();
127 }
128}
129
131{
132 if (event->button() == Qt::LeftButton && pressed_)
133 {
134 pressed_ = false;
136 update();
137 if (rect().contains(event->pos()))
138 {
139 emit clicked();
140 }
141 }
142}
143
144// ==============================================================================
145// RunningGamesIndicator Implementation
146// ==============================================================================
147
149 QWidget* parent, ThemeManager* themeManager)
150 : StatusIndicator(parent, themeManager), launchController_(launchController)
151{
153 setText("0 Running");
154 setTooltipText("Click to see running games");
155
156 connect(this, &StatusIndicator::clicked, this, &RunningGamesIndicator::showContextMenu);
157
158 // Refresh timer
159 refreshTimer_ = new QTimer(this);
160 refreshTimer_->setInterval(2000); // 2 second refresh
161 connect(refreshTimer_, &QTimer::timeout, this, &RunningGamesIndicator::refresh);
162 refreshTimer_->start();
163
164 refresh();
165}
166
168{
169 if (!launchController_)
170 return;
171
172 auto runningGames = launchController_->getAllRunningGames();
173 size_t count = runningGames.size();
174
175 if (count == 0)
176 {
177 setText("No games running");
178 setVisible(false); // Hide when no games running
179 }
180 else
181 {
182 setText(QString("%1 Running").arg(count));
183 setVisible(true);
184
185 // Build tooltip with game names
186 QString tooltip = "Running games:\n";
187 for (const auto& info : runningGames)
188 {
189 tooltip +=
190 QString::fromUtf8("\xE2\x80\xA2 ") + QString::fromStdString(info.gameName) + "\n";
191 }
192 setTooltipText(tooltip.trimmed());
193 }
194
195 update();
196}
197
198void RunningGamesIndicator::showContextMenu()
199{
200 if (!launchController_)
201 return;
202
203 auto runningGames = launchController_->getAllRunningGames();
204 if (runningGames.empty())
205 return;
206
207 QMenu menu(this);
208 themeManager_->styleMenu(&menu);
209
210 for (const auto& info : runningGames)
211 {
212 QString gameName = QString::fromStdString(info.gameName);
213 std::string gameId = info.gameId;
214
215 QMenu* gameMenu = menu.addMenu(QString("\u25B6 ") + gameName);
216 themeManager_->styleMenu(gameMenu);
217
218 QAction* showAction = gameMenu->addAction("Show in Library");
219 connect(showAction, &QAction::triggered, this,
220 [this, gameId]() { emit showGameRequested(gameId); });
221
222 gameMenu->addSeparator();
223
224 QAction* stopAction = gameMenu->addAction(QString::fromUtf8("\xE2\x96\xA0 Stop"));
225 connect(stopAction, &QAction::triggered, this,
226 [this, gameId]() { emit stopGameRequested(gameId); });
227 }
228
229 if (runningGames.size() > 1)
230 {
231 menu.addSeparator();
232 QAction* stopAllAction = menu.addAction(QString::fromUtf8("\xE2\x96\xA0 Stop All Games"));
233 connect(stopAllAction, &QAction::triggered, this,
234 [this, runningGames]()
235 {
236 for (const auto& info : runningGames)
237 {
238 emit stopGameRequested(info.gameId);
239 }
240 });
241 }
242
243 menu.exec(mapToGlobal(QPoint(0, -menu.sizeHint().height())));
244}
245
246// ==============================================================================
247// DownloadsIndicator Implementation
248// ==============================================================================
249
251 ThemeManager* themeManager)
252 : StatusIndicator(parent, themeManager), downloadMonitor_(downloadMonitor)
253{
255 setText("No downloads");
256 setVisible(false); // Hidden by default
257
258 connect(this, &StatusIndicator::clicked, this, &DownloadsIndicator::showContextMenu);
259
260 // Refresh timer
261 refreshTimer_ = new QTimer(this);
262 refreshTimer_->setInterval(1000); // 1 second refresh for downloads
263 connect(refreshTimer_, &QTimer::timeout, this, &DownloadsIndicator::refresh);
264 refreshTimer_->start();
265}
266
268{
269 // Remove completed downloads from active list
270 activeDownloads_.erase(std::remove_if(activeDownloads_.begin(), activeDownloads_.end(),
271 [](const DownloadInfo& d)
272 { return d.progress >= 100.0; }),
273 activeDownloads_.end());
274
275 if (activeDownloads_.empty())
276 {
277 setVisible(false);
278 }
279 else
280 {
281 setVisible(true);
282 if (activeDownloads_.size() == 1)
283 {
284 setText(QString("%1%").arg(static_cast<int>(activeDownloads_[0].progress)));
285 }
286 else
287 {
288 setText(QString("%1 downloads").arg(activeDownloads_.size()));
289 }
290
291 // Build tooltip
292 QString tooltip = "Active downloads:\n";
293 for (const auto& dl : activeDownloads_)
294 {
295 tooltip += QString("%1: %2%\n").arg(dl.gameName).arg(static_cast<int>(dl.progress));
296 }
297 setTooltipText(tooltip.trimmed());
298 }
299
300 update();
301}
302
303void DownloadsIndicator::onDownloadProgress(uint32_t appId, double progress, const QString& status)
304{
305 // Find or create download info
306 auto it = std::find_if(activeDownloads_.begin(), activeDownloads_.end(),
307 [appId](const DownloadInfo& d) { return d.appId == appId; });
308
309 if (it != activeDownloads_.end())
310 {
311 it->progress = progress;
312 it->status = status;
313 }
314 else
315 {
316 DownloadInfo info;
317 info.appId = appId;
318 info.gameName = QString("App %1").arg(appId); // Will be updated if we have the name
319 info.progress = progress;
320 info.status = status;
321 activeDownloads_.push_back(info);
322 }
323
324 refresh();
325}
326
328{
329 activeDownloads_.erase(std::remove_if(activeDownloads_.begin(), activeDownloads_.end(),
330 [appId](const DownloadInfo& d)
331 { return d.appId == appId; }),
332 activeDownloads_.end());
333
334 refresh();
335}
336
337void DownloadsIndicator::showContextMenu()
338{
339 if (activeDownloads_.empty())
340 return;
341
342 QMenu menu(this);
343 themeManager_->styleMenu(&menu);
344
345 for (const auto& dl : activeDownloads_)
346 {
347 QString text = QString("%1 - %2%").arg(dl.gameName).arg(static_cast<int>(dl.progress));
348 QAction* action = menu.addAction(QString("\u21E9 ") + text);
349 action->setEnabled(false); // Informational only
350 }
351
352 menu.exec(mapToGlobal(QPoint(0, -menu.sizeHint().height())));
353}
354
355// ==============================================================================
356// UpdatesIndicator Implementation
357// ==============================================================================
358
360 ThemeManager* themeManager)
361 : StatusIndicator(parent, themeManager), libraryController_(libraryController)
362{
364 setText("");
365 setVisible(false); // Hidden by default until updates are detected
366
367 connect(this, &StatusIndicator::clicked, this, &UpdatesIndicator::showContextMenu);
369
370 // Refresh periodically (every 60 seconds)
371 refreshTimer_ = new QTimer(this);
372 connect(refreshTimer_, &QTimer::timeout, this, &UpdatesIndicator::refresh);
373 refreshTimer_->start(60000);
374
375 refresh();
376}
377
379{
380 if (!libraryController_)
381 return;
382
383 auto allGames = libraryController_->getFilteredGames();
384 updateCount_ = 0;
385
386 for (const auto* game : allGames)
387 {
388 if (!game || game->source != GameSource::Steam || game->metadata.steamAppId <= 0)
389 continue;
390
391 auto progress =
392 SteamDownloadMonitor::readGameState(static_cast<uint32_t>(game->metadata.steamAppId));
393 if (SteamStateFlags::needsUpdate(progress.stateFlags))
394 {
395 updateCount_++;
396 }
397 }
398
399 if (updateCount_ > 0)
400 {
401 setText(QString("%1 Update%2").arg(updateCount_).arg(updateCount_ == 1 ? "" : "s"));
402 setTooltipText(QString("%1 game%2 %3 updates available\nClick to view")
403 .arg(updateCount_)
404 .arg(updateCount_ == 1 ? "" : "s")
405 .arg(updateCount_ == 1 ? "has" : "have"));
406 setVisible(true);
407 }
408 else
409 {
410 setVisible(false);
411 }
412
413 update();
414}
415
416void UpdatesIndicator::showContextMenu()
417{
418 if (!libraryController_ || updateCount_ == 0)
419 return;
420
421 QMenu menu(this);
422 themeManager_->styleMenu(&menu);
423
424 QAction* headerAction =
425 menu.addAction(QString::fromUtf8("\xE2\xAC\x86 Updates Available (%1)").arg(updateCount_));
426 headerAction->setEnabled(false);
427
428 menu.addSeparator();
429
430 // List games with updates
431 auto allGames = libraryController_->getFilteredGames();
432 int shown = 0;
433 for (const auto* game : allGames)
434 {
435 if (!game || game->source != GameSource::Steam || game->metadata.steamAppId <= 0)
436 continue;
437
438 auto progress =
439 SteamDownloadMonitor::readGameState(static_cast<uint32_t>(game->metadata.steamAppId));
440 if (SteamStateFlags::needsUpdate(progress.stateFlags))
441 {
442 uint32_t appId = static_cast<uint32_t>(game->metadata.steamAppId);
443 QString gameName = QString::fromStdString(game->name);
444
445 QAction* gameAction = menu.addAction(gameName);
446 connect(gameAction, &QAction::triggered, this,
447 [appId]()
448 {
449 // Use steam://install/ to update without auto-launching
450 QDesktopServices::openUrl(QUrl(QString("steam://install/%1").arg(appId)));
451 });
452
453 if (++shown >= 10)
454 {
455 if (updateCount_ > 10)
456 {
457 QAction* moreAction =
458 menu.addAction(QString("... and %1 more").arg(updateCount_ - 10));
459 moreAction->setEnabled(false);
460 }
461 break;
462 }
463 }
464 }
465
466 menu.addSeparator();
467
468 QAction* updateAllAction = menu.addAction("Update All via Steam");
469 connect(updateAllAction, &QAction::triggered, this,
470 []() { QDesktopServices::openUrl(QUrl("steam://open/downloads")); });
471
472 menu.exec(mapToGlobal(QPoint(0, -menu.sizeHint().height())));
473}
474
475// ==============================================================================
476// LibraryStatsIndicator Implementation
477// ==============================================================================
478
480 QWidget* parent, ThemeManager* themeManager)
481 : StatusIndicator(parent, themeManager), libraryController_(libraryController)
482{
484 setText("0 Games");
485
486 connect(this, &StatusIndicator::clicked, this, &LibraryStatsIndicator::showContextMenu);
487
488 refresh();
489}
490
492{
493 if (!libraryController_)
494 return;
495
496 size_t totalGames = libraryController_->getGameCount();
497 size_t favoriteCount = 0;
498 size_t installedCount = 0;
499
500 // Count favorites and installed
501 auto allGames = libraryController_->getFilteredGames();
502 for (const auto* game : allGames)
503 {
504 if (game->isFavorite)
505 ++favoriteCount;
506 if (game->isInstalled())
507 ++installedCount;
508 }
509
510 setText(QString("%1 Games").arg(totalGames));
511
512 QString tooltip = QString("Library Statistics:\n"
513 "Total: %1 games\n"
514 "Installed: %2\n"
515 "Favorites: %3")
516 .arg(totalGames)
517 .arg(installedCount)
518 .arg(favoriteCount);
519 setTooltipText(tooltip);
520
521 update();
522}
523
524void LibraryStatsIndicator::showContextMenu()
525{
526 if (!libraryController_)
527 return;
528
529 QMenu menu(this);
530 themeManager_->styleMenu(&menu);
531
532 size_t totalGames = libraryController_->getGameCount();
533 auto allGames = libraryController_->getFilteredGames();
534
535 size_t favoriteCount = 0;
536 size_t installedCount = 0;
537 size_t steamCount = 0;
538 size_t lutrisCount = 0;
539
540 for (const auto* game : allGames)
541 {
542 if (game->isFavorite)
543 ++favoriteCount;
544 if (game->isInstalled())
545 ++installedCount;
546 if (game->source == GameSource::Steam)
547 ++steamCount;
548 if (game->source == GameSource::Lutris)
549 ++lutrisCount;
550 }
551
552 QAction* totalAction = menu.addAction(QString("Total Games: %1").arg(totalGames));
553 totalAction->setEnabled(false);
554
555 menu.addSeparator();
556
557 QAction* installedAction =
558 menu.addAction(QString::fromUtf8("\xE2\x9C\x93 Installed: %1").arg(installedCount));
559 installedAction->setEnabled(false);
560
561 QAction* favAction = menu.addAction(QString("\u2605 Favorites: %1").arg(favoriteCount));
562 favAction->setEnabled(false);
563
564 menu.addSeparator();
565
566 if (steamCount > 0)
567 {
568 QAction* steamAction = menu.addAction(QString("\u25B6 Steam: %1").arg(steamCount));
569 steamAction->setEnabled(false);
570 }
571
572 if (lutrisCount > 0)
573 {
574 QAction* lutrisAction = menu.addAction(QString("\u2617 Lutris: %1").arg(lutrisCount));
575 lutrisAction->setEnabled(false);
576 }
577
578 menu.exec(mapToGlobal(QPoint(0, -menu.sizeHint().height())));
579}
580
581// ==============================================================================
582// StatusBarContainer Implementation
583// ==============================================================================
584
586 core::GameLibraryController* libraryController,
587 SteamDownloadMonitor* downloadMonitor, QWidget* parent,
588 ThemeManager* themeManager)
589 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
590{
591 auto* layout = new QHBoxLayout(this);
592 layout->setContentsMargins(0, 0, 0, 0);
593 layout->setSpacing(8);
594
595 // Library stats (always visible)
596 libraryStats_ = new LibraryStatsIndicator(libraryController, this, themeManager_);
597 layout->addWidget(libraryStats_);
598
599 // Separator
600 auto* sep1 = new QLabel(QString::fromUtf8("\xE2\x94\x82"), this); // Vertical bar
601 sep1->setStyleSheet(QString("color: %1;").arg(themeManager_->colors().textMuted.name()));
602 layout->addWidget(sep1);
603
604 // Running games indicator
605 runningGames_ = new RunningGamesIndicator(launchController, this, themeManager_);
606 layout->addWidget(runningGames_);
607 connect(runningGames_, &RunningGamesIndicator::stopGameRequested, this,
609 connect(runningGames_, &RunningGamesIndicator::showGameRequested, this,
611
612 // Downloads indicator
613 downloads_ = new DownloadsIndicator(downloadMonitor, this, themeManager_);
614 layout->addWidget(downloads_);
615
616 // Updates available indicator (only visible when updates exist)
617 updates_ = new UpdatesIndicator(libraryController, this, themeManager_);
618 layout->addWidget(updates_);
620
621 layout->addStretch();
622}
623
625{
626 runningGames_->refresh();
627 downloads_->refresh();
628 updates_->refresh();
629 libraryStats_->refresh();
630}
Shows active downloads with progress.
void onDownloadProgress(uint32_t appId, double progress, const QString &status)
DownloadsIndicator(SteamDownloadMonitor *downloadMonitor, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void onDownloadComplete(uint32_t appId)
static const QString & solidFamily()
Get the loaded Solid font family name.
Definition icon_font.h:136
static QString text(char32_t codepoint)
Convert a codepoint to a QString glyph.
Definition icon_font.h:113
Shows library statistics (game count, playtime).
LibraryStatsIndicator(core::GameLibraryController *libraryController, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
Shows count of running games with quick-stop menu.
void showGameRequested(const std::string &gameId)
RunningGamesIndicator(core::LaunchController *launchController, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void stopGameRequested(const std::string &gameId)
void stopGameRequested(const std::string &gameId)
StatusBarContainer(core::LaunchController *launchController, core::GameLibraryController *libraryController, SteamDownloadMonitor *downloadMonitor, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void showGameRequested(const std::string &gameId)
void leaveEvent(QEvent *event) override
void setIcon(const QString &icon)
void paintEvent(QPaintEvent *event) override
void setTooltipText(const QString &tip)
StatusIndicator(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void mouseReleaseEvent(QMouseEvent *event) override
void setText(const QString &text)
ThemeManager * themeManager_
void mousePressEvent(QMouseEvent *event) override
void enterEvent(QEnterEvent *event) override
Monitors Steam download/install progress for games.
static SteamDownloadProgress readGameState(uint32_t appId)
Read current state of a game directly from its appmanifest.
void styleMenu(QMenu *menu) const
Apply complete menu styling (stylesheet + palette) to a QMenu.
Shows count of games with updates available.
UpdatesIndicator(core::GameLibraryController *libraryController, QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
std::vector< const Game * > getFilteredGames() const
Get the currently filtered and sorted game list (returns copy for thread safety).
size_t getGameCount() const
Get total game count.
std::vector< RunningGameInfo > getAllRunningGames() const
Get all currently running games.
@ Steam
Steam store/platform.
Definition game.h:41
@ Lutris
Lutris community scripts.
Definition game.h:45
constexpr char32_t ArrowUp
Definition icon_font.h:70
constexpr char32_t CirclePlay
Definition icon_font.h:34
constexpr char32_t Gamepad
Definition icon_font.h:40
constexpr char32_t Download
Definition icon_font.h:69
bool needsUpdate(uint32_t flags)
Check if a game needs an update.
QColor textPrimary
QColor hoverBackground
QColor textOnAccent