Refactor duplicate/stale button handler

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
jpirnay
2026-04-27 13:42:39 +02:00
co-authored by Copilot
parent 94b6cd78e4
commit e1d623bd9c
6 changed files with 289 additions and 352 deletions
+97 -98
View File
@@ -141,110 +141,109 @@ void FileBrowserActivity::clearFileMetadata(const std::string& fullPath) {
void FileBrowserActivity::loop() {
const int pageItems = UITheme::getInstance().getNumberOfItemsPerPage(renderer, true, false, true, false);
// Long press BACK always navigates to home
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= GO_HOME_MS) {
onGoHome();
return;
}
// Short press BACK goes up one directory (if not root) or home (at root)
if (mappedInput.wasReleased(MappedInputManager::Button::Back) && mappedInput.getHeldTime() < GO_HOME_MS) {
if (basepath != "/") {
const std::string oldPath = basepath;
basepath.replace(basepath.find_last_of('/'), std::string::npos, "");
if (basepath.empty()) basepath = "/";
loadFiles();
const auto pos = oldPath.find_last_of('/');
const std::string dirName = oldPath.substr(pos + 1) + "/";
const size_t idx = findEntry(dirName);
selectorIndex = (idx < files.size()) ? idx : 0;
requestUpdate();
} else {
onGoHome();
}
return;
}
// Confirm short press opens selected entry; long press does nothing
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm) && mappedInput.getHeldTime() < GO_HOME_MS) {
if (files.empty()) return;
const std::string& entry = files[selectorIndex];
const bool isDirectory = (entry.back() == '/');
if (isDirectory) {
if (basepath.back() != '/') basepath += "/";
basepath += entry.substr(0, entry.length() - 1);
loadFiles();
selectorIndex = 0;
requestUpdate();
} else {
std::string fullPath = basepath;
if (fullPath.back() != '/') fullPath += "/";
fullPath += entry;
ReturnHint hint;
hint.target = ReturnTo::FileBrowser;
hint.path = basepath;
hint.selectName = entry;
activityManager.replaceWithReader(std::move(fullPath), std::move(hint));
}
return;
}
// Left short press does nothing; long press deletes selected file after confirmation
if (mappedInput.wasReleased(MappedInputManager::Button::Left)) {
if (mappedInput.getHeldTime() < GO_HOME_MS || files.empty()) {
return;
}
const std::string& entry = files[selectorIndex];
const bool isDirectory = (entry.back() == '/');
if (isDirectory) {
return;
}
std::string cleanBase = basepath;
if (cleanBase.back() != '/') cleanBase += "/";
const std::string fullPath = cleanBase + entry;
auto handler = [this, fullPath](const ActivityResult& res) {
if (!res.isCancelled) {
LOG_DBG("FileBrowser", "Attempting to delete: %s", fullPath.c_str());
clearFileMetadata(fullPath);
if (Storage.remove(fullPath.c_str())) {
LOG_DBG("FileBrowser", "Deleted successfully");
loadFiles();
if (files.empty()) {
selectorIndex = 0;
} else if (selectorIndex >= files.size()) {
selectorIndex = files.size() - 1;
}
requestUpdate(true);
} else {
LOG_ERR("FileBrowser", "Failed to delete file: %s", fullPath.c_str());
}
} else {
LOG_DBG("FileBrowser", "Delete cancelled by user");
ButtonEventManager::ButtonEvent ev;
while (buttonEvents.consumeEvent(ev)) {
if (ev.button == MappedInputManager::Button::Back) {
if (ev.type == ButtonEventManager::PressType::Long) {
onGoHome();
return;
}
};
if (ev.type == ButtonEventManager::PressType::Short) {
if (basepath != "/") {
const std::string oldPath = basepath;
basepath.replace(basepath.find_last_of('/'), std::string::npos, "");
if (basepath.empty()) basepath = "/";
loadFiles();
const auto pos = oldPath.find_last_of('/');
const std::string dirName = oldPath.substr(pos + 1) + "/";
const size_t idx = findEntry(dirName);
selectorIndex = (idx < files.size()) ? idx : 0;
requestUpdate();
} else {
onGoHome();
}
return;
}
}
startActivityForResult(
std::make_unique<ConfirmationActivity>(renderer, mappedInput, tr(STR_DELETE) + std::string("? "), entry),
handler);
return;
}
if (ev.button == MappedInputManager::Button::Confirm && ev.type == ButtonEventManager::PressType::Short) {
if (files.empty()) return;
const std::string& entry = files[selectorIndex];
const bool isDirectory = (entry.back() == '/');
if (isDirectory) {
if (basepath.back() != '/') basepath += "/";
basepath += entry.substr(0, entry.length() - 1);
loadFiles();
selectorIndex = 0;
requestUpdate();
} else {
std::string fullPath = basepath;
if (fullPath.back() != '/') fullPath += "/";
fullPath += entry;
ReturnHint hint;
hint.target = ReturnTo::FileBrowser;
hint.path = basepath;
hint.selectName = entry;
activityManager.replaceWithReader(std::move(fullPath), std::move(hint));
}
return;
}
if (ev.button == MappedInputManager::Button::Left && ev.type == ButtonEventManager::PressType::Long) {
if (files.empty()) {
return;
}
const std::string& entry = files[selectorIndex];
const bool isDirectory = (entry.back() == '/');
if (isDirectory) {
return;
}
// Right opens the info page for epub files
if (mappedInput.wasReleased(MappedInputManager::Button::Right)) {
if (files.empty()) return;
const std::string& entry = files[selectorIndex];
if (entry.back() != '/' && (FsHelpers::hasEpubExtension(entry) || FsHelpers::hasXtcExtension(entry))) {
std::string cleanBase = basepath;
if (cleanBase.back() != '/') cleanBase += "/";
startActivityForResult(std::make_unique<BookInfoActivity>(renderer, mappedInput, cleanBase + entry),
[this](const ActivityResult&) { requestUpdate(); });
const std::string fullPath = cleanBase + entry;
auto handler = [this, fullPath](const ActivityResult& res) {
if (!res.isCancelled) {
LOG_DBG("FileBrowser", "Attempting to delete: %s", fullPath.c_str());
clearFileMetadata(fullPath);
if (Storage.remove(fullPath.c_str())) {
LOG_DBG("FileBrowser", "Deleted successfully");
loadFiles();
if (files.empty()) {
selectorIndex = 0;
} else if (selectorIndex >= files.size()) {
selectorIndex = files.size() - 1;
}
requestUpdate(true);
} else {
LOG_ERR("FileBrowser", "Failed to delete file: %s", fullPath.c_str());
}
} else {
LOG_DBG("FileBrowser", "Delete cancelled by user");
}
};
startActivityForResult(
std::make_unique<ConfirmationActivity>(renderer, mappedInput, tr(STR_DELETE) + std::string("? "), entry),
handler);
return;
}
if (ev.button == MappedInputManager::Button::Right && ev.type == ButtonEventManager::PressType::Short) {
if (files.empty()) return;
const std::string& entry = files[selectorIndex];
if (entry.back() != '/' && (FsHelpers::hasEpubExtension(entry) || FsHelpers::hasXtcExtension(entry))) {
std::string cleanBase = basepath;
if (cleanBase.back() != '/') cleanBase += "/";
startActivityForResult(std::make_unique<BookInfoActivity>(renderer, mappedInput, cleanBase + entry),
[this](const ActivityResult&) { requestUpdate(); });
}
return;
}
return;
}
// Up/Down side buttons navigate the list