Settings¶
app.settings holds the app's preferences, saved in UserDefaults and loaded back on the next launch. Assign a dict of defaults to create it:
import nib
def main(app: nib.App):
app.settings = {
"dark_mode": False,
"font_size": 14,
"username": "",
}
app.settings.font_size # the value saved by an earlier launch, or 14
app.settings.font_size = 16 # saved in the background
app.build(nib.Text(f"Hello {app.settings.username or 'there'}"))
nib.run(main)
Assigning loads the values saved by earlier launches before it returns, so the next line already reads them. Every name in the dict is a setting; any other name raises AttributeError, so a typo fails instead of saving a new key. Setting names may not start with _ or match a method below. Assign the defaults in main() or a handler: in App.__init__ the runtime is not connected yet and the assignment raises nib.NotConnectedError.
Values¶
app.settings.font_size # read: the saved value, or the default
app.settings.font_size = 16 # write: kept at once, saved in the background
Reads come from memory. Writes update memory at once and are saved in the background.
A value keeps the type of its default: a float for an int setting is rounded, so a Slider bound to it stores whole numbers (14.6 is stored as 15), and an int for a float setting becomes a float. Assigning None resets the setting to its default.
Methods¶
| Method | Returns | Description |
|---|---|---|
bind(name) |
SettingBinding |
Tie a setting to a control, see below |
on_change(*names) |
decorator | Run a function after one of names changes; after any change with no names |
remove_on_change(callback) |
None |
Stop calling a function registered with on_change |
reset(*names) |
None |
Remove the saved values of names, or of every setting, so the defaults apply |
get(name, default=None) |
Any |
A value by name, or default for a name not in the defaults |
set(name, value) |
None |
Set a value by name |
to_dict() |
dict |
Every setting with its value |
bind(name)¶
Pass the binding where a control takes its value:
| Control | Parameter |
|---|---|
Toggle |
is_on |
Slider, TextField, SecureField, TextEditor |
value |
Picker |
selection |
The control starts from the setting, saves what the user chooses, and follows the setting when code changes it. Its own on_change still runs, after the setting is saved.
import nib
def main(app: nib.App):
app.settings = {"dark_mode": False, "font_size": 14, "theme": "Light"}
app.build(
nib.Form(
controls=[
nib.Toggle("Dark mode", is_on=app.settings.bind("dark_mode")),
nib.Slider(
value=app.settings.bind("font_size"), min_value=10, max_value=24
),
nib.Picker(
label="Theme",
selection=app.settings.bind("theme"),
options=["Light", "Dark", "Sepia"],
),
],
padding=16,
)
)
nib.run(main)
on_change(*names)¶
@app.settings.on_change("font_size", "theme")
def restyle():
editor.style = nib.TextEditorStyle(font=nib.Font.system(app.settings.font_size))
The function takes no arguments and reads app.settings. It runs on the event thread after the value is saved, whether a bound control or code changed it. Setting a value to what it already is does not call it.
reset(*names)¶
app.settings.reset("font_size") # one setting
app.settings.reset() # all of them
reset() removes the saved values instead of writing the defaults, so a default changed in a later version of the app applies.
Storage¶
Settings go to the app's UserDefaults domain, its bundle identifier, under their own names. nib run, python main.py and the built app use the same domain, taken from [tool.nib.build] in pyproject.toml (identifier, or else name), or from the app's title in the script.
For keys that are not settings, such as a cache or the last window position, use nib.UserDefaults. It writes to the same domain, so app.defaults.clear() removes the settings too.
Related¶
- SettingsWindow: the preferences window
- UserDefaults: direct key-value access
- MenuItem: a menu item that opens the settings window