feat: rapid toc navigation

This commit is contained in:
Istiak Tridip 2026-01-15 02:28:21 +06:00
parent 56ec3dfb6d
commit 28152b3fee
No known key found for this signature in database
4 changed files with 68 additions and 26 deletions

View File

@ -7,7 +7,8 @@
namespace { namespace {
// Time threshold for treating a long press as a page-up/page-down // Time threshold for treating a long press as a page-up/page-down
constexpr int SKIP_PAGE_MS = 700; constexpr int RAPID_NAV_START_MS = 500;
constexpr int RAPID_NAV_DELAY_MS = 700;
} // namespace } // namespace
int EpubReaderChapterSelectionActivity::getPageItems() const { int EpubReaderChapterSelectionActivity::getPageItems() const {
@ -75,8 +76,17 @@ void EpubReaderChapterSelectionActivity::loop() {
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) || const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) ||
mappedInput.wasReleased(MappedInputManager::Button::Right); mappedInput.wasReleased(MappedInputManager::Button::Right);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS; const bool prevPressed =
mappedInput.isPressed(MappedInputManager::Button::Up) || mappedInput.isPressed(MappedInputManager::Button::Left);
const bool nextPressed = mappedInput.isPressed(MappedInputManager::Button::Down) ||
mappedInput.isPressed(MappedInputManager::Button::Right);
const int pageItems = getPageItems(); const int pageItems = getPageItems();
const int currentTocPage = selectorIndex / pageItems;
const int lastTocPage = (epub->getTocItemsCount() - 1) / pageItems;
const bool shouldNavigateRapidly = mappedInput.getHeldTime() > RAPID_NAV_START_MS;
const bool isRapidNavigationDue = (millis() - lastRapidNavTime) > RAPID_NAV_DELAY_MS;
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const auto newSpineIndex = epub->getSpineIndexForTocIndex(selectorIndex); const auto newSpineIndex = epub->getSpineIndexForTocIndex(selectorIndex);
@ -88,20 +98,29 @@ void EpubReaderChapterSelectionActivity::loop() {
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) { } else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack(); onGoBack();
} else if (prevReleased) { } else if (prevReleased) {
if (skipPage) { if (lastRapidNavTime != 0) {
selectorIndex = lastRapidNavTime = 0;
((selectorIndex / pageItems - 1) * pageItems + epub->getTocItemsCount()) % epub->getTocItemsCount(); return;
} else {
selectorIndex = (selectorIndex + epub->getTocItemsCount() - 1) % epub->getTocItemsCount();
} }
selectorIndex = (selectorIndex + epub->getTocItemsCount() - 1) % epub->getTocItemsCount();
updateRequired = true; updateRequired = true;
} else if (nextReleased) { } else if (nextReleased) {
if (skipPage) { if (lastRapidNavTime != 0) {
selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % epub->getTocItemsCount(); lastRapidNavTime = 0;
} else { return;
selectorIndex = (selectorIndex + 1) % epub->getTocItemsCount();
} }
selectorIndex = (selectorIndex + 1) % epub->getTocItemsCount();
updateRequired = true; updateRequired = true;
} else if (prevPressed && shouldNavigateRapidly && isRapidNavigationDue) {
selectorIndex = currentTocPage > 0 ? (currentTocPage - 1) * pageItems : lastTocPage * pageItems;
updateRequired = true;
lastRapidNavTime = millis();
} else if (nextPressed && shouldNavigateRapidly && isRapidNavigationDue) {
selectorIndex = currentTocPage < lastTocPage ? (currentTocPage + 1) * pageItems : 0;
updateRequired = true;
lastRapidNavTime = millis();
} }
} }

View File

