Skip to content

Commit 6cad21d

Browse files
committed
improve lazy loading
1 parent b8b09ad commit 6cad21d

2 files changed

Lines changed: 52 additions & 15 deletions

File tree

src/mainwindow.cpp

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ void MainWindow::initUI() {
240240
m_resultsList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
241241
m_resultsList->setTextElideMode(Qt::ElideNone);
242242
connect(m_resultsList, &QListWidget::itemPressed, this, &MainWindow::onGameSelected);
243+
connect(m_resultsList->verticalScrollBar(), &QScrollBar::valueChanged, this, &MainWindow::loadVisibleThumbnails);
243244
m_stack->addWidget(m_resultsList);
244245
mainLayout->addWidget(m_stack);
245246

@@ -653,32 +654,22 @@ void MainWindow::displayResults(const QJsonArray& items) {
653654

654655
m_resultsList->addItem(listItem);
655656

656-
// Download thumbnail for this game
657+
// Lazy load: Only set if cached, otherwise wait for scroll/visible check
657658
if (m_thumbnailCache.contains(appid)) {
658-
// Use cached thumbnail
659659
listItem->setIcon(QIcon(m_thumbnailCache[appid]));
660-
} else {
661-
// Download thumbnail from Steam CDN
662-
QString thumbnailUrl = QString("https://cdn.akamai.steamstatic.com/steam/apps/%1/header.jpg").arg(appid);
663-
QNetworkRequest thumbRequest{QUrl(thumbnailUrl)};
664-
QNetworkReply* thumbReply = m_networkManager->get(thumbRequest);
665-
thumbReply->setProperty("appid", appid);
666-
connect(thumbReply, &QNetworkReply::finished, this, [this, thumbReply]() {
667-
onThumbnailDownloaded(thumbReply);
668-
});
669660
}
670661

671662
// Track unknown games for batch fetch
672663
if (name.startsWith("Unknown Game") || name == "Unknown") {
673664
m_pendingNameFetchIds.append(appid);
674665
}
675-
676-
// Also check if we just generated a patch for this game to update status effectively?
677-
// For now, no extra logic needed.
678666
}
679667

680668
m_statusLabel->setText(QString("Found %1 results").arg(items.size()));
681669

670+
// Trigger lazy load for currently visible items
671+
QTimer::singleShot(10, this, &MainWindow::loadVisibleThumbnails);
672+
682673
// Start fetching names for unknown games
683674
if (!m_pendingNameFetchIds.isEmpty()) {
684675
startBatchNameFetch();
@@ -1214,15 +1205,55 @@ void MainWindow::onGameNameFetched(QNetworkReply* reply) {
12141205
processNextNameFetch();
12151206
}
12161207

1208+
void MainWindow::loadVisibleThumbnails() {
1209+
// Get visible range
1210+
QListWidgetItem* firstItem = m_resultsList->itemAt(0, 0);
1211+
if (!firstItem) return;
1212+
1213+
int startRow = m_resultsList->row(firstItem);
1214+
int endRow = startRow + (m_resultsList->height() / 68) + 2; // Approximate visible count + buffer
1215+
1216+
for (int i = startRow; i <= endRow && i < m_resultsList->count(); ++i) {
1217+
QListWidgetItem* item = m_resultsList->item(i);
1218+
QMap<QString, QString> data = item->data(Qt::UserRole).value<QMap<QString, QString>>();
1219+
QString appId = data["appid"];
1220+
1221+
// Skip if valid thumbnail already exists (assume placeholder has specific key or check cache)
1222+
if (m_thumbnailCache.contains(appId)) continue;
1223+
1224+
// Skip if already downloading
1225+
if (m_activeThumbnailDownloads.contains(appId)) continue;
1226+
1227+
// Start download
1228+
m_activeThumbnailDownloads.insert(appId);
1229+
1230+
QString thumbnailUrl = QString("https://cdn.akamai.steamstatic.com/steam/apps/%1/header.jpg").arg(appId);
1231+
QNetworkRequest thumbRequest{QUrl(thumbnailUrl)};
1232+
QNetworkReply* thumbReply = m_networkManager->get(thumbRequest);
1233+
thumbReply->setProperty("appid", appId);
1234+
1235+
// Prioritize the top item if it's the first one in the loop
1236+
if (i == startRow) {
1237+
// Unfortunately QNetworkAccessManager doesn't support request prioritization directly
1238+
// easily without custom management, but order of request usually matters.
1239+
}
1240+
1241+
connect(thumbReply, &QNetworkReply::finished, this, [this, thumbReply]() {
1242+
onThumbnailDownloaded(thumbReply);
1243+
});
1244+
}
1245+
}
1246+
12171247
void MainWindow::onThumbnailDownloaded(QNetworkReply* reply) {
12181248
reply->deleteLater();
1249+
QString appId = reply->property("appid").toString();
1250+
m_activeThumbnailDownloads.remove(appId);
12191251

12201252
if (reply->error() != QNetworkReply::NoError) {
12211253
// Silently fail for thumbnails, don't show errors
12221254
return;
12231255
}
12241256

1225-
QString appId = reply->property("appid").toString();
12261257
if (appId.isEmpty()) return;
12271258

12281259
QByteArray imageData = reply->readAll();

src/mainwindow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <QTimer>
1515
#include <QNetworkAccessManager>
1616
#include <QNetworkReply>
17+
#include <QScrollBar>
18+
#include <QSet>
1719

1820
class GlassButton;
1921
#include "utils/gameinfo.h"
@@ -118,6 +120,10 @@ private slots:
118120

119121
// Thumbnail cache
120122
QMap<QString, QPixmap> m_thumbnailCache;
123+
QSet<QString> m_activeThumbnailDownloads;
124+
125+
private slots:
126+
void loadVisibleThumbnails();
121127
};
122128

123129
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)