#include "ListViewActivity.h" #include #include #include #include #include "MappedInputManager.h" #include "ScreenComponents.h" #include "fontIds.h" #include "util/StringUtils.h" namespace { // Base layout constants (bezel offsets added at render time) constexpr int BASE_HEADER_Y = 15; constexpr int BASE_CONTENT_START_Y = 60; constexpr int LINE_HEIGHT = 65; // Two-line items (title + author) constexpr int BASE_LEFT_MARGIN = 20; constexpr int BASE_RIGHT_MARGIN = 40; constexpr int MICRO_THUMB_WIDTH = 45; constexpr int MICRO_THUMB_HEIGHT = 60; constexpr int BASE_THUMB_RIGHT_MARGIN = 50; // Timing thresholds constexpr int SKIP_PAGE_MS = 700; // Helper function to get the micro-thumb path for a book based on its file path std::string getMicroThumbPathForBook(const std::string& bookPath) { const size_t hash = std::hash{}(bookPath); if (StringUtils::checkFileExtension(bookPath, ".epub")) { return "/.crosspoint/epub_" + std::to_string(hash) + "/micro_thumb.bmp"; } else if (StringUtils::checkFileExtension(bookPath, ".txt") || StringUtils::checkFileExtension(bookPath, ".TXT")) { return "/.crosspoint/txt_" + std::to_string(hash) + "/micro_thumb.bmp"; } return ""; } } // namespace int ListViewActivity::getPageItems() const { const int screenHeight = renderer.getScreenHeight(); const int bottomBarHeight = 60; const int bezelTop = renderer.getBezelOffsetTop(); const int bezelBottom = renderer.getBezelOffsetBottom(); const int availableHeight = screenHeight - (BASE_CONTENT_START_Y + bezelTop) - bottomBarHeight - bezelBottom; int items = availableHeight / LINE_HEIGHT; if (items < 1) { items = 1; } return items; } int ListViewActivity::getTotalPages() const { const int itemCount = static_cast(bookList.books.size()); const int pageItems = getPageItems(); if (itemCount == 0) return 1; return (itemCount + pageItems - 1) / pageItems; } int ListViewActivity::getCurrentPage() const { const int pageItems = getPageItems(); return selectorIndex / pageItems + 1; } void ListViewActivity::taskTrampoline(void* param) { auto* self = static_cast(param); self->displayTaskLoop(); } void ListViewActivity::onEnter() { Activity::onEnter(); renderingMutex = xSemaphoreCreateMutex(); // Load the list if (!BookListStore::loadList(listName, bookList)) { Serial.printf("[%lu] [LVA] Failed to load list: %s\n", millis(), listName.c_str()); } selectorIndex = 0; updateRequired = true; xTaskCreate(&ListViewActivity::taskTrampoline, "ListViewActivityTask", 4096, // Stack size this, // Parameters 1, // Priority &displayTaskHandle // Task handle ); } void ListViewActivity::onExit() { Activity::onExit(); xSemaphoreTake(renderingMutex, portMAX_DELAY); if (displayTaskHandle) { vTaskDelete(displayTaskHandle); displayTaskHandle = nullptr; vTaskDelay(10 / portTICK_PERIOD_MS); // Let idle task free stack } vSemaphoreDelete(renderingMutex); renderingMutex = nullptr; bookList.books.clear(); } void ListViewActivity::loop() { const int itemCount = static_cast(bookList.books.size()); const int pageItems = getPageItems(); // Back button - return to My Library if (mappedInput.wasReleased(MappedInputManager::Button::Back)) { if (onBack) { onBack(); } return; } // Confirm button - open selected book if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { if (!bookList.books.empty() && selectorIndex < itemCount) { const auto& book = bookList.books[selectorIndex]; // Check if file exists before opening if (SdMan.exists(book.path.c_str())) { if (onSelectBook) { onSelectBook(book.path); } } else { Serial.printf("[%lu] [LVA] Book file not found: %s\n", millis(), book.path.c_str()); // Could show an error message here } } return; } const bool upReleased = mappedInput.wasReleased(MappedInputManager::Button::Up); const bool downReleased = mappedInput.wasReleased(MappedInputManager::Button::Down); const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS; // Navigation: Up/Down moves through items if (upReleased && itemCount > 0) { if (skipPage) { selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + itemCount) % itemCount; } else { selectorIndex = (selectorIndex + itemCount - 1) % itemCount; } updateRequired = true; } else if (downReleased && itemCount > 0) { if (skipPage) { selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % itemCount; } else { selectorIndex = (selectorIndex + 1) % itemCount; } updateRequired = true; } } void ListViewActivity::displayTaskLoop() { while (true) { if (updateRequired) { updateRequired = false; xSemaphoreTake(renderingMutex, portMAX_DELAY); render(); xSemaphoreGive(renderingMutex); } vTaskDelay(10 / portTICK_PERIOD_MS); } } void ListViewActivity::render() const { renderer.clearScreen(); const auto pageWidth = renderer.getScreenWidth(); const int pageItems = getPageItems(); const int bookCount = static_cast(bookList.books.size()); // Calculate bezel-adjusted margins const int bezelTop = renderer.getBezelOffsetTop(); const int bezelLeft = renderer.getBezelOffsetLeft(); const int bezelRight = renderer.getBezelOffsetRight(); const int HEADER_Y = BASE_HEADER_Y + bezelTop; const int CONTENT_START_Y = BASE_CONTENT_START_Y + bezelTop; const int LEFT_MARGIN = BASE_LEFT_MARGIN + bezelLeft; const int RIGHT_MARGIN = BASE_RIGHT_MARGIN + bezelRight; const int THUMB_RIGHT_MARGIN = BASE_THUMB_RIGHT_MARGIN + bezelRight; // Draw header with list name auto truncatedTitle = renderer.truncatedText(UI_12_FONT_ID, listName.c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN); renderer.drawText(UI_12_FONT_ID, LEFT_MARGIN, HEADER_Y, truncatedTitle.c_str(), true, EpdFontFamily::BOLD); if (bookCount == 0) { renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, "This list is empty"); // Draw bottom button hints const auto labels = mappedInput.mapLabels("« Back", "", "", ""); renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4); renderer.displayBuffer(); return; } const auto pageStartIndex = selectorIndex / pageItems * pageItems; // Draw selection highlight renderer.fillRect(bezelLeft, CONTENT_START_Y + (selectorIndex % pageItems) * LINE_HEIGHT - 2, pageWidth - RIGHT_MARGIN - bezelLeft, LINE_HEIGHT); // Calculate available text width const int textMaxWidth = pageWidth - LEFT_MARGIN - RIGHT_MARGIN - MICRO_THUMB_WIDTH - 10; const int thumbX = pageWidth - THUMB_RIGHT_MARGIN - MICRO_THUMB_WIDTH; // Draw items for (int i = pageStartIndex; i < bookCount && i < pageStartIndex + pageItems; i++) { const auto& book = bookList.books[i]; const int y = CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT; const bool isSelected = (i == selectorIndex); // Try to load and draw micro-thumbnail bool hasThumb = false; const std::string microThumbPath = getMicroThumbPathForBook(book.path); if (!microThumbPath.empty() && SdMan.exists(microThumbPath.c_str())) { FsFile thumbFile; if (SdMan.openFileForRead("LVA", microThumbPath, thumbFile)) { Bitmap bitmap(thumbFile); if (bitmap.parseHeaders() == BmpReaderError::Ok) { const int bmpW = bitmap.getWidth(); const int bmpH = bitmap.getHeight(); const float scaleX = static_cast(MICRO_THUMB_WIDTH) / static_cast(bmpW); const float scaleY = static_cast(MICRO_THUMB_HEIGHT) / static_cast(bmpH); const float scale = std::min(scaleX, scaleY); const int drawnW = static_cast(bmpW * scale); const int drawnH = static_cast(bmpH * scale); const int thumbY = y + (LINE_HEIGHT - drawnH) / 2; if (isSelected) { renderer.fillRect(thumbX, thumbY, drawnW, drawnH, false); } renderer.drawBitmap(bitmap, thumbX, thumbY, MICRO_THUMB_WIDTH, MICRO_THUMB_HEIGHT, 0, 0, isSelected); hasThumb = true; } thumbFile.close(); } } // Use full width if no thumbnail const int baseAvailableWidth = hasThumb ? textMaxWidth : (pageWidth - LEFT_MARGIN - RIGHT_MARGIN); // Line 1: Title std::string title = book.title; if (title.empty()) { // Fallback: extract filename from path title = book.path; const size_t lastSlash = title.find_last_of('/'); if (lastSlash != std::string::npos) { title = title.substr(lastSlash + 1); } const size_t dot = title.find_last_of('.'); if (dot != std::string::npos) { title.resize(dot); } } // Extract tags for badges (only if we'll show them - when NOT selected) constexpr int badgeSpacing = 4; // Gap between badges constexpr int badgePadding = 10; // Horizontal padding inside badge (5 each side) constexpr int badgeToThumbGap = 8; // Gap between rightmost badge and cover art int totalBadgeWidth = 0; BookTags tags; if (!isSelected) { tags = StringUtils::extractBookTags(book.path); if (!tags.extensionTag.empty()) { totalBadgeWidth += renderer.getTextWidth(SMALL_FONT_ID, tags.extensionTag.c_str()) + badgePadding; } if (!tags.suffixTag.empty()) { if (totalBadgeWidth > 0) { totalBadgeWidth += badgeSpacing; } totalBadgeWidth += renderer.getTextWidth(SMALL_FONT_ID, tags.suffixTag.c_str()) + badgePadding; } } // When selected, use full width (no badges shown) // When not selected, reserve space for badges at the right edge (plus gap to thumbnail) const int badgeReservedWidth = totalBadgeWidth > 0 ? (totalBadgeWidth + badgeSpacing + badgeToThumbGap) : 0; const int availableWidth = isSelected ? baseAvailableWidth : (baseAvailableWidth - badgeReservedWidth); auto truncatedBookTitle = renderer.truncatedText(UI_12_FONT_ID, title.c_str(), availableWidth); renderer.drawText(UI_12_FONT_ID, LEFT_MARGIN, y + 2, truncatedBookTitle.c_str(), !isSelected); // Draw badges right-aligned (near thumbnail or right edge) - only when NOT selected if (!isSelected && totalBadgeWidth > 0) { // Position badges at the right edge of the available text area (with gap to thumbnail) const int badgeAreaRight = LEFT_MARGIN + baseAvailableWidth - badgeToThumbGap; int badgeX = badgeAreaRight - totalBadgeWidth; // Center badge vertically within title line height const int titleLineHeight = renderer.getLineHeight(UI_12_FONT_ID); const int badgeLineHeight = renderer.getLineHeight(SMALL_FONT_ID); const int badgeVerticalPadding = 4; // 2px padding top + bottom in badge const int badgeHeight = badgeLineHeight + badgeVerticalPadding; const int badgeY = y + 2 + (titleLineHeight - badgeHeight) / 2; if (!tags.extensionTag.empty()) { int badgeWidth = ScreenComponents::drawPillBadge(renderer, badgeX, badgeY, tags.extensionTag.c_str(), SMALL_FONT_ID, false); badgeX += badgeWidth + badgeSpacing; } if (!tags.suffixTag.empty()) { ScreenComponents::drawPillBadge(renderer, badgeX, badgeY, tags.suffixTag.c_str(), SMALL_FONT_ID, false); } } // Line 2: Author if (!book.author.empty()) { auto truncatedAuthor = renderer.truncatedText(UI_10_FONT_ID, book.author.c_str(), baseAvailableWidth); renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, y + 32, truncatedAuthor.c_str(), !isSelected); } } // Draw scroll indicator const int screenHeight = renderer.getScreenHeight(); const int contentHeight = screenHeight - CONTENT_START_Y - 60; ScreenComponents::drawScrollIndicator(renderer, getCurrentPage(), getTotalPages(), CONTENT_START_Y, contentHeight); // Draw side button hints renderer.drawSideButtonHints(UI_10_FONT_ID, ">", "<"); // Draw bottom button hints const auto labels = mappedInput.mapLabels("« Back", "Open", "", ""); renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4); renderer.displayBuffer(); }