Review changes

This commit is contained in:
jpirnay
2026-04-06 13:47:41 +02:00
parent eb95cacad4
commit 0ba4626157
+46 -12
View File
@@ -17,6 +17,8 @@
namespace { namespace {
constexpr const char* SLEEP_BMP_PATH = "/sleep.bmp"; constexpr const char* SLEEP_BMP_PATH = "/sleep.bmp";
constexpr const char* SLEEP_BMP_TMP_PATH = "/sleep.bmp.tmp";
constexpr const char* SLEEP_BMP_BACKUP_PATH = "/sleep.bmp.bak";
uint8_t normalizeImageDitherModeValue(uint8_t mode) { return static_cast<uint8_t>(imageDitherModeFromSetting(mode)); } uint8_t normalizeImageDitherModeValue(uint8_t mode) { return static_cast<uint8_t>(imageDitherModeFromSetting(mode)); }
@@ -37,20 +39,49 @@ void computeCenteredImagePlacement(const int imageWidth, const int imageHeight,
if (ratio > screenRatio) { if (ratio > screenRatio) {
renderWidth = pageWidth; renderWidth = pageWidth;
renderHeight = std::round(static_cast<float>(pageWidth) / ratio); renderHeight = std::max(1, static_cast<int>(std::round(static_cast<float>(pageWidth) / ratio)));
x = 0;
y = std::round((static_cast<float>(pageHeight) - renderHeight) / 2.0f);
} else { } else {
renderHeight = pageHeight; renderHeight = pageHeight;
renderWidth = std::round(static_cast<float>(pageHeight) * ratio); renderWidth = std::max(1, static_cast<int>(std::round(static_cast<float>(pageHeight) * ratio)));
x = std::round((static_cast<float>(pageWidth) - renderWidth) / 2.0f);
y = 0;
} }
x = std::max(0, (pageWidth - renderWidth) / 2);
y = std::max(0, (pageHeight - renderHeight) / 2);
return; return;
} }
x = (pageWidth - imageWidth) / 2; renderWidth = std::max(1, renderWidth);
y = (pageHeight - imageHeight) / 2; renderHeight = std::max(1, renderHeight);
x = std::max(0, (pageWidth - renderWidth) / 2);
y = std::max(0, (pageHeight - renderHeight) / 2);
}
bool replaceSleepBmpFromTemp() {
const bool hadExistingTarget = Storage.exists(SLEEP_BMP_PATH);
bool movedExistingToBackup = false;
if (Storage.exists(SLEEP_BMP_BACKUP_PATH)) {
Storage.remove(SLEEP_BMP_BACKUP_PATH);
}
if (hadExistingTarget) {
movedExistingToBackup = Storage.rename(SLEEP_BMP_PATH, SLEEP_BMP_BACKUP_PATH);
if (!movedExistingToBackup) {
return false;
}
}
if (Storage.rename(SLEEP_BMP_TMP_PATH, SLEEP_BMP_PATH)) {
if (movedExistingToBackup) {
Storage.remove(SLEEP_BMP_BACKUP_PATH);
}
return true;
}
if (movedExistingToBackup) {
Storage.rename(SLEEP_BMP_BACKUP_PATH, SLEEP_BMP_PATH);
}
return false;
} }
} // namespace } // namespace
@@ -203,12 +234,15 @@ void BmpViewerActivity::setAsSleepScreen() {
} else { } else {
const bool renderedForCapture = isBmpFile(filePath) ? renderBmpImage(false) : renderDecodedImage(false); const bool renderedForCapture = isBmpFile(filePath) ? renderBmpImage(false) : renderDecodedImage(false);
if (renderedForCapture) { if (renderedForCapture) {
success = ScreenshotUtil::saveFramebufferAsBmp(SLEEP_BMP_PATH, renderer.getFrameBuffer(), Storage.remove(SLEEP_BMP_TMP_PATH);
display.getDisplayWidth(), display.getDisplayHeight()); if (ScreenshotUtil::saveFramebufferAsBmp(SLEEP_BMP_TMP_PATH, renderer.getFrameBuffer(), display.getDisplayWidth(),
display.getDisplayHeight())) {
success = replaceSleepBmpFromTemp();
}
} }
if (!success && Storage.exists(SLEEP_BMP_PATH)) { if (!success && Storage.exists(SLEEP_BMP_TMP_PATH)) {
Storage.remove(SLEEP_BMP_PATH); Storage.remove(SLEEP_BMP_TMP_PATH);
} }
} }