game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
qt_notification_service.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/qt_notification_service.cpp
4// ==============================================================================
5// In-app toast notification implementation
6// ==============================================================================
7
9#include "theme_manager.h"
10
11#include <QApplication>
12#include <QEvent>
13#include <QHBoxLayout>
14#include <QMainWindow>
15
16// ==============================================================================
17// ToastWidget
18// ==============================================================================
19
20ToastWidget::ToastWidget(const QString& message, core::NotificationType type, float duration,
21 QWidget* parent, ThemeManager* themeManager)
22 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
23{
24 setFixedWidth(380);
25 setAttribute(Qt::WA_TransparentForMouseEvents, false);
26
27 auto* layout = new QHBoxLayout(this);
28 layout->setContentsMargins(12, 10, 8, 10);
29 layout->setSpacing(10);
30
31 iconLabel_ = new QLabel(this);
32 iconLabel_->setFixedWidth(18);
33 iconLabel_->setAlignment(Qt::AlignCenter);
34 layout->addWidget(iconLabel_);
35
36 messageLabel_ = new QLabel(message, this);
37 messageLabel_->setWordWrap(true);
38 messageLabel_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
39 layout->addWidget(messageLabel_, 1);
40
41 closeBtn_ = new QPushButton("\u2715", this);
42 closeBtn_->setFixedSize(22, 22);
43 closeBtn_->setCursor(Qt::PointingHandCursor);
44 closeBtn_->setFlat(true);
45 layout->addWidget(closeBtn_);
46
47 connect(closeBtn_, &QPushButton::clicked, this, &ToastWidget::dismissAnimated);
48
49 applyTypeStyle(type);
50
51 // Opacity effect for fade animation
52 opacityEffect_ = new QGraphicsOpacityEffect(this);
53 opacityEffect_->setOpacity(0.0);
54 setGraphicsEffect(opacityEffect_);
55
56 // Auto-close timer
57 if (duration > 0)
58 {
59 autoCloseTimer_ = new QTimer(this);
60 autoCloseTimer_->setSingleShot(true);
61 autoCloseTimer_->setInterval(static_cast<int>(duration * 1000));
62 connect(autoCloseTimer_, &QTimer::timeout, this, &ToastWidget::dismissAnimated);
63 }
64}
65
66void ToastWidget::applyTypeStyle(core::NotificationType type)
67{
68 const auto& c = themeManager_->colors();
69
70 QString icon;
71 QColor borderColor;
72 QColor iconColor;
73
74 switch (type)
75 {
77 icon = "\u2713";
78 borderColor = c.success;
79 iconColor = c.success;
80 break;
82 icon = "\u26A0";
83 borderColor = c.warning;
84 iconColor = c.warning;
85 break;
87 icon = "\u2715";
88 borderColor = c.error;
89 iconColor = c.error;
90 break;
91 default:
92 icon = "\u2139";
93 borderColor = c.accent;
94 iconColor = c.accent;
95 break;
96 }
97
98 iconLabel_->setText(icon);
99 iconLabel_->setStyleSheet(
100 QString("color: %1; font-size: %2px; font-weight: bold; background: transparent;")
101 .arg(iconColor.name())
102 .arg(themeManager_->scaledFontSize(14)));
103
104 messageLabel_->setStyleSheet(QString("color: %1; font-size: %2px; background: transparent;")
105 .arg(c.textPrimary.name())
106 .arg(themeManager_->scaledFontSize(12)));
107
108 closeBtn_->setStyleSheet(
109 QString("QPushButton { color: %1; background: transparent; border: none; font-size: %3px; }"
110 "QPushButton:hover { color: %2; }")
111 .arg(c.textSecondary.name(), c.textPrimary.name())
112 .arg(themeManager_->scaledFontSize(11)));
113
114 setStyleSheet(QString("ToastWidget {"
115 " background-color: %1;"
116 " border: 1px solid %2;"
117 " border-left: 3px solid %3;"
118 " border-radius: 6px;"
119 "}")
120 .arg(c.cardBackground.name(), c.border.name(), borderColor.name()));
121}
122
124{
125 show();
126 adjustSize();
127
128 auto* fadeIn = new QPropertyAnimation(opacityEffect_, "opacity", this);
129 fadeIn->setDuration(200);
130 fadeIn->setStartValue(0.0);
131 fadeIn->setEndValue(1.0);
132 fadeIn->setEasingCurve(QEasingCurve::OutCubic);
133 fadeIn->start(QAbstractAnimation::DeleteWhenStopped);
134
135 if (autoCloseTimer_)
136 autoCloseTimer_->start();
137}
138
140{
141 if (closing_)
142 return;
143 closing_ = true;
144
145 if (autoCloseTimer_)
146 autoCloseTimer_->stop();
147
148 auto* fadeOut = new QPropertyAnimation(opacityEffect_, "opacity", this);
149 fadeOut->setDuration(200);
150 fadeOut->setStartValue(opacityEffect_->opacity());
151 fadeOut->setEndValue(0.0);
152 fadeOut->setEasingCurve(QEasingCurve::InCubic);
153
154 connect(fadeOut, &QPropertyAnimation::finished, this, [this]() { emit dismissed(); });
155
156 fadeOut->start(QAbstractAnimation::DeleteWhenStopped);
157}
158
159// ==============================================================================
160// ToastContainer
161// ==============================================================================
162
163ToastContainer::ToastContainer(QWidget* parent, ThemeManager* themeManager)
164 : QWidget(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
165{
166 setAttribute(Qt::WA_TransparentForMouseEvents, true);
167 setAttribute(Qt::WA_TranslucentBackground);
168 setStyleSheet("background: transparent;");
169 setFixedSize(0, 0); // Container is just a logical manager, not a visual widget
170
171 // Track parent resize to reposition
172 if (parent)
173 parent->installEventFilter(this);
174
175 raise();
176}
177
178bool ToastContainer::eventFilter(QObject* obj, QEvent* event)
179{
180 if (obj == parent() && event->type() == QEvent::Resize)
181 {
182 repositionToasts();
183 }
184 return QWidget::eventFilter(obj, event);
185}
186
187void ToastContainer::addToast(const QString& message, core::NotificationType type, float duration)
188{
189 // Limit active toasts — dismiss the oldest one if at capacity.
190 // dismissAnimated() is async (200ms fade), so use 'if' not 'while' to
191 // avoid spinning on a no-op after closing_ is set on the first call.
192 if (activeToasts_.size() >= kMaxToasts)
193 {
194 activeToasts_.first()->dismissAnimated();
195 }
196
197 auto* toast = new ToastWidget(message, type, duration, parentWidget(), themeManager_);
198 activeToasts_.append(toast);
199
200 connect(toast, &ToastWidget::dismissed, this, [this, toast]() { removeToast(toast); });
201
202 toast->showAnimated();
203 repositionToasts();
204}
205
207{
208 for (auto* toast : activeToasts_)
209 {
210 toast->dismissAnimated();
211 }
212}
213
214void ToastContainer::removeToast(ToastWidget* toast)
215{
216 activeToasts_.removeOne(toast);
217 toast->deleteLater();
218 repositionToasts();
219}
220
221void ToastContainer::repositionToasts()
222{
223 auto* parent = parentWidget();
224 if (!parent)
225 return;
226
227 int parentW = parent->width();
228 int parentH = parent->height();
229
230 int y = parentH - kMarginBottom;
231
232 // Stack from bottom up
233 for (qsizetype i = activeToasts_.size() - 1; i >= 0; --i)
234 {
235 auto* toast = activeToasts_[i];
236 toast->adjustSize();
237 int toastH = toast->sizeHint().height();
238 y -= toastH;
239
240 int x = parentW - kToastWidth - kMarginRight;
241 toast->move(x, y);
242 toast->raise();
243
244 y -= kSpacing;
245 }
246}
247
248// ==============================================================================
249// QtNotificationService
250// ==============================================================================
251
253 : QObject(parent), themeManager_(themeManager ? themeManager : &ThemeManager::instance())
254{
256 &QtNotificationService::processNotification, Qt::QueuedConnection);
257}
258
260
262{
263 parentWidget_ = widget;
264 container_ = new ToastContainer(widget, themeManager_);
265}
266
267void QtNotificationService::info(const std::string& message, float duration)
268{
269 emit notificationReceived(QString::fromStdString(message),
270 static_cast<int>(core::NotificationType::Info), duration);
271}
272
273void QtNotificationService::success(const std::string& message, float duration)
274{
275 emit notificationReceived(QString::fromStdString(message),
276 static_cast<int>(core::NotificationType::Success), duration);
277}
278
279void QtNotificationService::warning(const std::string& message, float duration)
280{
281 emit notificationReceived(QString::fromStdString(message),
282 static_cast<int>(core::NotificationType::Warning), duration);
283}
284
285void QtNotificationService::error(const std::string& message, float duration)
286{
287 emit notificationReceived(QString::fromStdString(message),
288 static_cast<int>(core::NotificationType::Error), duration);
289}
290
292{
293 emit notificationReceived(QString::fromStdString(notification.message),
294 static_cast<int>(notification.type), notification.duration);
295}
296
298{
299 if (container_)
300 container_->clearAll();
301}
302
303void QtNotificationService::processNotification(const QString& message, int type, float duration)
304{
305 if (!container_)
306 return;
307 container_->addToast(message, static_cast<core::NotificationType>(type), duration);
308}
void info(const std::string &message, float duration=3.0f) override
Show an info notification.
void error(const std::string &message, float duration=5.0f) override
Show an error notification.
void clearAll() override
Clear all notifications.
QtNotificationService(QObject *parent=nullptr, ThemeManager *themeManager=nullptr)
void warning(const std::string &message, float duration=5.0f) override
Show a warning notification.
void setParentWidget(QWidget *widget)
~QtNotificationService() override
void success(const std::string &message, float duration=3.0f) override
Show a success notification.
void notificationReceived(const QString &message, int type, float duration)
void show(const core::Notification &notification) override
Show a notification with full control.
const ThemeColors & colors() const
Get effective colors (with accent customization applied).
void addToast(const QString &message, core::NotificationType type, float duration)
bool eventFilter(QObject *obj, QEvent *event) override
ToastContainer(QWidget *parent, ThemeManager *themeManager=nullptr)
ToastWidget(const QString &message, core::NotificationType type, float duration, QWidget *parent, ThemeManager *themeManager=nullptr)
void dismissed()