Settings that persist¶
A struct of fields can also be the app’s settings file. PRISM loads it when the app starts and saves it shortly after each change. The user finds the app as they left it.
The smallest example¶
#include <prism/prism.hpp>
#include <prism/app/settings.hpp> // the file code: only an app with settings pays for it
using namespace prism;
enum class Units { Metric, Imperial };
struct Settings {
core::Field<ui::Slider<>> volume{{.value = 0.6}};
core::Field<ui::Checkbox> autosave{{.checked = true, .label = "Save as I type"}};
core::Field<Units> units{Units::Metric};
app::WindowGeometry window; // the window reopens at its last size and place
};
struct MyApp {
Settings settings;
void view(app::ViewBuilder& vb) {
vb.vstack(settings.volume, settings.autosave, settings.units);
}
};
int main() {
MyApp app;
return app::run(app, {.title = "My App", .settings = &app.settings,
.organization = "my-lab"});
}
Move the slider and quit. The next start shows the slider where you left it. The file looks like this:
volume = 0.6
autosave = true
units = "Metric"
[window]
width = 800
height = 600
# then x, y, positioned and maximized
What is saved, and how¶
Each member of the struct is one key.
Member |
Written as |
|---|---|
|
The value. |
A widget field: |
Only its value: the slider’s position, the text. Its range and label stay in the code. |
|
Whether it is ticked. |
|
The value’s name, |
A plain struct member, such as a dock layout |
Written whole. |
A nested struct of fields |
A |
Saving waits for a short pause (300 ms), so a slider being dragged is written once, not a hundred times. The write happens on a thread of its own and goes through a temporary file, so a crash never leaves half a file. Quitting writes the last change.
Where the file goes¶
The file is settings.toml, in a folder named after the organization and the app:
System |
Folder |
|---|---|
Linux |
|
Windows |
|
macOS |
|
Browser |
The page’s IndexedDB storage. |
<name> is AppConfig::name, or the title in lower case with dashes: "My App" gives
my-app. backends::settings_dir(organization, name) gives the folder, for files of your own.
The window’s size and place¶
A member app::WindowGeometry window (that exact name) makes the main window reopen as it was
left:
Member |
Meaning |
|---|---|
|
The last size. 0 means nothing is remembered yet. |
|
The last place, and whether it is known. |
|
Whether it was maximized. |
Note
Wayland does not let an app place its window. There, only the size and the maximized state come back.
Split panes¶
A splitter’s pane sizes are settings like any other. Keep a core::Field<ui::SplitSizes> in the
struct and call vb.remember_split(field) inside the split container. See
Layout.
Without app::run¶
app::SettingsFile does the loading and saving on its own. It loads in its constructor and
writes the last change in its destructor:
Settings s;
app::SettingsFile file{s, "/path/to/settings.toml"};
if (!file.load_error().empty()) std::println("{}", file.load_error());
Call |
What it does |
|---|---|
|
Loads |
|
Why the file did not load. Empty when it did. |
|
The settings as TOML text. |
|
Sets every field the text names. Returns a |
|
Reads a file once. A missing file is not an error. |
|
Writes a file once. |
Old and broken files¶
A key the struct does not have is skipped. A file from an older or newer version still loads.
An enum name the code does not know is skipped: that field keeps its default.
A value of the wrong type is not skipped: the file does not parse.
count = "not a number"takes down every other key beside it, so a validvolume = 0.1in the same file is dropped too. The file is renamedsettings.toml.broken, and the app starts with the defaults.load_error()says why.
Pitfalls¶
Only
Fieldmembers trigger a save. A plainintmember is written, but changing it does not save the file.A
Field<ui::Password<>>does not compile in a settings struct: a password does not belong in a plain file.The struct must not sit in an anonymous namespace. The TOML code reads member names, and clang cannot see them there.
Settings are read and changed on the logic thread, like every field.