Some fixes

This commit is contained in:
jpirnay
2026-04-27 13:52:55 +02:00
parent e1d623bd9c
commit adc0a52af1
10 changed files with 236 additions and 90 deletions
+7
View File
@@ -33,6 +33,13 @@ void ButtonEventManager::pushEvent(const Button button, const PressType type) {
eventTail = next;
}
void ButtonEventManager::pushEventFront(const Button button, const PressType type) {
const int prev = (eventHead - 1 + EVENT_BUF) % EVENT_BUF;
if (prev == eventTail) return; // buffer full
eventHead = prev;
eventBuf[eventHead] = {button, type};
}
bool ButtonEventManager::consumeEvent(ButtonEvent& out) {
if (eventHead == eventTail) return false;
out = eventBuf[eventHead];
+5 -1
View File
@@ -34,7 +34,7 @@ class ButtonEventManager {
};
// Timing constants (milliseconds)
static constexpr unsigned long LONG_PRESS_MS = 600;
static constexpr unsigned long LONG_PRESS_MS = 1000;
static constexpr unsigned long DOUBLE_WINDOW_MS = 300;
explicit ButtonEventManager(MappedInputManager& input) : input(input) {}
@@ -49,6 +49,10 @@ class ButtonEventManager {
// Reset all per-button FSMs. Call on activity transitions to prevent bleed-through.
void drain();
// Preserve a default event for activity processing after main loop dispatch.
// This is used when the configured action is BTN_DEFAULT.
void pushEventFront(Button button, PressType type);
// Returns true if a double-click action is configured for this button.
// ButtonEventManager queries CrossPointSettings internally.
static bool hasDoubleAction(Button button);
@@ -42,22 +42,40 @@ void EpubReaderChapterSelectionActivity::loop() {
const int pageItems = getPageItems();
const int totalItems = getTotalItems();
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const auto newSpineIndex = epub->getSpineIndexForTocIndex(selectorIndex);
if (newSpineIndex == -1) {
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if (ev.button == MappedInputManager::Button::Confirm && ev.type == ButtonEventManager::PressType::Short) {
const auto newSpineIndex = epub->getSpineIndexForTocIndex(selectorIndex);
if (newSpineIndex == -1) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
} else {
setResult(ChapterResult{newSpineIndex, selectorIndex});
finish();
}
return;
}
if (ev.button == MappedInputManager::Button::Back && ev.type == ButtonEventManager::PressType::Short) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
} else {
setResult(ChapterResult{newSpineIndex, selectorIndex});
finish();
return;
}
if ((ev.button == MappedInputManager::Button::PageBack || ev.button == MappedInputManager::Button::Left) &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
if ((ev.button == MappedInputManager::Button::PageForward || ev.button == MappedInputManager::Button::Right) &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
}
buttonNavigator.onNextRelease([this, totalItems] {
@@ -18,30 +18,65 @@ void EpubReaderFootnotesActivity::onEnter() {
void EpubReaderFootnotesActivity::onExit() { Activity::onExit(); }
void EpubReaderFootnotesActivity::loop() {
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (selectedIndex >= 0 && selectedIndex < static_cast<int>(footnotes.size())) {
setResult(FootnoteResult{footnotes[selectedIndex].href});
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if (ev.button == MappedInputManager::Button::Back && ev.type == ButtonEventManager::PressType::Short) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
if (ev.button == MappedInputManager::Button::Confirm && ev.type == ButtonEventManager::PressType::Short) {
if (selectedIndex >= 0 && selectedIndex < static_cast<int>(footnotes.size())) {
setResult(FootnoteResult{footnotes[selectedIndex].href});
finish();
}
return;
}
if ((ev.button == MappedInputManager::Button::PageBack || ev.button == MappedInputManager::Button::Left) &&
ev.type == ButtonEventManager::PressType::Short) {
if (!footnotes.empty()) {
selectedIndex = (selectedIndex - 1 + footnotes.size()) % footnotes.size();
requestUpdate();
}
return;
}
if ((ev.button == MappedInputManager::Button::PageForward || ev.button == MappedInputManager::Button::Right) &&
ev.type == ButtonEventManager::PressType::Short) {
if (!footnotes.empty()) {
selectedIndex = (selectedIndex + 1) % footnotes.size();
requestUpdate();
}
return;
}
return;
}
buttonNavigator.onNext([this] {
buttonNavigator.onNextRelease([this] {
if (!footnotes.empty()) {
selectedIndex = (selectedIndex + 1) % footnotes.size();
requestUpdate();
}
});
buttonNavigator.onPrevious([this] {
buttonNavigator.onPreviousRelease([this] {
if (!footnotes.empty()) {
selectedIndex = (selectedIndex - 1 + footnotes.size()) % footnotes.size();
requestUpdate();
}
});
buttonNavigator.onNextContinuous([this] {
if (!footnotes.empty()) {
selectedIndex = (selectedIndex + 1) % footnotes.size();
requestUpdate();
}
});
buttonNavigator.onPreviousContinuous([this] {
if (!footnotes.empty()) {
selectedIndex = (selectedIndex - 1 + footnotes.size()) % footnotes.size();
requestUpdate();
@@ -34,22 +34,34 @@ void EpubReaderPercentSelectionActivity::adjustPercent(const int delta) {
void EpubReaderPercentSelectionActivity::loop() {
// Back cancels, confirm selects, arrows adjust the percent.
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if (ev.button == MappedInputManager::Button::Back && ev.type == ButtonEventManager::PressType::Short) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
setResult(PercentResult{percent});
finish();
return;
}
if (ev.button == MappedInputManager::Button::Confirm && ev.type == ButtonEventManager::PressType::Short) {
setResult(PercentResult{percent});
finish();
return;
}
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Left}, [this] { adjustPercent(-kSmallStep); });
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Right}, [this] { adjustPercent(kSmallStep); });
if ((ev.button == MappedInputManager::Button::PageBack || ev.button == MappedInputManager::Button::Left) &&
ev.type == ButtonEventManager::PressType::Short) {
adjustPercent(-kSmallStep);
return;
}
if ((ev.button == MappedInputManager::Button::PageForward || ev.button == MappedInputManager::Button::Right) &&
ev.type == ButtonEventManager::PressType::Short) {
adjustPercent(kSmallStep);
return;
}
}
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Up}, [this] { adjustPercent(kLargeStep); });
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Down}, [this] { adjustPercent(-kLargeStep); });
@@ -34,24 +34,41 @@ void MdReaderTocSelectionActivity::loop() {
const int pageItems = getPageItems();
const int totalItems = getTotalItems();
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (selectorIndex >= 0 && selectorIndex < totalItems) {
setResult(PageResult{static_cast<uint32_t>(headings[selectorIndex].pageIndex)});
} else {
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if (ev.button == MappedInputManager::Button::Confirm && ev.type == ButtonEventManager::PressType::Short) {
if (selectorIndex >= 0 && selectorIndex < totalItems) {
setResult(PageResult{static_cast<uint32_t>(headings[selectorIndex].pageIndex)});
} else {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
}
finish();
return;
}
if (ev.button == MappedInputManager::Button::Back && ev.type == ButtonEventManager::PressType::Short) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
finish();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
if ((ev.button == MappedInputManager::Button::PageBack || ev.button == MappedInputManager::Button::Left) &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
if ((ev.button == MappedInputManager::Button::PageForward || ev.button == MappedInputManager::Button::Right) &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
}
buttonNavigator.onNextRelease([this, totalItems] {
+7 -4
View File
@@ -16,10 +16,13 @@ void QrDisplayActivity::onEnter() {
void QrDisplayActivity::onExit() { Activity::onExit(); }
void QrDisplayActivity::loop() {
if (mappedInput.wasReleased(MappedInputManager::Button::Back) ||
mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
finish();
return;
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if ((ev.button == MappedInputManager::Button::Back || ev.button == MappedInputManager::Button::Confirm) &&
ev.type == ButtonEventManager::PressType::Short) {
finish();
return;
}
}
}
+45 -23
View File
@@ -78,33 +78,55 @@ void StarredPagesActivity::deleteSelected() {
void StarredPagesActivity::loop() {
const int totalItems = static_cast<int>(bookmarkStore.getAll().size());
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if (ev.button == MappedInputManager::Button::Back && ev.type == ButtonEventManager::PressType::Short) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
if (totalItems > 0 && ev.button == MappedInputManager::Button::Confirm &&
ev.type == ButtonEventManager::PressType::Short) {
const auto& bm = bookmarkStore.getAll()[selectorIndex];
setResult(StarredPageResult{bm.spineIndex, bm.pageNumber});
finish();
return;
}
if (totalItems > 0 &&
(ev.button == MappedInputManager::Button::PageBack || ev.button == MappedInputManager::Button::Left) &&
ev.type == ButtonEventManager::PressType::Short) {
startRename();
return;
}
if (totalItems > 0 &&
(ev.button == MappedInputManager::Button::PageForward || ev.button == MappedInputManager::Button::Right) &&
ev.type == ButtonEventManager::PressType::Short) {
deleteSelected();
return;
}
if (totalItems > 0 && ev.button == MappedInputManager::Button::PageBack &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
if (totalItems > 0 && ev.button == MappedInputManager::Button::PageForward &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
}
if (totalItems == 0) return;
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const auto& bm = bookmarkStore.getAll()[selectorIndex];
setResult(StarredPageResult{bm.spineIndex, bm.pageNumber});
finish();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Left)) {
startRename();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Right)) {
deleteSelected();
return;
}
const int pageItems = UITheme::getInstance().getNumberOfItemsPerPage(renderer, true, false, true, false);
buttonNavigator.onNextRelease([this, totalItems] {
@@ -50,17 +50,35 @@ void XtcReaderChapterSelectionActivity::loop() {
const int pageItems = getPageItems();
const int totalItems = static_cast<int>(xtc->getChapters().size());
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const auto& chapters = xtc->getChapters();
if (!chapters.empty() && selectorIndex >= 0 && selectorIndex < static_cast<int>(chapters.size())) {
setResult(PageResult{chapters[selectorIndex].startPage});
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if (ev.button == MappedInputManager::Button::Confirm && ev.type == ButtonEventManager::PressType::Short) {
const auto& chapters = xtc->getChapters();
if (!chapters.empty() && selectorIndex >= 0 && selectorIndex < static_cast<int>(chapters.size())) {
setResult(PageResult{chapters[selectorIndex].startPage});
finish();
}
return;
}
if (ev.button == MappedInputManager::Button::Back && ev.type == ButtonEventManager::PressType::Short) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
if ((ev.button == MappedInputManager::Button::PageBack || ev.button == MappedInputManager::Button::Left) &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
if ((ev.button == MappedInputManager::Button::PageForward || ev.button == MappedInputManager::Button::Right) &&
ev.type == ButtonEventManager::PressType::Short) {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
requestUpdate();
return;
}
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
}
buttonNavigator.onNextRelease([this, totalItems] {
+11 -1
View File
@@ -16,6 +16,7 @@
#include <esp_ota_ops.h>
#include <cstring>
#include <vector>
#include "ButtonEventManager.h"
#include "CrossPointSettings.h"
@@ -388,6 +389,8 @@ void loop() {
using BA = CrossPointSettings::BUTTON_ACTION;
using B = MappedInputManager::Button;
ButtonEventManager::ButtonEvent ev;
std::vector<ButtonEventManager::ButtonEvent> defaultEvents;
defaultEvents.reserve(8);
while (buttonEventManager.consumeEvent(ev)) {
auto actionFor = [&](B btn) -> uint8_t {
switch (btn) {
@@ -468,7 +471,10 @@ void loop() {
};
const uint8_t action = actionFor(ev.button);
if (action == BA::BTN_DEFAULT) continue;
if (action == BA::BTN_DEFAULT) {
defaultEvents.push_back(ev);
continue;
}
switch (static_cast<BA>(action)) {
case BA::BTN_PAGE_FORWARD:
@@ -525,6 +531,10 @@ void loop() {
break;
}
}
for (auto it = defaultEvents.rbegin(); it != defaultEvents.rend(); ++it) {
buttonEventManager.pushEventFront(it->button, it->type);
}
}
const unsigned long activityStartTime = millis();