Preferences
Monterey keeps an internal list of Settings, which are displayed and can be modified on the Preferences screen:
The definition of a Setting is as follows:
export interface Setting {
title: string;
type: 'string'|'boolean';
identifier: string;
visible?: boolean;
value?: string|boolean|undefined;
options?: Array<{ value: any, display: string }>;
}
The following fields are accepted:
Field | Description |
---|---|
title | The title is displayed to the user in the Preferences screen |
type | What is the type of the value of this setting? Checkboxes are used for boolean values, textboxes for string with no `options` and comboboxes for strings with `options` |
identifier | Unique idenfitier of this setting (such as `show-finished-tasks`) |
visible | Should this setting be displayed in the Preferences screen? |
value | The value of the setting |
options | Array of objects with value and display properties. Useful for comboboxes for settings like `Language`, where there is a finite amount of possible values |
The TaskManager modal defines such setting:
this.settings.addSetting({
identifier: 'show-finished-tasks',
title: 'Show finished tasks in taskmanager?',
type: 'boolean',
value: false
});
The Settings class, used by all parts of the application such as the TaskManager above, is responsible for:
- Adding settings
- Getting a value of a setting
- Updating the value of a setting
- Saving
Adding settings
Settings must always be registered to the Settings class, as otherwise it is not possible to render all settings in the Preferences screen. This is why the TaskManager plugin adds the show-finished-tasks
setting from the constructor, so that Monterey knows that this setting exists even if you don't use the TaskManager.
New settings can be added by calling addSettings
:
this.settings.addSetting({
identifier: 'show-finished-tasks',
title: 'Show finished tasks in taskmanager?',
type: 'boolean',
value: false
});
This addSetting
call will make Monterey aware of the fact that this setting exists, and it will render this new setting in the Preferences screen automatically.
Getting the value of a setting
You can get the value of a setting by calling getValue
on the Settings class. For example, the TaskManager modal uses this function here:
this.settings.getValue('show-finished-tasks');
In this case, because the value
of the show-finished-tasks
setting is a boolean
, it will return either true or false. The user is able to change the value of this setting on the Preferences screen.
Updating the value of
The recommended way of updating the values settings is by using the setValue
function. The TaskManager calls this function here, whenever the user clicks the "Show finished" checkbox:
this.settings.setValue('show-finished-tasks', this.showFinished);
Right after, the settings are saved with the save
function:
await this.settings.save();