While the module interface passes the settings and values thorough a JSON string, **use our helper functions**. In future we might move to a different implementation. All current modules use:
*`load_module_settings` to load the settings from the disk.
*`PowerToySettings::Settings` class to define module properties and the settings screen.
*`PowerToySettings::PowerToyValues` class to parse the JSON passed by the runner.
*`save_module_settings` to store the settings on the disk.
Most functions provide two overloads - one that accepts UINT with a resource ID and one that accepts strings. **Put all strings in the resource file and use the resource ID overload.**
The following documents internal workings of the settings system.
## Overview
PowerToys runner provides a generic way for modules to define their settings.
Each module on startup is responsible for loading its own settings and initializing accordingly. When the user wants to edit settings, the runner will call [`get_config()`](modules/interface.md#get_config) module method. The module must provide a JSON which includes the module name, description but also what settings options are provided.
When settings from all modules are collected, a separate [settings editor app](/src/settings) is spawned. The editor wraps [an React app](/src/settings-web) and handles the communication with the runner.
When loaded, the React app receives the JSON passed by the runner. When user saves the settings, the editor passes the new settings values as JSON string to the runner. Runner in turn will call [`set_config()`](modules/interface.md#set_config) for all modules with appropriate JSON. When user initiates a custom action (like the Zone Editor in FancyZones), the runner will call [`call_custom_action()`](modules/interface.md#call_custom_action) providing the action name in a JSON.
There are C++ helper functions in [/src/common/settings_objects.h](/src/common/settings_objects.h) and [/src/common/settings_helpers.h](/src/common/settings_helpers.h). Those include classes for creating the settings options JSON and ones for parsing the incoming settings JSON.
### Module settings
The value returned by the [`get_config()`](modules/interface.md#get_config) call should provide a JSON object with following fields:
*`name` - The name of the PowerToy. Used on the nav panel on the left.
*`version` - The settings version. Needs to be set to `"1.0"`.
*`description` - Description of the PowerToy module.
*`overview_link`, `video_link` - Optional links to the documentation and video preview of the PowerToy module.
*`icon_key` - Name of the icon of the PowerToy. The SVGs for the icons are located in [/src/settings-web/src/svg](/src/settings-web/src/svg). They also need to be added in [/settings-web/src/setup_icons.tsx](/settings-web/src/setup_icons.tsx).
*`properties` - Optional object that contains the definition of the settings screen.
The `properties` JSON object defines what settings controls are available to the user. Each key defines one control. The controls have some common properties:
* The key in the `properties` which identifies the control.
*`editor_type` - Defines the type of the control. Those are listed further.
*`order` - Defines the order of the elements on the settings screen.
Each `editor_type` has its own set of properties.
Example module JSON (taken from Shortcut Guide):
```json
{
"name": "Shortcut Guide",
"version": "1.0",
"description": "Shows a help overlay with Windows shortcuts when the Windows key is pressed.",
PowerToys provides [a helper class](/src/common/json.h) to parse and generate JSON strings.
In [`settings_helpers.h`](/src/common/settings_helpers.h) there are two helper functions: `load_module_settings(powertoy_name)` and `save_general_settings(settings)` for loading and saving the module configuration.
In [`settings_objects.h`](/src/common/settings_objects.h) there are some helper classes:
*`Settings` - for generating JSON with module settings definition.
*`PowerToyValues` - for parsing JSON with settings - either loaded from file or from the settings editor.
*`CustomActionObject` and `HotkeyObject` - for parsing custom actions and hotkey input specific JSON.
### General settings
General settings control the PowerToys runner and decide which modules are enabled and which are not. The general settings screen is special - while modules provide the definition of the settings controls, the available settings on the general screen are hardcoded.
General settings has following properties:
*`enabled` - Enabled/disabled status of each PowerToy.
*`startup` - Should PowerToys start at user logon.
*`theme` - Settings editor theme - `light`, `dark` or `system`.
*`system_theme` - Current Windows theme - `light` or `dark`.
*`powertoys_version` - The version of the PowerToys.
The runner combines general settings and each module settings into a single JSON that is passed to the settings editor. Example combined settings look like this:
```json
{
"general": {
"enabled": {
"FancyZones": true,
"PowerRename": true,
"Shortcut Guide": true
},
"startup": true,
"theme": "light",
"system_theme": "dark",
"powertoys_version": "0.14.2.0"
},
"powertoys": {
"FancyZones": { ... },
"PowerRename": { ... },
"Shortcut Guide":{
"name": "Shortcut Guide",
"version": "1.0",
"description": "Shows a help overlay with Windows shortcuts when the Windows key is pressed.",
declared in [`settings_helpers.h`](/src/common/settings_helpers.h). The function will return an `json::JsonObject` object containing the module settings.
Another option is using [`PowerToySettings::PowerToyValues`](/src/common/settings_objects.h#L67) class. A static method
to parse JSON string - for example when implementing [`set_config()`](modules/interface.md#set_config). The returned `PowerToyValues` object has helper methods that return `std::optional` with values, for example:
```c++
auto settings = PowerToyValues::load_from_settings_file(L"some_powertoy");
Numeric input with dials to increment and decrement the value. Parameters:
*`name` - Key for element in the JSON.
*`description` - String or resource ID of the text displayed to the user.
*`value` - Initial control value.
*`min`, `max` - Minimum and maximum values for the input. User cannot use dials to move beyond those values, if a value out of range is inserted using the keyboard, it will get clamped to the allowed range.
The color picker value is stored as `std::wstring`:
```c++
std::optional<std::wstring> value = settings.get_string_value(L"colorpicker_name");
```
### Hotkey
```c++
settings.add_hotkey(name, description, hotkey)
```
Input for capturing hotkeys. Parameters:
*`name` - Key for element in the JSON.
*`description` - String or resource ID of the text displayed to the user.
*`hotkey` - Instance of `PowerToysSettings::HotkeyObject` class.
You can create `PowerToysSettings::HotkeyObject` object either by using helper `from_settings` static method or by providing JSON object to `from_json` static method:
The `PowerToysSettings::HotkeyObject::from_settings` take following parameters:
*`win_pressed` - Is the WinKey pressed.
*`ctrl_pressed` - Is the Ctrl key pressed.
*`alt_pressed` - Is the Alt key pressed.
*`shift_pressed` - Is the Shift key pressed.
*`vk_code` - The [virtual key-code](https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes) of the key.
The displayed key is deduced from the `vk_code` using the users keyboard layout and language settings.
Similar parameters can be passed using the `from_json` static method:
and this generated JSON (`114` is the value of `VK_F5`):
```json
{
"properties": {
"hotkey_name": {
"editor_type": "hotkey",
"order": autoincremented_number,
"display_name": "description",
"value": {
"win": true,
"ctrl": true,
"alt": true,
"shift": true,
"code": 114,
"key": "F5"
}
}
}
}
```
The hotkey value is returned as JSON, with the same format as `from_json` method uses. You can use `HotkeyObject` class to parse this JSON, since it offers some helper methods. A typical example of registering a hotkey:
```c++
std::optional<json::JsonObject> value = settings.get_json(L"hotkey_name");
if (value) {
auto hotkey = PowerToysSettings::HotkeyObject::from_json(*value);