## Summary of the Pull Request Accumulated information from internal transition about the modules development, and reworked it to be added in dev docs. Also the dev docs intself was restructured to be more organized. New pages was verified by transition team. ## PR Checklist - [x] **Dev docs:** Added/updated --------- Co-authored-by: Zhaopeng Wang (from Dev Box) <zhaopengwang@microsoft.com> Co-authored-by: Hao Liu <liuhao3418@gmail.com> Co-authored-by: Peiyao Zhao <105847726+zhaopy536@users.noreply.github.com> Co-authored-by: Mengyuan <162882040+chenmy77@users.noreply.github.com> Co-authored-by: zhaopeng wang <33367956+wang563681252@users.noreply.github.com> Co-authored-by: Jaylyn Barbee <51131738+Jaylyn-Barbee@users.noreply.github.com>
5.8 KiB
Settings Implementation
This document describes how settings are implemented in PowerToys modules, including code examples for C++ and C# modules, and details on debugging settings issues.
C++ Settings Implementation
For C++ modules, the settings system is implemented in the following files:
settings_objects.h
andsettings_objects.cpp
: Define the basic settings objectssettings_helpers.h
andsettings_helpers.cpp
: Helper functions for reading/writing settingssettings_manager.h
andsettings_manager.cpp
: Main interface for managing settings
Reading Settings in C++
#include <common/settings_objects.h>
#include <common/settings_helpers.h>
auto settings = PowerToysSettings::Settings::LoadSettings(L"ModuleName");
bool enabled = settings.GetValue(L"enabled", true);
Writing Settings in C++
PowerToysSettings::Settings settings(L"ModuleName");
settings.SetValue(L"setting_name", true);
settings.Save();
C# Settings Implementation
For C# modules, the settings are accessed through the SettingsUtils
class in the Microsoft.PowerToys.Settings.UI.Library
namespace:
Reading Settings in C#
using Microsoft.PowerToys.Settings.UI.Library;
// Read settings
var settings = SettingsUtils.GetSettings<ModuleSettings>("ModuleName");
bool enabled = settings.Enabled;
Writing Settings in C#
using Microsoft.PowerToys.Settings.UI.Library;
// Write settings
settings.Enabled = true;
SettingsUtils.SaveSettings(settings.ToJsonString(), "ModuleName");
Settings Handling in Modules
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_config
function on the appropriate module - The module parses the JSON and applies the new settings
Debugging Settings
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
Common Issues
- 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
Adding a new module with settings requires changes across multiple projects. Here's a step-by-step guide with references to real implementation examples:
1. Settings UI Library (Data Models)
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
2. Settings UI (User Interface)
2.1 Add a navigation item in ShellPage.xaml
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
2.2 Create a settings page for your module
Create a new XAML page that contains all the settings controls for your module.
Example: New settings page
2.3 Implement the ViewModel
Create a ViewModel class that handles the settings data and operations for your module.
Example: ViewModel implementation
3. Module Implementation
3.1 Implement PowertoyModuleIface in dllmain.cpp
The module interface must implement the PowertoyModuleIface to allow the runner to interact with it.
Reference: PowertoyModuleIface definition
3.2 Implement Module UI
Create a UI for your module using either WPF (like ColorPicker) or WinUI3 (like Advanced Paste).
4. Runner Integration
Add your module to the known modules list in the runner so it can be brought up and initialized.
Example: Runner integration
5. Testing and Debugging
-
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.
Recommended Implementation Order
- Module/ModuleUI implementation
- Module interface (dllmain.cpp)
- Runner integration
- Settings UI implementation
- OOBE (Out of Box Experience) integration
- Other components