2026-02-01 08:34:30 +01:00
|
|
|
#pragma once
|
|
|
|
|
#include <Epub.h>
|
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
|
#include <freertos/task.h>
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "../ActivityWithSubactivity.h"
|
|
|
|
|
|
|
|
|
|
class EpubReaderMenuActivity final : public ActivityWithSubactivity {
|
|
|
|
|
public:
|
2026-02-05 15:17:51 +03:00
|
|
|
// Menu actions available from the reader menu.
|
feat: Move Sync feature to menu (#680)
## Summary
* **What is the goal of this PR?**
Move the "Sync Progress" option from TOC (Chapter Selection) screen to
the Reader Menu, and fix use-after-free crashes related to callback
handling in activity lifecycle.
* **What changes are included?**
- Added "Sync Progress" as a menu item in `EpubReaderMenuActivity` (now
4 items: Go to Chapter, Sync Progress, Go Home, Delete Book Cache)
- Removed sync-related logic from `EpubReaderChapterSelectionActivity` -
TOC now only displays chapters
- Implemented `pendingGoHome` and `pendingSubactivityExit` flags in
`EpubReaderActivity` to safely handle activity destruction
- Fixed GO_HOME, DELETE_CACHE, and SYNC menu actions to use deferred
callbacks avoiding use-after-free
## Additional Context
* Root cause of crashes: callbacks like `onGoHome()` or `onCancel()`
invoked from activity handlers could destroy the current activity while
code was still executing, causing use-after-free and race conditions
with FreeRTOS display task.
* Solution: Deferred execution pattern - set flags and process them in
`loop()` after all nested activity loops have safely returned.
* Files changed: `EpubReaderMenuActivity.h`,
`EpubReaderActivity.h/.cpp`, `EpubReaderChapterSelectionActivity.h/.cpp`
---
### AI Usage
While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.
Did you use AI tools to help write this code? _**YES**_
Co-authored-by: danoooob <danoooob@example.com>
Co-authored-by: Dave Allie <dave@daveallie.com>
2026-02-05 22:04:38 +07:00
|
|
|
enum class MenuAction { SELECT_CHAPTER, GO_TO_PERCENT, ROTATE_SCREEN, GO_HOME, SYNC, DELETE_CACHE };
|
2026-02-01 08:34:30 +01:00
|
|
|
|
|
|
|
|
explicit EpubReaderMenuActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& title,
|
2026-02-05 15:17:51 +03:00
|
|
|
const int currentPage, const int totalPages, const int bookProgressPercent,
|
2026-02-05 14:53:35 +03:00
|
|
|
const uint8_t currentOrientation, const std::function<void(uint8_t)>& onBack,
|
|
|
|
|
const std::function<void(MenuAction)>& onAction)
|
2026-02-01 08:34:30 +01:00
|
|
|
: ActivityWithSubactivity("EpubReaderMenu", renderer, mappedInput),
|
|
|
|
|
title(title),
|
2026-02-05 14:53:35 +03:00
|
|
|
pendingOrientation(currentOrientation),
|
2026-02-05 15:17:51 +03:00
|
|
|
currentPage(currentPage),
|
|
|
|
|
totalPages(totalPages),
|
|
|
|
|
bookProgressPercent(bookProgressPercent),
|
2026-02-01 08:34:30 +01:00
|
|
|
onBack(onBack),
|
|
|
|
|
onAction(onAction) {}
|
|
|
|
|
|
|
|
|
|
void onEnter() override;
|
|
|
|
|
void onExit() override;
|
|
|
|
|
void loop() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct MenuItem {
|
|
|
|
|
MenuAction action;
|
|
|
|
|
std::string label;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-05 15:17:51 +03:00
|
|
|
// Fixed menu layout (order matters for up/down navigation).
|
feat: Move Sync feature to menu (#680)
## Summary
* **What is the goal of this PR?**
Move the "Sync Progress" option from TOC (Chapter Selection) screen to
the Reader Menu, and fix use-after-free crashes related to callback
handling in activity lifecycle.
* **What changes are included?**
- Added "Sync Progress" as a menu item in `EpubReaderMenuActivity` (now
4 items: Go to Chapter, Sync Progress, Go Home, Delete Book Cache)
- Removed sync-related logic from `EpubReaderChapterSelectionActivity` -
TOC now only displays chapters
- Implemented `pendingGoHome` and `pendingSubactivityExit` flags in
`EpubReaderActivity` to safely handle activity destruction
- Fixed GO_HOME, DELETE_CACHE, and SYNC menu actions to use deferred
callbacks avoiding use-after-free
## Additional Context
* Root cause of crashes: callbacks like `onGoHome()` or `onCancel()`
invoked from activity handlers could destroy the current activity while
code was still executing, causing use-after-free and race conditions
with FreeRTOS display task.
* Solution: Deferred execution pattern - set flags and process them in
`loop()` after all nested activity loops have safely returned.
* Files changed: `EpubReaderMenuActivity.h`,
`EpubReaderActivity.h/.cpp`, `EpubReaderChapterSelectionActivity.h/.cpp`
---
### AI Usage
While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.
Did you use AI tools to help write this code? _**YES**_
Co-authored-by: danoooob <danoooob@example.com>
Co-authored-by: Dave Allie <dave@daveallie.com>
2026-02-05 22:04:38 +07:00
|
|
|
const std::vector<MenuItem> menuItems = {
|
|
|
|
|
{MenuAction::SELECT_CHAPTER, "Go to Chapter"}, {MenuAction::ROTATE_SCREEN, "Reading Orientation"},
|
|
|
|
|
{MenuAction::GO_TO_PERCENT, "Go to %"}, {MenuAction::GO_HOME, "Go Home"},
|
|
|
|
|
{MenuAction::SYNC, "Sync Progress"}, {MenuAction::DELETE_CACHE, "Delete Book Cache"}};
|
2026-02-01 08:34:30 +01:00
|
|
|
|
|
|
|
|
int selectedIndex = 0;
|
|
|
|
|
bool updateRequired = false;
|
|
|
|
|
TaskHandle_t displayTaskHandle = nullptr;
|
|
|
|
|
SemaphoreHandle_t renderingMutex = nullptr;
|
|
|
|
|
std::string title = "Reader Menu";
|
2026-02-05 14:53:35 +03:00
|
|
|
uint8_t pendingOrientation = 0;
|
|
|
|
|
const std::vector<const char*> orientationLabels = {"Portrait", "Landscape CW", "Inverted", "Landscape CCW"};
|
2026-02-05 15:17:51 +03:00
|
|
|
int currentPage = 0;
|
|
|
|
|
int totalPages = 0;
|
|
|
|
|
int bookProgressPercent = 0;
|
2026-02-01 08:34:30 +01:00
|
|
|
|
2026-02-05 14:53:35 +03:00
|
|
|
const std::function<void(uint8_t)> onBack;
|
2026-02-01 08:34:30 +01:00
|
|
|
const std::function<void(MenuAction)> onAction;
|
|
|
|
|
|
|
|
|
|
static void taskTrampoline(void* param);
|
|
|
|
|
[[noreturn]] void displayTaskLoop();
|
|
|
|
|
void renderScreen();
|
|
|
|
|
};
|