feat(settings): persist the theme through the settings binding

The palette lives in Lua, so C++ stores the name only and ui.setTheme()
writes through here before rebuilding and repainting.
This commit is contained in:
2026-08-04 20:44:08 -04:00
parent 291f0f20b5
commit e85adfa757
7 changed files with 60 additions and 6 deletions
+12
View File
@@ -24,3 +24,15 @@ function settings.getTimezone() end
---@return true? ok
---@return string? error
function settings.setTimezone(timezone) end
---Returns the saved palette name. Apps read ui.getTheme() instead; this
---is the stored value, which only ui.setTheme() knows how to apply.
---@return string
function settings.getTheme() end
---Persists a palette name without applying it. Call ui.setTheme(), which
---writes through here and then rebuilds the palette and repaints.
---@param theme string
---@return true? ok
---@return string? error
function settings.setTheme(theme) end
+2 -4
View File
@@ -42,7 +42,6 @@ local ui = {}
---@field on_cancel? UiHandler
---@field on_outside? UiHandler
local THEME_PATH = "/.lua/theme"
local THEMES = {
light = { background = { 255, 255, 255 }, color = { 0, 0, 0 }, accent = { 0, 120, 255 }, radius = 6 },
dark = { background = { 18, 18, 20 }, color = { 235, 235, 235 }, accent = { 166, 118, 255 }, radius = 6 },
@@ -113,7 +112,7 @@ function ui.setTheme(name)
if not THEMES[name] then
return nil, "Unknown theme"
end
local ok, err = fs.writeFile(THEME_PATH, name)
local ok, err = settings.setTheme(name)
if not ok then
return nil, err
end
@@ -126,8 +125,7 @@ function ui.setTheme(name)
return true
end
local savedTheme = fs.readFile(THEME_PATH, 32)
loadTheme(savedTheme and savedTheme:match "^%s*(.-)%s*$" or "light")
loadTheme(settings.getTheme())
local function clearState()
enterHandlers, exitHandlers, clickHandlers, painters = {}, {}, {}, {}
+12 -1
View File
@@ -17,6 +17,17 @@ fs = {
end,
}
local savedTheme = "light"
settings = {
getTheme = function()
return savedTheme
end,
setTheme = function(name)
savedTheme = name
return true
end,
}
local frameWidth, frameHeight = 320, 480
gui = {
@@ -166,7 +177,7 @@ assert(table.concat(ui.themeNames(), ",") == "dark,light,mono")
local ok, err = ui.setTheme "missing"
assert(ok == nil and err == "Unknown theme")
assert(ui.setTheme "dark" == true)
assert(files["/.lua/theme"] == "dark" and ui.getTheme() == "dark")
assert(savedTheme == "dark" and ui.getTheme() == "dark")
local events = {}
local function handler(name)
+4
View File
@@ -42,6 +42,10 @@ public:
virtual Status setRotation(int32_t degrees) = 0;
virtual std::string timezone() const = 0;
virtual Status setTimezone(const std::string& timezone) = 0;
// Only the name of a palette; the colours themselves live in Lua, so the
// firmware can read the saved theme before a lua_State exists.
virtual std::string theme() const = 0;
virtual Status setTheme(const std::string& theme) = 0;
};
// Only what the firmware alone can answer. App identity, titles, data paths,
+23
View File
@@ -47,6 +47,19 @@ int setTimezone(lua_State* state) {
state, Runtime::from(state)->settings().setTimezone({value, length}));
}
int getTheme(lua_State* state) {
const std::string theme = Runtime::from(state)->settings().theme();
lua_pushlstring(state, theme.data(), theme.size());
return 1;
}
int setTheme(lua_State* state) {
size_t length = 0;
const char* value = luaL_checklstring(state, 1, &length);
return pushStatus(state,
Runtime::from(state)->settings().setTheme({value, length}));
}
const luaL_Reg FUNCTIONS[] = {
// --- Returns the saved rotation in degrees clockwise.
// @return integer
@@ -64,6 +77,16 @@ const luaL_Reg FUNCTIONS[] = {
// @return true|nil ok
// @return string|nil error
{"setTimezone", setTimezone},
// --- Returns the saved palette name. Apps read ui.getTheme() instead; this
// --- is the stored value, which only ui.setTheme() knows how to apply.
// @return string
{"getTheme", getTheme},
// --- Persists a palette name without applying it. Call ui.setTheme(), which
// --- writes through here and then rebuilds the palette and repaints.
// @param theme string
// @return true|nil ok
// @return string|nil error
{"setTheme", setTheme},
{nullptr, nullptr},
};
File diff suppressed because one or more lines are too long
+6
View File
@@ -26,6 +26,7 @@ struct Log : LogProvider {
struct Settings : SettingsProvider {
int32_t degrees = 0;
std::string tz = "UTC0";
std::string themeName = "light";
int32_t rotation() const override { return degrees; }
Status setRotation(int32_t value) override {
degrees = value;
@@ -36,6 +37,11 @@ struct Settings : SettingsProvider {
tz = value;
return Status::success();
}
std::string theme() const override { return themeName; }
Status setTheme(const std::string& value) override {
themeName = value;
return Status::success();
}
};
struct Sys : SysProvider {