forked from crosspoint-reader/crosspoint-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEpubReaderBookmarksActivity.cpp
More file actions
265 lines (230 loc) · 10.2 KB
/
Copy pathEpubReaderBookmarksActivity.cpp
File metadata and controls
265 lines (230 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "EpubReaderBookmarksActivity.h"
#include <GfxRenderer.h>
#include <I18n.h>
#include <algorithm>
#include "../../util/BookmarkFile.h"
#include "MappedInputManager.h"
#include "components/UITheme.h"
#include "fontIds.h"
namespace {
constexpr int ENTER_DELETE_MODE_MS = 700;
// Layout constants used in renderScreen
constexpr int LINE_HEIGHT = 60;
} // namespace
void EpubReaderBookmarksActivity::onEnter() {
Activity::onEnter();
if (!epub) {
return;
}
if (!BookmarkFile::load(epubPath, bookmarks)) {
bookmarks.shrink_to_fit();
}
LOG_DBG("EPB", "Loaded %d bookmarks for book: %s", static_cast<int>(bookmarks.size()), epubPath.c_str());
// Trigger first update
requestUpdate();
}
void EpubReaderBookmarksActivity::onExit() { Activity::onExit(); }
int EpubReaderBookmarksActivity::getGutterBottom(const GfxRenderer& renderer) {
const auto orientation = renderer.getOrientation();
const bool isPortrait = orientation == GfxRenderer::Orientation::Portrait;
return isPortrait ? 75 : 40; // Reserve vertical space for button hints at the bottom
}
int EpubReaderBookmarksActivity::getListHeight(const GfxRenderer& renderer) {
const auto pageHeight = renderer.getScreenHeight();
return pageHeight - getGutterBottom(renderer) - LINE_HEIGHT; // Reserve vertical space for title and button hints
}
void EpubReaderBookmarksActivity::loop() {
auto openBookmark = [this] {
if (bookmarks.empty()) {
return;
}
auto bookmark = bookmarks.at(selectorIndex);
ProgressChangeResult result{};
result.xpath = bookmark.xpath;
result.percentage = bookmark.percentage;
result.hasSavedProgress = true;
result.hasVisibleTextOffset = bookmark.hasVisibleTextOffset;
result.visibleTextOffset = bookmark.visibleTextOffset;
// The offset is spine-relative, so carry its spine even when the legacy page hints below
// are stale. The reader validates the index.
result.spineIndex = bookmark.computedSpineIndex;
if (bookmark.computedChapterPageCount > 0 && bookmark.computedChapterProgress < bookmark.computedChapterPageCount &&
bookmark.computedSpineIndex < epub->getSpineItemsCount()) {
result.page = bookmark.computedChapterProgress;
result.totalPages = bookmark.computedChapterPageCount;
}
setResult(std::move(result));
finish();
};
// Delete confirmation popup
if (confirmPopup.handleInput(mappedInput, [this] { requestUpdate(); })) return;
if (confirmingDelete) {
// Popup dismissed without a selection (Back button or tap outside): cancel delete
confirmingDelete = false;
requestUpdate();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
const auto orientation = renderer.getOrientation();
const bool isPortraitInverted = orientation == GfxRenderer::Orientation::PortraitInverted;
const bool isLandscapeCw = orientation == GfxRenderer::Orientation::LandscapeClockwise;
const bool isLandscapeCcw = orientation == GfxRenderer::Orientation::LandscapeCounterClockwise;
const int hintGutterWidth = (isLandscapeCw || isLandscapeCcw) ? 40 : 0;
const int contentX = isLandscapeCw ? hintGutterWidth : 0;
const int contentWidth = renderer.getScreenWidth() - hintGutterWidth;
const int contentY = isPortraitInverted ? 50 : 0;
const int listY = contentY + LINE_HEIGHT;
const int listHeight = getListHeight(renderer);
int tapped = 0;
int tx = 0;
int ty = 0;
if (mappedInput.wasScreenTouchDown(tx, ty) && tx >= contentX && tx < contentX + contentWidth &&
mappedInput.wasListItemTouchedDown(tapped, static_cast<int>(bookmarks.size()), selectorIndex, listY, listHeight,
true)) {
if (selectorIndex != tapped) {
selectorIndex = tapped;
requestUpdate();
}
return;
}
if (mappedInput.wasScreenTapped(tx, ty) && tx >= contentX && tx < contentX + contentWidth &&
mappedInput.wasListItemTapped(tapped, static_cast<int>(bookmarks.size()), selectorIndex, listY, listHeight,
true)) {
selectorIndex = tapped;
openBookmark();
return;
}
const auto swipe = mappedInput.wasSwipe();
if (swipe == MappedInputManager::SwipeDir::Up && !bookmarks.empty()) {
selectorIndex =
ButtonNavigator::nextPageIndex(selectorIndex, bookmarks.size(), GUI.getListPageItems(listHeight, true));
requestUpdate();
return;
}
if (swipe == MappedInputManager::SwipeDir::Down && !bookmarks.empty()) {
selectorIndex =
ButtonNavigator::previousPageIndex(selectorIndex, bookmarks.size(), GUI.getListPageItems(listHeight, true));
requestUpdate();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { // Open
openBookmark();
return;
}
if (mappedInput.isPressed(MappedInputManager::Button::Confirm) && mappedInput.getHeldTime() > ENTER_DELETE_MODE_MS) {
if (bookmarks.empty()) {
return;
}
confirmingDelete = true;
const char* options[] = {tr(STR_CANCEL), tr(STR_DELETE)};
confirmPopup.show(tr(STR_CONFIRM_DELETE_BOOKMARK), options, 2, 0, [this](int idx) {
confirmingDelete = false;
if (idx == 1) {
deleteSelectedBookmark();
}
requestUpdate();
});
requestUpdate();
}
buttonNavigator.onNextRelease([this] {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, bookmarks.size());
requestUpdate();
});
buttonNavigator.onPreviousRelease([this] {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, bookmarks.size());
requestUpdate();
});
buttonNavigator.onNextContinuous([this] {
selectorIndex = ButtonNavigator::nextPageIndex(selectorIndex, bookmarks.size(),
GUI.getListPageItems(getListHeight(renderer), true));
requestUpdate();
});
buttonNavigator.onPreviousContinuous([this] {
selectorIndex = ButtonNavigator::previousPageIndex(selectorIndex, bookmarks.size(),
GUI.getListPageItems(getListHeight(renderer), true));
requestUpdate();
});
}
void EpubReaderBookmarksActivity::deleteSelectedBookmark() {
bookmarks.erase(bookmarks.begin() + selectorIndex);
if (!BookmarkFile::save(epubPath, bookmarks)) {
LOG_ERR("EPB", "Failed to save bookmarks after delete");
}
// Move selector up if we deleted the last item
if (selectorIndex >= bookmarks.size() && selectorIndex > 0) {
selectorIndex--;
}
if (bookmarks.empty()) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
}
}
void EpubReaderBookmarksActivity::render(RenderLock&&) {
renderer.clearScreen();
const auto pageWidth = renderer.getScreenWidth();
const auto pageHeight = renderer.getScreenHeight();
const auto orientation = renderer.getOrientation();
// Landscape orientation: reserve a horizontal gutter for button hints.
const bool isLandscapeCw = orientation == GfxRenderer::Orientation::LandscapeClockwise;
const bool isLandscapeCcw = orientation == GfxRenderer::Orientation::LandscapeCounterClockwise;
// Inverted portrait: reserve vertical space for hints at the top.
const bool isPortraitInverted = orientation == GfxRenderer::Orientation::PortraitInverted;
const bool isPortrait = orientation == GfxRenderer::Orientation::Portrait;
const int hintGutterWidth = (isLandscapeCw || isLandscapeCcw) ? 40 : 0;
// Landscape CW places hints on the left edge; CCW keeps them on the right.
const int contentX = isLandscapeCw ? hintGutterWidth : 0;
const int contentWidth = pageWidth - hintGutterWidth;
const int hintGutterHeight = isPortraitInverted ? 50 : 0;
const int hintGutterBottom = getGutterBottom(renderer);
const int contentY = hintGutterHeight;
const int listY = contentY + LINE_HEIGHT; // Reserve vertical space for title
const int listHeight = getListHeight(renderer);
const int numBookmarks = bookmarks.size();
// Manual centering to honor content gutters.
const int titleX =
contentX + (contentWidth - renderer.getTextWidth(UI_12_FONT_ID, tr(STR_BOOKMARKS), EpdFontFamily::BOLD)) / 2;
renderer.drawText(UI_12_FONT_ID, titleX, 15 + contentY, tr(STR_BOOKMARKS), true, EpdFontFamily::BOLD);
const auto getBookmarkTitle = [this](int index) {
return bookmarks.at(confirmingDelete ? selectorIndex : index).summary;
};
const auto getBookmarkSubtitle = [this](int index) {
auto bookmark = bookmarks.at(confirmingDelete ? selectorIndex : index);
auto tocIndex = epub->getTocIndexForSpineIndex(bookmark.computedSpineIndex);
auto tocTitle = (tocIndex >= 0) ? (epub->getTocItem(tocIndex)).title : tr(STR_UNNAMED);
std::string subtitle = std::to_string((int)(std::clamp(bookmark.percentage, 0.0f, 1.0f) * 100.0f + 0.5f)) + "% - ";
if (bookmark.computedChapterPageCount > 0) {
subtitle += std::to_string(bookmark.computedChapterProgress + 1) + "/" +
std::to_string(bookmark.computedChapterPageCount) + " - ";
}
return subtitle + tocTitle;
};
const auto getBookmarkIcon = [isPortrait](int index) {
// only enabled icon in portrait mode due to limitation with rotating icons for other orientations
return isPortrait ? UIIcon::Bookmark : UIIcon::None;
};
if (numBookmarks > 0) {
if (confirmingDelete) {
// Render just the selected item near the top; the confirmation popup occupies the center
GUI.drawList(renderer, Rect{contentX, listY, contentWidth, LINE_HEIGHT}, 1, 0, getBookmarkTitle,
getBookmarkSubtitle, getBookmarkIcon);
} else {
GUI.drawList(renderer, Rect{contentX, listY, contentWidth, listHeight}, numBookmarks, selectorIndex,
getBookmarkTitle, getBookmarkSubtitle, getBookmarkIcon);
GUI.drawHelpText(renderer, Rect{contentX, pageHeight - hintGutterBottom, contentWidth, LINE_HEIGHT},
tr(STR_HOLD_OPEN_TO_DELETE));
}
}
if (confirmPopup.processRender(renderer, mappedInput)) return;
const auto confirmLabel = bookmarks.size() > 0 ? tr(STR_SELECT) : "";
const auto labels = mappedInput.mapLabels(tr(STR_BACK), confirmLabel, tr(STR_DIR_UP), tr(STR_DIR_DOWN));
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
}