game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
running_game_status_widget.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
3#include "icon_font.h"
4#include "theme_manager.h"
5
6#include <QMouseEvent>
7#include <QPainter>
8#include <QPainterPath>
9#include <QToolTip>
10#include <algorithm>
11#include <cmath>
12
14 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
15{
16 setMouseTracking(true);
17 setCursor(Qt::PointingHandCursor);
18
19 // Create layout
20 layout_ = new QHBoxLayout(this);
21 layout_->setContentsMargins(12, 6, 12, 6);
22 layout_->setSpacing(0);
23
24 // Running indicator icon (green play symbol)
25 iconLabel_ = new QLabel(this);
26 iconLabel_->setText(IconFont::text(FA::Play));
27 iconLabel_->setStyleSheet(
28 QString("color: %1; font-family: '%2'; font-size: 12px;")
29 .arg(themeManager_->colors().success.name(), IconFont::solidFamily()));
30 layout_->addWidget(iconLabel_);
31
32 layout_->addSpacing(8);
33
34 // Game name (truncated)
35 nameLabel_ = new QLabel(this);
36 nameLabel_->setMaximumWidth(150);
37 layout_->addWidget(nameLabel_);
38
39 layout_->addSpacing(12);
40
41 // Time section with icon
42 QLabel* clockIcon = new QLabel(IconFont::text(FA::ClockRotateLeft), this);
43 clockIcon->setStyleSheet(
44 QString("font-family: '%1'; font-size: 10px;").arg(IconFont::solidFamily()));
45 layout_->addWidget(clockIcon);
46 layout_->addSpacing(4);
47
48 timeLabel_ = new QLabel(this);
49 timeLabel_->setMinimumWidth(50);
50 layout_->addWidget(timeLabel_);
51
52 layout_->addSpacing(12);
53
54 // FPS section - just the colored number, no label needed
55 fpsLabel_ = new QLabel(this);
56 fpsLabel_->setMinimumWidth(70);
57 layout_->addWidget(fpsLabel_);
58
59 layout_->addSpacing(8);
60
61 // Sparkline widget (custom painted area)
62 sparklineWidget_ = new QWidget(this);
63 sparklineWidget_->setFixedSize(SPARKLINE_WIDTH, SPARKLINE_HEIGHT);
64 layout_->addWidget(sparklineWidget_);
65
66 // Apply initial styling
67 updateStyleSheet();
68
70
71 // Initially hidden
72 hide();
73}
74
75void RunningGameStatusWidget::updateStyleSheet()
76{
77 const ThemeColors& c = themeManager_->colors();
78
79 // Subtle pill-shaped background using theme colors
80 QColor restBg = c.panelBackground.darker(110);
81 QString bgColor = hovered_ ? c.hoverBackground.name() : restBg.name();
82
83 QString baseStyle =
84 QString("RunningGameStatusWidget { background-color: %1; border-radius: 6px; border: 1px "
85 "solid %3; }"
86 "QLabel { color: %2; font-size: 11px; background: transparent; border: none; }")
87 .arg(bgColor)
88 .arg(c.textPrimary.name())
89 .arg(c.borderLight.name());
90
91 setStyleSheet(baseStyle);
92
93 // Game name - white, bold
94 if (nameLabel_)
95 {
96 nameLabel_->setStyleSheet(QString("color: %1; font-weight: 600; font-size: 11px; "
97 "background: transparent; border: none;")
98 .arg(c.textPrimary.name()));
99 }
100
101 // Time - secondary color
102 if (timeLabel_)
103 {
104 timeLabel_->setStyleSheet(
105 QString("color: %1; font-size: 11px; background: transparent; border: none;")
106 .arg(c.textSecondary.name()));
107 }
108}
109
110void RunningGameStatusWidget::updateStatus(const QString& gameId, const QString& gameName,
111 int runningSeconds, double fps, double frametime,
112 const std::vector<double>& frametimeHistory,
113 const QString& trackedProcessName)
114{
115 // Skip expensive updates if nothing meaningful changed
116 // FPS/frametime need some tolerance to avoid constant updates
117 bool needsUpdate = currentGameId_ != gameId || gameName_ != gameName ||
118 runningSeconds_ != runningSeconds ||
119 trackedProcessName_ != trackedProcessName || std::abs(fps_ - fps) > 0.5 ||
120 std::abs(frametime_ - frametime) > 0.1;
121
122 if (!needsUpdate)
123 {
124 return; // Skip update, values haven't changed meaningfully
125 }
126
127 currentGameId_ = gameId;
128 gameName_ = gameName;
129 trackedProcessName_ = trackedProcessName;
130 runningSeconds_ = runningSeconds;
131 fps_ = fps;
132 frametime_ = frametime;
133 frametimeHistory_ = frametimeHistory;
134
135 // Update game name with smart truncation
136 // For launcher games, show the tracked process name (e.g., "WoWClassic.exe" instead of
137 // "Battle.net")
138 QString displayName = trackedProcessName.isEmpty() ? gameName : trackedProcessName;
139
140 // Remove .exe extension for cleaner display
141 if (displayName.endsWith(".exe", Qt::CaseInsensitive))
142 {
143 displayName = displayName.left(displayName.length() - 4);
144 }
145
146 if (displayName.length() > 20)
147 {
148 // Try to truncate at a word boundary
149 qsizetype cutoff = 18;
150 qsizetype lastSpace = displayName.lastIndexOf(' ', cutoff);
151 if (lastSpace > 10)
152 cutoff = lastSpace;
153 displayName = displayName.left(cutoff).trimmed() + "…";
154 }
155 nameLabel_->setText(displayName);
156
157 // Tooltip shows full info including launcher if applicable
158 if (!trackedProcessName.isEmpty())
159 {
160 nameLabel_->setToolTip(QString("%1 (via %2)").arg(trackedProcessName, gameName));
161 }
162 else
163 {
164 nameLabel_->setToolTip(gameName);
165 }
166
167 // Time display
168 timeLabel_->setText(formatDuration(runningSeconds));
169
170 // FPS color coding using theme semantic colors
171 const ThemeColors& tc = themeManager_->colors();
172 QString fpsColor;
173 if (fps >= 144 * 0.95) // Green: >= 95% of 144 target
174 fpsColor = tc.success.name();
175 else if (fps >= 60) // Yellow: 60-136
176 fpsColor = tc.warning.name();
177 else if (fps > 0) // Red: < 60
178 fpsColor = tc.error.name();
179 else
180 fpsColor = tc.textMuted.name();
181
182 if (fps > 0)
183 {
184 // Show FPS prominently with frametime in smaller text
185 QString ftStr = frametime > 0
186 ? QString(" <span style='color: %1; font-size: 9px;'>%2ms</span>")
187 .arg(themeManager_->colors().textMuted.name())
188 .arg(frametime, 0, 'f', 1)
189 : "";
190 fpsLabel_->setText(
191 QString("<span style='color: %1; font-weight: bold; font-size: 12px;'>%2</span>"
192 "<span style='color: %3; font-size: 10px;'> FPS</span>%4")
193 .arg(fpsColor)
194 .arg(static_cast<int>(fps))
195 .arg(themeManager_->colors().textMuted.name())
196 .arg(ftStr));
197 }
198 else
199 {
200 fpsLabel_->setText(QString("<span style='color: %1; font-size: 10px;'>-- FPS</span>")
201 .arg(themeManager_->colors().textMuted.name()));
202 }
203
204 updateTooltip();
205 update(); // Trigger repaint for sparkline
206}
207
208void RunningGameStatusWidget::setLaunchInfo(const QStringList& optimizations, const QString& runner,
209 const QString& protonVersion)
210{
211 optimizations_ = optimizations;
212 runner_ = runner;
213 protonVersion_ = protonVersion;
214 updateTooltip();
215}
216
218{
219 currentGameId_.clear();
220 gameName_.clear();
221 runningSeconds_ = 0;
222 fps_ = 0.0;
223 frametime_ = 0.0;
224 frametimeHistory_.clear();
225 optimizations_.clear();
226 runner_.clear();
227 protonVersion_.clear();
228
229 hide();
230}
231
232void RunningGameStatusWidget::updateTooltip()
233{
234 if (currentGameId_.isEmpty())
235 {
236 setToolTip("");
237 return;
238 }
239
240 const ThemeColors& c = themeManager_->colors();
241
242 QStringList lines;
243 lines << QString("<b style='font-size: 12px;'>%1</b>").arg(gameName_.toHtmlEscaped());
244 lines << QString("<span style='color: %1;'>Running for %2</span>")
245 .arg(c.success.name())
246 .arg(formatDuration(runningSeconds_));
247 lines << "";
248
249 if (fps_ > 0)
250 {
251 lines << QString("<b>Frame Metrics</b>");
252 lines << QString(" FPS: <b>%1</b>").arg(static_cast<int>(fps_));
253 if (frametime_ > 0)
254 lines << QString(" Frametime: %1 ms").arg(frametime_, 0, 'f', 2);
255 lines << "";
256 }
257
258 if (!runner_.isEmpty())
259 {
260 lines << QString("<b>Launch Config</b>");
261 // If we have a specific Proton version, show it as the runner
262 // Otherwise just show the runner type
263 if (!protonVersion_.isEmpty() && protonVersion_ != "Proton (Steam-managed)")
264 {
265 lines << QString(" Runner: %1").arg(protonVersion_.toHtmlEscaped());
266 }
267 else
268 {
269 lines << QString(" Runner: %1").arg(runner_.toHtmlEscaped());
270 }
271 }
272
273 if (!optimizations_.isEmpty())
274 {
275 lines << "";
276 lines << QString("<b>Optimizations</b>");
277 for (const QString& opt : optimizations_)
278 {
279 lines << QString(" • %1").arg(opt.toHtmlEscaped());
280 }
281 }
282
283 lines << "";
284 lines << QString("<i style='color: %1;'>Click to show details panel</i>")
285 .arg(themeManager_->colors().textMuted.name());
286
287 setToolTip(lines.join("<br>"));
288}
289
290QString RunningGameStatusWidget::formatDuration(int seconds) const
291{
292 int hours = seconds / 3600;
293 int mins = (seconds % 3600) / 60;
294 int secs = seconds % 60;
295
296 if (hours > 0)
297 return QString("%1h %2m").arg(hours).arg(mins, 2, 10, QChar('0'));
298 else if (mins > 0)
299 return QString("%1:%2").arg(mins).arg(secs, 2, 10, QChar('0'));
300 else
301 return QString("0:%1").arg(secs, 2, 10, QChar('0'));
302}
303
305{
306 QWidget::paintEvent(event);
307
308 // Draw sparkline in the sparkline widget area
309 if (!frametimeHistory_.empty() && sparklineWidget_ && sparklineWidget_->isVisible())
310 {
311 QPainter painter(this);
312 painter.setRenderHint(QPainter::Antialiasing);
313
314 QRect sparklineRect = sparklineWidget_->geometry();
315 drawSparkline(painter, sparklineRect);
316 }
317}
318
319void RunningGameStatusWidget::drawSparkline(QPainter& painter, const QRect& rect)
320{
321 if (frametimeHistory_.empty() || frametimeHistory_.size() < 2)
322 return;
323
324 // Find min/max for scaling
325 double minFt = *std::min_element(frametimeHistory_.begin(), frametimeHistory_.end());
326 double maxFt = *std::max_element(frametimeHistory_.begin(), frametimeHistory_.end());
327
328 // Ensure some range
329 if (maxFt - minFt < 1.0)
330 {
331 minFt = std::max(0.0, minFt - 2.0);
332 maxFt = minFt + 4.0;
333 }
334
335 // Add padding
336 double range = maxFt - minFt;
337 minFt -= range * 0.1;
338 maxFt += range * 0.1;
339 range = maxFt - minFt;
340
341 // Draw subtle background
342 QColor sparkBg = themeManager_->colors().panelBackground;
343 sparkBg.setAlpha(80);
344 painter.fillRect(rect, sparkBg);
345
346 // Draw sparkline path
347 QPainterPath path;
348 size_t numPoints = frametimeHistory_.size();
349 double xStep =
350 static_cast<double>(rect.width()) / static_cast<double>(std::max(size_t(1), numPoints - 1));
351
352 for (size_t i = 0; i < numPoints; ++i)
353 {
354 double value = frametimeHistory_[i];
355 double normalized = (value - minFt) / range;
356 double x = rect.left() + static_cast<double>(i) * xStep;
357 double y = rect.bottom() - normalized * rect.height();
358
359 if (i == 0)
360 path.moveTo(x, y);
361 else
362 path.lineTo(x, y);
363 }
364
365 // Determine color based on average frametime
366 double avgFt = 0;
367 for (double ft : frametimeHistory_)
368 avgFt += ft;
369 avgFt /= static_cast<double>(frametimeHistory_.size());
370
371 const ThemeColors& sc = themeManager_->colors();
372 QColor lineColor;
373 if (avgFt <= 7.0) // ~144+ FPS
374 lineColor = sc.success;
375 else if (avgFt <= 16.7) // ~60+ FPS
376 lineColor = sc.warning;
377 else
378 lineColor = sc.error;
379
380 // Draw with slight glow effect
381 QPen glowPen(lineColor.darker(150), 3.0);
382 glowPen.setCapStyle(Qt::RoundCap);
383 glowPen.setJoinStyle(Qt::RoundJoin);
384 painter.setPen(glowPen);
385 painter.setOpacity(0.3);
386 painter.drawPath(path);
387
388 // Main line
389 painter.setOpacity(1.0);
390 QPen pen(lineColor, 1.5);
391 pen.setCapStyle(Qt::RoundCap);
392 pen.setJoinStyle(Qt::RoundJoin);
393 painter.setPen(pen);
394 painter.drawPath(path);
395
396 // Draw current value dot with glow
397 if (!frametimeHistory_.empty())
398 {
399 double lastValue = frametimeHistory_.back();
400 double normalized = (lastValue - minFt) / range;
401 double x = rect.right() - 1;
402 double y = rect.bottom() - normalized * rect.height();
403
404 // Glow
405 painter.setBrush(lineColor);
406 painter.setPen(Qt::NoPen);
407 painter.setOpacity(0.4);
408 painter.drawEllipse(QPointF(x, y), 4, 4);
409
410 // Dot
411 painter.setOpacity(1.0);
412 painter.drawEllipse(QPointF(x, y), 2.5, 2.5);
413 }
414}
415
417{
418 if (event->button() == Qt::LeftButton)
419 {
420 emit clicked();
421 if (!currentGameId_.isEmpty())
422 {
423 emit showDetailsRequested(currentGameId_);
424 }
425 }
426 QWidget::mousePressEvent(event);
427}
428
430{
431 hovered_ = true;
432 updateStyleSheet();
433 QWidget::enterEvent(event);
434}
435
437{
438 hovered_ = false;
439 updateStyleSheet();
440 QWidget::leaveEvent(event);
441}
442
444{
445 const ThemeColors& c = themeManager_->colors();
446
447 // Update icon color
448 if (iconLabel_)
449 {
450 iconLabel_->setStyleSheet(QString("color: %1; font-size: 12px;").arg(c.success.name()));
451 }
452
453 // Re-apply stylesheet with current theme
454 updateStyleSheet();
455
456 // Trigger repaint for sparkline
457 update();
458}
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
void updateStatus(const QString &gameId, const QString &gameName, int runningSeconds, double fps, double frametime, const std::vector< double > &frametimeHistory, const QString &trackedProcessName=QString())
Update the widget with current game info.
void clear()
Clear the widget (no game running).
void mousePressEvent(QMouseEvent *event) override
void enterEvent(QEnterEvent *event) override
void applyTheme()
Apply current theme colors.
RunningGameStatusWidget(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
void leaveEvent(QEvent *event) override
void paintEvent(QPaintEvent *event) override
void clicked()
Emitted when widget is clicked.
void setLaunchInfo(const QStringList &optimizations, const QString &runner, const QString &protonVersion)
Set detailed tooltip info about launch configuration.
void showDetailsRequested(const QString &gameId)
Emitted when user wants to show details for this game.
void themeChanged()
Emitted when theme or accent color changes.
const ThemeColors & colors() const
Get effective colors (with accent customization applied).
constexpr char32_t ClockRotateLeft
Definition icon_font.h:30
constexpr char32_t Play
Definition icon_font.h:67
QColor panelBackground
QColor borderLight
QColor textSecondary
QColor textPrimary
QColor textMuted
QColor hoverBackground