diff --git a/src/activities/home/MyLibraryActivity.cpp b/src/activities/home/MyLibraryActivity.cpp index 29c6ea73..6d9d79fc 100644 --- a/src/activities/home/MyLibraryActivity.cpp +++ b/src/activities/home/MyLibraryActivity.cpp @@ -21,7 +21,6 @@ constexpr int LEFT_MARGIN = 20; 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) { @@ -178,13 +177,9 @@ 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 skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS; - // Confirm button - open selected item if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { if (currentTab == Tab::Recent) { @@ -249,24 +244,28 @@ void MyLibraryActivity::loop() { } // Navigation: Up/Down moves through items only - const bool prevReleased = upReleased; - const bool nextReleased = downReleased; + constexpr auto upButton = MappedInputManager::Button::Up; + constexpr auto downButton = MappedInputManager::Button::Down; - if (prevReleased && itemCount > 0) { - if (skipPage) { - selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + itemCount) % itemCount; - } else { - selectorIndex = (selectorIndex + itemCount - 1) % itemCount; - } + buttonNavigator.onRelease({downButton}, [this, itemCount] { + selectorIndex = ButtonNavigator::nextIndex(selectorIndex, itemCount); updateRequired = true; - } else if (nextReleased && itemCount > 0) { - if (skipPage) { - selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % itemCount; - } else { - selectorIndex = (selectorIndex + 1) % itemCount; - } + }); + + buttonNavigator.onRelease({upButton}, [this, itemCount] { + selectorIndex = ButtonNavigator::previousIndex(selectorIndex, itemCount); updateRequired = true; - } + }); + + buttonNavigator.onContinuous({downButton}, [this, itemCount, pageItems] { + selectorIndex = ButtonNavigator::nextPageIndex(selectorIndex, itemCount, pageItems); + updateRequired = true; + }); + + buttonNavigator.onContinuous({upButton}, [this, itemCount, pageItems] { + selectorIndex = ButtonNavigator::previousPageIndex(selectorIndex, itemCount, pageItems); + updateRequired = true; + }); } void MyLibraryActivity::displayTaskLoop() { diff --git a/src/activities/home/MyLibraryActivity.h b/src/activities/home/MyLibraryActivity.h index 39a27ed7..1241ba7c 100644 --- a/src/activities/home/MyLibraryActivity.h +++ b/src/activities/home/MyLibraryActivity.h @@ -9,6 +9,7 @@ #include "../Activity.h" #include "RecentBooksStore.h" +#include "util/ButtonNavigator.h" class MyLibraryActivity final : public Activity { public: @@ -17,6 +18,7 @@ class MyLibraryActivity final : public Activity { private: TaskHandle_t displayTaskHandle = nullptr; SemaphoreHandle_t renderingMutex = nullptr; + ButtonNavigator buttonNavigator; Tab currentTab = Tab::Recent; int selectorIndex = 0;