feat: Set sleep cover from BMP viewer (#1104)

This commit is contained in:
Eliz
2026-05-05 12:23:20 +03:00
committed by GitHub
parent f44722a0f2
commit efa4f71a68
7 changed files with 210 additions and 74 deletions
+113 -3
View File
@@ -1,19 +1,67 @@
#include "BmpViewerActivity.h"
#include <Bitmap.h>
#include <FsHelpers.h>
#include <GfxRenderer.h>
#include <HalStorage.h>
#include <I18n.h>
#include <algorithm>
#include "CrossPointSettings.h"
#include "components/UITheme.h"
#include "fontIds.h"
BmpViewerActivity::BmpViewerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string path)
: Activity("BmpViewer", renderer, mappedInput), filePath(std::move(path)) {}
void BmpViewerActivity::loadSiblingImages() {
siblingImages.clear();
currentImageIndex = -1;
if (filePath.empty()) return;
std::string dirPath = FsHelpers::extractFolderPath(filePath);
size_t lastSlash = filePath.find_last_of('/');
std::string fileName = (lastSlash != std::string::npos) ? filePath.substr(lastSlash + 1) : filePath;
auto dir = Storage.open(dirPath.c_str());
if (!dir || !dir.isDirectory()) {
if (dir) dir.close();
return;
}
char name[500];
for (auto file = dir.openNextFile(); file; file = dir.openNextFile()) {
if (!file.isDirectory()) {
file.getName(name, sizeof(name));
if (name[0] != '.') {
std::string fname(name);
if (fname.length() >= 4 && fname.substr(fname.length() - 4) == ".bmp") {
siblingImages.push_back(fname);
}
}
}
file.close();
}
dir.close();
FsHelpers::sortFileList(siblingImages);
for (size_t i = 0; i < siblingImages.size(); ++i) {
if (siblingImages[i] == fileName) {
currentImageIndex = static_cast<int>(i);
break;
}
}
}
void BmpViewerActivity::onEnter() {
Activity::onEnter();
// Removed the redundant initial renderer.clearScreen()
if (siblingImages.empty() && !filePath.empty()) {
loadSiblingImages();
}
FsFile file;
@@ -49,7 +97,8 @@ void BmpViewerActivity::onEnter() {
}
// 4. Prepare Rendering
const auto labels = mappedInput.mapLabels(tr(STR_BACK), "", "", "");
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SET_SLEEP_COVER), "", "");
GUI.fillPopupProgress(renderer, popupRect, 50);
renderer.clearScreen();
@@ -61,7 +110,7 @@ void BmpViewerActivity::onEnter() {
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
// Single pass for non-grayscale images
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
renderer.displayBuffer(HalDisplay::FAST_REFRESH);
} else {
// Handle file parsing error
@@ -89,6 +138,39 @@ void BmpViewerActivity::onExit() {
renderer.displayBuffer(HalDisplay::HALF_REFRESH);
}
void BmpViewerActivity::doSetSleepCover() {
GUI.drawPopup(renderer, tr(STR_LOADING_POPUP));
bool success = false;
FsFile inFile, outFile;
if (Storage.openFileForRead("BMP", filePath, inFile)) {
if (Storage.openFileForWrite("BMP", "/sleep.bmp", outFile)) {
char buffer[2048];
int bytesRead;
success = true;
while ((bytesRead = inFile.read(buffer, sizeof(buffer))) > 0) {
if (outFile.write(buffer, bytesRead) != bytesRead) {
success = false;
break;
}
}
outFile.close();
}
inFile.close();
}
if (success) {
SETTINGS.sleepScreen = CrossPointSettings::SLEEP_SCREEN_MODE::CUSTOM;
SETTINGS.saveToFile();
GUI.drawPopup(renderer, tr(STR_DONE));
} else {
GUI.drawPopup(renderer, tr(STR_FAILED_LOWER));
}
delay(1000);
onEnter();
}
void BmpViewerActivity::loop() {
// Keep CPU awake/polling so 1st click works
Activity::loop();
@@ -97,4 +179,32 @@ void BmpViewerActivity::loop() {
activityManager.goToFileBrowser(filePath);
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
doSetSleepCover();
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Up)) {
if (siblingImages.size() > 1 && currentImageIndex > 0) {
currentImageIndex--;
std::string dirPath = FsHelpers::extractFolderPath(filePath);
if (dirPath.back() != '/') dirPath += "/";
filePath = dirPath + siblingImages[currentImageIndex];
onEnter();
}
return;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Down)) {
if (siblingImages.size() > 1 && currentImageIndex != -1 &&
currentImageIndex < static_cast<int>(siblingImages.size()) - 1) {
currentImageIndex++;
std::string dirPath = FsHelpers::extractFolderPath(filePath);
if (dirPath.back() != '/') dirPath += "/";
filePath = dirPath + siblingImages[currentImageIndex];
onEnter();
}
return;
}
}