diff --git a/lib/FsHelpers/FsHelpers.cpp b/lib/FsHelpers/FsHelpers.cpp index 08ca44606..616b094b5 100644 --- a/lib/FsHelpers/FsHelpers.cpp +++ b/lib/FsHelpers/FsHelpers.cpp @@ -78,4 +78,12 @@ bool hasTxtExtension(std::string_view fileName) { return checkFileExtension(file bool hasMarkdownExtension(std::string_view fileName) { return checkFileExtension(fileName, ".md"); } +std::string extractFolderPath(const std::string& filePath) { + const auto lastSlash = filePath.find_last_of('/'); + if (lastSlash == std::string::npos || lastSlash == 0) { + return "/"; + } + return filePath.substr(0, lastSlash); +} + } // namespace FsHelpers diff --git a/lib/FsHelpers/FsHelpers.h b/lib/FsHelpers/FsHelpers.h index a21135127..f8af636a0 100644 --- a/lib/FsHelpers/FsHelpers.h +++ b/lib/FsHelpers/FsHelpers.h @@ -55,4 +55,6 @@ inline bool hasTxtExtension(const String& fileName) { // Check for .md extension (case-insensitive) bool hasMarkdownExtension(std::string_view fileName); +std::string extractFolderPath(const std::string& filePath); + } // namespace FsHelpers diff --git a/src/activities/home/FileBrowserActivity.cpp b/src/activities/home/FileBrowserActivity.cpp index 9ac714ebb..723a3e199 100644 --- a/src/activities/home/FileBrowserActivity.cpp +++ b/src/activities/home/FileBrowserActivity.cpp @@ -108,9 +108,28 @@ void FileBrowserActivity::loadFiles() { void FileBrowserActivity::onEnter() { Activity::onEnter(); - loadFiles(); selectorIndex = 0; + auto root = Storage.open(basepath.c_str()); + if (!root) { + basepath = "/"; + loadFiles(); + } else if (!root.isDirectory()) { + root.close(); + lockLongPressBack = mappedInput.isPressed(MappedInputManager::Button::Back); + + const std::string oldPath = basepath; + basepath = FsHelpers::extractFolderPath(basepath); + loadFiles(); + + const auto pos = oldPath.find_last_of('/'); + const std::string fileName = oldPath.substr(pos + 1); + selectorIndex = findEntry(fileName); + } else { + root.close(); + loadFiles(); + } + requestUpdate(); } @@ -129,11 +148,19 @@ void FileBrowserActivity::clearFileMetadata(const std::string& fullPath) { void FileBrowserActivity::loop() { // Long press BACK (1s+) goes to root folder + // but Long press BACK (1s+) from ReaderActivity sends us here with the MappedInput already set. + // So ignore it the first time. if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= GO_HOME_MS && - basepath != "/") { + basepath != "/" && !lockLongPressBack) { basepath = "/"; loadFiles(); selectorIndex = 0; + requestUpdate(); + return; + } + + if (lockLongPressBack && mappedInput.wasReleased(MappedInputManager::Button::Back)) { + lockLongPressBack = false; return; } diff --git a/src/activities/home/FileBrowserActivity.h b/src/activities/home/FileBrowserActivity.h index c4f359ddf..a8235088b 100644 --- a/src/activities/home/FileBrowserActivity.h +++ b/src/activities/home/FileBrowserActivity.h @@ -17,6 +17,8 @@ class FileBrowserActivity final : public Activity { size_t selectorIndex = 0; + bool lockLongPressBack = false; + // Files state std::string basepath = "/"; std::vector files; diff --git a/src/activities/reader/ReaderActivity.cpp b/src/activities/reader/ReaderActivity.cpp index 2164a7f66..989e6d223 100644 --- a/src/activities/reader/ReaderActivity.cpp +++ b/src/activities/reader/ReaderActivity.cpp @@ -13,14 +13,6 @@ #include "activities/util/BmpViewerActivity.h" #include "activities/util/FullScreenMessageActivity.h" -std::string ReaderActivity::extractFolderPath(const std::string& filePath) { - const auto lastSlash = filePath.find_last_of('/'); - if (lastSlash == std::string::npos || lastSlash == 0) { - return "/"; - } - return filePath.substr(0, lastSlash); -} - bool ReaderActivity::isXtcFile(const std::string& path) { return FsHelpers::hasXtcExtension(path); } bool ReaderActivity::isTxtFile(const std::string& path) { @@ -77,7 +69,7 @@ std::unique_ptr ReaderActivity::loadTxt(const std::string& path) { void ReaderActivity::goToLibrary(const std::string& fromBookPath) { // If coming from a book, start in that book's folder; otherwise start from root - auto initialPath = fromBookPath.empty() ? "/" : extractFolderPath(fromBookPath); + auto initialPath = fromBookPath.empty() ? "/" : FsHelpers::extractFolderPath(fromBookPath); activityManager.goToFileBrowser(std::move(initialPath)); } diff --git a/src/activities/reader/ReaderActivity.h b/src/activities/reader/ReaderActivity.h index 6a3756db2..f5c61a393 100644 --- a/src/activities/reader/ReaderActivity.h +++ b/src/activities/reader/ReaderActivity.h @@ -18,7 +18,6 @@ class ReaderActivity final : public Activity { static bool isTxtFile(const std::string& path); static bool isBmpFile(const std::string& path); - static std::string extractFolderPath(const std::string& filePath); void goToLibrary(const std::string& fromBookPath = ""); void onGoToEpubReader(std::unique_ptr epub); void onGoToXtcReader(std::unique_ptr xtc); diff --git a/src/activities/util/BmpViewerActivity.cpp b/src/activities/util/BmpViewerActivity.cpp index 51fd12b10..62a02ce67 100644 --- a/src/activities/util/BmpViewerActivity.cpp +++ b/src/activities/util/BmpViewerActivity.cpp @@ -94,7 +94,7 @@ void BmpViewerActivity::loop() { Activity::loop(); if (mappedInput.wasReleased(MappedInputManager::Button::Back)) { - onGoHome(); + activityManager.goToFileBrowser(filePath); return; } } \ No newline at end of file