From 7a4d2b377dfe87648438bf13bdbb029f69fef7b2 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Tue, 13 Jan 2026 13:02:29 -0500 Subject: [PATCH] Improvements to button labels and navigation --- src/activities/home/MyLibraryActivity.cpp | 153 ++++++++++++++-------- 1 file changed, 99 insertions(+), 54 deletions(-) diff --git a/src/activities/home/MyLibraryActivity.cpp b/src/activities/home/MyLibraryActivity.cpp index 7103c8bb..0b684bcf 100644 --- a/src/activities/home/MyLibraryActivity.cpp +++ b/src/activities/home/MyLibraryActivity.cpp @@ -17,25 +17,31 @@ constexpr int TAB_BAR_Y = 15; constexpr int CONTENT_START_Y = 60; constexpr int LINE_HEIGHT = 30; constexpr int LEFT_MARGIN = 20; -constexpr int RIGHT_MARGIN = 40; // Extra space for scroll indicator +constexpr int RIGHT_MARGIN = 40; // Extra space for scroll indicator // Timing thresholds constexpr int SKIP_PAGE_MS = 700; constexpr unsigned long GO_HOME_MS = 1000; -void sortFileList(std::vector& strs) { - std::sort(begin(strs), end(strs), [](const std::string& str1, const std::string& str2) { - if (str1.back() == '/' && str2.back() != '/') return true; - if (str1.back() != '/' && str2.back() == '/') return false; - return lexicographical_compare(begin(str1), end(str1), begin(str2), end(str2), - [](const char& char1, const char& char2) { return tolower(char1) < tolower(char2); }); - }); +void sortFileList(std::vector &strs) { + std::sort(begin(strs), end(strs), + [](const std::string &str1, const std::string &str2) { + if (str1.back() == '/' && str2.back() != '/') + return true; + if (str1.back() != '/' && str2.back() == '/') + return false; + return lexicographical_compare( + begin(str1), end(str1), begin(str2), end(str2), + [](const char &char1, const char &char2) { + return tolower(char1) < tolower(char2); + }); + }); } -} // namespace +} // namespace int MyLibraryActivity::getPageItems() const { const int screenHeight = renderer.getScreenHeight(); - const int bottomBarHeight = 60; // Space for button hints + const int bottomBarHeight = 60; // Space for button hints const int availableHeight = screenHeight - CONTENT_START_Y - bottomBarHeight; int items = availableHeight / LINE_HEIGHT; if (items < 1) { @@ -54,7 +60,8 @@ int MyLibraryActivity::getCurrentItemCount() const { int MyLibraryActivity::getTotalPages() const { const int itemCount = getCurrentItemCount(); const int pageItems = getPageItems(); - if (itemCount == 0) return 1; + if (itemCount == 0) + return 1; return (itemCount + pageItems - 1) / pageItems; } @@ -66,11 +73,11 @@ int MyLibraryActivity::getCurrentPage() const { void MyLibraryActivity::loadRecentBooks() { bookTitles.clear(); bookPaths.clear(); - const auto& books = RECENT_BOOKS.getBooks(); + const auto &books = RECENT_BOOKS.getBooks(); bookTitles.reserve(books.size()); bookPaths.reserve(books.size()); - for (const auto& path : books) { + for (const auto &path : books) { // Skip if file no longer exists if (!SdMan.exists(path.c_str())) { continue; @@ -83,8 +90,10 @@ void MyLibraryActivity::loadRecentBooks() { title = title.substr(lastSlash + 1); } - const std::string ext5 = title.length() >= 5 ? title.substr(title.length() - 5) : ""; - const std::string ext4 = title.length() >= 4 ? title.substr(title.length() - 4) : ""; + const std::string ext5 = + title.length() >= 5 ? title.substr(title.length() - 5) : ""; + const std::string ext4 = + title.length() >= 4 ? title.substr(title.length() - 4) : ""; // If epub, try to load the metadata for title if (ext5 == ".epub") { @@ -109,7 +118,8 @@ void MyLibraryActivity::loadFiles() { auto root = SdMan.open(basepath.c_str()); if (!root || !root.isDirectory()) { - if (root) root.close(); + if (root) + root.close(); return; } @@ -127,8 +137,10 @@ void MyLibraryActivity::loadFiles() { files.emplace_back(std::string(name) + "/"); } else { auto filename = std::string(name); - std::string ext4 = filename.length() >= 4 ? filename.substr(filename.length() - 4) : ""; - std::string ext5 = filename.length() >= 5 ? filename.substr(filename.length() - 5) : ""; + std::string ext4 = + filename.length() >= 4 ? filename.substr(filename.length() - 4) : ""; + std::string ext5 = + filename.length() >= 5 ? filename.substr(filename.length() - 5) : ""; if (ext5 == ".epub" || ext5 == ".xtch" || ext4 == ".xtc") { files.emplace_back(filename); } @@ -139,8 +151,8 @@ void MyLibraryActivity::loadFiles() { sortFileList(files); } -void MyLibraryActivity::taskTrampoline(void* param) { - auto* self = static_cast(param); +void MyLibraryActivity::taskTrampoline(void *param) { + auto *self = static_cast(param); self->displayTaskLoop(); } @@ -157,17 +169,18 @@ void MyLibraryActivity::onEnter() { updateRequired = true; xTaskCreate(&MyLibraryActivity::taskTrampoline, "MyLibraryActivityTask", - 4096, // Stack size (increased for epub metadata loading) - this, // Parameters - 1, // Priority - &displayTaskHandle // Task handle + 4096, // Stack size (increased for epub metadata loading) + this, // Parameters + 1, // Priority + &displayTaskHandle // Task handle ); } void MyLibraryActivity::onExit() { Activity::onExit(); - // Wait until not rendering to delete task to avoid killing mid-instruction to EPD + // Wait until not rendering to delete task to avoid killing mid-instruction to + // EPD xSemaphoreTake(renderingMutex, portMAX_DELAY); if (displayTaskHandle) { vTaskDelete(displayTaskHandle); @@ -186,7 +199,8 @@ void MyLibraryActivity::loop() { const int pageItems = getPageItems(); // Long press BACK (1s+) in Files tab goes to root folder - if (currentTab == Tab::Files && mappedInput.isPressed(MappedInputManager::Button::Back) && + if (currentTab == Tab::Files && + mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= GO_HOME_MS) { if (basepath != "/") { basepath = "/"; @@ -197,26 +211,33 @@ void MyLibraryActivity::loop() { return; } - const bool upReleased = mappedInput.wasReleased(MappedInputManager::Button::Up); - const bool downReleased = mappedInput.wasReleased(MappedInputManager::Button::Down); - const bool leftReleased = mappedInput.wasReleased(MappedInputManager::Button::Left); - const bool rightReleased = mappedInput.wasReleased(MappedInputManager::Button::Right); + const bool upReleased = + mappedInput.wasReleased(MappedInputManager::Button::Up); + const bool downReleased = + mappedInput.wasReleased(MappedInputManager::Button::Down); + const bool leftReleased = + mappedInput.wasReleased(MappedInputManager::Button::Left); + const bool rightReleased = + mappedInput.wasReleased(MappedInputManager::Button::Right); const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS; // Confirm button - open selected item if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { if (currentTab == Tab::Recent) { - if (!bookPaths.empty() && selectorIndex < static_cast(bookPaths.size())) { + if (!bookPaths.empty() && + selectorIndex < static_cast(bookPaths.size())) { onSelectBook(bookPaths[selectorIndex]); } } else { // Files tab if (!files.empty() && selectorIndex < static_cast(files.size())) { - if (basepath.back() != '/') basepath += "/"; + if (basepath.back() != '/') + basepath += "/"; if (files[selectorIndex].back() == '/') { // Enter directory - basepath += files[selectorIndex].substr(0, files[selectorIndex].length() - 1); + basepath += + files[selectorIndex].substr(0, files[selectorIndex].length() - 1); loadFiles(); selectorIndex = 0; updateRequired = true; @@ -235,7 +256,8 @@ void MyLibraryActivity::loop() { if (currentTab == Tab::Files && basepath != "/") { // Go up one directory basepath.replace(basepath.find_last_of('/'), std::string::npos, ""); - if (basepath.empty()) basepath = "/"; + if (basepath.empty()) + basepath = "/"; loadFiles(); selectorIndex = 0; updateRequired = true; @@ -269,7 +291,8 @@ void MyLibraryActivity::loop() { if (prevReleased && itemCount > 0) { if (skipPage) { - selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + itemCount) % itemCount; + selectorIndex = + ((selectorIndex / pageItems - 1) * pageItems + itemCount) % itemCount; } else { selectorIndex = (selectorIndex + itemCount - 1) % itemCount; } @@ -300,7 +323,8 @@ void MyLibraryActivity::render() const { renderer.clearScreen(); // Draw tab bar - std::vector tabs = {{"Recent", currentTab == Tab::Recent}, {"Files", currentTab == Tab::Files}}; + std::vector tabs = {{"Recent", currentTab == Tab::Recent}, + {"Files", currentTab == Tab::Files}}; ScreenComponents::drawTabBar(renderer, TAB_BAR_Y, tabs); // Draw content based on current tab @@ -312,12 +336,23 @@ void MyLibraryActivity::render() const { // Draw scroll indicator const int screenHeight = renderer.getScreenHeight(); - const int contentHeight = screenHeight - CONTENT_START_Y - 60; // 60 for bottom bar - ScreenComponents::drawScrollIndicator(renderer, getCurrentPage(), getTotalPages(), CONTENT_START_Y, contentHeight); + const int contentHeight = + screenHeight - CONTENT_START_Y - 60; // 60 for bottom bar + ScreenComponents::drawScrollIndicator(renderer, getCurrentPage(), + getTotalPages(), CONTENT_START_Y, + contentHeight); + + // Draw side button hints (up/down navigation on right side) + // Note: text is rotated 90° CW, so ">" appears as "^" and "<" appears as "v" + renderer.drawSideButtonHints(UI_10_FONT_ID, ">", "<"); // Draw bottom button hints - const auto labels = mappedInput.mapLabels("HOME", "OPEN", "<", ">"); - renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4); + // In Files tab, show "BACK" when in subdirectory, "HOME" when at root + const char *backLabel = + (currentTab == Tab::Files && basepath != "/") ? "BACK" : "HOME"; + const auto labels = mappedInput.mapLabels(backLabel, "OPEN", "<", ">"); + renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, + labels.btn4); renderer.displayBuffer(); } @@ -328,21 +363,26 @@ void MyLibraryActivity::renderRecentTab() const { const int bookCount = static_cast(bookTitles.size()); if (bookCount == 0) { - renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, "No recent books"); + renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, + "No recent books"); return; } const auto pageStartIndex = selectorIndex / pageItems * pageItems; // Draw selection highlight - renderer.fillRect(0, CONTENT_START_Y + (selectorIndex % pageItems) * LINE_HEIGHT - 2, pageWidth - RIGHT_MARGIN, - LINE_HEIGHT); + renderer.fillRect( + 0, CONTENT_START_Y + (selectorIndex % pageItems) * LINE_HEIGHT - 2, + pageWidth - RIGHT_MARGIN, LINE_HEIGHT); // Draw items - for (int i = pageStartIndex; i < bookCount && i < pageStartIndex + pageItems; i++) { - auto item = renderer.truncatedText(UI_10_FONT_ID, bookTitles[i].c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN); - renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT, item.c_str(), - i != selectorIndex); + for (int i = pageStartIndex; i < bookCount && i < pageStartIndex + pageItems; + i++) { + auto item = renderer.truncatedText(UI_10_FONT_ID, bookTitles[i].c_str(), + pageWidth - LEFT_MARGIN - RIGHT_MARGIN); + renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, + CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT, + item.c_str(), i != selectorIndex); } } @@ -352,20 +392,25 @@ void MyLibraryActivity::renderFilesTab() const { const int fileCount = static_cast(files.size()); if (fileCount == 0) { - renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, "No books found"); + renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, + "No books found"); return; } const auto pageStartIndex = selectorIndex / pageItems * pageItems; // Draw selection highlight - renderer.fillRect(0, CONTENT_START_Y + (selectorIndex % pageItems) * LINE_HEIGHT - 2, pageWidth - RIGHT_MARGIN, - LINE_HEIGHT); + renderer.fillRect( + 0, CONTENT_START_Y + (selectorIndex % pageItems) * LINE_HEIGHT - 2, + pageWidth - RIGHT_MARGIN, LINE_HEIGHT); // Draw items - for (int i = pageStartIndex; i < fileCount && i < pageStartIndex + pageItems; i++) { - auto item = renderer.truncatedText(UI_10_FONT_ID, files[i].c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN); - renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT, item.c_str(), - i != selectorIndex); + for (int i = pageStartIndex; i < fileCount && i < pageStartIndex + pageItems; + i++) { + auto item = renderer.truncatedText(UI_10_FONT_ID, files[i].c_str(), + pageWidth - LEFT_MARGIN - RIGHT_MARGIN); + renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, + CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT, + item.c_str(), i != selectorIndex); } }