forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathsplashscreen.cpp
More file actions
453 lines (395 loc) · 17.2 KB
/
splashscreen.cpp
File metadata and controls
453 lines (395 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (c) 2011-2021 The Bitcoin Core developers
// Copyright (c) 2014-2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif
#include <qt/splashscreen.h>
#include <chainparams.h>
#include <clientversion.h>
#include <interfaces/handler.h>
#include <interfaces/node.h>
#include <interfaces/wallet.h>
#include <qt/guiutil.h>
#include <qt/guiutil_font.h>
#include <qt/networkstyle.h>
#include <qt/walletmodel.h>
#include <util/system.h>
#include <util/translation.h>
#include <cmath>
#include <QApplication>
#include <QCloseEvent>
#include <QPainter>
#include <QScreen>
// Padding around the card
static constexpr int SPLASH_PADDING = 16;
// Cap for time-based exponential fill: prevents bar from appearing complete
// before the phase actually finishes (bar fills to 95% of range, then waits)
static constexpr qreal EXPONENTIAL_FILL_CAP = 0.95;
/** Phase weight table: maps init message keys to overall progress ranges.
* Keys are untranslated strings passed through _() at lookup time so that
* phase matching works correctly regardless of the active locale.
* Phases without ShowProgress use time-based exponential fill.
* Phases with ShowProgress interpolate linearly within their range.
* Phases with resetsBar=true reset the bar to 0 and use the full range,
* since they can take an arbitrarily long time (e.g. rescanning, wallet loading). */
struct PhaseInfo {
const char* msg_key; // untranslated init message (translated at lookup time)
qreal start; // overall progress at phase start (0.0-1.0)
qreal end; // overall progress at phase end (0.0-1.0)
bool resetsBar; // if true, resets the bar to show this phase's own 0-100%
bool snapsToEnd; // if true, instantly completes the bar (for "Done loading")
};
static const PhaseInfo PHASE_TABLE[] = {
{"Loading P2P addresses…", 0.00, 0.02, false, false},
{"Loading banlist…", 0.02, 0.03, false, false},
{"Loading block index…", 0.03, 0.75, false, false},
{"Verifying blocks…", 0.75, 0.88, false, false},
{"Replaying blocks…", 0.75, 0.88, false, false},
{"Pruning blockstore…", 0.88, 0.90, false, false},
{"Starting network threads…", 0.90, 0.94, false, false},
{"Rescanning…", 0.00, 1.00, true, false},
{"Verifying wallet(s)…", 0.00, 0.20, true, false},
{"Loading wallet…", 0.20, 1.00, false, false},
{"Done loading", 0.94, 1.00, false, true},
};
/** Look up phase by translating each key and checking if the message contains it.
* Uses contains() because ShowProgress may prepend the wallet name. */
static const PhaseInfo* LookupPhase(const QString& message)
{
static const auto cache = [] {
std::vector<std::pair<const PhaseInfo*, QString>> phases_with_translations;
for (const auto& phase : PHASE_TABLE) {
phases_with_translations.push_back({&phase, QString::fromStdString(_(phase.msg_key).translated)});
}
return phases_with_translations;
}();
for (const auto& [phase, translated] : cache) {
if (message.contains(translated, Qt::CaseInsensitive)) {
return phase;
}
}
return nullptr;
}
SplashScreen::SplashScreen(const NetworkStyle* networkStyle)
: QWidget(),
messageColor(GUIUtil::getThemedQColor(GUIUtil::ThemedColor::DEFAULT))
{
// transparent background
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet("background:transparent;");
// no window decorations
setWindowFlags(Qt::FramelessWindowHint);
// Modern splash dimensions — wider and shorter for a contemporary feel
int width = 440;
int height = 360;
int logoSize = 140;
int cornerRadius = 16;
float fontFactor = 1.0;
float scale = qApp->devicePixelRatio();
// define text to place
QString titleText = PACKAGE_NAME;
QString versionText = QString::fromStdString(FormatFullVersion()).remove(0, 1);
const QString& titleAddText = networkStyle->getTitleAddText();
QFont fontNormal = GUIUtil::getFontNormal();
QFont fontBold = GUIUtil::getFontBold();
QPixmap pixmapLogo = networkStyle->getSplashImage();
// Adjust logo color based on the current theme
QImage imgLogo = pixmapLogo.toImage().convertToFormat(QImage::Format_ARGB32);
QColor logoColor = GUIUtil::getThemedQColor(GUIUtil::ThemedColor::BLUE);
for (int x = 0; x < imgLogo.width(); ++x) {
for (int y = 0; y < imgLogo.height(); ++y) {
const QRgb rgb = imgLogo.pixel(x, y);
imgLogo.setPixel(x, y, qRgba(logoColor.red(), logoColor.green(), logoColor.blue(), qAlpha(rgb)));
}
}
pixmapLogo.convertFromImage(imgLogo);
pixmapLogo.setDevicePixelRatio(scale);
int canvasWidth = width + SPLASH_PADDING * 2;
int canvasHeight = height + SPLASH_PADDING * 2;
pixmap = QPixmap(canvasWidth * scale, canvasHeight * scale);
pixmap.setDevicePixelRatio(scale);
pixmap.fill(Qt::transparent);
// Scope the painter so it releases the pixmap before timer/signal setup
{
QPainter pixPaint(&pixmap);
pixPaint.setRenderHint(QPainter::Antialiasing, true);
pixPaint.setRenderHint(QPainter::SmoothPixmapTransform, true);
QColor bgColor = GUIUtil::getThemedQColor(GUIUtil::ThemedColor::BACKGROUND_WIDGET);
// Draw rounded background card with a subtle border
QRectF cardRect(SPLASH_PADDING, SPLASH_PADDING, width, height);
pixPaint.setPen(QPen(QColor(128, 128, 128, 40), 0.5));
pixPaint.setBrush(bgColor);
pixPaint.drawRoundedRect(cardRect, cornerRadius, cornerRadius);
// Offsets relative to card origin
int cardX = SPLASH_PADDING;
int cardY = SPLASH_PADDING;
int contentTop = cardY + 48;
// Draw logo centered horizontally, near the top
int logoX = cardX + (width - logoSize) / 2;
int logoY = contentTop;
pixPaint.drawPixmap(logoX, logoY, logoSize, logoSize, pixmapLogo);
// Title text below logo
pixPaint.setPen(messageColor);
fontBold.setPointSize(28 * fontFactor);
pixPaint.setFont(fontBold);
QFontMetrics fm = pixPaint.fontMetrics();
int titleTextWidth = GUIUtil::TextWidth(fm, titleText);
if (titleTextWidth > width * 0.8) {
fontFactor = 0.75;
fontBold.setPointSize(28 * fontFactor);
pixPaint.setFont(fontBold);
fm = pixPaint.fontMetrics();
titleTextWidth = GUIUtil::TextWidth(fm, titleText);
}
int titleBaseline = logoY + logoSize + 36 + fm.ascent();
pixPaint.drawText(cardX + (width - titleTextWidth) / 2, titleBaseline, titleText);
// Version text — subtle, below title
QColor subtleColor(messageColor);
subtleColor.setAlpha(130);
pixPaint.setPen(subtleColor);
fontNormal.setPointSize(12 * fontFactor);
pixPaint.setFont(fontNormal);
fm = pixPaint.fontMetrics();
int versionTextWidth = GUIUtil::TextWidth(fm, versionText);
int versionBaseline = titleBaseline + fm.height() + 4;
pixPaint.drawText(cardX + (width - versionTextWidth) / 2, versionBaseline, versionText);
// Draw network badge if special network (testnet, devnet, regtest)
if(!titleAddText.isEmpty()) {
fontBold.setPointSize(9 * fontFactor);
pixPaint.setFont(fontBold);
fm = pixPaint.fontMetrics();
int titleAddTextWidth = GUIUtil::TextWidth(fm, titleAddText);
int badgePadH = 10;
int badgePadV = 4;
int badgeW = titleAddTextWidth + badgePadH * 2;
int badgeH = fm.height() + badgePadV * 2;
int badgeX = cardX + width - badgeW - 16;
int badgeY = cardY + 14;
// Rounded badge
pixPaint.setPen(Qt::NoPen);
pixPaint.setBrush(networkStyle->getBadgeColor());
pixPaint.drawRoundedRect(badgeX, badgeY, badgeW, badgeH, badgeH / 2, badgeH / 2);
// Badge text
pixPaint.setPen(QColor(255, 255, 255));
pixPaint.drawText(badgeX + badgePadH, badgeY + badgePadV + fm.ascent(), titleAddText);
}
} // QPainter released here
// Resize window and move to center of desktop, disallow resizing
QRect r(QPoint(), QSize(canvasWidth, canvasHeight));
resize(r.size());
setFixedSize(r.size());
move(QGuiApplication::primaryScreen()->geometry().center() - r.center());
installEventFilter(this);
// Animation timer: smoothly advance displayProgress toward target and repaint.
// Timer is started on first message to avoid wasteful repaints before init begins.
// Note: calcOverallProgress() is safe to call here because animTimer is only
// started after phaseTimer.start() in showMessage().
connect(&animTimer, &QTimer::timeout, this, [this]() {
qreal target = calcOverallProgress();
// Smoothly animate toward target (never go backwards)
if (target > displayProgress) {
qreal gap = target - displayProgress;
// Faster easing for larger gaps so fast phases don't lag behind
qreal ease = gap > 0.1 ? 0.3 : 0.15;
displayProgress += gap * ease;
// Snap if very close
if (target - displayProgress < 0.002) displayProgress = target;
}
update();
// Stop timer once progress is complete
if (displayProgress >= 0.999) animTimer.stop();
});
GUIUtil::handleCloseWindowShortcut(this);
}
SplashScreen::~SplashScreen()
{
if (m_node) unsubscribeFromCoreSignals();
}
void SplashScreen::setNode(interfaces::Node& node)
{
assert(!m_node);
m_node = &node;
subscribeToCoreSignals();
if (m_shutdown) m_node->startShutdown();
}
void SplashScreen::shutdown()
{
m_shutdown = true;
if (m_node) m_node->startShutdown();
}
bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
if (ev->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
if (keyEvent->key() == Qt::Key_Q) {
shutdown();
}
}
return QObject::eventFilter(obj, ev);
}
qreal SplashScreen::calcOverallProgress() const
{
if (curProgress >= 0) {
// When a phase reports real sub-progress, map that 0-100 value into the
// phase's assigned range directly.
return phaseStart + (phaseEnd - phaseStart) * (curProgress / 100.0);
}
// Otherwise fall back to a time-based curve so phases driven only by
// initMessage() still show movement without claiming exact progress.
qreal elapsed = phaseTimer.elapsed() / 1000.0;
qreal fraction = 1.0 - std::exp(-elapsed / 5.0);
return phaseStart + (phaseEnd - phaseStart) * fraction * EXPONENTIAL_FILL_CAP;
}
/** Thread-safe helper: queue a message and/or progress update to the GUI thread.
* @param color Text color, captured by value from the GUI thread at signal
* connection time to avoid cross-thread access to member fields. */
static void PostMessageAndProgress(SplashScreen *splash, const QColor &color,
const std::string &message, int progress)
{
if (!message.empty()) {
bool invoked = QMetaObject::invokeMethod(splash, "showMessage",
Qt::QueuedConnection,
Q_ARG(QString, QString::fromStdString(message)),
Q_ARG(int, Qt::AlignBottom | Qt::AlignHCenter),
Q_ARG(QColor, color));
assert(invoked);
}
bool invoked = QMetaObject::invokeMethod(splash, "setProgress",
Qt::QueuedConnection, Q_ARG(int, progress));
assert(invoked);
}
void SplashScreen::subscribeToCoreSignals()
{
// Capture messageColor by value for thread-safe use in non-GUI thread callbacks
const QColor color = messageColor;
m_handler_init_message = m_node->handleInitMessage([this, color](const std::string& msg) {
PostMessageAndProgress(this, color, msg, /*progress=*/-1);
});
m_handler_show_progress = m_node->handleShowProgress([this, color](const std::string& title, int nProgress, bool /*resume_possible*/) {
PostMessageAndProgress(this, color, title, nProgress);
});
m_handler_init_wallet = m_node->handleInitWallet([this]() { handleLoadingWallet(); });
}
void SplashScreen::handleLoadingWallet()
{
#ifdef ENABLE_WALLET
if (!WalletModel::isWalletEnabled()) return;
const QColor color = messageColor;
m_handler_loading_wallet = m_node->walletLoader().handleLoadWalletLoading([this, color](std::unique_ptr<interfaces::Wallet> wallet) {
m_connected_wallet_handlers.emplace_back(wallet->handleShowProgress([this, color](const std::string& title, int nProgress) {
PostMessageAndProgress(this, color, title, nProgress);
}));
m_connected_wallets.emplace_back(std::move(wallet));
});
#endif
}
void SplashScreen::unsubscribeFromCoreSignals()
{
// Disconnect signals from client
m_handler_init_message->disconnect();
m_handler_show_progress->disconnect();
m_handler_init_wallet->disconnect();
#ifdef ENABLE_WALLET
if (m_handler_loading_wallet != nullptr) {
m_handler_loading_wallet->disconnect();
}
#endif // ENABLE_WALLET
for (const auto& handler : m_connected_wallet_handlers) {
handler->disconnect();
}
m_connected_wallet_handlers.clear();
m_connected_wallets.clear();
}
void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color)
{
curMessage = message;
curAlignment = alignment;
curColor = color;
// Start animation timer on first message (avoids wasteful repaints before init begins)
if (!animTimer.isActive()) {
phaseTimer.start();
animTimer.start(30);
}
// Look up the phase range for this message; only reinitialize when the
// phase actually changes so that repeated progress callbacks (e.g. during
// wallet rescans) don't reset displayProgress and phaseTimer each time.
// For resetsBar phases, also reinitialize when the message text changes
// (e.g. different wallet name prefix during multi-wallet rescans).
const PhaseInfo* phase = LookupPhase(message);
const bool phase_changed = phase != nullptr &&
(phase != m_current_phase || (phase->resetsBar && message != m_current_phase_message));
if (phase_changed) {
m_current_phase = phase;
m_current_phase_message = message;
// Progress values are phase-local; drop any numeric progress from the
// previous phase until a new ShowProgress update arrives.
curProgress = -1;
if (phase->snapsToEnd) {
// Final phase: snap to 100% immediately since the splash
// will be destroyed moments after "Done loading" arrives
displayProgress = 1.0;
phaseStart = 1.0;
phaseEnd = 1.0;
animTimer.stop();
} else if (phase->resetsBar) {
// Long independent phases get their own full bar
displayProgress = 0.0;
phaseStart = phase->start;
phaseEnd = phase->end;
animTimer.start(30);
} else {
// Normal phase: ensure we never jump backwards
phaseStart = std::max(phase->start, displayProgress);
phaseEnd = std::max(phase->end, phaseStart);
}
phaseTimer.restart();
}
update();
}
void SplashScreen::setProgress(int value)
{
curProgress = value;
}
void SplashScreen::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawPixmap(0, 0, pixmap);
int barMargin = 32;
int barHeight = 4;
int barRadius = barHeight / 2;
int barLeft = SPLASH_PADDING + barMargin;
int barWidth = width() - SPLASH_PADDING * 2 - barMargin * 2;
// Draw status message above the progress bar area
QFont messageFont = GUIUtil::getFontNormal();
messageFont.setPointSize(12);
painter.setFont(messageFont);
QRect messageRect = rect().adjusted(
SPLASH_PADDING + 24, 0,
-SPLASH_PADDING - 24,
-SPLASH_PADDING - 28);
QColor msgColor(curColor);
msgColor.setAlpha(160);
painter.setPen(msgColor);
painter.drawText(messageRect, curAlignment, curMessage);
// Draw unified progress bar at the bottom of the card
int barY = height() - SPLASH_PADDING - 18;
// Track background (always visible once we have a message)
if (!curMessage.isEmpty()) {
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(128, 128, 128, 40));
painter.drawRoundedRect(QRectF(barLeft, barY, barWidth, barHeight), barRadius, barRadius);
// Fill based on overall progress
int fillWidth = static_cast<int>(barWidth * std::min(displayProgress, 1.0));
if (fillWidth > 0) {
painter.setBrush(GUIUtil::getThemedQColor(GUIUtil::ThemedColor::BLUE));
painter.drawRoundedRect(QRectF(barLeft, barY, fillWidth, barHeight), barRadius, barRadius);
}
}
}
void SplashScreen::closeEvent(QCloseEvent *event)
{
shutdown(); // allows an "emergency" shutdown during startup
event->ignore();
}