feat: A web editor for settings (#667)

## Summary

This is an updated version of @itsthisjustin's #346 that builds on
current master and also deduplicates the settings list so we don't have
two copies of the settings. In the Web UI, it should organize the
settings a little closer to what you see on device.

## Additional Context

I tested this live on device and it seems to play nicely for me. It's
re-based on master since master's settings stuff has moved somewhat
since the original PR and addresses the sole review comment #346 - it
also means that I don't need to manually key in the URL for my OPDS
server. :)

---

### AI Usage

My changes were implemented with Claude Opus 4.5 and Claude Code 2.1.25.
I don't know if @itsthisjustin's original work used AI assistance.

Co-authored-by: Dave Allie <dave@daveallie.com>
This commit is contained in:
Jesse Vincent
2026-02-08 12:46:14 -08:00
committed by GitHub
parent 7f40c3f477
commit cda0a3f898
8 changed files with 839 additions and 84 deletions

101
src/SettingsList.h Normal file
View File

@@ -0,0 +1,101 @@
#pragma once
#include <vector>
#include "CrossPointSettings.h"
#include "KOReaderCredentialStore.h"
#include "activities/settings/SettingsActivity.h"
// Shared settings list used by both the device settings UI and the web settings API.
// Each entry has a key (for JSON API) and category (for grouping).
// ACTION-type entries and entries without a key are device-only.
inline std::vector<SettingInfo> getSettingsList() {
return {
// --- Display ---
SettingInfo::Enum("Sleep Screen", &CrossPointSettings::sleepScreen,
{"Dark", "Light", "Custom", "Cover", "None", "Cover + Custom"}, "sleepScreen", "Display"),
SettingInfo::Enum("Sleep Screen Cover Mode", &CrossPointSettings::sleepScreenCoverMode, {"Fit", "Crop"},
"sleepScreenCoverMode", "Display"),
SettingInfo::Enum("Sleep Screen Cover Filter", &CrossPointSettings::sleepScreenCoverFilter,
{"None", "Contrast", "Inverted"}, "sleepScreenCoverFilter", "Display"),
SettingInfo::Enum(
"Status Bar", &CrossPointSettings::statusBar,
{"None", "No Progress", "Full w/ Percentage", "Full w/ Book Bar", "Book Bar Only", "Full w/ Chapter Bar"},
"statusBar", "Display"),
SettingInfo::Enum("Hide Battery %", &CrossPointSettings::hideBatteryPercentage, {"Never", "In Reader", "Always"},
"hideBatteryPercentage", "Display"),
SettingInfo::Enum("Refresh Frequency", &CrossPointSettings::refreshFrequency,
{"1 page", "5 pages", "10 pages", "15 pages", "30 pages"}, "refreshFrequency", "Display"),
SettingInfo::Enum("UI Theme", &CrossPointSettings::uiTheme, {"Classic", "Lyra"}, "uiTheme", "Display"),
SettingInfo::Toggle("Sunlight Fading Fix", &CrossPointSettings::fadingFix, "fadingFix", "Display"),
// --- Reader ---
SettingInfo::Enum("Font Family", &CrossPointSettings::fontFamily, {"Bookerly", "Noto Sans", "Open Dyslexic"},
"fontFamily", "Reader"),
SettingInfo::Enum("Font Size", &CrossPointSettings::fontSize, {"Small", "Medium", "Large", "X Large"}, "fontSize",
"Reader"),
SettingInfo::Enum("Line Spacing", &CrossPointSettings::lineSpacing, {"Tight", "Normal", "Wide"}, "lineSpacing",
"Reader"),
SettingInfo::Value("Screen Margin", &CrossPointSettings::screenMargin, {5, 40, 5}, "screenMargin", "Reader"),
SettingInfo::Enum("Paragraph Alignment", &CrossPointSettings::paragraphAlignment,
{"Justify", "Left", "Center", "Right", "Book's Style"}, "paragraphAlignment", "Reader"),
SettingInfo::Toggle("Book's Embedded Style", &CrossPointSettings::embeddedStyle, "embeddedStyle", "Reader"),
SettingInfo::Toggle("Hyphenation", &CrossPointSettings::hyphenationEnabled, "hyphenationEnabled", "Reader"),
SettingInfo::Enum("Reading Orientation", &CrossPointSettings::orientation,
{"Portrait", "Landscape CW", "Inverted", "Landscape CCW"}, "orientation", "Reader"),
SettingInfo::Toggle("Extra Paragraph Spacing", &CrossPointSettings::extraParagraphSpacing,
"extraParagraphSpacing", "Reader"),
SettingInfo::Toggle("Text Anti-Aliasing", &CrossPointSettings::textAntiAliasing, "textAntiAliasing", "Reader"),
// --- Controls ---
SettingInfo::Enum("Side Button Layout (reader)", &CrossPointSettings::sideButtonLayout,
{"Prev, Next", "Next, Prev"}, "sideButtonLayout", "Controls"),
SettingInfo::Toggle("Long-press Chapter Skip", &CrossPointSettings::longPressChapterSkip, "longPressChapterSkip",
"Controls"),
SettingInfo::Enum("Short Power Button Click", &CrossPointSettings::shortPwrBtn, {"Ignore", "Sleep", "Page Turn"},
"shortPwrBtn", "Controls"),
// --- System ---
SettingInfo::Enum("Time to Sleep", &CrossPointSettings::sleepTimeout,
{"1 min", "5 min", "10 min", "15 min", "30 min"}, "sleepTimeout", "System"),
// --- KOReader Sync (web-only, uses KOReaderCredentialStore) ---
SettingInfo::DynamicString(
"KOReader Username", [] { return KOREADER_STORE.getUsername(); },
[](const std::string& v) {
KOREADER_STORE.setCredentials(v, KOREADER_STORE.getPassword());
KOREADER_STORE.saveToFile();
},
"koUsername", "KOReader Sync"),
SettingInfo::DynamicString(
"KOReader Password", [] { return KOREADER_STORE.getPassword(); },
[](const std::string& v) {
KOREADER_STORE.setCredentials(KOREADER_STORE.getUsername(), v);
KOREADER_STORE.saveToFile();
},
"koPassword", "KOReader Sync"),
SettingInfo::DynamicString(
"Sync Server URL", [] { return KOREADER_STORE.getServerUrl(); },
[](const std::string& v) {
KOREADER_STORE.setServerUrl(v);
KOREADER_STORE.saveToFile();
},
"koServerUrl", "KOReader Sync"),
SettingInfo::DynamicEnum(
"Document Matching", {"Filename", "Binary"},
[] { return static_cast<uint8_t>(KOREADER_STORE.getMatchMethod()); },
[](uint8_t v) {
KOREADER_STORE.setMatchMethod(static_cast<DocumentMatchMethod>(v));
KOREADER_STORE.saveToFile();
},
"koMatchMethod", "KOReader Sync"),
// --- OPDS Browser (web-only, uses CrossPointSettings char arrays) ---
SettingInfo::String("OPDS Server URL", SETTINGS.opdsServerUrl, sizeof(SETTINGS.opdsServerUrl), "opdsServerUrl",
"OPDS Browser"),
SettingInfo::String("OPDS Username", SETTINGS.opdsUsername, sizeof(SETTINGS.opdsUsername), "opdsUsername",
"OPDS Browser"),
SettingInfo::String("OPDS Password", SETTINGS.opdsPassword, sizeof(SETTINGS.opdsPassword), "opdsPassword",
"OPDS Browser"),
};
}

