mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2025-12-18 15:17:42 +03:00
Add directory picking to home screen
This commit is contained in:
parent
72aa7ba3f6
commit
2631613b8d
@ -8,26 +8,30 @@ void FileSelectionScreen::taskTrampoline(void* param) {
|
|||||||
self->displayTaskLoop();
|
self->displayTaskLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileSelectionScreen::onEnter() {
|
void FileSelectionScreen::loadFiles() {
|
||||||
files.clear();
|
files.clear();
|
||||||
auto root = SD.open("/");
|
selectorIndex = 0;
|
||||||
File file;
|
auto root = SD.open(basepath.c_str());
|
||||||
while ((file = root.openNextFile())) {
|
for (File file = root.openNextFile(); file; file = root.openNextFile()) {
|
||||||
if (file.isDirectory()) {
|
|
||||||
file.close();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto filename = std::string(file.name());
|
auto filename = std::string(file.name());
|
||||||
if (filename.substr(filename.length() - 5) != ".epub" || filename[0] == '.') {
|
if (filename[0] == '.') {
|
||||||
file.close();
|
file.close();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
files.emplace_back(filename);
|
if (file.isDirectory()) {
|
||||||
|
files.emplace_back(filename + "/");
|
||||||
|
} else if (filename.substr(filename.length() - 5) == ".epub") {
|
||||||
|
files.emplace_back(filename);
|
||||||
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
root.close();
|
root.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSelectionScreen::onEnter() {
|
||||||
|
basepath = "/";
|
||||||
|
loadFiles();
|
||||||
|
|
||||||
// Trigger first update
|
// Trigger first update
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
@ -53,8 +57,23 @@ void FileSelectionScreen::handleInput(const Input input) {
|
|||||||
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
|
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
} else if (input.button == CONFIRM) {
|
} else if (input.button == CONFIRM) {
|
||||||
Serial.printf("Selected file: %s\n", files[selectorIndex].c_str());
|
if (files.empty()) {
|
||||||
onSelect("/" + files[selectorIndex]);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (files[selectorIndex].back() == '/') {
|
||||||
|
if (basepath.back() != '/') basepath += "/";
|
||||||
|
basepath += files[selectorIndex].substr(0, files[selectorIndex].length() - 1);
|
||||||
|
loadFiles();
|
||||||
|
updateRequired = true;
|
||||||
|
} else {
|
||||||
|
onSelect(basepath + files[selectorIndex]);
|
||||||
|
}
|
||||||
|
} else if (input.button == BACK && basepath != "/") {
|
||||||
|
basepath = basepath.substr(0, basepath.rfind('/'));
|
||||||
|
if (basepath.empty()) basepath = "/";
|
||||||
|
loadFiles();
|
||||||
|
updateRequired = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,12 +94,16 @@ void FileSelectionScreen::render() const {
|
|||||||
const auto titleWidth = renderer->getTextWidth("CrossPoint Reader", true);
|
const auto titleWidth = renderer->getTextWidth("CrossPoint Reader", true);
|
||||||
renderer->drawText((pageWidth - titleWidth) / 2, 0, "CrossPoint Reader", true);
|
renderer->drawText((pageWidth - titleWidth) / 2, 0, "CrossPoint Reader", true);
|
||||||
|
|
||||||
// Draw selection
|
if (files.empty()) {
|
||||||
renderer->fillRect(0, 50 + selectorIndex * 20 + 2, pageWidth - 1, 20, 1);
|
renderer->drawSmallText(50, 50, "No EPUBs found");
|
||||||
|
} else {
|
||||||
|
// Draw selection
|
||||||
|
renderer->fillRect(0, 50 + selectorIndex * 20 + 2, pageWidth - 1, 20, 1);
|
||||||
|
|
||||||
for (size_t i = 0; i < files.size(); i++) {
|
for (size_t i = 0; i < files.size(); i++) {
|
||||||
const auto file = files[i];
|
const auto file = files[i];
|
||||||
renderer->drawSmallText(50, 50 + i * 20, file.c_str(), i == selectorIndex ? 0 : 1);
|
renderer->drawSmallText(50, 50 + i * 20, file.c_str(), i == selectorIndex ? 0 : 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer->flushDisplay();
|
renderer->flushDisplay();
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
class FileSelectionScreen final : public Screen {
|
class FileSelectionScreen final : public Screen {
|
||||||
TaskHandle_t displayTaskHandle = nullptr;
|
TaskHandle_t displayTaskHandle = nullptr;
|
||||||
|
std::string basepath = "/";
|
||||||
std::vector<std::string> files;
|
std::vector<std::string> files;
|
||||||
int selectorIndex = 0;
|
int selectorIndex = 0;
|
||||||
bool updateRequired = false;
|
bool updateRequired = false;
|
||||||
@ -18,8 +19,9 @@ class FileSelectionScreen final : public Screen {
|
|||||||
static void taskTrampoline(void* param);
|
static void taskTrampoline(void* param);
|
||||||
[[noreturn]] void displayTaskLoop();
|
[[noreturn]] void displayTaskLoop();
|
||||||
void render() const;
|
void render() const;
|
||||||
|
void loadFiles();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit FileSelectionScreen(EpdRenderer* renderer, const std::function<void(const std::string&)>& onSelect)
|
explicit FileSelectionScreen(EpdRenderer* renderer, const std::function<void(const std::string&)>& onSelect)
|
||||||
: Screen(renderer), onSelect(onSelect) {}
|
: Screen(renderer), onSelect(onSelect) {}
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user