Files
Justin Mitchell 526cf1b6f9 Add touch gesture support and LilyGo T5 S3 board
Implements bottom-edge swipe-up gesture detection and delayed touch-select with tracking state. Adds isTouchTapCandidate() to distinguish taps from swipes. Includes LilyGo T5 S3 board configuration with USB CDC logging transport for boards where Serial operator bool reports false incorrectly.
2026-06-19 13:39:10 -04:00

307 lines
10 KiB
C++

#include "HomeActivity.h"
#include <Bitmap.h>
#include <Epub.h>
#include <FsHelpers.h>
#include <GfxRenderer.h>
#include <HalStorage.h>
#include <I18n.h>
#include <Utf8.h>
#include <Xtc.h>
#include <cstring>
#include <vector>
#include "CrossPointSettings.h"
#include "CrossPointState.h"
#include "MappedInputManager.h"
#include "OpdsServerStore.h"
#include "RecentBooksStore.h"
#include "components/UITheme.h"
#include "fontIds.h"
int HomeActivity::getMenuItemCount() const {
int count = 4; // File Browser, Recents, File transfer, Settings
if (!recentBooks.empty()) {
count += recentBooks.size();
}
if (hasOpdsServers) {
count++;
}
return count;
}
void HomeActivity::loadRecentBooks(int maxBooks) {
recentBooks.clear();
const auto& books = RECENT_BOOKS.getBooks();
recentBooks.reserve(std::min(static_cast<int>(books.size()), maxBooks));
for (const RecentBook& book : books) {
// Limit to maximum number of recent books
if (recentBooks.size() >= maxBooks) {
break;
}
// Skip if file no longer exists
if (RecentBooksStore::isMissing(book)) {
continue;
}
recentBooks.push_back(book);
}
}
void HomeActivity::loadRecentCovers(int coverHeight) {
recentsLoading = true;
bool showingLoading = false;
Rect popupRect;
int progress = 0;
for (RecentBook& book : recentBooks) {
if (!book.coverBmpPath.empty()) {
std::string coverPath = UITheme::getCoverThumbPath(book.coverBmpPath, coverHeight);
if (!Storage.exists(coverPath.c_str())) {
// If epub, try to load the metadata for title/author and cover
if (FsHelpers::hasEpubExtension(book.path)) {
Epub epub(book.path, "/.crosspoint");
// Skip loading css since we only need metadata here
epub.load(false, true);
// Try to generate thumbnail image for Continue Reading card
if (!showingLoading) {
showingLoading = true;
popupRect = GUI.drawPopup(renderer, tr(STR_LOADING_POPUP));
}
GUI.fillPopupProgress(renderer, popupRect, 10 + progress * (90 / recentBooks.size()));
bool success = epub.generateThumbBmp(coverHeight);
if (!success) {
RECENT_BOOKS.updateBook(book.path, book.title, book.author, "");
book.coverBmpPath = "";
}
coverRendered = false;
requestUpdate();
} else if (FsHelpers::hasXtcExtension(book.path)) {
// Handle XTC file
Xtc xtc(book.path, "/.crosspoint");
if (xtc.load()) {
// Try to generate thumbnail image for Continue Reading card
if (!showingLoading) {
showingLoading = true;
popupRect = GUI.drawPopup(renderer, tr(STR_LOADING_POPUP));
}
GUI.fillPopupProgress(renderer, popupRect, 10 + progress * (90 / recentBooks.size()));
bool success = xtc.generateThumbBmp(coverHeight);
if (!success) {
RECENT_BOOKS.updateBook(book.path, book.title, book.author, "");
book.coverBmpPath = "";
}
coverRendered = false;
requestUpdate();
}
}
}
}
progress++;
}
recentsLoaded = true;
recentsLoading = false;
}
void HomeActivity::onEnter() {
Activity::onEnter();
hasOpdsServers = OPDS_STORE.hasServers();
const auto& metrics = UITheme::getInstance().getMetrics();
loadRecentBooks(metrics.homeRecentBooksCount);
const auto base = static_cast<int>(recentBooks.size());
selectorIndex = initialMenuItem == HomeMenuItem::NONE ? 0 : base + menuItemToIndex(initialMenuItem, hasOpdsServers);
// Trigger first update
requestUpdate();
}
void HomeActivity::onExit() {
Activity::onExit();
// Free the stored cover buffer if any
freeCoverBuffer();
}
bool HomeActivity::storeCoverBuffer() {
// render() must have already set the cover rect; without it we'd be back to
// cloning the whole framebuffer.
if (coverRectW <= 0 || coverRectH <= 0) return false;
freeCoverBuffer();
const size_t needed = renderer.getRegionByteSize(coverRectX, coverRectY, coverRectW, coverRectH);
if (needed == 0) return false;
coverBuffer = static_cast<uint8_t*>(malloc(needed));
if (!coverBuffer) {
LOG_ERR("HOME", "OOM: cover buffer (%u bytes)", (unsigned)needed);
return false;
}
coverBufferSize = needed;
if (!renderer.copyRegionToBuffer(coverRectX, coverRectY, coverRectW, coverRectH, coverBuffer, coverBufferSize)) {
free(coverBuffer);
coverBuffer = nullptr;
coverBufferSize = 0;
return false;
}
return true;
}
bool HomeActivity::restoreCoverBuffer() {
if (!coverBuffer || coverRectW <= 0 || coverRectH <= 0) return false;
return renderer.copyBufferToRegion(coverRectX, coverRectY, coverRectW, coverRectH, coverBuffer, coverBufferSize);
}
void HomeActivity::freeCoverBuffer() {
if (coverBuffer) {
free(coverBuffer);
coverBuffer = nullptr;
}
coverBufferSize = 0;
coverBufferStored = false;
}
void HomeActivity::loop() {
const int menuCount = getMenuItemCount();
buttonNavigator.onNext([this, menuCount] {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, menuCount);
requestUpdate();
});
buttonNavigator.onPrevious([this, menuCount] {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, menuCount);
requestUpdate();
});
// Tap a menu button to select + activate it. The button menu registers
// menu-local ids (drawn with selectorIndex offset by recentBooks.size()), so map
// back into the global selector space.
int downId = -1;
if (mappedInput.wasItemTouchedDown(downId)) {
selectorIndex = static_cast<int>(recentBooks.size()) + downId;
requestUpdate();
}
int tappedId = -1;
const bool tapped = mappedInput.wasItemTapped(tappedId);
if (tapped) {
selectorIndex = static_cast<int>(recentBooks.size()) + tappedId;
}
// A tap on the continue-reading cover selects that book (home selector index).
int coverId = -1;
const bool coverTapped = mappedInput.wasCoverTapped(coverId);
if (coverTapped && coverId >= 0 && coverId < static_cast<int>(recentBooks.size())) {
selectorIndex = coverId;
}
if (tapped || coverTapped || mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (selectorIndex < recentBooks.size()) {
onSelectBook(recentBooks[selectorIndex].path);
} else {
const int menuIndex = selectorIndex - static_cast<int>(recentBooks.size());
switch (indexToMenuItem(menuIndex, hasOpdsServers)) {
case HomeMenuItem::FILE_BROWSER:
onFileBrowserOpen();
break;
case HomeMenuItem::RECENTS:
onRecentsOpen();
break;
case HomeMenuItem::OPDS_BROWSER:
onOpdsBrowserOpen();
break;
case HomeMenuItem::FILE_TRANSFER:
onFileTransferOpen();
break;
case HomeMenuItem::SETTINGS_MENU:
onSettingsOpen();
break;
default:
break;
}
}
}
}
void HomeActivity::render(RenderLock&&) {
const auto& metrics = UITheme::getInstance().getMetrics();
const auto pageWidth = renderer.getScreenWidth();
const auto pageHeight = renderer.getScreenHeight();
renderer.clearScreen();
bool bufferRestored = coverBufferStored && restoreCoverBuffer();
GUI.drawHeader(renderer, Rect{0, metrics.topPadding, pageWidth, metrics.homeTopPadding},
metrics.homeContinueReadingInMenu && !recentBooks.empty() ? recentBooks[0].title.c_str() : nullptr);
// Record the tile rect so storeCoverBuffer (called from the theme) knows
// which sub-region of the framebuffer to snapshot. ~16 KB in Portrait
// instead of the 48 KB full framebuffer the previous bind captured.
coverRectX = 0;
coverRectY = metrics.homeTopPadding;
coverRectW = pageWidth;
coverRectH = metrics.homeCoverTileHeight;
GUI.drawRecentBookCover(renderer, Rect{0, metrics.homeTopPadding, pageWidth, metrics.homeCoverTileHeight},
recentBooks, selectorIndex, coverRendered, coverBufferStored, bufferRestored,
std::bind(&HomeActivity::storeCoverBuffer, this));
// Build menu items dynamically
std::vector<const char*> menuItems = {tr(STR_BROWSE_FILES), tr(STR_MENU_RECENT_BOOKS), tr(STR_FILE_TRANSFER),
tr(STR_SETTINGS_TITLE)};
std::vector<UIIcon> menuIcons = {Folder, Book, Transfer, Settings};
if (hasOpdsServers) {
menuItems.insert(menuItems.begin() + 2, tr(STR_OPDS_BROWSER));
menuIcons.insert(menuIcons.begin() + 2, Library);
}
if (metrics.homeContinueReadingInMenu && !recentBooks.empty()) {
// Insert Continue Reading at the top if enabled in theme
menuItems.insert(menuItems.begin(), tr(STR_CONTINUE_READING));
menuIcons.insert(menuIcons.begin(), Book);
}
// Menu fills the space between the cover and the bottom reserve (button hints
// when shown, else a margin). Sizing it to the real remaining height lets
// drawButtonMenu fit the items, so a scaled-up menu never runs off the bottom.
const int menuY = metrics.homeTopPadding + metrics.homeCoverTileHeight + metrics.homeMenuTopOffset;
const int bottomReserve = (BaseTheme::showButtonHints() ? metrics.buttonHintsHeight : 0) + metrics.verticalSpacing;
GUI.drawButtonMenu(
renderer, Rect{0, menuY, pageWidth, pageHeight - menuY - bottomReserve}, static_cast<int>(menuItems.size()),
metrics.homeContinueReadingInMenu ? selectorIndex : selectorIndex - recentBooks.size(),
[&menuItems](int index) { return std::string(menuItems[index]); },
[&menuIcons](int index) { return menuIcons[index]; });
const auto labels = mappedInput.mapLabels("", tr(STR_SELECT), tr(STR_DIR_UP), tr(STR_DIR_DOWN));
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
if (!firstRenderDone) {
firstRenderDone = true;
requestUpdate();
} else if (!recentsLoaded && !recentsLoading) {
recentsLoading = true;
loadRecentCovers(metrics.homeCoverHeight);
}
}
void HomeActivity::onSelectBook(const std::string& path) { activityManager.goToReader(path); }
void HomeActivity::onFileBrowserOpen() { activityManager.goToFileBrowser(); }
void HomeActivity::onRecentsOpen() { activityManager.goToRecentBooks(); }
void HomeActivity::onSettingsOpen() { activityManager.goToSettings(); }
void HomeActivity::onFileTransferOpen() { activityManager.goToFileTransfer(); }
void HomeActivity::onOpdsBrowserOpen() { activityManager.goToBrowser(); }