Extending

This commit is contained in:
jpirnay
2026-04-14 18:23:10 +02:00
parent 4aabcc934c
commit f9f856f961
8 changed files with 177 additions and 49 deletions
+26 -16
View File
@@ -216,21 +216,24 @@ void EpubReaderActivity::loop() {
bookProgress = epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
}
const int bookProgressPercent = clampPercent(static_cast<int>(bookProgress + 0.5f));
startActivityForResult(std::make_unique<EpubReaderMenuActivity>(
renderer, mappedInput, epub->getTitle(), currentPage, totalPages, bookProgressPercent,
SETTINGS.orientation, !currentPageFootnotes.empty(), bookEmbeddedStyleOverride,
bookImageRenderingOverride, SETTINGS.textDarkness, !bookmarkStore.isEmpty()),
[this](const ActivityResult& result) {
// Always apply orientation/darkness change even if the menu was cancelled
const auto& menu = std::get<MenuResult>(result.data);
applyOrientation(menu.orientation);
applyTextDarkness(menu.textDarkness);
toggleAutoPageTurn(menu.pageTurnOption);
applyBookReaderOverrides(menu.embeddedStyleOverride, menu.imageRenderingOverride);
if (!result.isCancelled) {
onReaderMenuConfirm(static_cast<EpubReaderMenuActivity::MenuAction>(menu.action));
}
});
const bool isCurrentPageStarred = section && bookmarkStore.has(static_cast<uint16_t>(currentSpineIndex),
static_cast<uint16_t>(section->currentPage));
startActivityForResult(
std::make_unique<EpubReaderMenuActivity>(
renderer, mappedInput, epub->getTitle(), currentPage, totalPages, bookProgressPercent, SETTINGS.orientation,
!currentPageFootnotes.empty(), bookEmbeddedStyleOverride, bookImageRenderingOverride, SETTINGS.textDarkness,
!bookmarkStore.isEmpty(), isCurrentPageStarred),
[this](const ActivityResult& result) {
// Always apply orientation/darkness change even if the menu was cancelled
const auto& menu = std::get<MenuResult>(result.data);
applyOrientation(menu.orientation);
applyTextDarkness(menu.textDarkness);
toggleAutoPageTurn(menu.pageTurnOption);
applyBookReaderOverrides(menu.embeddedStyleOverride, menu.imageRenderingOverride);
if (!result.isCancelled) {
onReaderMenuConfirm(static_cast<EpubReaderMenuActivity::MenuAction>(menu.action));
}
});
}
// Long press BACK (1s+) goes to home screen
@@ -516,9 +519,16 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
requestUpdate();
break;
}
case EpubReaderMenuActivity::MenuAction::STAR_PAGE: {
if (section && section->currentPage >= 0 && section->currentPage < section->pageCount) {
bookmarkStore.toggle(static_cast<uint16_t>(currentSpineIndex), static_cast<uint16_t>(section->currentPage));
requestUpdate();
}
break;
}
case EpubReaderMenuActivity::MenuAction::STARRED_PAGES: {
startActivityForResult(
std::make_unique<StarredPagesActivity>(renderer, mappedInput, bookmarkStore.getAll(), epub),
std::make_unique<StarredPagesActivity>(renderer, mappedInput, bookmarkStore, epub),
[this](const ActivityResult& result) {
if (!result.isCancelled) {
const auto& starred = std::get<StarredPageResult>(result.data);
@@ -13,8 +13,10 @@ EpubReaderMenuActivity::EpubReaderMenuActivity(GfxRenderer& renderer, MappedInpu
const int bookProgressPercent, const uint8_t currentOrientation,
const bool hasFootnotes, const int8_t initialEmbeddedStyleOverride,
const int8_t initialImageRenderingOverride,
const uint8_t initialTextDarkness, const bool hasStarredPages)
const uint8_t initialTextDarkness, const bool hasStarredPages,
const bool isCurrentPageStarred)
: MenuListActivity("EpubReaderMenu", renderer, mappedInput),
currentPageStarred(isCurrentPageStarred),
pendingOrientation(currentOrientation),
pendingEmbeddedStyleOverride(initialEmbeddedStyleOverride),
pendingImageRenderingOverride(initialImageRenderingOverride),
@@ -33,14 +35,17 @@ void EpubReaderMenuActivity::buildMenuItems(bool hasFootnotes, bool hasStarredPa
menuItems.push_back(SettingInfo::Separator(StrId::STR_READER_NAVIGATION));
menuItems.push_back(SettingInfo::Action(StrId::STR_SELECT_CHAPTER, SettingAction::None));
menuItems.push_back(SettingInfo::Action(StrId::STR_GO_TO_PERCENT, SettingAction::None));
// Bookmarks, footnotes
menuItems.push_back(SettingInfo::Separator(StrId::STR_READER_BOOKMARKS));
menuItems.push_back(SettingInfo::Action(StrId::STR_STAR_PAGE, SettingAction::None));
if (hasStarredPages) {
menuItems.push_back(SettingInfo::Action(StrId::STR_STARRED_PAGES, SettingAction::None));
}
if (hasFootnotes) {
menuItems.push_back(SettingInfo::Action(StrId::STR_FOOTNOTES, SettingAction::None));
}
// Auto page turn: ACTION type with custom cycling in onActionSelected
menuItems.push_back(SettingInfo::Action(StrId::STR_AUTO_TURN_PAGES_PER_MIN, SettingAction::None));
// --- Appearance ---
menuItems.push_back(SettingInfo::Separator(StrId::STR_READER_APPEARANCE));
@@ -74,6 +79,10 @@ void EpubReaderMenuActivity::buildMenuItems(bool hasFootnotes, bool hasStarredPa
StrId::STR_TEXT_DARKNESS, {StrId::STR_NORMAL, StrId::STR_DARK, StrId::STR_EXTRA_DARK, StrId::STR_MAX_DARK},
[this]() -> uint8_t { return pendingTextDarkness; }, [this](uint8_t v) { pendingTextDarkness = v; }));
// Helper functions, reading ruler, auto page turn, orientation
menuItems.push_back(SettingInfo::Separator(StrId::STR_READER_UTILS));
// Auto page turn: ACTION type with custom cycling in onActionSelected
menuItems.push_back(SettingInfo::Action(StrId::STR_AUTO_TURN_PAGES_PER_MIN, SettingAction::None));
// Orientation: straightforward 0-3 cycle
menuItems.push_back(SettingInfo::DynamicEnum(
StrId::STR_ORIENTATION,
@@ -103,6 +112,8 @@ EpubReaderMenuActivity::MenuAction EpubReaderMenuActivity::actionForNameId(StrId
return MenuAction::GO_TO_PERCENT;
case StrId::STR_STARRED_PAGES:
return MenuAction::STARRED_PAGES;
case StrId::STR_STAR_PAGE:
return MenuAction::STAR_PAGE;
case StrId::STR_FOOTNOTES:
return MenuAction::FOOTNOTES;
case StrId::STR_AUTO_TURN_PAGES_PER_MIN:
@@ -178,6 +189,11 @@ std::string EpubReaderMenuActivity::getItemValueString(int index) const {
return std::string(pageTurnLabels[selectedPageTurnOption]);
}
// Star page: reflect current page's star state
if (item.nameId == StrId::STR_STAR_PAGE) {
return currentPageStarred ? std::string(tr(STR_STATE_ON)) : std::string(tr(STR_STATE_OFF));
}
// Plain ACTION items (select chapter, screenshot, etc.) show no value
if (item.type == SettingType::ACTION) return {};
@@ -28,6 +28,7 @@ class EpubReaderMenuActivity final : public MenuListActivity {
PUSH_LOCAL,
SYNC,
STARRED_PAGES,
STAR_PAGE,
DELETE_CACHE
};
@@ -35,14 +36,16 @@ class EpubReaderMenuActivity final : public MenuListActivity {
const int currentPage, const int totalPages, const int bookProgressPercent,
const uint8_t currentOrientation, const bool hasFootnotes,
const int8_t initialEmbeddedStyleOverride, const int8_t initialImageRenderingOverride,
const uint8_t initialTextDarkness,
const bool hasStarredPages);
const uint8_t initialTextDarkness, const bool hasStarredPages,
const bool isCurrentPageStarred);
void onEnter() override;
void render(RenderLock&&) override;
private:
void buildMenuItems(bool hasFootnotes, bool hasStarredPages);
bool currentPageStarred = false;
void finishWithAction(MenuAction action);
// MenuListActivity overrides
+72 -19
View File
@@ -4,6 +4,7 @@
#include <I18n.h>
#include "MappedInputManager.h"
#include "activities/util/KeyboardEntryActivity.h"
#include "components/UITheme.h"
#include "fontIds.h"
@@ -18,40 +19,81 @@ int StarredPagesActivity::getPageItems() const {
return std::max(1, availableHeight / lineHeight);
}
std::string StarredPagesActivity::getItemLabel(int index) const {
const auto& bm = bookmarks[index];
std::string StarredPagesActivity::getDefaultLabel(int index) const {
const auto& bm = bookmarkStore.getAll()[index];
char buf[64];
if (epub) {
// Try to get chapter title from TOC
const int tocIndex = epub->getTocIndexForSpineIndex(bm.spineIndex);
if (tocIndex != -1) {
const auto tocItem = epub->getTocItem(tocIndex);
snprintf(buf, sizeof(buf), "%d. ", index + 1);
return std::string(buf) + tocItem.title + " - " + tr(STR_PAGE_PREFIX) + std::to_string(bm.pageNumber + 1);
return tocItem.title + " - " + tr(STR_PAGE_PREFIX) + std::to_string(bm.pageNumber + 1);
}
snprintf(buf, sizeof(buf), "%d. %s%d, %s%d", index + 1, tr(STR_SECTION_PREFIX), bm.spineIndex + 1,
tr(STR_PAGE_PREFIX), bm.pageNumber + 1);
snprintf(buf, sizeof(buf), "%s%d, %s%d", tr(STR_SECTION_PREFIX), bm.spineIndex + 1, tr(STR_PAGE_PREFIX),
bm.pageNumber + 1);
} else {
// TXT file: just page number (spineIndex is always 0)
snprintf(buf, sizeof(buf), "%d. %s%d", index + 1, tr(STR_PAGE_PREFIX), bm.pageNumber + 1);
snprintf(buf, sizeof(buf), "%s%d", tr(STR_PAGE_PREFIX), bm.pageNumber + 1);
}
return std::string(buf);
}
std::string StarredPagesActivity::getItemLabel(int index) const {
char prefix[16];
snprintf(prefix, sizeof(prefix), "%d. ", index + 1);
const auto& bm = bookmarkStore.getAll()[index];
return std::string(prefix) + (bm.name.empty() ? getDefaultLabel(index) : bm.name);
}
void StarredPagesActivity::onEnter() {
Activity::onEnter();
requestUpdate();
}
void StarredPagesActivity::onExit() { Activity::onExit(); }
void StarredPagesActivity::onExit() {
bookmarkStore.save();
Activity::onExit();
}
void StarredPagesActivity::startRename() {
const auto& all = bookmarkStore.getAll();
if (all.empty() || selectorIndex >= static_cast<int>(all.size())) return;
const int renamingIndex = selectorIndex;
const std::string initial =
all[renamingIndex].name.empty() ? getDefaultLabel(renamingIndex) : all[renamingIndex].name;
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_RENAME), initial,
BookmarkStore::MAX_NAME_LENGTH, false),
[this, renamingIndex](const ActivityResult& result) {
if (!result.isCancelled) {
const auto& kr = std::get<KeyboardResult>(result.data);
bookmarkStore.rename(renamingIndex, kr.text);
}
requestUpdate();
});
}
void StarredPagesActivity::deleteSelected() {
const auto& all = bookmarkStore.getAll();
if (all.empty() || selectorIndex >= static_cast<int>(all.size())) return;
bookmarkStore.removeAt(selectorIndex);
const int remaining = static_cast<int>(bookmarkStore.getAll().size());
if (remaining == 0) {
// Nothing left — drop back to the reader.
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
if (selectorIndex >= remaining) selectorIndex = remaining - 1;
requestUpdate();
}
void StarredPagesActivity::loop() {
const int totalItems = static_cast<int>(bookmarks.size());
const int totalItems = static_cast<int>(bookmarkStore.getAll().size());
const int pageItems = getPageItems();
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (!bookmarks.empty()) {
const auto& bm = bookmarks[selectorIndex];
if (totalItems > 0) {
const auto& bm = bookmarkStore.getAll()[selectorIndex];
setResult(StarredPageResult{bm.spineIndex, bm.pageNumber});
finish();
}
@@ -66,22 +108,33 @@ void StarredPagesActivity::loop() {
return;
}
buttonNavigator.onNextRelease([this, totalItems] {
if (mappedInput.wasReleased(MappedInputManager::Button::Left)) {
startRename();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Right)) {
deleteSelected();
return;
}
// Side buttons (Up/Down) drive list navigation; Left/Right are reserved for rename/delete.
buttonNavigator.onRelease({MappedInputManager::Button::Down}, [this, totalItems] {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
requestUpdate();
});
buttonNavigator.onPreviousRelease([this, totalItems] {
buttonNavigator.onRelease({MappedInputManager::Button::Up}, [this, totalItems] {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
requestUpdate();
});
buttonNavigator.onNextContinuous([this, totalItems, pageItems] {
buttonNavigator.onContinuous({MappedInputManager::Button::Down}, [this, totalItems, pageItems] {
selectorIndex = ButtonNavigator::nextPageIndex(selectorIndex, totalItems, pageItems);
requestUpdate();
});
buttonNavigator.onPreviousContinuous([this, totalItems, pageItems] {
buttonNavigator.onContinuous({MappedInputManager::Button::Up}, [this, totalItems, pageItems] {
selectorIndex = ButtonNavigator::previousPageIndex(selectorIndex, totalItems, pageItems);
requestUpdate();
});
@@ -90,7 +143,7 @@ void StarredPagesActivity::loop() {
void StarredPagesActivity::render(RenderLock&&) {
renderer.clearScreen();
const int totalItems = static_cast<int>(bookmarks.size());
const int totalItems = static_cast<int>(bookmarkStore.getAll().size());
if (totalItems == 0) {
renderer.drawCenteredText(UI_12_FONT_ID, 300, tr(STR_NO_STARRED_PAGES), true, EpdFontFamily::BOLD);
@@ -130,7 +183,7 @@ void StarredPagesActivity::render(RenderLock&&) {
renderer.drawText(UI_10_FONT_ID, contentX + 20, displayY, label.c_str(), !isSelected);
}
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SELECT), tr(STR_DIR_UP), tr(STR_DIR_DOWN));
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SELECT), tr(STR_RENAME), tr(STR_DELETE));
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
+6 -4
View File
@@ -11,18 +11,20 @@
class StarredPagesActivity final : public Activity {
std::shared_ptr<Epub> epub; // nullptr for TXT files
const std::vector<BookmarkStore::Bookmark> bookmarks;
BookmarkStore& bookmarkStore;
ButtonNavigator buttonNavigator;
int selectorIndex = 0;
int getPageItems() const;
std::string getItemLabel(int index) const;
std::string getDefaultLabel(int index) const;
void startRename();
void deleteSelected();
public:
explicit StarredPagesActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::vector<BookmarkStore::Bookmark>& bookmarks,
explicit StarredPagesActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, BookmarkStore& bookmarkStore,
std::shared_ptr<Epub> epub = nullptr)
: Activity("StarredPages", renderer, mappedInput), epub(std::move(epub)), bookmarks(bookmarks) {}
: Activity("StarredPages", renderer, mappedInput), epub(std::move(epub)), bookmarkStore(bookmarkStore) {}
void onEnter() override;
void onExit() override;
void loop() override;
+1 -1
View File
@@ -160,7 +160,7 @@ void TxtReaderActivity::loop() {
// Open starred pages list via Confirm button
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm) && !bookmarkStore.isEmpty()) {
startActivityForResult(std::make_unique<StarredPagesActivity>(renderer, mappedInput, bookmarkStore.getAll()),
startActivityForResult(std::make_unique<StarredPagesActivity>(renderer, mappedInput, bookmarkStore),
[this](const ActivityResult& result) {
if (!result.isCancelled) {
const auto& starred = std::get<StarredPageResult>(result.data);