This document describes how settings are implemented in PowerToys modules, including code examples for C++ and C# modules, and details on debugging settings issues.
For C++ modules, the settings system is implemented in the following files:
settings_objects.handsettings_objects.cpp: Define the basic settings objectssettings_helpers.handsettings_helpers.cpp: Helper functions for reading/writing settingssettings_manager.handsettings_manager.cpp: Main interface for managing settings
#include <common/settings_objects.h>
#include <common/settings_helpers.h>
auto settings = PowerToysSettings::Settings::LoadSettings(L"ModuleName");
bool enabled = settings.GetValue(L"enabled", true);PowerToysSettings::Settings settings(L"ModuleName");
settings.SetValue(L"setting_name", true);
settings.Save();For C# modules, the settings are accessed through the SettingsUtils class in the Microsoft.PowerToys.Settings.UI.Library namespace:
using Microsoft.PowerToys.Settings.UI.Library;
// Read settings
var settings = SettingsUtils.Default.GetSettings<ModuleSettings>("ModuleName");
bool enabled = settings.Enabled;using Microsoft.PowerToys.Settings.UI.Library;
// Write settings
settings.Enabled = true;
SettingsUtils.Default.SaveSettings(settings.ToJsonString(), "ModuleName");Each PowerToys module must implement settings-related functions in its module interface:
// Get the module's settings
virtual PowertoyModuleSettings get_settings() = 0;
// Called when settings are changed
virtual void set_config(const wchar_t* config_string) = 0;When the user changes settings in the UI:
- The settings UI serializes the settings to JSON
- The JSON is sent to the PowerToys runner via IPC
- The runner calls the
set_configfunction on the appropriate module - The module parses the JSON and applies the new settings
Steps to enable conflict detection for a hotkey:
Ensure the module interface provides either size_t get_hotkeys(Hotkey* hotkeys, size_t buffer_size) or std::optional<HotkeyEx> GetHotkeyEx().
- If not yet implemented, you need to add it so that it returns all hotkeys used by the module.
- Important: The order of the returned hotkeys matters. This order is used as an index to uniquely identify each hotkey for conflict detection and lookup.
- For reference, see:
src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp
Make sure the module’s settings file inherits from IHotkeyConfig and implements HotkeyAccessor[] GetAllHotkeyAccessors().
- This method should return all hotkeys used in the module.
- Important: The order of the returned hotkeys must be consistent with step 1 (
get_hotkeys()orGetHotkeyEx()). - For reference, see:
src/settings-ui/Settings.UI.Library/AdvancedPasteSettings.cs - Note:
HotkeyAccessoris a wrapper around HotkeySettings. It provides bothgetterandsettermethods to read and update the corresponding hotkey. Additionally, eachHotkeyAccessorrequires a resource string that describes the purpose of the hotkey. This string is typically defined in:src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
The corresponding ViewModel should inherit from PageViewModelBase and implement Dictionary<string, HotkeySettings[]> GetAllHotkeySettings().
- This method should return all hotkeys, maintaining the same order as in steps 1 and 2.
- For reference, see:
src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs
Once the module’s view is loaded, make sure to invoke the ViewModel’s OnPageLoaded() method:
Loaded += (s, e) => ViewModel.OnPageLoaded();- For reference, see:
src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPaste.xaml.cs
To debug settings issues:
- Check the settings files in
%LOCALAPPDATA%\Microsoft\PowerToys\ - Ensure JSON is well-formed
- Monitor IPC communication between settings UI and runner using debugger breakpoints at key points:
- In the Settings UI when sending configuration changes
- In the Runner when receiving and dispatching changes
- In the Module when applying changes
- Look for log messages related to settings changes in the PowerToys logs
- Settings not saving: Check file permissions or conflicts with other processes accessing the file
- Settings not applied: Verify IPC communication is working and the module is properly handling the configuration
- Incorrect settings values: Check JSON parsing and type conversion in the module code
Adding a new module with settings requires changes across multiple projects. Here's a step-by-step guide with references to real implementation examples:
Define the data models for your module's settings in the Settings UI Library project. These data models will be serialized to JSON configuration files stored in %LOCALAPPDATA%\Microsoft\PowerToys\.
Example: Settings UI Library implementation
The ShellPage.xaml is the entry point for the PowerToys settings, providing a navigation view of all modules. Add a navigation item for your new module.
Example: Adding navigation item
Create a new XAML page that contains all the settings controls for your module.
Example: New settings page
Create a ViewModel class that handles the settings data and operations for your module.
Example: ViewModel implementation
The module interface must implement the PowertoyModuleIface to allow the runner to interact with it.
Reference: PowertoyModuleIface definition
Create a UI for your module using either WPF (like ColorPicker) or WinUI3 (like Advanced Paste).
Add your module to the known modules list in the runner so it can be brought up and initialized.
Example: Runner integration
-
Test each component individually:
- Verify settings serialization/deserialization
- Test module activation/deactivation
- Test IPC communication
-
For signal-related issues, ensure all modules work correctly before debugging signal handling.
-
You can debug each module directly in Visual Studio or by attaching to running processes.
- Module/ModuleUI implementation
- Module interface (dllmain.cpp)
- Runner integration
- Settings UI implementation
- OOBE (Out of Box Experience) integration
- Other components