View File

@@ -10,6 +10,7 @@
#include "KOReaderSettingsActivity.h" #include "KOReaderSettingsActivity.h"
#include "MappedInputManager.h" #include "MappedInputManager.h"
#include "OtaUpdateActivity.h" #include "OtaUpdateActivity.h"
#include "SettingsList.h"
#include "components/UITheme.h" #include "components/UITheme.h"
#include "fontIds.h" #include "fontIds.h"
@@ -17,54 +18,6 @@ const char* SettingsActivity::categoryNames[categoryCount] = {"Display", "Reader
namespace { namespace {
constexpr int changeTabsMs = 700; constexpr int changeTabsMs = 700;
constexpr int displaySettingsCount = 8;
const SettingInfo displaySettings[displaySettingsCount] = {
// Should match with SLEEP_SCREEN_MODE
SettingInfo::Enum("Sleep Screen", &CrossPointSettings::sleepScreen,
{"Dark", "Light", "Custom", "Cover", "None", "Cover + Custom"}),
SettingInfo::Enum("Sleep Screen Cover Mode", &CrossPointSettings::sleepScreenCoverMode, {"Fit", "Crop"}),
SettingInfo::Enum("Sleep Screen Cover Filter", &CrossPointSettings::sleepScreenCoverFilter,
{"None", "Contrast", "Inverted"}),
SettingInfo::Enum(
"Status Bar", &CrossPointSettings::statusBar,
{"None", "No Progress", "Full w/ Percentage", "Full w/ Book Bar", "Book Bar Only", "Full w/ Chapter Bar"}),
SettingInfo::Enum("Hide Battery %", &CrossPointSettings::hideBatteryPercentage, {"Never", "In Reader", "Always"}),
SettingInfo::Enum("Refresh Frequency", &CrossPointSettings::refreshFrequency,
{"1 page", "5 pages", "10 pages", "15 pages", "30 pages"}),
SettingInfo::Enum("UI Theme", &CrossPointSettings::uiTheme, {"Classic", "Lyra"}),
SettingInfo::Toggle("Sunlight Fading Fix", &CrossPointSettings::fadingFix),
};
constexpr int readerSettingsCount = 10;
const SettingInfo readerSettings[readerSettingsCount] = {
SettingInfo::Enum("Font Family", &CrossPointSettings::fontFamily, {"Bookerly", "Noto Sans", "Open Dyslexic"}),
SettingInfo::Enum("Font Size", &CrossPointSettings::fontSize, {"Small", "Medium", "Large", "X Large"}),
SettingInfo::Enum("Line Spacing", &CrossPointSettings::lineSpacing, {"Tight", "Normal", "Wide"}),
SettingInfo::Value("Screen Margin", &CrossPointSettings::screenMargin, {5, 40, 5}),
SettingInfo::Enum("Paragraph Alignment", &CrossPointSettings::paragraphAlignment,
{"Justify", "Left", "Center", "Right", "Book's Style"}),
SettingInfo::Toggle("Book's Embedded Style", &CrossPointSettings::embeddedStyle),
SettingInfo::Toggle("Hyphenation", &CrossPointSettings::hyphenationEnabled),
SettingInfo::Enum("Reading Orientation", &CrossPointSettings::orientation,
{"Portrait", "Landscape CW", "Inverted", "Landscape CCW"}),
SettingInfo::Toggle("Extra Paragraph Spacing", &CrossPointSettings::extraParagraphSpacing),
SettingInfo::Toggle("Text Anti-Aliasing", &CrossPointSettings::textAntiAliasing)};
constexpr int controlsSettingsCount = 4;
const SettingInfo controlsSettings[controlsSettingsCount] = {
// Launches the remap wizard for front buttons.
SettingInfo::Action("Remap Front Buttons"),
SettingInfo::Enum("Side Button Layout (reader)", &CrossPointSettings::sideButtonLayout,
{"Prev, Next", "Next, Prev"}),
SettingInfo::Toggle("Long-press Chapter Skip", &CrossPointSettings::longPressChapterSkip),
SettingInfo::Enum("Short Power Button Click", &CrossPointSettings::shortPwrBtn, {"Ignore", "Sleep", "Page Turn"})};
constexpr int systemSettingsCount = 5;
const SettingInfo systemSettings[systemSettingsCount] = {
SettingInfo::Enum("Time to Sleep", &CrossPointSettings::sleepTimeout,
{"1 min", "5 min", "10 min", "15 min", "30 min"}),
SettingInfo::Action("KOReader Sync"), SettingInfo::Action("OPDS Browser"), SettingInfo::Action("Clear Cache"),
SettingInfo::Action("Check for updates")};
} // namespace } // namespace
void SettingsActivity::taskTrampoline(void* param) { void SettingsActivity::taskTrampoline(void* param) {
@@ -76,13 +29,40 @@ void SettingsActivity::onEnter() {
Activity::onEnter(); Activity::onEnter();
renderingMutex = xSemaphoreCreateMutex(); renderingMutex = xSemaphoreCreateMutex();
// Build per-category vectors from the shared settings list
displaySettings.clear();
readerSettings.clear();
controlsSettings.clear();
systemSettings.clear();
for (auto& setting : getSettingsList()) {
if (!setting.category) continue;
if (strcmp(setting.category, "Display") == 0) {
displaySettings.push_back(std::move(setting));
} else if (strcmp(setting.category, "Reader") == 0) {
readerSettings.push_back(std::move(setting));
} else if (strcmp(setting.category, "Controls") == 0) {
controlsSettings.push_back(std::move(setting));
} else if (strcmp(setting.category, "System") == 0) {
systemSettings.push_back(std::move(setting));
}
// Web-only categories (KOReader Sync, OPDS Browser) are skipped for device UI
}
// Append device-only ACTION items
controlsSettings.insert(controlsSettings.begin(), SettingInfo::Action("Remap Front Buttons"));
systemSettings.push_back(SettingInfo::Action("KOReader Sync"));
systemSettings.push_back(SettingInfo::Action("OPDS Browser"));
systemSettings.push_back(SettingInfo::Action("Clear Cache"));
systemSettings.push_back(SettingInfo::Action("Check for updates"));
// Reset selection to first category // Reset selection to first category
selectedCategoryIndex = 0; selectedCategoryIndex = 0;
selectedSettingIndex = 0; selectedSettingIndex = 0;
// Initialize with first category (Display) // Initialize with first category (Display)
settingsList = displaySettings; currentSettings = &displaySettings;
settingsCount = displaySettingsCount; settingsCount = static_cast<int>(displaySettings.size());
// Trigger first update // Trigger first update
updateRequired = true; updateRequired = true;
@@ -162,23 +142,20 @@ void SettingsActivity::loop() {
if (hasChangedCategory) { if (hasChangedCategory) {
selectedSettingIndex = (selectedSettingIndex == 0) ? 0 : 1; selectedSettingIndex = (selectedSettingIndex == 0) ? 0 : 1;
switch (selectedCategoryIndex) { switch (selectedCategoryIndex) {
case 0: // Display case 0:
settingsList = displaySettings; currentSettings = &displaySettings;
settingsCount = displaySettingsCount;
break; break;
case 1: // Reader case 1:
settingsList = readerSettings; currentSettings = &readerSettings;
settingsCount = readerSettingsCount;
break; break;
case 2: // Controls case 2:
settingsList = controlsSettings; currentSettings = &controlsSettings;
settingsCount = controlsSettingsCount;
break; break;
case 3: // System case 3:
settingsList = systemSettings; currentSettings = &systemSettings;
settingsCount = systemSettingsCount;
break; break;
} }
settingsCount = static_cast<int>(currentSettings->size());
} }
} }
@@ -188,7 +165,7 @@ void SettingsActivity::toggleCurrentSetting() {
return; return;
} }
const auto& setting = settingsList[selectedSetting]; const auto& setting = (*currentSettings)[selectedSetting];
if (setting.type == SettingType::TOGGLE && setting.valuePtr != nullptr) { if (setting.type == SettingType::TOGGLE && setting.valuePtr != nullptr) {
// Toggle the boolean value using the member pointer // Toggle the boolean value using the member pointer
@@ -283,24 +260,24 @@ void SettingsActivity::render() const {
GUI.drawTabBar(renderer, Rect{0, metrics.topPadding + metrics.headerHeight, pageWidth, metrics.tabBarHeight}, tabs, GUI.drawTabBar(renderer, Rect{0, metrics.topPadding + metrics.headerHeight, pageWidth, metrics.tabBarHeight}, tabs,
selectedSettingIndex == 0); selectedSettingIndex == 0);
const auto& settings = *currentSettings;
GUI.drawList( GUI.drawList(
renderer, renderer,
Rect{0, metrics.topPadding + metrics.headerHeight + metrics.tabBarHeight + metrics.verticalSpacing, pageWidth, Rect{0, metrics.topPadding + metrics.headerHeight + metrics.tabBarHeight + metrics.verticalSpacing, pageWidth,
pageHeight - (metrics.topPadding + metrics.headerHeight + metrics.tabBarHeight + metrics.buttonHintsHeight + pageHeight - (metrics.topPadding + metrics.headerHeight + metrics.tabBarHeight + metrics.buttonHintsHeight +
metrics.verticalSpacing * 2)}, metrics.verticalSpacing * 2)},
settingsCount, selectedSettingIndex - 1, [this](int index) { return std::string(settingsList[index].name); }, settingsCount, selectedSettingIndex - 1, [&settings](int index) { return std::string(settings[index].name); },
nullptr, nullptr, nullptr, nullptr,
[this](int i) { [&settings](int i) {
const auto& setting = settingsList[i];
std::string valueText = ""; std::string valueText = "";
if (settingsList[i].type == SettingType::TOGGLE && settingsList[i].valuePtr != nullptr) { if (settings[i].type == SettingType::TOGGLE && settings[i].valuePtr != nullptr) {
const bool value = SETTINGS.*(settingsList[i].valuePtr); const bool value = SETTINGS.*(settings[i].valuePtr);
valueText = value ? "ON" : "OFF"; valueText = value ? "ON" : "OFF";
} else if (settingsList[i].type == SettingType::ENUM && settingsList[i].valuePtr != nullptr) { } else if (settings[i].type == SettingType::ENUM && settings[i].valuePtr != nullptr) {
const uint8_t value = SETTINGS.*(settingsList[i].valuePtr); const uint8_t value = SETTINGS.*(settings[i].valuePtr);
valueText = settingsList[i].enumValues[value]; valueText = settings[i].enumValues[value];
} else if (settingsList[i].type == SettingType::VALUE && settingsList[i].valuePtr != nullptr) { } else if (settings[i].type == SettingType::VALUE && settings[i].valuePtr != nullptr) {
valueText = std::to_string(SETTINGS.*(settingsList[i].valuePtr)); valueText = std::to_string(SETTINGS.*(settings[i].valuePtr));
} }
return valueText; return valueText;
}); });

View File

@@ -11,12 +11,12 @@
class CrossPointSettings; class CrossPointSettings;
enum class SettingType { TOGGLE, ENUM, ACTION, VALUE }; enum class SettingType { TOGGLE, ENUM, ACTION, VALUE, STRING };
struct SettingInfo { struct SettingInfo {
const char* name; const char* name;
SettingType type; SettingType type;
uint8_t CrossPointSettings::* valuePtr; uint8_t CrossPointSettings::* valuePtr = nullptr;
std::vector<std::string> enumValues; std::vector<std::string> enumValues;
struct ValueRange { struct ValueRange {
@@ -24,20 +24,100 @@ struct SettingInfo {
uint8_t max; uint8_t max;
uint8_t step; uint8_t step;
}; };
ValueRange valueRange; ValueRange valueRange = {};
static SettingInfo Toggle(const char* name, uint8_t CrossPointSettings::* ptr) { const char* key = nullptr; // JSON API key (nullptr for ACTION types)
return {name, SettingType::TOGGLE, ptr}; const char* category = nullptr; // Category for web UI grouping
// Direct char[] string fields (for settings stored in CrossPointSettings)
char* stringPtr = nullptr;
size_t stringMaxLen = 0;
// Dynamic accessors (for settings stored outside CrossPointSettings, e.g. KOReaderCredentialStore)
std::function<uint8_t()> valueGetter;
std::function<void(uint8_t)> valueSetter;
std::function<std::string()> stringGetter;
std::function<void(const std::string&)> stringSetter;
static SettingInfo Toggle(const char* name, uint8_t CrossPointSettings::* ptr, const char* key = nullptr,
const char* category = nullptr) {
SettingInfo s;
s.name = name;
s.type = SettingType::TOGGLE;
s.valuePtr = ptr;
s.key = key;
s.category = category;
return s;
} }
static SettingInfo Enum(const char* name, uint8_t CrossPointSettings::* ptr, std::vector<std::string> values) { static SettingInfo Enum(const char* name, uint8_t CrossPointSettings::* ptr, std::vector<std::string> values,
return {name, SettingType::ENUM, ptr, std::move(values)}; const char* key = nullptr, const char* category = nullptr) {
SettingInfo s;
s.name = name;
s.type = SettingType::ENUM;
s.valuePtr = ptr;
s.enumValues = std::move(values);
s.key = key;
s.category = category;
return s;
} }
static SettingInfo Action(const char* name) { return {name, SettingType::ACTION, nullptr}; } static SettingInfo Action(const char* name) {
SettingInfo s;
s.name = name;
s.type = SettingType::ACTION;
return s;
}
static SettingInfo Value(const char* name, uint8_t CrossPointSettings::* ptr, const ValueRange valueRange) { static SettingInfo Value(const char* name, uint8_t CrossPointSettings::* ptr, const ValueRange valueRange,
return {name, SettingType::VALUE, ptr, {}, valueRange}; const char* key = nullptr, const char* category = nullptr) {
SettingInfo s;
s.name = name;
s.type = SettingType::VALUE;
s.valuePtr = ptr;
s.valueRange = valueRange;
s.key = key;
s.category = category;
return s;
}
static SettingInfo String(const char* name, char* ptr, size_t maxLen, const char* key = nullptr,
const char* category = nullptr) {
SettingInfo s;
s.name = name;
s.type = SettingType::STRING;
s.stringPtr = ptr;
s.stringMaxLen = maxLen;
s.key = key;
s.category = category;
return s;
}
static SettingInfo DynamicEnum(const char* name, std::vector<std::string> values, std::function<uint8_t()> getter,
std::function<void(uint8_t)> setter, const char* key = nullptr,
const char* category = nullptr) {
SettingInfo s;
s.name = name;
s.type = SettingType::ENUM;
s.enumValues = std::move(values);
s.valueGetter = std::move(getter);
s.valueSetter = std::move(setter);
s.key = key;
s.category = category;
return s;
}
static SettingInfo DynamicString(const char* name, std::function<std::string()> getter,
std::function<void(const std::string&)> setter, const char* key = nullptr,
const char* category = nullptr) {
SettingInfo s;
s.name = name;
s.type = SettingType::STRING;
s.stringGetter = std::move(getter);
s.stringSetter = std::move(setter);
s.key = key;
s.category = category;
return s;
} }
}; };
@@ -48,7 +128,13 @@ class SettingsActivity final : public ActivityWithSubactivity {
int selectedCategoryIndex = 0; // Currently selected category int selectedCategoryIndex = 0; // Currently selected category
int selectedSettingIndex = 0; int selectedSettingIndex = 0;
int settingsCount = 0; int settingsCount = 0;
const SettingInfo* settingsList = nullptr;
// Per-category settings derived from shared list + device-only actions
std::vector<SettingInfo> displaySettings;
std::vector<SettingInfo> readerSettings;
std::vector<SettingInfo> controlsSettings;
std::vector<SettingInfo> systemSettings;
const std::vector<SettingInfo>* currentSettings = nullptr;
const std::function<void()> onGoHome; const std::function<void()> onGoHome;

View File

@@ -9,8 +9,11 @@
#include <algorithm> #include <algorithm>
#include "CrossPointSettings.h"
#include "SettingsList.h"
#include "html/FilesPageHtml.generated.h" #include "html/FilesPageHtml.generated.h"
#include "html/HomePageHtml.generated.h" #include "html/HomePageHtml.generated.h"
#include "html/SettingsPageHtml.generated.h"
#include "util/StringUtils.h" #include "util/StringUtils.h"
namespace { namespace {
@@ -148,6 +151,11 @@ void CrossPointWebServer::begin() {
// Delete file/folder endpoint // Delete file/folder endpoint
server->on("/delete", HTTP_POST, [this] { handleDelete(); }); server->on("/delete", HTTP_POST, [this] { handleDelete(); });
// Settings endpoints
server->on("/settings", HTTP_GET, [this] { handleSettingsPage(); });
server->on("/api/settings", HTTP_GET, [this] { handleGetSettings(); });
server->on("/api/settings", HTTP_POST, [this] { handlePostSettings(); });
server->onNotFound([this] { handleNotFound(); }); server->onNotFound([this] { handleNotFound(); });
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap()); Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
@@ -983,6 +991,168 @@ void CrossPointWebServer::handleDelete() const {
} }
} }
void CrossPointWebServer::handleSettingsPage() const {
server->send(200, "text/html", SettingsPageHtml);
Serial.printf("[%lu] [WEB] Served settings page\n", millis());
}
void CrossPointWebServer::handleGetSettings() const {
auto settings = getSettingsList();
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
server->send(200, "application/json", "");
server->sendContent("[");
char output[512];
constexpr size_t outputSize = sizeof(output);
bool seenFirst = false;
JsonDocument doc;
for (const auto& s : settings) {
if (!s.key) continue; // Skip ACTION-only entries
doc.clear();
doc["key"] = s.key;
doc["name"] = s.name;
doc["category"] = s.category;
switch (s.type) {
case SettingType::TOGGLE: {
doc["type"] = "toggle";
if (s.valuePtr) {
doc["value"] = static_cast<int>(SETTINGS.*(s.valuePtr));
}
break;
}
case SettingType::ENUM: {
doc["type"] = "enum";
if (s.valuePtr) {
doc["value"] = static_cast<int>(SETTINGS.*(s.valuePtr));
} else if (s.valueGetter) {
doc["value"] = static_cast<int>(s.valueGetter());
}
JsonArray options = doc["options"].to<JsonArray>();
for (const auto& opt : s.enumValues) {
options.add(opt);
}
break;
}
case SettingType::VALUE: {
doc["type"] = "value";
if (s.valuePtr) {
doc["value"] = static_cast<int>(SETTINGS.*(s.valuePtr));
}
doc["min"] = s.valueRange.min;
doc["max"] = s.valueRange.max;
doc["step"] = s.valueRange.step;
break;
}
case SettingType::STRING: {
doc["type"] = "string";
if (s.stringGetter) {
doc["value"] = s.stringGetter();
} else if (s.stringPtr) {
doc["value"] = s.stringPtr;
}
break;
}
default:
continue;
}
const size_t written = serializeJson(doc, output, outputSize);
if (written >= outputSize) {
Serial.printf("[%lu] [WEB] Skipping oversized setting JSON for: %s\n", millis(), s.key);
continue;
}
if (seenFirst) {
server->sendContent(",");
} else {
seenFirst = true;
}
server->sendContent(output);
}
server->sendContent("]");
server->sendContent("");
Serial.printf("[%lu] [WEB] Served settings API\n", millis());
}
void CrossPointWebServer::handlePostSettings() {
if (!server->hasArg("plain")) {
server->send(400, "text/plain", "Missing JSON body");
return;
}
const String body = server->arg("plain");
JsonDocument doc;
const DeserializationError err = deserializeJson(doc, body);
if (err) {
server->send(400, "text/plain", String("Invalid JSON: ") + err.c_str());
return;
}
auto settings = getSettingsList();
int applied = 0;
for (auto& s : settings) {
if (!s.key) continue;
if (!doc[s.key].is<JsonVariant>()) continue;
switch (s.type) {
case SettingType::TOGGLE: {
const int val = doc[s.key].as<int>() ? 1 : 0;
if (s.valuePtr) {
SETTINGS.*(s.valuePtr) = val;
}
applied++;
break;
}
case SettingType::ENUM: {
const int val = doc[s.key].as<int>();
if (val >= 0 && val < static_cast<int>(s.enumValues.size())) {
if (s.valuePtr) {
SETTINGS.*(s.valuePtr) = static_cast<uint8_t>(val);
} else if (s.valueSetter) {
s.valueSetter(static_cast<uint8_t>(val));
}
applied++;
}
break;
}
case SettingType::VALUE: {
const int val = doc[s.key].as<int>();
if (val >= s.valueRange.min && val <= s.valueRange.max) {
if (s.valuePtr) {
SETTINGS.*(s.valuePtr) = static_cast<uint8_t>(val);
}
applied++;
}
break;
}
case SettingType::STRING: {
const std::string val = doc[s.key].as<std::string>();
if (s.stringSetter) {
s.stringSetter(val);
} else if (s.stringPtr && s.stringMaxLen > 0) {
strncpy(s.stringPtr, val.c_str(), s.stringMaxLen - 1);
s.stringPtr[s.stringMaxLen - 1] = '\0';
}
applied++;
break;
}
default:
break;
}
}
SETTINGS.saveToFile();
Serial.printf("[%lu] [WEB] Applied %d setting(s)\n", millis(), applied);
server->send(200, "text/plain", String("Applied ") + String(applied) + " setting(s)");
}
// WebSocket callback trampoline // WebSocket callback trampoline
void CrossPointWebServer::wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length) { void CrossPointWebServer::wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
if (wsInstance) { if (wsInstance) {

View File

@@ -100,4 +100,9 @@ class CrossPointWebServer {
void handleRename() const; void handleRename() const;
void handleMove() const; void handleMove() const;
void handleDelete() const; void handleDelete() const;
// Settings handlers
void handleSettingsPage() const;
void handleGetSettings() const;
void handlePostSettings();
}; };

View File

@@ -628,6 +628,7 @@
<div class="nav-links"> <div class="nav-links">
<a href="/">Home</a> <a href="/">Home</a>
<a href="/files">File Manager</a> <a href="/files">File Manager</a>
<a href="/settings">Settings</a>
</div> </div>
<div class="page-header"> <div class="page-header">

View File

@@ -77,6 +77,7 @@
<div class="nav-links"> <div class="nav-links">
<a href="/">Home</a> <a href="/">Home</a>
<a href="/files">File Manager</a> <a href="/files">File Manager</a>
<a href="/settings">Settings</a>
</div> </div>
<div class="card"> <div class="card">

View File

@@ -0,0 +1,414 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CrossPoint Reader - Settings</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
color: #333;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
h2 {
color: #34495e;
margin-top: 0;
}
.card {
background: white;
border-radius: 8px;
padding: 20px;
margin: 15px 0;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.nav-links {
margin: 20px 0;
}
.nav-links a {
display: inline-block;
padding: 10px 20px;
background-color: #3498db;
color: white;
text-decoration: none;
border-radius: 4px;
margin-right: 10px;
}
.nav-links a:hover {
background-color: #2980b9;
}
.setting-row {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 0;
border-bottom: 1px solid #eee;
}
.setting-row:last-child {
border-bottom: none;
}
.setting-name {
font-weight: 500;
color: #2c3e50;
flex: 1;
min-width: 0;
padding-right: 12px;
}
.setting-control {
flex-shrink: 0;
}
.setting-control select,
.setting-control input[type="number"],
.setting-control input[type="text"],
.setting-control input[type="password"] {
padding: 6px 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 0.95em;
background: white;
}
.setting-control select {
min-width: 160px;
}
.setting-control input[type="text"],
.setting-control input[type="password"] {
width: 220px;
}
.setting-control input[type="number"] {
width: 80px;
}
/* Toggle switch */
.toggle-switch {
display: inline-block;
position: relative;
width: 48px;
height: 26px;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: absolute;
cursor: pointer;
top: 0; left: 0; right: 0; bottom: 0;
background-color: #ccc;
border-radius: 26px;
transition: 0.3s;
}
.toggle-slider:before {
position: absolute;
content: "";
height: 20px;
width: 20px;
left: 3px;
bottom: 3px;
background-color: white;
border-radius: 50%;
transition: 0.3s;
}
.toggle-switch input:checked + .toggle-slider {
background-color: #27ae60;
}
.toggle-switch input:checked + .toggle-slider:before {
transform: translateX(22px);
}
.save-container {
text-align: center;
margin: 20px 0;
}
.save-btn {
background-color: #27ae60;
color: white;
padding: 12px 40px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1em;
font-weight: 600;
}
.save-btn:hover {
background-color: #219a52;
}
.save-btn:disabled {
background-color: #95a5a6;
cursor: not-allowed;
}
.message {
padding: 12px;
border-radius: 4px;
margin: 15px 0;
text-align: center;
display: none;
}
.message.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.message.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.loader-container {
display: flex;
justify-content: center;
align-items: center;
margin: 20px 0;
}
.loader {
width: 48px;
height: 48px;
border: 5px solid #AAA;
border-bottom-color: transparent;
border-radius: 50%;
display: inline-block;
box-sizing: border-box;
animation: rotation 1s linear infinite;
}
@keyframes rotation {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
@media (max-width: 600px) {
body {
padding: 10px;
font-size: 14px;
}
.card {
padding: 12px;
margin: 10px 0;
}
h1 {
font-size: 1.3em;
}
.nav-links a {
padding: 8px 12px;
margin-right: 6px;
font-size: 0.9em;
}
.setting-row {
flex-wrap: wrap;
gap: 6px;
}
.setting-control select,
.setting-control input[type="text"],
.setting-control input[type="password"] {
min-width: 0;
width: 100%;
}
}
</style>
</head>
<body>
<h1>⚙️ Settings</h1>
<div class="nav-links">
<a href="/">Home</a>
<a href="/files">File Manager</a>
<a href="/settings">Settings</a>
</div>
<div id="message" class="message"></div>
<div id="settings-container">
<div class="loader-container">
<span class="loader"></span>
</div>
</div>
<div class="save-container" id="save-container" style="display:none;">
<button class="save-btn" id="saveBtn" onclick="saveSettings()">Save Settings</button>
</div>
<div class="card">
<p style="text-align: center; color: #95a5a6; margin: 0;">
CrossPoint E-Reader • Open Source
</p>
</div>
<script>
let allSettings = [];
let originalValues = {};
function escapeHtml(unsafe) {
return unsafe
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#039;");
}
function showMessage(text, isError) {
const msg = document.getElementById('message');
msg.textContent = text;
msg.className = 'message ' + (isError ? 'error' : 'success');
msg.style.display = 'block';
setTimeout(function() { msg.style.display = 'none'; }, 4000);
}
function renderControl(setting) {
const id = 'setting-' + setting.key;
if (setting.type === 'toggle') {
const checked = setting.value ? 'checked' : '';
return '<label class="toggle-switch">' +
'<input type="checkbox" id="' + id + '" ' + checked + ' onchange="markChanged()">' +
'<span class="toggle-slider"></span></label>';
}
if (setting.type === 'enum') {
let html = '<select id="' + id + '" onchange="markChanged()">';
setting.options.forEach(function(opt, idx) {
const selected = idx === setting.value ? ' selected' : '';
html += '<option value="' + idx + '"' + selected + '>' + escapeHtml(opt) + '</option>';
});
html += '</select>';
return html;
}
if (setting.type === 'value') {
return '<input type="number" id="' + id + '" value="' + setting.value + '"' +
' min="' + setting.min + '" max="' + setting.max + '" step="' + setting.step + '"' +
' onchange="markChanged()">';
}
if (setting.type === 'string') {
const inputType = setting.name.toLowerCase().includes('password') ? 'password' : 'text';
const val = setting.value || '';
return '<input type="' + inputType + '" id="' + id + '" value="' + escapeHtml(val) + '"' +
' oninput="markChanged()">';
}
return '';
}
function getValue(setting) {
const el = document.getElementById('setting-' + setting.key);
if (!el) return undefined;
if (setting.type === 'toggle') {
return el.checked ? 1 : 0;
}
if (setting.type === 'enum') {
return parseInt(el.value, 10);
}
if (setting.type === 'value') {
return parseInt(el.value, 10);
}
if (setting.type === 'string') {
return el.value;
}
return undefined;
}
function markChanged() {
document.getElementById('saveBtn').disabled = false;
}
async function loadSettings() {
try {
const response = await fetch('/api/settings');
if (!response.ok) {
throw new Error('Failed to load settings: ' + response.status);
}
allSettings = await response.json();
// Store original values
originalValues = {};
allSettings.forEach(function(s) {
originalValues[s.key] = s.value;
});
// Group by category
const groups = {};
allSettings.forEach(function(s) {
if (!groups[s.category]) groups[s.category] = [];
groups[s.category].push(s);
});
const container = document.getElementById('settings-container');
let html = '';
for (const category in groups) {
html += '<div class="card"><h2>' + escapeHtml(category) + '</h2>';
groups[category].forEach(function(s) {
html += '<div class="setting-row">' +
'<span class="setting-name">' + escapeHtml(s.name) + '</span>' +
'<span class="setting-control">' + renderControl(s) + '</span>' +
'</div>';
});
html += '</div>';
}
container.innerHTML = html;
document.getElementById('save-container').style.display = '';
document.getElementById('saveBtn').disabled = true;
} catch (e) {
console.error(e);
document.getElementById('settings-container').innerHTML =
'<div class="card"><p style="text-align:center;color:#e74c3c;">Failed to load settings</p></div>';
}
}
async function saveSettings() {
const btn = document.getElementById('saveBtn');
btn.disabled = true;
btn.textContent = 'Saving...';
// Collect only changed values
const changes = {};
allSettings.forEach(function(s) {
const current = getValue(s);
if (current !== undefined && current !== originalValues[s.key]) {
changes[s.key] = current;
}
});
if (Object.keys(changes).length === 0) {
showMessage('No changes to save.', false);
btn.textContent = 'Save Settings';
return;
}
try {
const response = await fetch('/api/settings', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(changes)
});
if (!response.ok) {
const text = await response.text();
throw new Error(text || 'Save failed');
}
// Update original values to new values
for (const key in changes) {
originalValues[key] = changes[key];
}
showMessage('Settings saved successfully!', false);
} catch (e) {
console.error(e);
showMessage('Error: ' + e.message, true);
}
btn.textContent = 'Save Settings';
}
loadSettings();
</script>
</body>
</html>