prerender book covers and thumbnails when opening a book for the first time

Moves cover/thumbnail generation from lazy (Home screen, Sleep screen) into
each reader activity's onEnter(). On first open, generates all needed BMPs
(cover, cover_crop, thumbnails for all theme heights) with a "Preparing
book..." progress popup. Subsequent opens skip instantly when files exist.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cottongin
2026-02-12 16:13:55 -05:00
parent e798065a5c
commit 905f694576
4 changed files with 81 additions and 0 deletions

View File

@@ -102,6 +102,42 @@ void EpubReaderActivity::onEnter() {
} }
} }
// Prerender covers and thumbnails on first open so Home and Sleep screens are instant.
// Each generate* call is a no-op if the file already exists, so this only does work once.
{
int totalSteps = 0;
if (!Storage.exists(epub->getCoverBmpPath(false).c_str())) totalSteps++;
if (!Storage.exists(epub->getCoverBmpPath(true).c_str())) totalSteps++;
for (int i = 0; i < PRERENDER_THUMB_HEIGHTS_COUNT; i++) {
if (!Storage.exists(epub->getThumbBmpPath(PRERENDER_THUMB_HEIGHTS[i]).c_str())) totalSteps++;
}
if (totalSteps > 0) {
Rect popupRect = GUI.drawPopup(renderer, "Preparing book...");
int completedSteps = 0;
auto updateProgress = [&]() {
completedSteps++;
GUI.fillPopupProgress(renderer, popupRect, completedSteps * 100 / totalSteps);
};
if (!Storage.exists(epub->getCoverBmpPath(false).c_str())) {
epub->generateCoverBmp(false);
updateProgress();
}
if (!Storage.exists(epub->getCoverBmpPath(true).c_str())) {
epub->generateCoverBmp(true);
updateProgress();
}
for (int i = 0; i < PRERENDER_THUMB_HEIGHTS_COUNT; i++) {
if (!Storage.exists(epub->getThumbBmpPath(PRERENDER_THUMB_HEIGHTS[i]).c_str())) {
epub->generateThumbBmp(PRERENDER_THUMB_HEIGHTS[i]);
updateProgress();
}
}
}
}
// Save current epub as last opened epub and add to recent books // Save current epub as last opened epub and add to recent books
APP_STATE.openEpubPath = epub->getPath(); APP_STATE.openEpubPath = epub->getPath();
APP_STATE.saveToFile(); APP_STATE.saveToFile();

View File

@@ -57,6 +57,15 @@ void TxtReaderActivity::onEnter() {
txt->setupCacheDir(); txt->setupCacheDir();
// Prerender cover on first open so the Sleep screen is instant.
// generateCoverBmp() is a no-op if the file already exists, so this only does work once.
// TXT has no thumbnail support, so only the sleep screen cover is generated.
if (!Storage.exists(txt->getCoverBmpPath().c_str())) {
Rect popupRect = GUI.drawPopup(renderer, "Preparing book...");
txt->generateCoverBmp();
GUI.fillPopupProgress(renderer, popupRect, 100);
}
// Save current txt as last opened file and add to recent books // Save current txt as last opened file and add to recent books
auto filePath = txt->getPath(); auto filePath = txt->getPath();
auto fileName = filePath.substr(filePath.rfind('/') + 1); auto fileName = filePath.substr(filePath.rfind('/') + 1);

View File

@@ -43,6 +43,37 @@ void XtcReaderActivity::onEnter() {
// Load saved progress // Load saved progress
loadProgress(); loadProgress();
// Prerender covers and thumbnails on first open so Home and Sleep screens are instant.
// Each generate* call is a no-op if the file already exists, so this only does work once.
{
int totalSteps = 0;
if (!Storage.exists(xtc->getCoverBmpPath().c_str())) totalSteps++;
for (int i = 0; i < PRERENDER_THUMB_HEIGHTS_COUNT; i++) {
if (!Storage.exists(xtc->getThumbBmpPath(PRERENDER_THUMB_HEIGHTS[i]).c_str())) totalSteps++;
}
if (totalSteps > 0) {
Rect popupRect = GUI.drawPopup(renderer, "Preparing book...");
int completedSteps = 0;
auto updateProgress = [&]() {
completedSteps++;
GUI.fillPopupProgress(renderer, popupRect, completedSteps * 100 / totalSteps);
};
if (!Storage.exists(xtc->getCoverBmpPath().c_str())) {
xtc->generateCoverBmp();
updateProgress();
}
for (int i = 0; i < PRERENDER_THUMB_HEIGHTS_COUNT; i++) {
if (!Storage.exists(xtc->getThumbBmpPath(PRERENDER_THUMB_HEIGHTS[i]).c_str())) {
xtc->generateThumbBmp(PRERENDER_THUMB_HEIGHTS[i]);
updateProgress();
}
}
}
}
// Save current XTC as last opened book and add to recent books // Save current XTC as last opened book and add to recent books
APP_STATE.openEpubPath = xtc->getPath(); APP_STATE.openEpubPath = xtc->getPath();
APP_STATE.saveToFile(); APP_STATE.saveToFile();

View File

@@ -27,5 +27,10 @@ class UITheme {
const BaseTheme* currentTheme; const BaseTheme* currentTheme;
}; };
// Known theme thumbnail heights to prerender when opening a book for the first time.
// These correspond to homeCoverHeight values across all themes (Lyra=226, Base=400).
static constexpr int PRERENDER_THUMB_HEIGHTS[] = {226, 400};
static constexpr int PRERENDER_THUMB_HEIGHTS_COUNT = 2;
// Helper macro to access current theme // Helper macro to access current theme
#define GUI UITheme::getInstance().getTheme() #define GUI UITheme::getInstance().getTheme()