@ -15,6 +15,7 @@ class EpubReaderChapterSelectionActivity final : public Activity {
int currentSpineIndex = 0; int currentSpineIndex = 0;
int selectorIndex = 0; int selectorIndex = 0;
bool updateRequired = false; bool updateRequired = false;
unsigned long lastRapidNavTime = 0;
const std::function<void()> onGoBack; const std::function<void()> onGoBack;
const std::function<void(int newSpineIndex)> onSelectSpineIndex; const std::function<void(int newSpineIndex)> onSelectSpineIndex;

View File

@ -6,7 +6,8 @@
#include "fontIds.h" #include "fontIds.h"
namespace { namespace {
constexpr int SKIP_PAGE_MS = 700; constexpr int RAPID_NAV_START_MS = 500;
constexpr int RAPID_NAV_DELAY_MS = 700;
} // namespace } // namespace
int XtcReaderChapterSelectionActivity::getPageItems() const { int XtcReaderChapterSelectionActivity::getPageItems() const {
@ -80,8 +81,18 @@ void XtcReaderChapterSelectionActivity::loop() {
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) || const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) ||
mappedInput.wasReleased(MappedInputManager::Button::Right); mappedInput.wasReleased(MappedInputManager::Button::Right);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS; const bool prevPressed =
mappedInput.isPressed(MappedInputManager::Button::Up) || mappedInput.isPressed(MappedInputManager::Button::Left);
const bool nextPressed = mappedInput.isPressed(MappedInputManager::Button::Down) ||
mappedInput.isPressed(MappedInputManager::Button::Right);
const int pageItems = getPageItems(); const int pageItems = getPageItems();
const int currentTocPage = selectorIndex / pageItems;
const int total = static_cast<int>(xtc->getChapters().size());
const int lastTocPage = total > 0 ? (total - 1) / pageItems : 0;
const bool shouldNavigateRapidly = mappedInput.getHeldTime() > RAPID_NAV_START_MS;
const bool isRapidNavigationDue = (millis() - lastRapidNavTime) > RAPID_NAV_DELAY_MS;
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const auto& chapters = xtc->getChapters(); const auto& chapters = xtc->getChapters();
@ -91,27 +102,37 @@ void XtcReaderChapterSelectionActivity::loop() {
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) { } else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack(); onGoBack();
} else if (prevReleased) { } else if (prevReleased) {
const int total = static_cast<int>(xtc->getChapters().size()); if (total == 0 || lastRapidNavTime != 0) {
if (total == 0) { lastRapidNavTime = 0;
return; return;
} }
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + total) % total; selectorIndex = (selectorIndex + total - 1) % total;
} else {
selectorIndex = (selectorIndex + total - 1) % total;
}
updateRequired = true; updateRequired = true;
} else if (nextReleased) { } else if (nextReleased) {
const int total = static_cast<int>(xtc->getChapters().size()); if (total == 0 || lastRapidNavTime != 0) {
lastRapidNavTime = 0;
return;
}
selectorIndex = (selectorIndex + 1) % total;
updateRequired = true;
} else if (prevPressed && shouldNavigateRapidly && isRapidNavigationDue) {
if (total == 0) { if (total == 0) {
return; return;
} }
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % total; selectorIndex = currentTocPage > 0 ? (currentTocPage - 1) * pageItems : lastTocPage * pageItems;
} else {
selectorIndex = (selectorIndex + 1) % total;
}
updateRequired = true; updateRequired = true;
lastRapidNavTime = millis();
} else if (nextPressed && shouldNavigateRapidly && isRapidNavigationDue) {
if (total == 0) {
return;
}
selectorIndex = currentTocPage < lastTocPage ? (currentTocPage + 1) * pageItems : 0;
updateRequired = true;
lastRapidNavTime = millis();
} }
} }

View File

@ -15,6 +15,7 @@ class XtcReaderChapterSelectionActivity final : public Activity {
uint32_t currentPage = 0; uint32_t currentPage = 0;
int selectorIndex = 0; int selectorIndex = 0;
bool updateRequired = false; bool updateRequired = false;
unsigned long lastRapidNavTime = 0;
const std::function<void()> onGoBack; const std::function<void()> onGoBack;
const std::function<void(uint32_t newPage)> onSelectPage; const std::function<void(uint32_t newPage)> onSelectPage;