fix: reset clang correctly

This commit is contained in:
Brackyt 2026-01-25 17:48:57 +01:00
parent 1fab1f9ec5
commit dccd200b86
20 changed files with 720 additions and 937 deletions

View File

@ -1,8 +1,10 @@
#include "Bitmap.h" #include "Bitmap.h"
#include "BitmapHelpers.h"
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include "BitmapHelpers.h"
// ============================================================================ // ============================================================================
// IMAGE PROCESSING OPTIONS // IMAGE PROCESSING OPTIONS
// ============================================================================ // ============================================================================
@ -36,8 +38,7 @@ size_t Bitmap::readBytes(void *buf, size_t count) const {
return file->read(buf, count); return file->read(buf, count);
} else if (memoryBuffer) { } else if (memoryBuffer) {
size_t available = memorySize - bufferPos; size_t available = memorySize - bufferPos;
if (count > available) if (count > available) count = available;
count = available;
memcpy(buf, memoryBuffer + bufferPos, count); memcpy(buf, memoryBuffer + bufferPos, count);
bufferPos += count; bufferPos += count;
return count; return count;
@ -74,8 +75,7 @@ bool Bitmap::seekCur(int32_t offset) const {
uint16_t Bitmap::readLE16() { uint16_t Bitmap::readLE16() {
const int c0 = readByte(); const int c0 = readByte();
const int c1 = readByte(); const int c1 = readByte();
return static_cast<uint16_t>(c0 & 0xFF) | return static_cast<uint16_t>(c0 & 0xFF) | (static_cast<uint16_t>(c1 & 0xFF) << 8);
(static_cast<uint16_t>(c1 & 0xFF) << 8);
} }
uint32_t Bitmap::readLE32() { uint32_t Bitmap::readLE32() {
@ -83,10 +83,8 @@ uint32_t Bitmap::readLE32() {
const int c1 = readByte(); const int c1 = readByte();
const int c2 = readByte(); const int c2 = readByte();
const int c3 = readByte(); const int c3 = readByte();
return static_cast<uint32_t>(c0 & 0xFF) | return static_cast<uint32_t>(c0 & 0xFF) | (static_cast<uint32_t>(c1 & 0xFF) << 8) |
(static_cast<uint32_t>(c1 & 0xFF) << 8) | (static_cast<uint32_t>(c2 & 0xFF) << 16) | (static_cast<uint32_t>(c3 & 0xFF) << 24);
(static_cast<uint32_t>(c2 & 0xFF) << 16) |
(static_cast<uint32_t>(c3 & 0xFF) << 24);
} }
const char* Bitmap::errorToString(BmpReaderError err) { const char* Bitmap::errorToString(BmpReaderError err) {
@ -126,21 +124,17 @@ const char *Bitmap::errorToString(BmpReaderError err) {
} }
BmpReaderError Bitmap::parseHeaders() { BmpReaderError Bitmap::parseHeaders() {
if (!file && !memoryBuffer) if (!file && !memoryBuffer) return BmpReaderError::FileInvalid;
return BmpReaderError::FileInvalid; if (!seekSet(0)) return BmpReaderError::SeekStartFailed;
if (!seekSet(0))
return BmpReaderError::SeekStartFailed;
const uint16_t bfType = readLE16(); const uint16_t bfType = readLE16();
if (bfType != 0x4D42) if (bfType != 0x4D42) return BmpReaderError::NotBMP;
return BmpReaderError::NotBMP;
seekCur(8); seekCur(8);
bfOffBits = readLE32(); bfOffBits = readLE32();
const uint32_t biSize = readLE32(); const uint32_t biSize = readLE32();
if (biSize < 40) if (biSize < 40) return BmpReaderError::DIBTooSmall;
return BmpReaderError::DIBTooSmall;
width = static_cast<int32_t>(readLE32()); width = static_cast<int32_t>(readLE32());
const auto rawHeight = static_cast<int32_t>(readLE32()); const auto rawHeight = static_cast<int32_t>(readLE32());
@ -150,24 +144,18 @@ BmpReaderError Bitmap::parseHeaders() {
const uint16_t planes = readLE16(); const uint16_t planes = readLE16();
bpp = readLE16(); bpp = readLE16();
const uint32_t comp = readLE32(); const uint32_t comp = readLE32();
const bool validBpp = const bool validBpp = bpp == 1 || bpp == 2 || bpp == 8 || bpp == 24 || bpp == 32;
bpp == 1 || bpp == 2 || bpp == 8 || bpp == 24 || bpp == 32;
if (planes != 1) if (planes != 1) return BmpReaderError::BadPlanes;
return BmpReaderError::BadPlanes; if (!validBpp) return BmpReaderError::UnsupportedBpp;
if (!validBpp) if (!(comp == 0 || (bpp == 32 && comp == 3))) return BmpReaderError::UnsupportedCompression;
return BmpReaderError::UnsupportedBpp;
if (!(comp == 0 || (bpp == 32 && comp == 3)))
return BmpReaderError::UnsupportedCompression;
seekCur(12); seekCur(12);
const uint32_t colorsUsed = readLE32(); const uint32_t colorsUsed = readLE32();
if (colorsUsed > 256u) if (colorsUsed > 256u) return BmpReaderError::PaletteTooLarge;
return BmpReaderError::PaletteTooLarge;
seekCur(4); seekCur(4);
if (width <= 0 || height <= 0) if (width <= 0 || height <= 0) return BmpReaderError::BadDimensions;
return BmpReaderError::BadDimensions;
constexpr int MAX_IMAGE_WIDTH = 2048; constexpr int MAX_IMAGE_WIDTH = 2048;
constexpr int MAX_IMAGE_HEIGHT = 3072; constexpr int MAX_IMAGE_HEIGHT = 3072;
@ -177,8 +165,7 @@ BmpReaderError Bitmap::parseHeaders() {
rowBytes = (width * bpp + 31) / 32 * 4; rowBytes = (width * bpp + 31) / 32 * 4;
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++) paletteLum[i] = static_cast<uint8_t>(i);
paletteLum[i] = static_cast<uint8_t>(i);
if (colorsUsed > 0) { if (colorsUsed > 0) {
for (uint32_t i = 0; i < colorsUsed; i++) { for (uint32_t i = 0; i < colorsUsed; i++) {
uint8_t rgb[4]; uint8_t rgb[4];
@ -187,8 +174,7 @@ BmpReaderError Bitmap::parseHeaders() {
} }
} }
if (!seekSet(bfOffBits)) if (!seekSet(bfOffBits)) return BmpReaderError::SeekPixelDataFailed;
return BmpReaderError::SeekPixelDataFailed;
if (bpp > 2 && dithering) { if (bpp > 2 && dithering) {
if (USE_ATKINSON) { if (USE_ATKINSON) {
@ -202,8 +188,7 @@ BmpReaderError Bitmap::parseHeaders() {
} }
BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const { BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
if (readBytes(rowBuffer, rowBytes) != (size_t)rowBytes) if (readBytes(rowBuffer, rowBytes) != (size_t)rowBytes) return BmpReaderError::ShortReadRow;
return BmpReaderError::ShortReadRow;
prevRowY += 1; prevRowY += 1;
uint8_t* outPtr = data; uint8_t* outPtr = data;
@ -255,14 +240,12 @@ BmpReaderError Bitmap::readNextRow(uint8_t *data, uint8_t *rowBuffer) const {
break; break;
} }
case 8: { case 8: {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++) packPixel(paletteLum[rowBuffer[x]]);
packPixel(paletteLum[rowBuffer[x]]);
break; break;
} }
case 2: { case 2: {
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
uint8_t lum = uint8_t lum = paletteLum[(rowBuffer[x >> 2] >> (6 - ((x & 3) * 2))) & 0x03];
paletteLum[(rowBuffer[x >> 2] >> (6 - ((x & 3) * 2))) & 0x03];
packPixel(lum); packPixel(lum);
} }
break; break;
@ -283,17 +266,13 @@ BmpReaderError Bitmap::readNextRow(uint8_t *data, uint8_t *rowBuffer) const {
else if (fsDitherer) else if (fsDitherer)
fsDitherer->nextRow(); fsDitherer->nextRow();
if (bitShift != 6) if (bitShift != 6) *outPtr = currentOutByte;
*outPtr = currentOutByte;
return BmpReaderError::Ok; return BmpReaderError::Ok;
} }
BmpReaderError Bitmap::rewindToData() const { BmpReaderError Bitmap::rewindToData() const {
if (!seekSet(bfOffBits)) if (!seekSet(bfOffBits)) return BmpReaderError::SeekPixelDataFailed;
return BmpReaderError::SeekPixelDataFailed; if (fsDitherer) fsDitherer->reset();
if (fsDitherer) if (atkinsonDitherer) atkinsonDitherer->reset();
fsDitherer->reset();
if (atkinsonDitherer)
atkinsonDitherer->reset();
return BmpReaderError::Ok; return BmpReaderError::Ok;
} }

View File

@ -32,11 +32,9 @@ class Bitmap {
public: public:
static const char* errorToString(BmpReaderError err); static const char* errorToString(BmpReaderError err);
explicit Bitmap(FsFile &file, bool dithering = false) explicit Bitmap(FsFile& file, bool dithering = false) : file(&file), dithering(dithering) {}
: file(&file), dithering(dithering) {}
explicit Bitmap(const uint8_t* buffer, size_t size, bool dithering = false) explicit Bitmap(const uint8_t* buffer, size_t size, bool dithering = false)
: file(nullptr), memoryBuffer(buffer), memorySize(size), : file(nullptr), memoryBuffer(buffer), memorySize(size), dithering(dithering) {}
dithering(dithering) {}
~Bitmap(); ~Bitmap();
BmpReaderError parseHeaders(); BmpReaderError parseHeaders();

View File

@ -1,11 +1,13 @@
#pragma once #pragma once
#include <Bitmap.h>
#include <SDCardManager.h>
#include <vector>
#include "ThemeContext.h" #include "ThemeContext.h"
#include "ThemeTypes.h" #include "ThemeTypes.h"
#include "UIElement.h" #include "UIElement.h"
#include <Bitmap.h>
#include <SDCardManager.h>
#include <vector>
namespace ThemeEngine { namespace ThemeEngine {
@ -21,12 +23,9 @@ protected:
int borderRadius = 0; // Corner radius (for future rounded rect support) int borderRadius = 0; // Corner radius (for future rounded rect support)
public: public:
Container(const std::string &id) : UIElement(id) { Container(const std::string& id) : UIElement(id) { bgColorExpr = Expression::parse("0xFF"); }
bgColorExpr = Expression::parse("0xFF");
}
virtual ~Container() { virtual ~Container() {
for (auto child : children) for (auto child : children) delete child;
delete child;
} }
Container* asContainer() override { return this; } Container* asContainer() override { return this; }
@ -69,8 +68,7 @@ public:
int getBorderRadius() const { return borderRadius; } int getBorderRadius() const { return borderRadius; }
void layout(const ThemeContext &context, int parentX, int parentY, void layout(const ThemeContext& context, int parentX, int parentY, int parentW, int parentH) override {
int parentW, int parentH) override {
UIElement::layout(context, parentX, parentY, parentW, parentH); UIElement::layout(context, parentX, parentY, parentW, parentH);
// Children are laid out with padding offset // Children are laid out with padding offset
int childX = absX + padding; int childX = absX + padding;
@ -90,8 +88,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
if (hasBg) { if (hasBg) {
std::string colStr = context.evaluatestring(bgColorExpr); std::string colStr = context.evaluatestring(bgColorExpr);
@ -148,9 +145,7 @@ class Rectangle : public UIElement {
Expression colorExpr; Expression colorExpr;
public: public:
Rectangle(const std::string &id) : UIElement(id) { Rectangle(const std::string& id) : UIElement(id) { colorExpr = Expression::parse("0x00"); }
colorExpr = Expression::parse("0x00");
}
ElementType getType() const override { return ElementType::Rectangle; } ElementType getType() const override { return ElementType::Rectangle; }
void setFill(bool f) { void setFill(bool f) {
@ -169,8 +164,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
std::string colStr = context.evaluatestring(colorExpr); std::string colStr = context.evaluatestring(colorExpr);
uint8_t color = Color::parse(colStr).value; uint8_t color = Color::parse(colStr).value;
@ -205,9 +199,7 @@ private:
bool ellipsis = true; // Truncate with ... if too long bool ellipsis = true; // Truncate with ... if too long
public: public:
Label(const std::string &id) : UIElement(id) { Label(const std::string& id) : UIElement(id) { colorExpr = Expression::parse("0x00"); }
colorExpr = Expression::parse("0x00");
}
ElementType getType() const override { return ElementType::Label; } ElementType getType() const override { return ElementType::Label; }
void setText(const std::string& expr) { void setText(const std::string& expr) {
@ -240,8 +232,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
std::string finalText = context.evaluatestring(textExpr); std::string finalText = context.evaluatestring(textExpr);
if (finalText.empty()) { if (finalText.empty()) {
@ -353,22 +344,18 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
std::string valStr = context.evaluatestring(valueExpr); std::string valStr = context.evaluatestring(valueExpr);
std::string maxStr = context.evaluatestring(maxExpr); std::string maxStr = context.evaluatestring(maxExpr);
int value = valStr.empty() ? 0 : std::stoi(valStr); int value = valStr.empty() ? 0 : std::stoi(valStr);
int maxVal = maxStr.empty() ? 100 : std::stoi(maxStr); int maxVal = maxStr.empty() ? 100 : std::stoi(maxStr);
if (maxVal <= 0) if (maxVal <= 0) maxVal = 100;
maxVal = 100;
float ratio = static_cast<float>(value) / static_cast<float>(maxVal); float ratio = static_cast<float>(value) / static_cast<float>(maxVal);
if (ratio < 0) if (ratio < 0) ratio = 0;
ratio = 0; if (ratio > 1) ratio = 1;
if (ratio > 1)
ratio = 1;
// Draw background // Draw background
std::string bgStr = context.evaluatestring(bgColorExpr); std::string bgStr = context.evaluatestring(bgColorExpr);
@ -399,9 +386,7 @@ class Divider : public UIElement {
int thickness = 1; int thickness = 1;
public: public:
Divider(const std::string &id) : UIElement(id) { Divider(const std::string& id) : UIElement(id) { colorExpr = Expression::parse("0x00"); }
colorExpr = Expression::parse("0x00");
}
ElementType getType() const override { return ElementType::Divider; } ElementType getType() const override { return ElementType::Divider; }
@ -419,8 +404,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
std::string colStr = context.evaluatestring(colorExpr); std::string colStr = context.evaluatestring(colorExpr);
uint8_t color = Color::parse(colStr).value; uint8_t color = Color::parse(colStr).value;
@ -464,8 +448,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
std::string valStr = context.evaluatestring(valueExpr); std::string valStr = context.evaluatestring(valueExpr);
int percentage = valStr.empty() ? 0 : std::stoi(valStr); int percentage = valStr.empty() ? 0 : std::stoi(valStr);
@ -480,22 +463,17 @@ public:
int x = absX; int x = absX;
int y = absY; int y = absY;
if (absW > batteryWidth) if (absW > batteryWidth) x += (absW - batteryWidth) / 2;
x += (absW - batteryWidth) / 2; if (absH > batteryHeight) y += (absH - batteryHeight) / 2;
if (absH > batteryHeight)
y += (absH - batteryHeight) / 2;
renderer.drawLine(x + 1, y, x + batteryWidth - 3, y, black); renderer.drawLine(x + 1, y, x + batteryWidth - 3, y, black);
renderer.drawLine(x + 1, y + batteryHeight - 1, x + batteryWidth - 3, renderer.drawLine(x + 1, y + batteryHeight - 1, x + batteryWidth - 3, y + batteryHeight - 1, black);
y + batteryHeight - 1, black);
renderer.drawLine(x, y + 1, x, y + batteryHeight - 2, black); renderer.drawLine(x, y + 1, x, y + batteryHeight - 2, black);
renderer.drawLine(x + batteryWidth - 2, y + 1, x + batteryWidth - 2, renderer.drawLine(x + batteryWidth - 2, y + 1, x + batteryWidth - 2, y + batteryHeight - 2, black);
y + batteryHeight - 2, black);
renderer.drawPixel(x + batteryWidth - 1, y + 3, black); renderer.drawPixel(x + batteryWidth - 1, y + 3, black);
renderer.drawPixel(x + batteryWidth - 1, y + batteryHeight - 4, black); renderer.drawPixel(x + batteryWidth - 1, y + batteryHeight - 4, black);
renderer.drawLine(x + batteryWidth - 0, y + 4, x + batteryWidth - 0, renderer.drawLine(x + batteryWidth - 0, y + 4, x + batteryWidth - 0, y + batteryHeight - 5, black);
y + batteryHeight - 5, black);
if (percentage > 0) { if (percentage > 0) {
int filledWidth = percentage * (batteryWidth - 5) / 100 + 1; int filledWidth = percentage * (batteryWidth - 5) / 100 + 1;

View File

@ -26,12 +26,10 @@ struct IniSection {
class IniParser { class IniParser {
public: public:
// Parse a stream (File, Serial, etc.) // Parse a stream (File, Serial, etc.)
static std::map<std::string, std::map<std::string, std::string>> static std::map<std::string, std::map<std::string, std::string>> parse(Stream& stream);
parse(Stream &stream);
// Parse a string buffer (useful for testing) // Parse a string buffer (useful for testing)
static std::map<std::string, std::map<std::string, std::string>> static std::map<std::string, std::map<std::string, std::string>> parseString(const std::string& content);
parseString(const std::string &content);
private: private:
static void trim(std::string& s); static void trim(std::string& s);

View File

@ -1,10 +1,11 @@
#pragma once #pragma once
#include <vector>
#include "BasicElements.h" #include "BasicElements.h"
#include "ThemeContext.h" #include "ThemeContext.h"
#include "ThemeTypes.h" #include "ThemeTypes.h"
#include "UIElement.h" #include "UIElement.h"
#include <vector>
namespace ThemeEngine { namespace ThemeEngine {
@ -32,8 +33,7 @@ public:
markDirty(); markDirty();
} }
void layout(const ThemeContext &context, int parentX, int parentY, void layout(const ThemeContext& context, int parentX, int parentY, int parentW, int parentH) override {
int parentW, int parentH) override {
UIElement::layout(context, parentX, parentY, parentW, parentH); UIElement::layout(context, parentX, parentY, parentW, parentH);
int currentX = absX + padding; int currentX = absX + padding;
@ -85,8 +85,7 @@ public:
markDirty(); markDirty();
} }
void layout(const ThemeContext &context, int parentX, int parentY, void layout(const ThemeContext& context, int parentX, int parentY, int parentW, int parentH) override {
int parentW, int parentH) override {
UIElement::layout(context, parentX, parentY, parentW, parentH); UIElement::layout(context, parentX, parentY, parentW, parentH);
int currentY = absY + padding; int currentY = absY + padding;
@ -141,12 +140,10 @@ public:
markDirty(); markDirty();
} }
void layout(const ThemeContext &context, int parentX, int parentY, void layout(const ThemeContext& context, int parentX, int parentY, int parentW, int parentH) override {
int parentW, int parentH) override {
UIElement::layout(context, parentX, parentY, parentW, parentH); UIElement::layout(context, parentX, parentY, parentW, parentH);
if (children.empty()) if (children.empty()) return;
return;
int availableW = absW - 2 * padding - (columns - 1) * colSpacing; int availableW = absW - 2 * padding - (columns - 1) * colSpacing;
int cellW = availableW / columns; int cellW = availableW / columns;
@ -162,8 +159,7 @@ public:
// Pass cell dimensions to avoid clamping issues // Pass cell dimensions to avoid clamping issues
child->layout(context, cellX, currentY, cellW, availableH); child->layout(context, cellX, currentY, cellW, availableH);
int childH = child->getAbsH(); int childH = child->getAbsH();
if (childH > maxRowHeight) if (childH > maxRowHeight) maxRowHeight = childH;
maxRowHeight = childH;
col++; col++;
if (col >= columns) { if (col >= columns) {
@ -222,8 +218,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
std::string text = context.evaluatestring(textExpr); std::string text = context.evaluatestring(textExpr);
if (text.empty()) { if (text.empty()) {
@ -238,10 +233,8 @@ public:
int badgeH = textH + 2 * paddingV; int badgeH = textH + 2 * paddingV;
// Use absX, absY as position, but we may auto-size // Use absX, absY as position, but we may auto-size
if (absW == 0) if (absW == 0) absW = badgeW;
absW = badgeW; if (absH == 0) absH = badgeH;
if (absH == 0)
absH = badgeH;
// Draw background // Draw background
std::string bgStr = context.evaluatestring(bgColorExpr); std::string bgStr = context.evaluatestring(bgColorExpr);
@ -306,14 +299,12 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
bool isOn = context.evaluateBool(valueExpr.rawExpr); bool isOn = context.evaluateBool(valueExpr.rawExpr);
// Get colors // Get colors
std::string colorStr = std::string colorStr = isOn ? context.evaluatestring(onColorExpr) : context.evaluatestring(offColorExpr);
isOn ? context.evaluatestring(onColorExpr) : context.evaluatestring(offColorExpr);
uint8_t trackColor = Color::parse(colorStr).value; uint8_t trackColor = Color::parse(colorStr).value;
// Draw track // Draw track
@ -324,8 +315,7 @@ public:
// Draw knob // Draw knob
int knobMargin = (trackHeight - knobSize) / 2; int knobMargin = (trackHeight - knobSize) / 2;
int knobX = isOn ? (trackX + trackWidth - knobSize - knobMargin) int knobX = isOn ? (trackX + trackWidth - knobSize - knobMargin) : (trackX + knobMargin);
: (trackX + knobMargin);
int knobY = trackY + knobMargin; int knobY = trackY + knobMargin;
// Knob is opposite color of track // Knob is opposite color of track
@ -369,12 +359,10 @@ public:
markDirty(); markDirty();
} }
void layout(const ThemeContext &context, int parentX, int parentY, void layout(const ThemeContext& context, int parentX, int parentY, int parentW, int parentH) override {
int parentW, int parentH) override {
UIElement::layout(context, parentX, parentY, parentW, parentH); UIElement::layout(context, parentX, parentY, parentW, parentH);
if (children.empty()) if (children.empty()) return;
return;
// Distribute tabs evenly // Distribute tabs evenly
int numTabs = children.size(); int numTabs = children.size();
@ -390,8 +378,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
// Draw background if set // Draw background if set
if (hasBg) { if (hasBg) {
@ -501,8 +488,7 @@ public:
} }
void draw(const GfxRenderer& renderer, const ThemeContext& context) override { void draw(const GfxRenderer& renderer, const ThemeContext& context) override {
if (!isVisible(context)) if (!isVisible(context)) return;
return;
// Get values // Get values
std::string posStr = context.evaluatestring(positionExpr); std::string posStr = context.evaluatestring(positionExpr);
@ -526,8 +512,7 @@ public:
// Calculate thumb size and position // Calculate thumb size and position
float ratio = static_cast<float>(visible) / static_cast<float>(total); float ratio = static_cast<float>(visible) / static_cast<float>(total);
int thumbH = static_cast<int>(absH * ratio); int thumbH = static_cast<int>(absH * ratio);
if (thumbH < 20) if (thumbH < 20) thumbH = 20; // Minimum thumb size
thumbH = 20; // Minimum thumb size
int maxScroll = total - visible; int maxScroll = total - visible;
float scrollRatio = maxScroll > 0 ? position / maxScroll : 0; float scrollRatio = maxScroll > 0 ? position / maxScroll : 0;

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include <map>
#include <vector>
#include "BasicElements.h" #include "BasicElements.h"
#include "UIElement.h" #include "UIElement.h"
#include <map>
#include <vector>
namespace ThemeEngine { namespace ThemeEngine {
@ -64,18 +65,14 @@ public:
} }
int getItemHeight() const { int getItemHeight() const {
if (itemHeight > 0) if (itemHeight > 0) return itemHeight;
return itemHeight; if (itemTemplate) return itemTemplate->getAbsH() > 0 ? itemTemplate->getAbsH() : 45;
if (itemTemplate)
return itemTemplate->getAbsH() > 0 ? itemTemplate->getAbsH() : 45;
return 45; return 45;
} }
int getItemWidth() const { int getItemWidth() const {
if (itemWidth > 0) if (itemWidth > 0) return itemWidth;
return itemWidth; if (itemTemplate) return itemTemplate->getAbsW() > 0 ? itemTemplate->getAbsW() : 100;
if (itemTemplate)
return itemTemplate->getAbsW() > 0 ? itemTemplate->getAbsW() : 100;
return 100; return 100;
} }
@ -98,8 +95,7 @@ public:
void setColumns(int c) { void setColumns(int c) {
columns = c > 0 ? c : 1; columns = c > 0 ? c : 1;
if (columns > 1) if (columns > 1) layoutMode = LayoutMode::Grid;
layoutMode = LayoutMode::Grid;
markDirty(); markDirty();
} }
@ -129,8 +125,7 @@ public:
} }
} }
void layout(const ThemeContext &context, int parentX, int parentY, void layout(const ThemeContext& context, int parentX, int parentY, int parentW, int parentH) override {
int parentW, int parentH) override {
// Layout self first (bounds) // Layout self first (bounds)
UIElement::layout(context, parentX, parentY, parentW, parentH); UIElement::layout(context, parentX, parentY, parentW, parentH);

View File

@ -25,8 +25,7 @@ struct Expression {
Expression expr; Expression expr;
expr.rawExpr = str; expr.rawExpr = str;
if (str.empty()) if (str.empty()) return expr;
return expr;
size_t start = 0; size_t start = 0;
while (start < str.length()) { while (start < str.length()) {
@ -39,8 +38,7 @@ struct Expression {
if (open > start) { if (open > start) {
// Literal before variable // Literal before variable
expr.tokens.push_back( expr.tokens.push_back({ExpressionToken::LITERAL, str.substr(start, open - start)});
{ExpressionToken::LITERAL, str.substr(start, open - start)});
} }
size_t close = str.find('}', open); size_t close = str.find('}', open);
@ -51,8 +49,7 @@ struct Expression {
} }
// Variable // Variable
expr.tokens.push_back( expr.tokens.push_back({ExpressionToken::VARIABLE, str.substr(open + 1, close - open - 1)});
{ExpressionToken::VARIABLE, str.substr(open + 1, close - open - 1)});
start = close + 1; start = close + 1;
} }
return expr; return expr;
@ -70,20 +67,17 @@ private:
// Helper to trim whitespace // Helper to trim whitespace
static std::string trim(const std::string& s) { static std::string trim(const std::string& s) {
size_t start = s.find_first_not_of(" \t\n\r"); size_t start = s.find_first_not_of(" \t\n\r");
if (start == std::string::npos) if (start == std::string::npos) return "";
return "";
size_t end = s.find_last_not_of(" \t\n\r"); size_t end = s.find_last_not_of(" \t\n\r");
return s.substr(start, end - start + 1); return s.substr(start, end - start + 1);
} }
// Helper to check if string is a number // Helper to check if string is a number
static bool isNumber(const std::string& s) { static bool isNumber(const std::string& s) {
if (s.empty()) if (s.empty()) return false;
return false;
size_t start = (s[0] == '-') ? 1 : 0; size_t start = (s[0] == '-') ? 1 : 0;
for (size_t i = start; i < s.length(); i++) { for (size_t i = start; i < s.length(); i++) {
if (!isdigit(s[i])) if (!isdigit(s[i])) return false;
return false;
} }
return start < s.length(); return start < s.length();
} }
@ -91,45 +85,34 @@ private:
public: public:
ThemeContext(const ThemeContext* parent = nullptr) : parent(parent) {} ThemeContext(const ThemeContext* parent = nullptr) : parent(parent) {}
void setString(const std::string &key, const std::string &value) { void setString(const std::string& key, const std::string& value) { strings[key] = value; }
strings[key] = value;
}
void setInt(const std::string& key, int value) { ints[key] = value; } void setInt(const std::string& key, int value) { ints[key] = value; }
void setBool(const std::string& key, bool value) { bools[key] = value; } void setBool(const std::string& key, bool value) { bools[key] = value; }
std::string getString(const std::string &key, std::string getString(const std::string& key, const std::string& defaultValue = "") const {
const std::string &defaultValue = "") const {
auto it = strings.find(key); auto it = strings.find(key);
if (it != strings.end()) if (it != strings.end()) return it->second;
return it->second; if (parent) return parent->getString(key, defaultValue);
if (parent)
return parent->getString(key, defaultValue);
return defaultValue; return defaultValue;
} }
int getInt(const std::string& key, int defaultValue = 0) const { int getInt(const std::string& key, int defaultValue = 0) const {
auto it = ints.find(key); auto it = ints.find(key);
if (it != ints.end()) if (it != ints.end()) return it->second;
return it->second; if (parent) return parent->getInt(key, defaultValue);
if (parent)
return parent->getInt(key, defaultValue);
return defaultValue; return defaultValue;
} }
bool getBool(const std::string& key, bool defaultValue = false) const { bool getBool(const std::string& key, bool defaultValue = false) const {
auto it = bools.find(key); auto it = bools.find(key);
if (it != bools.end()) if (it != bools.end()) return it->second;
return it->second; if (parent) return parent->getBool(key, defaultValue);
if (parent)
return parent->getBool(key, defaultValue);
return defaultValue; return defaultValue;
} }
bool hasKey(const std::string& key) const { bool hasKey(const std::string& key) const {
if (strings.count(key) || ints.count(key) || bools.count(key)) if (strings.count(key) || ints.count(key) || bools.count(key)) return true;
return true; if (parent) return parent->hasKey(key);
if (parent)
return parent->hasKey(key);
return false; return false;
} }
@ -137,22 +120,18 @@ public:
std::string getAnyAsString(const std::string& key) const { std::string getAnyAsString(const std::string& key) const {
// Check strings first // Check strings first
auto sit = strings.find(key); auto sit = strings.find(key);
if (sit != strings.end()) if (sit != strings.end()) return sit->second;
return sit->second;
// Check ints // Check ints
auto iit = ints.find(key); auto iit = ints.find(key);
if (iit != ints.end()) if (iit != ints.end()) return std::to_string(iit->second);
return std::to_string(iit->second);
// Check bools // Check bools
auto bit = bools.find(key); auto bit = bools.find(key);
if (bit != bools.end()) if (bit != bools.end()) return bit->second ? "true" : "false";
return bit->second ? "true" : "false";
// Check parent // Check parent
if (parent) if (parent) return parent->getAnyAsString(key);
return parent->getAnyAsString(key);
return ""; return "";
} }
@ -161,14 +140,11 @@ public:
// Supports: !, &&, ||, ==, !=, <, >, <=, >=, parentheses // Supports: !, &&, ||, ==, !=, <, >, <=, >=, parentheses
bool evaluateBool(const std::string& expression) const { bool evaluateBool(const std::string& expression) const {
std::string expr = trim(expression); std::string expr = trim(expression);
if (expr.empty()) if (expr.empty()) return false;
return false;
// Handle literal true/false // Handle literal true/false
if (expr == "true" || expr == "1") if (expr == "true" || expr == "1") return true;
return true; if (expr == "false" || expr == "0") return false;
if (expr == "false" || expr == "0")
return false;
// Handle {var} wrapper // Handle {var} wrapper
if (expr.size() > 2 && expr.front() == '{' && expr.back() == '}') { if (expr.size() > 2 && expr.front() == '{' && expr.back() == '}') {
@ -185,10 +161,8 @@ public:
int depth = 1; int depth = 1;
size_t closePos = 1; size_t closePos = 1;
while (closePos < expr.length() && depth > 0) { while (closePos < expr.length() && depth > 0) {
if (expr[closePos] == '(') if (expr[closePos] == '(') depth++;
depth++; if (expr[closePos] == ')') depth--;
if (expr[closePos] == ')')
depth--;
closePos++; closePos++;
} }
if (closePos <= expr.length()) { if (closePos <= expr.length()) {
@ -212,14 +186,11 @@ public:
size_t orPos = expr.find("||"); size_t orPos = expr.find("||");
// Process || first (lower precedence than &&) // Process || first (lower precedence than &&)
if (orPos != std::string::npos && if (orPos != std::string::npos && (andPos == std::string::npos || orPos < andPos)) {
(andPos == std::string::npos || orPos < andPos)) { return evaluateBool(expr.substr(0, orPos)) || evaluateBool(expr.substr(orPos + 2));
return evaluateBool(expr.substr(0, orPos)) ||
evaluateBool(expr.substr(orPos + 2));
} }
if (andPos != std::string::npos) { if (andPos != std::string::npos) {
return evaluateBool(expr.substr(0, andPos)) && return evaluateBool(expr.substr(0, andPos)) && evaluateBool(expr.substr(andPos + 2));
evaluateBool(expr.substr(andPos + 2));
} }
// Handle comparisons // Handle comparisons
@ -298,8 +269,7 @@ public:
} }
// If it's a number, return as-is // If it's a number, return as-is
if (isNumber(v)) if (isNumber(v)) return v;
return v;
// Check for hex color literals (0x00, 0xFF, etc.) // Check for hex color literals (0x00, 0xFF, etc.)
if (v.size() > 2 && v[0] == '0' && (v[1] == 'x' || v[1] == 'X')) { if (v.size() > 2 && v[0] == '0' && (v[1] == 'x' || v[1] == 'X')) {
@ -327,8 +297,7 @@ public:
// Evaluate a string expression with variable substitution // Evaluate a string expression with variable substitution
std::string evaluatestring(const Expression& expr) const { std::string evaluatestring(const Expression& expr) const {
if (expr.empty()) if (expr.empty()) return "";
return "";
std::string result; std::string result;
for (const auto& token : expr.tokens) { for (const auto& token : expr.tokens) {
@ -339,10 +308,8 @@ public:
std::string varName = token.value; std::string varName = token.value;
// If the variable contains comparison operators, evaluate as condition // If the variable contains comparison operators, evaluate as condition
if (varName.find("==") != std::string::npos || if (varName.find("==") != std::string::npos || varName.find("!=") != std::string::npos ||
varName.find("!=") != std::string::npos || varName.find("&&") != std::string::npos || varName.find("||") != std::string::npos) {
varName.find("&&") != std::string::npos ||
varName.find("||") != std::string::npos) {
result += evaluateBool(varName) ? "true" : "false"; result += evaluateBool(varName) ? "true" : "false";
continue; continue;
} }
@ -372,8 +339,7 @@ public:
// Legacy method for backward compatibility // Legacy method for backward compatibility
std::string evaluateString(const std::string& expression) const { std::string evaluateString(const std::string& expression) const {
if (expression.empty()) if (expression.empty()) return "";
return "";
Expression expr = Expression::parse(expression); Expression expr = Expression::parse(expression);
return evaluatestring(expr); return evaluatestring(expr);
} }

View File

@ -1,12 +1,14 @@
#pragma once #pragma once
#include <GfxRenderer.h>
#include <map>
#include <string>
#include <vector>
#include "BasicElements.h" #include "BasicElements.h"
#include "IniParser.h" #include "IniParser.h"
#include "ThemeContext.h" #include "ThemeContext.h"
#include <GfxRenderer.h>
#include <map>
#include <string>
#include <vector>
namespace ThemeEngine { namespace ThemeEngine {
@ -50,8 +52,7 @@ private:
// Factory and property methods // Factory and property methods
UIElement* createElement(const std::string& id, const std::string& type); UIElement* createElement(const std::string& id, const std::string& type);
void applyProperties(UIElement *elem, void applyProperties(UIElement* elem, const std::map<std::string, std::string>& props);
const std::map<std::string, std::string> &props);
public: public:
static ThemeManager& get() { static ThemeManager& get() {
@ -76,13 +77,10 @@ public:
int getNavBookCount() const { return navBookCount; } int getNavBookCount() const { return navBookCount; }
// Render a screen // Render a screen
void renderScreen(const std::string &screenName, const GfxRenderer &renderer, void renderScreen(const std::string& screenName, const GfxRenderer& renderer, const ThemeContext& context);
const ThemeContext &context);
// Render with dirty tracking (only redraws changed regions) // Render with dirty tracking (only redraws changed regions)
void renderScreenOptimized(const std::string &screenName, void renderScreenOptimized(const std::string& screenName, const GfxRenderer& renderer, const ThemeContext& context,
const GfxRenderer &renderer,
const ThemeContext &context,
const ThemeContext* prevContext = nullptr); const ThemeContext* prevContext = nullptr);
// Invalidate all caches (call when theme changes or screen switches) // Invalidate all caches (call when theme changes or screen switches)
@ -100,12 +98,9 @@ public:
// Asset caching // Asset caching
const std::vector<uint8_t>* getCachedAsset(const std::string& path); const std::vector<uint8_t>* getCachedAsset(const std::string& path);
const ProcessedAsset *getProcessedAsset(const std::string &path, const ProcessedAsset* getProcessedAsset(const std::string& path, GfxRenderer::Orientation orientation,
GfxRenderer::Orientation orientation,
int targetW = 0, int targetH = 0);
void cacheProcessedAsset(const std::string &path,
const ProcessedAsset &asset,
int targetW = 0, int targetH = 0); int targetW = 0, int targetH = 0);
void cacheProcessedAsset(const std::string& path, const ProcessedAsset& asset, int targetW = 0, int targetH = 0);
// Clear asset caches (for memory management) // Clear asset caches (for memory management)
void clearAssetCaches(); void clearAssetCaches();
@ -121,8 +116,7 @@ private:
std::map<std::string, ProcessedAsset> processedCache; std::map<std::string, ProcessedAsset> processedCache;
// Compute a simple hash of context data for cache invalidation // Compute a simple hash of context data for cache invalidation
uint32_t computeContextHash(const ThemeContext &context, uint32_t computeContextHash(const ThemeContext& context, const std::string& screenName);
const std::string &screenName);
}; };
} // namespace ThemeEngine } // namespace ThemeEngine

View File

@ -15,12 +15,10 @@ struct Dimension {
Dimension() : value(0), unit(DimensionUnit::PIXELS) {} Dimension() : value(0), unit(DimensionUnit::PIXELS) {}
static Dimension parse(const std::string& str) { static Dimension parse(const std::string& str) {
if (str.empty()) if (str.empty()) return Dimension(0, DimensionUnit::PIXELS);
return Dimension(0, DimensionUnit::PIXELS);
if (str.back() == '%') { if (str.back() == '%') {
return Dimension(std::stoi(str.substr(0, str.length() - 1)), return Dimension(std::stoi(str.substr(0, str.length() - 1)), DimensionUnit::PERCENT);
DimensionUnit::PERCENT);
} }
return Dimension(std::stoi(str), DimensionUnit::PIXELS); return Dimension(std::stoi(str), DimensionUnit::PIXELS);
} }
@ -40,14 +38,10 @@ struct Color {
Color() : value(0) {} Color() : value(0) {}
static Color parse(const std::string& str) { static Color parse(const std::string& str) {
if (str.empty()) if (str.empty()) return Color(0);
return Color(0); if (str == "black") return Color(0x00);
if (str == "black") if (str == "white") return Color(0xFF);
return Color(0x00); if (str == "gray" || str == "grey") return Color(0x80);
if (str == "white")
return Color(0xFF);
if (str == "gray" || str == "grey")
return Color(0x80);
if (str.size() > 2 && str.substr(0, 2) == "0x") { if (str.size() > 2 && str.substr(0, 2) == "0x") {
return Color((uint8_t)std::strtol(str.c_str(), nullptr, 16)); return Color((uint8_t)std::strtol(str.c_str(), nullptr, 16));
} }
@ -65,15 +59,12 @@ struct Rect {
bool isEmpty() const { return w <= 0 || h <= 0; } bool isEmpty() const { return w <= 0 || h <= 0; }
bool intersects(const Rect& other) const { bool intersects(const Rect& other) const {
return !(x + w <= other.x || other.x + other.w <= x || y + h <= other.y || return !(x + w <= other.x || other.x + other.w <= x || y + h <= other.y || other.y + other.h <= y);
other.y + other.h <= y);
} }
Rect unite(const Rect& other) const { Rect unite(const Rect& other) const {
if (isEmpty()) if (isEmpty()) return other;
return other; if (other.isEmpty()) return *this;
if (other.isEmpty())
return *this;
int nx = std::min(x, other.x); int nx = std::min(x, other.x);
int ny = std::min(y, other.y); int ny = std::min(y, other.y);
int nx2 = std::max(x + w, other.x + other.w); int nx2 = std::max(x + w, other.x + other.w);

View File

@ -1,10 +1,12 @@
#pragma once #pragma once
#include <GfxRenderer.h>
#include <string>
#include <vector>
#include "ThemeContext.h" #include "ThemeContext.h"
#include "ThemeTypes.h" #include "ThemeTypes.h"
#include <GfxRenderer.h>
#include <string>
#include <vector>
namespace ThemeEngine { namespace ThemeEngine {
@ -38,15 +40,12 @@ protected:
bool dirty = true; // Needs redraw bool dirty = true; // Needs redraw
bool isVisible(const ThemeContext& context) const { bool isVisible(const ThemeContext& context) const {
if (visibleExpr.empty()) if (visibleExpr.empty()) return true;
return true;
return context.evaluateBool(visibleExpr.rawExpr); return context.evaluateBool(visibleExpr.rawExpr);
} }
public: public:
UIElement(const std::string &id) : id(id) { UIElement(const std::string& id) : id(id) { visibleExpr = Expression::parse("true"); }
visibleExpr = Expression::parse("true");
}
virtual ~UIElement() { virtual ~UIElement() {
if (cachedRender) { if (cachedRender) {
@ -74,8 +73,8 @@ public:
void setVisibleExpr(const std::string& expr) { void setVisibleExpr(const std::string& expr) {
visibleExpr = Expression::parse(expr); visibleExpr = Expression::parse(expr);
// Check if expression contains variables // Check if expression contains variables
visibleExprIsStatic = (expr == "true" || expr == "false" || expr == "1" || visibleExprIsStatic =
expr == "0" || expr.find('{') == std::string::npos); (expr == "true" || expr == "false" || expr == "1" || expr == "0" || expr.find('{') == std::string::npos);
markDirty(); markDirty();
} }
@ -97,31 +96,24 @@ public:
} }
// Calculate absolute position based on parent // Calculate absolute position based on parent
virtual void layout(const ThemeContext &context, int parentX, int parentY, virtual void layout(const ThemeContext& context, int parentX, int parentY, int parentW, int parentH) {
int parentW, int parentH) {
int newX = parentX + x.resolve(parentW); int newX = parentX + x.resolve(parentW);
int newY = parentY + y.resolve(parentH); int newY = parentY + y.resolve(parentH);
int newW = width.resolve(parentW); int newW = width.resolve(parentW);
int newH = height.resolve(parentH); int newH = height.resolve(parentH);
// Clamp to parent bounds // Clamp to parent bounds
if (newX >= parentX + parentW) if (newX >= parentX + parentW) newX = parentX + parentW - 1;
newX = parentX + parentW - 1; if (newY >= parentY + parentH) newY = parentY + parentH - 1;
if (newY >= parentY + parentH)
newY = parentY + parentH - 1;
int maxX = parentX + parentW; int maxX = parentX + parentW;
int maxY = parentY + parentH; int maxY = parentY + parentH;
if (newX + newW > maxX) if (newX + newW > maxX) newW = maxX - newX;
newW = maxX - newX; if (newY + newH > maxY) newH = maxY - newY;
if (newY + newH > maxY)
newH = maxY - newY;
if (newW < 0) if (newW < 0) newW = 0;
newW = 0; if (newH < 0) newH = 0;
if (newH < 0)
newH = 0;
// Check if position changed // Check if position changed
if (newX != absX || newY != absY || newW != absW || newH != absH) { if (newX != absX || newY != absY || newW != absW || newH != absH) {
@ -166,8 +158,7 @@ public:
Rect getBounds() const { return Rect(absX, absY, absW, absH); } Rect getBounds() const { return Rect(absX, absY, absW, absH); }
// Main draw method - handles caching automatically // Main draw method - handles caching automatically
virtual void draw(const GfxRenderer &renderer, virtual void draw(const GfxRenderer& renderer, const ThemeContext& context) = 0;
const ThemeContext &context) = 0;
protected: protected:
// Cache the rendered output // Cache the rendered output
@ -191,11 +182,8 @@ protected:
// Restore from cache // Restore from cache
bool restoreFromCache(const GfxRenderer& renderer) const { bool restoreFromCache(const GfxRenderer& renderer) const {
if (!cacheValid || !cachedRender) if (!cacheValid || !cachedRender) return false;
return false; if (absX != cachedX || absY != cachedY || absW != cachedW || absH != cachedH) return false;
if (absX != cachedX || absY != cachedY || absW != cachedW ||
absH != cachedH)
return false;
renderer.restoreRegion(cachedRender, absX, absY, absW, absH); renderer.restoreRegion(cachedRender, absX, absY, absW, absH);
return true; return true;

View File

@ -1,4 +1,5 @@
#include "BasicElements.h" #include "BasicElements.h"
#include "Bitmap.h" #include "Bitmap.h"
#include "ListElement.h" #include "ListElement.h"
#include "ThemeManager.h" #include "ThemeManager.h"
@ -7,8 +8,7 @@
namespace ThemeEngine { namespace ThemeEngine {
// --- BitmapElement --- // --- BitmapElement ---
void BitmapElement::draw(const GfxRenderer &renderer, void BitmapElement::draw(const GfxRenderer& renderer, const ThemeContext& context) {
const ThemeContext &context) {
if (!isVisible(context)) { if (!isVisible(context)) {
markClean(); markClean();
return; return;
@ -21,8 +21,7 @@ void BitmapElement::draw(const GfxRenderer &renderer,
} }
// Check if we have a cached 1-bit render of this bitmap at this size // Check if we have a cached 1-bit render of this bitmap at this size
const ProcessedAsset *processed = const ProcessedAsset* processed = ThemeManager::get().getProcessedAsset(path, renderer.getOrientation(), absW, absH);
ThemeManager::get().getProcessedAsset(path, renderer.getOrientation(), absW, absH);
if (processed && processed->w == absW && processed->h == absH) { if (processed && processed->w == absW && processed->h == absH) {
// Draw cached 1-bit data directly // Draw cached 1-bit data directly
const int rowBytes = (absW + 7) / 8; const int rowBytes = (absW + 7) / 8;
@ -148,8 +147,7 @@ void List::draw(const GfxRenderer &renderer, const ThemeContext &context) {
currentX += itemW + spacing; currentX += itemW + spacing;
continue; continue;
} }
if (currentX > absX + absW) if (currentX > absX + absW) break;
break;
} else { } else {
// Grid mode // Grid mode
if (currentY + itemH < absY) { if (currentY + itemH < absY) {
@ -162,8 +160,7 @@ void List::draw(const GfxRenderer &renderer, const ThemeContext &context) {
currentX = absX + col * (itemW + spacing); currentX = absX + col * (itemW + spacing);
continue; continue;
} }
if (currentY > absY + absH) if (currentY > absY + absH) break;
break;
} }
// Layout and draw // Layout and draw

View File

@ -1,11 +1,11 @@
#include "IniParser.h" #include "IniParser.h"
#include <sstream> #include <sstream>
namespace ThemeEngine { namespace ThemeEngine {
void IniParser::trim(std::string& s) { void IniParser::trim(std::string& s) {
if (s.empty()) if (s.empty()) return;
return;
// Trim left // Trim left
size_t first = s.find_first_not_of(" \t\n\r"); size_t first = s.find_first_not_of(" \t\n\r");
@ -19,8 +19,7 @@ void IniParser::trim(std::string &s) {
s = s.substr(first, (last - first + 1)); s = s.substr(first, (last - first + 1));
} }
std::map<std::string, std::map<std::string, std::string>> std::map<std::string, std::map<std::string, std::string>> IniParser::parse(Stream& stream) {
IniParser::parse(Stream &stream) {
std::map<std::string, std::map<std::string, std::string>> sections; std::map<std::string, std::map<std::string, std::string>> sections;
// stream check not strictly possible like file, can rely on available() // stream check not strictly possible like file, can rely on available()
@ -62,8 +61,7 @@ IniParser::parse(Stream &stream) {
return sections; return sections;
} }
std::map<std::string, std::map<std::string, std::string>> std::map<std::string, std::map<std::string, std::string>> IniParser::parseString(const std::string& content) {
IniParser::parseString(const std::string &content) {
std::map<std::string, std::map<std::string, std::string>> sections; std::map<std::string, std::map<std::string, std::string>> sections;
std::stringstream ss(content); std::stringstream ss(content);
std::string line; std::string line;

View File

@ -1,7 +1,9 @@
#include "LayoutElements.h" #include "LayoutElements.h"
#include "ThemeManager.h"
#include <Bitmap.h> #include <Bitmap.h>
#include "ThemeManager.h"
namespace ThemeEngine { namespace ThemeEngine {
// Built-in icon drawing // Built-in icon drawing
@ -29,8 +31,7 @@ void Icon::draw(const GfxRenderer &renderer, const ThemeContext &context) {
int cy = absY + h / 2; int cy = absY + h / 2;
// Check if it's a path to a BMP file // Check if it's a path to a BMP file
if (iconName.find('/') != std::string::npos || if (iconName.find('/') != std::string::npos || iconName.find('.') != std::string::npos) {
iconName.find('.') != std::string::npos) {
// Try to load as bitmap // Try to load as bitmap
std::string path = iconName; std::string path = iconName;
if (path[0] != '/') { if (path[0] != '/') {

View File

@ -1,77 +1,58 @@
#include "ThemeManager.h" #include "ThemeManager.h"
#include "DefaultTheme.h"
#include "LayoutElements.h"
#include "ListElement.h"
#include <SDCardManager.h> #include <SDCardManager.h>
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <vector> #include <vector>
#include "DefaultTheme.h"
#include "LayoutElements.h"
#include "ListElement.h"
namespace ThemeEngine { namespace ThemeEngine {
void ThemeManager::begin() { void ThemeManager::begin() {
// Default fonts or setup // Default fonts or setup
} }
void ThemeManager::registerFont(const std::string &name, int id) { void ThemeManager::registerFont(const std::string& name, int id) { fontMap[name] = id; }
fontMap[name] = id;
}
std::string ThemeManager::getAssetPath(const std::string& assetName) { std::string ThemeManager::getAssetPath(const std::string& assetName) {
// Check if absolute path // Check if absolute path
if (!assetName.empty() && assetName[0] == '/') if (!assetName.empty() && assetName[0] == '/') return assetName;
return assetName;
// Otherwise relative to theme assets // Otherwise relative to theme assets
return "/themes/" + currentThemeName + "/assets/" + assetName; return "/themes/" + currentThemeName + "/assets/" + assetName;
} }
UIElement *ThemeManager::createElement(const std::string &id, UIElement* ThemeManager::createElement(const std::string& id, const std::string& type) {
const std::string &type) {
// Basic elements // Basic elements
if (type == "Container") if (type == "Container") return new Container(id);
return new Container(id); if (type == "Rectangle") return new Rectangle(id);
if (type == "Rectangle") if (type == "Label") return new Label(id);
return new Rectangle(id); if (type == "Bitmap") return new BitmapElement(id);
if (type == "Label") if (type == "List") return new List(id);
return new Label(id); if (type == "ProgressBar") return new ProgressBar(id);
if (type == "Bitmap") if (type == "Divider") return new Divider(id);
return new BitmapElement(id);
if (type == "List")
return new List(id);
if (type == "ProgressBar")
return new ProgressBar(id);
if (type == "Divider")
return new Divider(id);
// Layout elements // Layout elements
if (type == "HStack") if (type == "HStack") return new HStack(id);
return new HStack(id); if (type == "VStack") return new VStack(id);
if (type == "VStack") if (type == "Grid") return new Grid(id);
return new VStack(id);
if (type == "Grid")
return new Grid(id);
// Advanced elements // Advanced elements
if (type == "Badge") if (type == "Badge") return new Badge(id);
return new Badge(id); if (type == "Toggle") return new Toggle(id);
if (type == "Toggle") if (type == "TabBar") return new TabBar(id);
return new Toggle(id); if (type == "Icon") return new Icon(id);
if (type == "TabBar") if (type == "ScrollIndicator") return new ScrollIndicator(id);
return new TabBar(id); if (type == "BatteryIcon") return new BatteryIcon(id);
if (type == "Icon")
return new Icon(id);
if (type == "ScrollIndicator")
return new ScrollIndicator(id);
if (type == "BatteryIcon")
return new BatteryIcon(id);
return nullptr; return nullptr;
} }
void ThemeManager::applyProperties( void ThemeManager::applyProperties(UIElement* elem, const std::map<std::string, std::string>& props) {
UIElement *elem, const std::map<std::string, std::string> &props) {
const auto elemType = elem->getType(); const auto elemType = elem->getType();
for (const auto& kv : props) { for (const auto& kv : props) {
@ -105,10 +86,8 @@ void ThemeManager::applyProperties(
} else if (key == "Color") { } else if (key == "Color") {
if (elemType == UIElement::ElementType::Rectangle) { if (elemType == UIElement::ElementType::Rectangle) {
static_cast<Rectangle*>(elem)->setColorExpr(val); static_cast<Rectangle*>(elem)->setColorExpr(val);
} else if (elemType == UIElement::ElementType::Container || } else if (elemType == UIElement::ElementType::Container || elemType == UIElement::ElementType::HStack ||
elemType == UIElement::ElementType::HStack || elemType == UIElement::ElementType::VStack || elemType == UIElement::ElementType::Grid ||
elemType == UIElement::ElementType::VStack ||
elemType == UIElement::ElementType::Grid ||
elemType == UIElement::ElementType::TabBar) { elemType == UIElement::ElementType::TabBar) {
static_cast<Container*>(elem)->setBackgroundColorExpr(val); static_cast<Container*>(elem)->setBackgroundColorExpr(val);
} else if (elemType == UIElement::ElementType::Label) { } else if (elemType == UIElement::ElementType::Label) {
@ -132,19 +111,15 @@ void ThemeManager::applyProperties(
} }
} }
} else if (key == "Padding") { } else if (key == "Padding") {
if (elemType == UIElement::ElementType::Container || if (elemType == UIElement::ElementType::Container || elemType == UIElement::ElementType::HStack ||
elemType == UIElement::ElementType::HStack || elemType == UIElement::ElementType::VStack || elemType == UIElement::ElementType::Grid) {
elemType == UIElement::ElementType::VStack ||
elemType == UIElement::ElementType::Grid) {
static_cast<Container*>(elem)->setPadding(std::stoi(val)); static_cast<Container*>(elem)->setPadding(std::stoi(val));
} else if (elemType == UIElement::ElementType::TabBar) { } else if (elemType == UIElement::ElementType::TabBar) {
static_cast<TabBar*>(elem)->setPadding(std::stoi(val)); static_cast<TabBar*>(elem)->setPadding(std::stoi(val));
} }
} else if (key == "BorderRadius") { } else if (key == "BorderRadius") {
if (elemType == UIElement::ElementType::Container || if (elemType == UIElement::ElementType::Container || elemType == UIElement::ElementType::HStack ||
elemType == UIElement::ElementType::HStack || elemType == UIElement::ElementType::VStack || elemType == UIElement::ElementType::Grid) {
elemType == UIElement::ElementType::VStack ||
elemType == UIElement::ElementType::Grid) {
static_cast<Container*>(elem)->setBorderRadius(std::stoi(val)); static_cast<Container*>(elem)->setBorderRadius(std::stoi(val));
} }
} else if (key == "Spacing") { } else if (key == "Spacing") {
@ -189,10 +164,8 @@ void ThemeManager::applyProperties(
} else if (key == "Align") { } else if (key == "Align") {
if (elemType == UIElement::ElementType::Label) { if (elemType == UIElement::ElementType::Label) {
Label::Alignment align = Label::Alignment::Left; Label::Alignment align = Label::Alignment::Left;
if (val == "Center" || val == "center") if (val == "Center" || val == "center") align = Label::Alignment::Center;
align = Label::Alignment::Center; if (val == "Right" || val == "right") align = Label::Alignment::Right;
if (val == "Right" || val == "right")
align = Label::Alignment::Right;
static_cast<Label*>(elem)->setAlignment(align); static_cast<Label*>(elem)->setAlignment(align);
} }
} else if (key == "MaxLines") { } else if (key == "MaxLines") {
@ -209,8 +182,7 @@ void ThemeManager::applyProperties(
else if (key == "Src") { else if (key == "Src") {
if (elemType == UIElement::ElementType::Bitmap) { if (elemType == UIElement::ElementType::Bitmap) {
auto b = static_cast<BitmapElement*>(elem); auto b = static_cast<BitmapElement*>(elem);
if (val.find('{') == std::string::npos && if (val.find('{') == std::string::npos && val.find('/') == std::string::npos) {
val.find('/') == std::string::npos) {
b->setSrc(getAssetPath(val)); b->setSrc(getAssetPath(val));
} else { } else {
b->setSrc(val); b->setSrc(val);
@ -220,13 +192,11 @@ void ThemeManager::applyProperties(
} }
} else if (key == "ScaleToFit") { } else if (key == "ScaleToFit") {
if (elemType == UIElement::ElementType::Bitmap) { if (elemType == UIElement::ElementType::Bitmap) {
static_cast<BitmapElement *>(elem)->setScaleToFit(val == "true" || static_cast<BitmapElement*>(elem)->setScaleToFit(val == "true" || val == "1");
val == "1");
} }
} else if (key == "PreserveAspect") { } else if (key == "PreserveAspect") {
if (elemType == UIElement::ElementType::Bitmap) { if (elemType == UIElement::ElementType::Bitmap) {
static_cast<BitmapElement *>(elem)->setPreserveAspect(val == "true" || static_cast<BitmapElement*>(elem)->setPreserveAspect(val == "true" || val == "1");
val == "1");
} }
} else if (key == "IconSize") { } else if (key == "IconSize") {
if (elemType == UIElement::ElementType::Icon) { if (elemType == UIElement::ElementType::Icon) {
@ -295,16 +265,13 @@ void ThemeManager::applyProperties(
static_cast<ProgressBar*>(elem)->setBgColor(val); static_cast<ProgressBar*>(elem)->setBgColor(val);
} else if (elemType == UIElement::ElementType::Badge) { } else if (elemType == UIElement::ElementType::Badge) {
static_cast<Badge*>(elem)->setBgColor(val); static_cast<Badge*>(elem)->setBgColor(val);
} else if (elemType == UIElement::ElementType::Container || } else if (elemType == UIElement::ElementType::Container || elemType == UIElement::ElementType::HStack ||
elemType == UIElement::ElementType::HStack || elemType == UIElement::ElementType::VStack || elemType == UIElement::ElementType::Grid) {
elemType == UIElement::ElementType::VStack ||
elemType == UIElement::ElementType::Grid) {
static_cast<Container*>(elem)->setBackgroundColorExpr(val); static_cast<Container*>(elem)->setBackgroundColorExpr(val);
} }
} else if (key == "ShowBorder") { } else if (key == "ShowBorder") {
if (elemType == UIElement::ElementType::ProgressBar) { if (elemType == UIElement::ElementType::ProgressBar) {
static_cast<ProgressBar *>(elem)->setShowBorder(val == "true" || static_cast<ProgressBar*>(elem)->setShowBorder(val == "true" || val == "1");
val == "1");
} }
} }
@ -391,14 +358,12 @@ void ThemeManager::applyProperties(
} }
} }
const std::vector<uint8_t> * const std::vector<uint8_t>* ThemeManager::getCachedAsset(const std::string& path) {
ThemeManager::getCachedAsset(const std::string &path) {
if (assetCache.count(path)) { if (assetCache.count(path)) {
return &assetCache.at(path); return &assetCache.at(path);
} }
if (!SdMan.exists(path.c_str())) if (!SdMan.exists(path.c_str())) return nullptr;
return nullptr;
FsFile file; FsFile file;
if (SdMan.openFileForRead("ThemeCache", path, file)) { if (SdMan.openFileForRead("ThemeCache", path, file)) {
@ -412,9 +377,7 @@ ThemeManager::getCachedAsset(const std::string &path) {
return nullptr; return nullptr;
} }
const ProcessedAsset * const ProcessedAsset* ThemeManager::getProcessedAsset(const std::string& path, GfxRenderer::Orientation orientation,
ThemeManager::getProcessedAsset(const std::string &path,
GfxRenderer::Orientation orientation,
int targetW, int targetH) { int targetW, int targetH) {
// Include dimensions in cache key for scaled images // Include dimensions in cache key for scaled images
std::string cacheKey = path; std::string cacheKey = path;
@ -431,9 +394,7 @@ ThemeManager::getProcessedAsset(const std::string &path,
return nullptr; return nullptr;
} }
void ThemeManager::cacheProcessedAsset(const std::string &path, void ThemeManager::cacheProcessedAsset(const std::string& path, const ProcessedAsset& asset, int targetW, int targetH) {
const ProcessedAsset &asset,
int targetW, int targetH) {
std::string cacheKey = path; std::string cacheKey = path;
if (targetW > 0 && targetH > 0) { if (targetW > 0 && targetH > 0) {
cacheKey += ":" + std::to_string(targetW) + "x" + std::to_string(targetH); cacheKey += ":" + std::to_string(targetW) + "x" + std::to_string(targetH);
@ -467,8 +428,7 @@ void ThemeManager::invalidateScreenCache(const std::string &screenName) {
} }
} }
uint32_t ThemeManager::computeContextHash(const ThemeContext &context, uint32_t ThemeManager::computeContextHash(const ThemeContext& context, const std::string& screenName) {
const std::string &screenName) {
uint32_t hash = 2166136261u; uint32_t hash = 2166136261u;
for (char c : screenName) { for (char c : screenName) {
hash ^= static_cast<uint32_t>(c); hash ^= static_cast<uint32_t>(c);
@ -511,8 +471,7 @@ void ThemeManager::loadTheme(const std::string &themeName) {
Serial.printf("[ThemeManager] Parsing theme file...\n"); Serial.printf("[ThemeManager] Parsing theme file...\n");
sections = IniParser::parse(file); sections = IniParser::parse(file);
file.close(); file.close();
Serial.printf("[ThemeManager] Parsed %d sections from %s\n", Serial.printf("[ThemeManager] Parsed %d sections from %s\n", (int)sections.size(), themeName.c_str());
(int)sections.size(), themeName.c_str());
} else { } else {
Serial.printf("[ThemeManager] Failed to open %s, using Default\n", path.c_str()); Serial.printf("[ThemeManager] Failed to open %s, using Default\n", path.c_str());
sections = IniParser::parseString(getDefaultThemeIni()); sections = IniParser::parseString(getDefaultThemeIni());
@ -537,15 +496,12 @@ void ThemeManager::loadTheme(const std::string &themeName) {
std::string id = sec.first; std::string id = sec.first;
const std::map<std::string, std::string>& props = sec.second; const std::map<std::string, std::string>& props = sec.second;
if (id == "Global") if (id == "Global") continue;
continue;
auto it = props.find("Type"); auto it = props.find("Type");
if (it == props.end()) if (it == props.end()) continue;
continue;
std::string type = it->second; std::string type = it->second;
if (type.empty()) if (type.empty()) continue;
continue;
UIElement* elem = createElement(id, type); UIElement* elem = createElement(id, type);
if (elem) { if (elem) {
@ -557,10 +513,8 @@ void ThemeManager::loadTheme(const std::string &themeName) {
std::vector<List*> lists; std::vector<List*> lists;
for (const auto& sec : sections) { for (const auto& sec : sections) {
std::string id = sec.first; std::string id = sec.first;
if (id == "Global") if (id == "Global") continue;
continue; if (elements.find(id) == elements.end()) continue;
if (elements.find(id) == elements.end())
continue;
UIElement* elem = elements[id]; UIElement* elem = elements[id];
applyProperties(elem, sec.second); applyProperties(elem, sec.second);
@ -577,8 +531,7 @@ void ThemeManager::loadTheme(const std::string &themeName) {
c->addChild(elem); c->addChild(elem);
} }
} else { } else {
Serial.printf("[ThemeManager] WARN: Parent %s not found for %s\n", Serial.printf("[ThemeManager] WARN: Parent %s not found for %s\n", parentId.c_str(), id.c_str());
parentId.c_str(), id.c_str());
} }
} }
} }
@ -588,12 +541,10 @@ void ThemeManager::loadTheme(const std::string &themeName) {
l->resolveTemplate(elements); l->resolveTemplate(elements);
} }
Serial.printf("[ThemeManager] Theme loaded with %d elements\n", Serial.printf("[ThemeManager] Theme loaded with %d elements\n", (int)elements.size());
(int)elements.size());
} }
void ThemeManager::renderScreen(const std::string &screenName, void ThemeManager::renderScreen(const std::string& screenName, const GfxRenderer& renderer,
const GfxRenderer &renderer,
const ThemeContext& context) { const ThemeContext& context) {
if (elements.count(screenName) == 0) { if (elements.count(screenName) == 0) {
Serial.printf("[ThemeManager] Screen '%s' not found\n", screenName.c_str()); Serial.printf("[ThemeManager] Screen '%s' not found\n", screenName.c_str());
@ -602,16 +553,13 @@ void ThemeManager::renderScreen(const std::string &screenName,
UIElement* root = elements[screenName]; UIElement* root = elements[screenName];
root->layout(context, 0, 0, renderer.getScreenWidth(), root->layout(context, 0, 0, renderer.getScreenWidth(), renderer.getScreenHeight());
renderer.getScreenHeight());
root->draw(renderer, context); root->draw(renderer, context);
} }
void ThemeManager::renderScreenOptimized(const std::string &screenName, void ThemeManager::renderScreenOptimized(const std::string& screenName, const GfxRenderer& renderer,
const GfxRenderer &renderer, const ThemeContext& context, const ThemeContext* prevContext) {
const ThemeContext &context,
const ThemeContext *prevContext) {
renderScreen(screenName, renderer, context); renderScreen(screenName, renderer, context);
} }

View File

@ -4,6 +4,7 @@
#include <Epub.h> #include <Epub.h>
#include <GfxRenderer.h> #include <GfxRenderer.h>
#include <SDCardManager.h> #include <SDCardManager.h>
#include <ThemeManager.h>
#include <Xtc.h> #include <Xtc.h>
#include <cstring> #include <cstring>
@ -17,7 +18,6 @@
#include "ScreenComponents.h" #include "ScreenComponents.h"
#include "fontIds.h" #include "fontIds.h"
#include "util/StringUtils.h" #include "util/StringUtils.h"
#include <ThemeManager.h>
void HomeActivity::taskTrampoline(void* param) { void HomeActivity::taskTrampoline(void* param) {
auto* self = static_cast<HomeActivity*>(param); auto* self = static_cast<HomeActivity*>(param);
@ -26,8 +26,7 @@ void HomeActivity::taskTrampoline(void *param) {
int HomeActivity::getMenuItemCount() const { int HomeActivity::getMenuItemCount() const {
int count = 3; // Browse Files, File Transfer, Settings int count = 3; // Browse Files, File Transfer, Settings
if (hasOpdsUrl) if (hasOpdsUrl) count++; // + Calibre Library
count++; // + Calibre Library
return count; return count;
} }
@ -43,8 +42,7 @@ void HomeActivity::onEnter() {
selectorIndex = 0; // Start at first item (first book if any, else first menu) selectorIndex = 0; // Start at first item (first book if any, else first menu)
// Check if we have a book to continue reading // Check if we have a book to continue reading
hasContinueReading = !APP_STATE.openEpubPath.empty() && hasContinueReading = !APP_STATE.openEpubPath.empty() && SdMan.exists(APP_STATE.openEpubPath.c_str());
SdMan.exists(APP_STATE.openEpubPath.c_str());
// Check if OPDS browser URL is configured // Check if OPDS browser URL is configured
hasOpdsUrl = strlen(SETTINGS.opdsServerUrl) > 0; hasOpdsUrl = strlen(SETTINGS.opdsServerUrl) > 0;
@ -190,8 +188,8 @@ void HomeActivity::loadRecentBooksData() {
} }
} }
Serial.printf("[HOME] Book %d: title='%s', cover='%s', progress=%d%%\n", Serial.printf("[HOME] Book %d: title='%s', cover='%s', progress=%d%%\n", i, info.title.c_str(),
i, info.title.c_str(), info.coverPath.c_str(), info.progressPercent); info.coverPath.c_str(), info.progressPercent);
cachedRecentBooks.push_back(info); cachedRecentBooks.push_back(info);
} }
@ -258,11 +256,9 @@ void HomeActivity::freeCoverBuffer() {
} }
void HomeActivity::loop() { void HomeActivity::loop() {
const bool prevPressed = const bool prevPressed = mappedInput.wasPressed(MappedInputManager::Button::Up) ||
mappedInput.wasPressed(MappedInputManager::Button::Up) ||
mappedInput.wasPressed(MappedInputManager::Button::Left); mappedInput.wasPressed(MappedInputManager::Button::Left);
const bool nextPressed = const bool nextPressed = mappedInput.wasPressed(MappedInputManager::Button::Down) ||
mappedInput.wasPressed(MappedInputManager::Button::Down) ||
mappedInput.wasPressed(MappedInputManager::Button::Right); mappedInput.wasPressed(MappedInputManager::Button::Right);
const bool confirmPressed = mappedInput.wasReleased(MappedInputManager::Button::Confirm); const bool confirmPressed = mappedInput.wasReleased(MappedInputManager::Button::Confirm);
@ -324,8 +320,7 @@ void HomeActivity::displayTaskLoop() {
void HomeActivity::render() { void HomeActivity::render() {
// Battery check logic (only update every 60 seconds) // Battery check logic (only update every 60 seconds)
const uint32_t now = millis(); const uint32_t now = millis();
const bool needBatteryUpdate = const bool needBatteryUpdate = (now - lastBatteryCheck > 60000) || (lastBatteryCheck == 0);
(now - lastBatteryCheck > 60000) || (lastBatteryCheck == 0);
if (needBatteryUpdate) { if (needBatteryUpdate) {
cachedBatteryLevel = battery.readPercentage(); cachedBatteryLevel = battery.readPercentage();
lastBatteryCheck = now; lastBatteryCheck = now;
@ -339,8 +334,7 @@ void HomeActivity::render() {
// --- Bind Global Data --- // --- Bind Global Data ---
context.setString("BatteryPercent", std::to_string(cachedBatteryLevel)); context.setString("BatteryPercent", std::to_string(cachedBatteryLevel));
context.setBool("ShowBatteryPercent", context.setBool("ShowBatteryPercent",
SETTINGS.hideBatteryPercentage != SETTINGS.hideBatteryPercentage != CrossPointSettings::HIDE_BATTERY_PERCENTAGE::HIDE_ALWAYS);
CrossPointSettings::HIDE_BATTERY_PERCENTAGE::HIDE_ALWAYS);
// --- Navigation counts (must match loop()) --- // --- Navigation counts (must match loop()) ---
const int recentCount = static_cast<int>(cachedRecentBooks.size()); const int recentCount = static_cast<int>(cachedRecentBooks.size());
@ -370,8 +364,7 @@ void HomeActivity::render() {
context.setString("BookTitle", lastBookTitle); context.setString("BookTitle", lastBookTitle);
context.setString("BookAuthor", lastBookAuthor); context.setString("BookAuthor", lastBookAuthor);
context.setString("BookCoverPath", coverBmpPath); context.setString("BookCoverPath", coverBmpPath);
context.setBool("HasCover", context.setBool("HasCover", hasContinueReading && hasCoverImage && !coverBmpPath.empty());
hasContinueReading && hasCoverImage && !coverBmpPath.empty());
context.setBool("ShowInfoBox", true); context.setBool("ShowInfoBox", true);
// --- Main Menu Data --- // --- Main Menu Data ---

View File

@ -53,14 +53,14 @@ class HomeActivity final : public Activity {
public: public:
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::function<void()> &onContinueReading, const std::function<void()>& onContinueReading, const std::function<void()>& onMyLibraryOpen,
const std::function<void()> &onMyLibraryOpen, const std::function<void()>& onSettingsOpen, const std::function<void()>& onFileTransferOpen,
const std::function<void()> &onSettingsOpen,
const std::function<void()> &onFileTransferOpen,
const std::function<void()>& onOpdsBrowserOpen) const std::function<void()>& onOpdsBrowserOpen)
: Activity("Home", renderer, mappedInput), : Activity("Home", renderer, mappedInput),
onContinueReading(onContinueReading), onMyLibraryOpen(onMyLibraryOpen), onContinueReading(onContinueReading),
onSettingsOpen(onSettingsOpen), onFileTransferOpen(onFileTransferOpen), onMyLibraryOpen(onMyLibraryOpen),
onSettingsOpen(onSettingsOpen),
onFileTransferOpen(onFileTransferOpen),
onOpdsBrowserOpen(onOpdsBrowserOpen) {} onOpdsBrowserOpen(onOpdsBrowserOpen) {}
void onEnter() override; void onEnter() override;
void onExit() override; void onExit() override;

View File

@ -30,8 +30,7 @@ void CategorySettingsActivity::onEnter() {
selectedSettingIndex = 0; selectedSettingIndex = 0;
updateRequired = true; updateRequired = true;
xTaskCreate(&CategorySettingsActivity::taskTrampoline, xTaskCreate(&CategorySettingsActivity::taskTrampoline, "CategorySettingsActivityTask", 4096, this, 1,
"CategorySettingsActivityTask", 4096, this, 1,
&displayTaskHandle); &displayTaskHandle);
} }
@ -71,15 +70,11 @@ void CategorySettingsActivity::loop() {
// Handle navigation // Handle navigation
if (mappedInput.wasPressed(MappedInputManager::Button::Up) || if (mappedInput.wasPressed(MappedInputManager::Button::Up) ||
mappedInput.wasPressed(MappedInputManager::Button::Left)) { mappedInput.wasPressed(MappedInputManager::Button::Left)) {
selectedSettingIndex = (selectedSettingIndex > 0) selectedSettingIndex = (selectedSettingIndex > 0) ? (selectedSettingIndex - 1) : (settingsCount - 1);
? (selectedSettingIndex - 1)
: (settingsCount - 1);
updateRequired = true; updateRequired = true;
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) || } else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
mappedInput.wasPressed(MappedInputManager::Button::Right)) { mappedInput.wasPressed(MappedInputManager::Button::Right)) {
selectedSettingIndex = (selectedSettingIndex < settingsCount - 1) selectedSettingIndex = (selectedSettingIndex < settingsCount - 1) ? (selectedSettingIndex + 1) : 0;
? (selectedSettingIndex + 1)
: 0;
updateRequired = true; updateRequired = true;
} }
} }
@ -97,10 +92,8 @@ void CategorySettingsActivity::toggleCurrentSetting() {
SETTINGS.*(setting.valuePtr) = !currentValue; SETTINGS.*(setting.valuePtr) = !currentValue;
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) { } else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
const uint8_t currentValue = SETTINGS.*(setting.valuePtr); const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
SETTINGS.*(setting.valuePtr) = SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size());
(currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size()); } else if (setting.type == SettingType::VALUE && setting.valuePtr != nullptr) {
} else if (setting.type == SettingType::VALUE &&
setting.valuePtr != nullptr) {
const int8_t currentValue = SETTINGS.*(setting.valuePtr); const int8_t currentValue = SETTINGS.*(setting.valuePtr);
if (currentValue + setting.valueRange.step > setting.valueRange.max) { if (currentValue + setting.valueRange.step > setting.valueRange.max) {
SETTINGS.*(setting.valuePtr) = setting.valueRange.min; SETTINGS.*(setting.valuePtr) = setting.valueRange.min;
@ -111,8 +104,7 @@ void CategorySettingsActivity::toggleCurrentSetting() {
if (strcmp(setting.name, "KOReader Sync") == 0) { if (strcmp(setting.name, "KOReader Sync") == 0) {
xSemaphoreTake(renderingMutex, portMAX_DELAY); xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity(); exitActivity();
enterNewActivity( enterNewActivity(new KOReaderSettingsActivity(renderer, mappedInput, [this] {
new KOReaderSettingsActivity(renderer, mappedInput, [this] {
exitActivity(); exitActivity();
updateRequired = true; updateRequired = true;
})); }));
@ -120,8 +112,7 @@ void CategorySettingsActivity::toggleCurrentSetting() {
} else if (strcmp(setting.name, "OPDS Browser") == 0) { } else if (strcmp(setting.name, "OPDS Browser") == 0) {
xSemaphoreTake(renderingMutex, portMAX_DELAY); xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity(); exitActivity();
enterNewActivity( enterNewActivity(new CalibreSettingsActivity(renderer, mappedInput, [this] {
new CalibreSettingsActivity(renderer, mappedInput, [this] {
exitActivity(); exitActivity();
updateRequired = true; updateRequired = true;
})); }));
@ -145,8 +136,7 @@ void CategorySettingsActivity::toggleCurrentSetting() {
} else if (strcmp(setting.name, "Theme") == 0) { } else if (strcmp(setting.name, "Theme") == 0) {
xSemaphoreTake(renderingMutex, portMAX_DELAY); xSemaphoreTake(renderingMutex, portMAX_DELAY);
exitActivity(); exitActivity();
enterNewActivity( enterNewActivity(new ThemeSelectionActivity(renderer, mappedInput, [this] {
new ThemeSelectionActivity(renderer, mappedInput, [this] {
exitActivity(); exitActivity();
updateRequired = true; updateRequired = true;
})); }));
@ -177,8 +167,7 @@ void CategorySettingsActivity::render() const {
const auto pageWidth = renderer.getScreenWidth(); const auto pageWidth = renderer.getScreenWidth();
const auto pageHeight = renderer.getScreenHeight(); const auto pageHeight = renderer.getScreenHeight();
renderer.drawCenteredText(UI_12_FONT_ID, 15, categoryName, true, renderer.drawCenteredText(UI_12_FONT_ID, 15, categoryName, true, EpdFontFamily::BOLD);
EpdFontFamily::BOLD);
// Draw selection highlight // Draw selection highlight
renderer.fillRect(0, 60 + selectedSettingIndex * 30 - 2, pageWidth - 1, 30); renderer.fillRect(0, 60 + selectedSettingIndex * 30 - 2, pageWidth - 1, 30);
@ -189,39 +178,30 @@ void CategorySettingsActivity::render() const {
const bool isSelected = (i == selectedSettingIndex); const bool isSelected = (i == selectedSettingIndex);
// Draw setting name // Draw setting name
renderer.drawText(UI_10_FONT_ID, 20, settingY, settingsList[i].name, renderer.drawText(UI_10_FONT_ID, 20, settingY, settingsList[i].name, !isSelected);
!isSelected);
// Draw value based on setting type // Draw value based on setting type
std::string valueText; std::string valueText;
if (settingsList[i].type == SettingType::TOGGLE && if (settingsList[i].type == SettingType::TOGGLE && settingsList[i].valuePtr != nullptr) {
settingsList[i].valuePtr != nullptr) {
const bool value = SETTINGS.*(settingsList[i].valuePtr); const bool value = SETTINGS.*(settingsList[i].valuePtr);
valueText = value ? "ON" : "OFF"; valueText = value ? "ON" : "OFF";
} else if (settingsList[i].type == SettingType::ENUM && } else if (settingsList[i].type == SettingType::ENUM && settingsList[i].valuePtr != nullptr) {
settingsList[i].valuePtr != nullptr) {
const uint8_t value = SETTINGS.*(settingsList[i].valuePtr); const uint8_t value = SETTINGS.*(settingsList[i].valuePtr);
valueText = settingsList[i].enumValues[value]; valueText = settingsList[i].enumValues[value];
} else if (settingsList[i].type == SettingType::VALUE && } else if (settingsList[i].type == SettingType::VALUE && settingsList[i].valuePtr != nullptr) {
settingsList[i].valuePtr != nullptr) {
valueText = std::to_string(SETTINGS.*(settingsList[i].valuePtr)); valueText = std::to_string(SETTINGS.*(settingsList[i].valuePtr));
} }
if (!valueText.empty()) { if (!valueText.empty()) {
const auto width = const auto width = renderer.getTextWidth(UI_10_FONT_ID, valueText.c_str());
renderer.getTextWidth(UI_10_FONT_ID, valueText.c_str()); renderer.drawText(UI_10_FONT_ID, pageWidth - 20 - width, settingY, valueText.c_str(), !isSelected);
renderer.drawText(UI_10_FONT_ID, pageWidth - 20 - width, settingY,
valueText.c_str(), !isSelected);
} }
} }
renderer.drawText( renderer.drawText(SMALL_FONT_ID, pageWidth - 20 - renderer.getTextWidth(SMALL_FONT_ID, CROSSPOINT_VERSION),
SMALL_FONT_ID,
pageWidth - 20 - renderer.getTextWidth(SMALL_FONT_ID, CROSSPOINT_VERSION),
pageHeight - 60, CROSSPOINT_VERSION); pageHeight - 60, CROSSPOINT_VERSION);
const auto labels = mappedInput.mapLabels("« Back", "Toggle", "", ""); const auto labels = mappedInput.mapLabels("« Back", "Toggle", "", "");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
labels.btn4);
renderer.displayBuffer(); renderer.displayBuffer();
} }

View File

@ -1,11 +1,14 @@
#include "ThemeSelectionActivity.h" #include "ThemeSelectionActivity.h"
#include <GfxRenderer.h>
#include <SDCardManager.h>
#include <esp_system.h>
#include <cstring>
#include "CrossPointSettings.h" #include "CrossPointSettings.h"
#include "MappedInputManager.h" #include "MappedInputManager.h"
#include "fontIds.h" #include "fontIds.h"
#include <GfxRenderer.h>
#include <SDCardManager.h>
#include <cstring>
#include <esp_system.h>
void ThemeSelectionActivity::taskTrampoline(void* param) { void ThemeSelectionActivity::taskTrampoline(void* param) {
auto* self = static_cast<ThemeSelectionActivity*>(param); auto* self = static_cast<ThemeSelectionActivity*>(param);
@ -49,8 +52,7 @@ void ThemeSelectionActivity::onEnter() {
} }
updateRequired = true; updateRequired = true;
xTaskCreate(&ThemeSelectionActivity::taskTrampoline, "ThemeSelTask", 4096, xTaskCreate(&ThemeSelectionActivity::taskTrampoline, "ThemeSelTask", 4096, this, 1, &displayTaskHandle);
this, 1, &displayTaskHandle);
} }
void ThemeSelectionActivity::onExit() { void ThemeSelectionActivity::onExit() {
@ -72,17 +74,14 @@ void ThemeSelectionActivity::loop() {
// Only reboot if theme actually changed // Only reboot if theme actually changed
if (selected != std::string(SETTINGS.themeName)) { if (selected != std::string(SETTINGS.themeName)) {
strncpy(SETTINGS.themeName, selected.c_str(), strncpy(SETTINGS.themeName, selected.c_str(), sizeof(SETTINGS.themeName) - 1);
sizeof(SETTINGS.themeName) - 1);
SETTINGS.themeName[sizeof(SETTINGS.themeName) - 1] = '\0'; SETTINGS.themeName[sizeof(SETTINGS.themeName) - 1] = '\0';
SETTINGS.saveToFile(); SETTINGS.saveToFile();
// Show reboot message // Show reboot message
renderer.clearScreen(); renderer.clearScreen();
renderer.drawCenteredText(UI_12_FONT_ID, renderer.getScreenHeight() / 2 - 20, renderer.drawCenteredText(UI_12_FONT_ID, renderer.getScreenHeight() / 2 - 20, "Applying theme...", true);
"Applying theme...", true); renderer.drawCenteredText(UI_10_FONT_ID, renderer.getScreenHeight() / 2 + 10, "Device will restart", true);
renderer.drawCenteredText(UI_10_FONT_ID, renderer.getScreenHeight() / 2 + 10,
"Device will restart", true);
renderer.displayBuffer(); renderer.displayBuffer();
// Small delay to ensure display updates // Small delay to ensure display updates
@ -103,13 +102,11 @@ void ThemeSelectionActivity::loop() {
if (mappedInput.wasPressed(MappedInputManager::Button::Up) || if (mappedInput.wasPressed(MappedInputManager::Button::Up) ||
mappedInput.wasPressed(MappedInputManager::Button::Left)) { mappedInput.wasPressed(MappedInputManager::Button::Left)) {
selectedIndex = selectedIndex = (selectedIndex > 0) ? (selectedIndex - 1) : (themeNames.size() - 1);
(selectedIndex > 0) ? (selectedIndex - 1) : (themeNames.size() - 1);
updateRequired = true; updateRequired = true;
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) || } else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
mappedInput.wasPressed(MappedInputManager::Button::Right)) { mappedInput.wasPressed(MappedInputManager::Button::Right)) {
selectedIndex = selectedIndex = (selectedIndex < themeNames.size() - 1) ? (selectedIndex + 1) : 0;
(selectedIndex < themeNames.size() - 1) ? (selectedIndex + 1) : 0;
updateRequired = true; updateRequired = true;
} }
} }
@ -133,8 +130,7 @@ void ThemeSelectionActivity::render() const {
const auto pageHeight = renderer.getScreenHeight(); const auto pageHeight = renderer.getScreenHeight();
// Header // Header
renderer.drawCenteredText(UI_12_FONT_ID, 15, "Select Theme", true, renderer.drawCenteredText(UI_12_FONT_ID, 15, "Select Theme", true, EpdFontFamily::BOLD);
EpdFontFamily::BOLD);
// Layout constants // Layout constants
const int entryHeight = 30; const int entryHeight = 30;
@ -155,8 +151,7 @@ void ThemeSelectionActivity::render() const {
// Draw Highlight // Draw Highlight
int visibleIndex = selectedIndex - startIdx; int visibleIndex = selectedIndex - startIdx;
if (visibleIndex >= 0 && visibleIndex < maxVisible) { if (visibleIndex >= 0 && visibleIndex < maxVisible) {
renderer.fillRect(0, startY + visibleIndex * entryHeight - 2, pageWidth - 1, renderer.fillRect(0, startY + visibleIndex * entryHeight - 2, pageWidth - 1, entryHeight);
entryHeight);
} }
// Draw List // Draw List
@ -176,15 +171,13 @@ void ThemeSelectionActivity::render() const {
if (themeNames.size() > maxVisible) { if (themeNames.size() > maxVisible) {
int barHeight = pageHeight - startY - 40; int barHeight = pageHeight - startY - 40;
int thumbHeight = barHeight * maxVisible / themeNames.size(); int thumbHeight = barHeight * maxVisible / themeNames.size();
int thumbY = startY + (barHeight - thumbHeight) * startIdx / int thumbY = startY + (barHeight - thumbHeight) * startIdx / (themeNames.size() - maxVisible);
(themeNames.size() - maxVisible);
renderer.fillRect(pageWidth - 5, startY, 2, barHeight, 0); renderer.fillRect(pageWidth - 5, startY, 2, barHeight, 0);
renderer.fillRect(pageWidth - 7, thumbY, 6, thumbHeight, 1); renderer.fillRect(pageWidth - 7, thumbY, 6, thumbHeight, 1);
} }
const auto labels = mappedInput.mapLabels("Cancel", "Select", "", ""); const auto labels = mappedInput.mapLabels("Cancel", "Select", "", "");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
labels.btn4);
renderer.displayBuffer(); renderer.displayBuffer();
} }

View File

@ -1,12 +1,14 @@
#pragma once #pragma once
#include "activities/Activity.h"
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/semphr.h> #include <freertos/semphr.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <functional> #include <functional>
#include <string> #include <string>
#include <vector> #include <vector>
#include "activities/Activity.h"
class ThemeSelectionActivity final : public Activity { class ThemeSelectionActivity final : public Activity {
TaskHandle_t displayTaskHandle = nullptr; TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr; SemaphoreHandle_t renderingMutex = nullptr;
@ -20,8 +22,7 @@ class ThemeSelectionActivity final : public Activity {
void render() const; void render() const;
public: public:
ThemeSelectionActivity(GfxRenderer &renderer, MappedInputManager &mappedInput, ThemeSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::function<void()>& onGoBack)
const std::function<void()> &onGoBack)
: Activity("ThemeSelection", renderer, mappedInput), onGoBack(onGoBack) {} : Activity("ThemeSelection", renderer, mappedInput), onGoBack(onGoBack) {}
void onEnter() override; void onEnter() override;
void onExit() override; void onExit() override;