Refactor ActivityManager calls

This commit is contained in:
jpirnay
2026-04-17 20:23:42 +02:00
parent ef088db789
commit b4881e1563
12 changed files with 156 additions and 45 deletions
+15 -1
View File
@@ -8,6 +8,7 @@
#include <algorithm>
#include "../ActivityManager.h"
#include "../util/ConfirmationActivity.h"
#include "BookInfoActivity.h"
#include "CrossPointSettings.h"
@@ -113,6 +114,14 @@ void FileBrowserActivity::onEnter() {
loadFiles();
selectorIndex = 0;
if (!focusName.empty()) {
const size_t idx = findEntry(focusName);
if (idx < files.size()) {
selectorIndex = idx;
}
focusName.clear();
}
requestUpdate();
}
@@ -171,7 +180,12 @@ void FileBrowserActivity::loop() {
} else {
std::string fullPath = basepath;
if (fullPath.back() != '/') fullPath += "/";
onSelectBook(fullPath + entry);
fullPath += entry;
ReturnHint hint;
hint.target = ReturnTo::FileBrowser;
hint.path = basepath;
hint.selectName = entry;
activityManager.replaceWithReader(std::move(fullPath), std::move(hint));
}
return;
}
+6 -2
View File
@@ -19,6 +19,7 @@ class FileBrowserActivity final : public Activity {
// Files state
std::string basepath = "/";
std::string focusName; // entry to select on first load (e.g. the file just returned from)
std::vector<std::string> files;
// Data loading
@@ -26,8 +27,11 @@ class FileBrowserActivity final : public Activity {
size_t findEntry(const std::string& name) const;
public:
explicit FileBrowserActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string initialPath = "/")
: Activity("FileBrowser", renderer, mappedInput), basepath(initialPath.empty() ? "/" : std::move(initialPath)) {}
explicit FileBrowserActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string initialPath = "/",
std::string focusName = {})
: Activity("FileBrowser", renderer, mappedInput),
basepath(initialPath.empty() ? "/" : std::move(initialPath)),
focusName(std::move(focusName)) {}
void onEnter() override;
void onExit() override;
void loop() override;
@@ -9,6 +9,7 @@
#include <algorithm>
#include <cstdio>
#include "../ActivityManager.h"
#include "BookmarkStore.h"
#include "CrossPointState.h"
#include "GlobalBookmarkIndex.h"
@@ -124,7 +125,10 @@ void GlobalBookmarksActivity::openSelected() {
APP_STATE.saveToFile();
LOG_DBG("GBA", "Jumping to bookmark in %s at %u/%u", entry.sourcePath.c_str(), bm.spineIndex, bm.pageNumber);
onSelectBook(entry.sourcePath);
ReturnHint hint;
hint.target = ReturnTo::Home;
hint.selectName = entry.sourcePath;
activityManager.replaceWithReader(entry.sourcePath, std::move(hint));
}
template <typename Op>
+16 -1
View File
@@ -211,6 +211,16 @@ void HomeActivity::onEnter() {
recentsLoaded = true;
}
if (!focusBookPath.empty()) {
for (size_t i = 0; i < recentBooks.size(); ++i) {
if (recentBooks[i].path == focusBookPath) {
selectorIndex = static_cast<int>(i);
break;
}
}
focusBookPath.clear();
}
// Trigger first update
menuEntriesDirty = true;
requestUpdate();
@@ -348,7 +358,12 @@ void HomeActivity::render(RenderLock&&) {
}
}
void HomeActivity::onSelectBook(const std::string& path) { activityManager.pushReader(path); }
void HomeActivity::onSelectBook(const std::string& path) {
ReturnHint hint;
hint.target = ReturnTo::Home;
hint.selectName = path; // used to re-focus the book in the recents strip after return
activityManager.replaceWithReader(path, std::move(hint));
}
void HomeActivity::dispatchMenuAction(MenuAction action) {
switch (action) {
+4 -2
View File
@@ -44,6 +44,8 @@ class HomeActivity final : public Activity {
std::vector<MenuEntry> menuEntries;
bool menuEntriesDirty = true;
std::string focusBookPath; // book path to re-select on first render, if present in recents
void onSelectBook(const std::string& path);
void dispatchMenuAction(MenuAction action);
@@ -55,8 +57,8 @@ class HomeActivity final : public Activity {
void loadRecentCovers(int coverHeight);
public:
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
: Activity("Home", renderer, mappedInput) {}
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string focusBookPath = {})
: Activity("Home", renderer, mappedInput), focusBookPath(std::move(focusBookPath)) {}
void onEnter() override;
void onExit() override;
void loop() override;
+9 -1
View File
@@ -7,6 +7,7 @@
#include <algorithm>
#include "../ActivityManager.h"
#include "../util/ConfirmationActivity.h"
#include "BookInfoActivity.h"
#include "MappedInputManager.h"
@@ -35,6 +36,10 @@ void RecentBooksActivity::onEnter() {
loadRecentBooks();
selectorIndex = 0;
if (initialFocusIndex >= 0 && static_cast<size_t>(initialFocusIndex) < recentBooks.size()) {
selectorIndex = static_cast<size_t>(initialFocusIndex);
}
initialFocusIndex = -1;
requestUpdate();
}
@@ -49,7 +54,10 @@ void RecentBooksActivity::loop() {
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm) && !recentBooks.empty() &&
selectorIndex < static_cast<int>(recentBooks.size())) {
LOG_DBG("RBA", "Selected recent book: %s", recentBooks[selectorIndex].path.c_str());
onSelectBook(recentBooks[selectorIndex].path);
ReturnHint hint;
hint.target = ReturnTo::RecentBooks;
hint.selectIndex = static_cast<int>(selectorIndex);
activityManager.replaceWithReader(recentBooks[selectorIndex].path, std::move(hint));
return;
}
+3 -2
View File
@@ -14,6 +14,7 @@ class RecentBooksActivity final : public Activity {
ButtonNavigator buttonNavigator;
size_t selectorIndex = 0;
int initialFocusIndex = -1; // applied once in onEnter(), then cleared
// Recent tab state
std::vector<RecentBook> recentBooks;
@@ -22,8 +23,8 @@ class RecentBooksActivity final : public Activity {
void loadRecentBooks();
public:
explicit RecentBooksActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
: Activity("RecentBooks", renderer, mappedInput) {}
explicit RecentBooksActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, int focusIndex = -1)
: Activity("RecentBooks", renderer, mappedInput), initialFocusIndex(focusIndex) {}
void onEnter() override;
void onExit() override;
void loop() override;