HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
How to use preferences

Preferences

Opens the preferences file of the application. The location of the preferences file is operating system depended. On windows this file is located in: \USERPROFILE%\AppData\Local\<vendor>\<application name>\preferences.json

auto preferences = tt::preferences(tt::URL::urlFromApplicationPreferencesFile());
user preferences.
Definition preferences.hpp:107

Observable values are linked to values in the preferences located using a json-path. In the example below the json-path is "bar.foo" which results in the preferences file as {"bar": {"foo": 1}}:

preferences.add("bar.foo", foo);
// Everytime the program loads the value in the preference file is incremented.
++foo;
A value which can be observed for modifications.
Definition observable.hpp:464

Complex types

The example above will work with observers with types that can be natively stored in a json file: integers, floating point, booleans, strings, vectors and maps.

For more complex types, you will need to add a template specialization for tt::pickle. This specialization adds an encoder and decoder between the complex type and a tt::datum. The tt::preferences knows how to read and write a json file using tt::datum values.

The example below shows how to specialize tt::pickle for the complex foo_type type:

struct foo_type {
int one;
int two;
friend bool operator==(foo_type const &, foo_type const &) = default;
};
template<>
struct tt::pickle<foo_type> {
[[nodiscard]] datum encode(foo_type const& x) const noexcept {
return datum::make_map("one", x.one, "two", x.two);
}
[[nodiscard]] foo_type decode(datum const& x) const {
return foo_type{ static_cast<int>(x["one"]), static_cast<int>(x["two"]) };
};
};
preferences.add("foo", foo);
tt_log_info("old: {}", foo->one);
foo->one += 1;
tt_log_info("new: {}", foo->one);
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:22
T decode(datum rhs) const
Decode a UTF-8 string into a value of a given type.
datum encode(T const &rhs) const noexcept
Encode the value of a type into a UTF-8 string.