PowerToys/tools/project_template
Jaime Bernardo 864b862952
[General]Reduce installer size by flattening application paths (#27451)
* Flatten everything and succeed build

* Figure out Settings assets

* Remove UseCommonOutputDirectory tag

* Proper settings app path

* [VCM] Fix assets location

* Fix some runtime paths

* [RegistryPreview]Use MRTCore specific pri file

* [Hosts]Use MRTCore specific pri file

* [Settings]Use MRTCore specific pri file

* [Peek]Use MRTCore specific pri file

* [FileLocksmith]Use MRTCore specific pri file

* [ScreenRuler]Use MRTCore specific pri file

* [PowerRename]Use MRTCore specific pri file

* [Peek]Move assets to own folder

* [FileLocksmith] Use own Assets path

* [Hosts]Use own assets folder

* [PowerRename]Use own assets dir

* [MeasureTool] Use its own assets folder

* [ImageResizer]Use its own assets path

* Fix spellcheck

* Fix tab instead of space in project files

* Normalize target frameworks and platforms

* Remove WINRT_NO_MAKE_DETECTION flag. No longer needed?

* Fix AOT and Hosts locations

* Fix Dll version differences on dependency

* Add Common.UI.csproj as refernce to fix dll versions

* Update ControlzEx to normalize dll versions

* Update System.Management version to 7.0.2

* Add GPOWrapper to Registry Preview to fix dll versions

* [PTRun]Reference Microsoft.Extensions.Hosting to fix dll versions

* Fix remaining output paths / dll version conflicts

* [KeyboardManager]Executables still on their own directories

* Fix Monaco paths

* WinAppSDK apps get to play outside again

* Enable VCM settings again

* Fix KBM Editor path again

* [Monaco]Set to own Assets dir

* Fix installer preamble; Fix publish. remove unneeded publishes

* Remove Hardlink functions

* Installer builds again (still needs work to work)

* Readd Monaco to spellcheck excludes

* Fix spellcheck and call publish.cmd again

* [Installer] Remove components that don't need own dirs

* [Installer] Refactor Power Launcher

* [Installer] Refactor Color Picker

* [Installer] Refactor Monaco assets

* [Installer]Generate File script no longer needs to remove files

* [Installer]Refactor FileLocksmith

* [Installer] Refactor Hosts

* [Installer]Refactor Image Resizer

* [Installer]Refactor MouseUtils

* [Installer]Refactor MWB

* [Installer]Refactor MeasureTool

* [Installer]Refactor Peek

* [Installer]Refactor PowerRename and registry fixes

* [Installer]Refactor RegistryPreview

* [Installer]Refactor ShortcutGuide

* [Installer]Refactor Settings

* [Installer]Clean up some unused stuff

* [Installer]Clean up stuff for user install

* [Installer]Fix WinUi3Apps wxs

* [Installer]Fix misplaced folders

* [Installer]Move x86 VCM dll to right path

* Fix monaco language list location

* [Installer]Fix VCM directory reference

* [CI]Fix signing

* [Installer] Fix resources folder for release CI

* [ci]Looks like we still ship NLog on PowerToys Run

* [Settings]Add dependency to avoid dll collision with Experimentation

* [RegistryPreview]Move XAML files to own path

* [RegistryPreview]Fix app icon

* [Hosts]Move XAML files to their own path

* [FileLocksmith]Move XAML files to their own path

* [Peek]Move XAML files to own path

* [ScreenRuler]Move XAML files to its own path

* [Settings]Move XAML to its own path

* [ColorPicker]Move Resources to Assets

* [ShortcutGuide]Move svgs to own Assets path

* [Awake]Move images to assets path

* [Runner]Remove debug checks for PowerToys Run assets

* [PTRun]Move images to its own assets path

* [ImageResizer]Icon for context menu on own assets path

* [PowerRename]Move ico to its own path

* Remove unneeded intermediary directories

* Remove further int dirs

* Move tests to its own output path

* Fix spellcheck

* spellcheck: remove warnings

* [CppAnalyzers]Ignore rule in a tool

* [CI]Check if all deps.json files reference same versions

* fix spellcheck

* [ci]Fix task identation

* [ci]Add script to guard against asset conflicts

* [ci]Add more deps.json audit steps in the release build

* Add xbf to spellcheck expects

* Fix typo in asset conflict check scripts

* Fix some more dependency conflicts

* Downgrade CsWinRT to have the same dll version as sdk

* [ci]Do a recursive check for every deps.json

* Fix spellcheck error inside comment

* [ci]Fix asset script error

* [ci]Name deps.json verify tasks a bit better

* [ci]Improve deps json verify script output

* [ci]Update WinRT version to the same running in CI

* Also upgrade CsWinRT in NOTICE.MD

* [PowerRename]Move XAML files to own path

* [Common]Fix Settings executable path

* [ci]Verify there's no xbf files in app directories

* [installer]Fix firewall path

* [Monaco]Move new files to their proper assets path

* [Monaco]Fix paths for new files after merge

* [Feedback]Removed unneeded build conditions

* [Feedback]Clear vcxproj direct reference to frameworks

* [Feedback]RunPlugins name to hold PTRun plugins

* [Feedback]Remove unneeded foreach

* [Feedback]Shortcut Guide svgs with ** in project file

* [Feedback]Fix spellcheck
2023-07-20 00:12:46 +01:00
..
ModuleTemplate [General]Reduce installer size by flattening application paths (#27451) 2023-07-20 00:12:46 +01:00
ModuleTemplate.zip project template: fix rc file in module template (#992) 2019-12-24 17:06:58 +03:00
PowerToyTemplate.sln project template: fix rc file in module template (#992) 2019-12-24 17:06:58 +03:00
README.md Catch std::exception by reference (#20385) 2022-09-11 18:50:13 +02:00
TemplateIcon.ico FancyZones and Shortcut Guide initial commit 2019-09-05 18:12:40 +02:00

PowerToy DLL Project For Visual Studio 2022

Installation

  • Put the ModuleTemplate.zip file inside the %USERPROFILE%\Documents\Visual Studio 2022\Templates\ProjectTemplates\ folder, which is the default User project templates location. You can change that location via Tools > Options > Projects and Solutions.
  • The template will be available in Visual Studio, when adding a new project, under the Visual C++ tab.

Contributing

If you'd like to work on a PowerToy template, make required modifications to \tools\project_template\ModuleTemplate.vcxproj and then use the dedicated solution PowerToyTemplate.sln to export it as a template. Note that ModuleTemplate.vcxproj is actually a project template, therefore uncompilable, so we also have a dedicated ModuleTemplateCompileTest.vcxproj project referenced from the PowerToys.sln to help keeping the template sources up to date and verify it compiles correctly.

Create a new PowerToy Module

  • Add the new PowerToy project to the src\modules\ folder for all the relative paths to work.
  • For the module interface implementation take a look at the interface.
  • Each PowerToy is built as a DLL and in order to be loaded at run-time, the PowerToy's DLL name needs to be added to the known_dlls map in src/runner/main.cpp.

DPI Awareness

All PowerToy modules need to be DPI aware and calculate dimensions and positions of the UI elements using the Windows API for DPI awareness. The /src/common library has some helpers that you can use and extend:

PowerToy settings

Settings architecture overview

PowerToys provides a settings infrastructure to add a settings page for new modules. The PowerToys Settings application is accessed from the PowerToys tray icon, it provides a global settings page and a dedicated settings page for each module.

The PowerToys settings API provides a way to define the required information and controls for the module's settings page and methods to read and persist the settings values. A module may need a more complex way to configure the user's preferences, in that case it can provide its own custom settings editor that can be invoked from the module's settings page through a dedicated button.

The settings specification can be read at doc/specs/PowerToys-settings.md.

A PowerToy can provide this general information about itself:

A PowerToy can define settings of the following types:

  • bool_toggle: A boolean property, edited with a Toggle control.
  • int_spinner: An integer property, edited with a Spinner control.
  • string: A string property, edited with a TextBox control.
  • color_picker: A color property, edited with a ColorPicker control.
  • custom_action: A custom action property, invoked from the settings by a Button control.

Here's an example of what the settings look like in the Settings screen:

Image of the Options

How to add your module's settings page

The PowerToy can set its settings information and controls by overriding the PowerToy's Interface get_config method and returning a serialized PowerToysSettings::Settings object that's been filled with the required information and controls.

The PowerToy can receive the new values by overriding the PowerToy's Interface set_config method, parsing the serialized PowerToysSettings::PowerToyValues object and applying the new settings.

Here's an example settings implementation:

  // Return JSON with the configuration options.
  virtual bool get_config(wchar_t* buffer, int* buffer_size) override {
    HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);

    // Create a Settings object.
    PowerToysSettings::Settings settings(hinstance, get_name());
    settings.set_description(L"Serves as an example powertoy, with example settings.");

    // Show an overview link in the Settings page
    settings.set_overview_link(L"https://github.com/microsoft/PowerToys");

    // Show a video link in the Settings page.
    settings.set_video_link(L"https://www.youtube.com/watch?v=d3LHo2yXKoY&t=21462");

    // Add a bool property with a toggle editor.
    settings.add_bool_toggle(
      L"test_bool_toggle", // property name.
      L"This is what a BoolToggle property looks like", // description or resource id of the localized string.
      g_settings.test_bool_prop // property value.
    );

    // Add an integer property with a spinner editor.
    settings.add_int_spinner(
      L"test_int_spinner", // property name
      L"This is what a IntSpinner property looks like", // description or resource id of the localized string.
      g_settings.test_int_prop, // property value.
      0, // min value.
      100, // max value.
      10 // incremental step.
    );

    // Add a string property with a textbox editor.
    settings.add_string(
      L"test_string_text", // property name.
      L"This is what a String property looks like", // description or resource id of the localized string.
      g_settings.test_string_prop // property value.
    );

    // Add a string property with a color picker editor.
    settings.add_color_picker(
      L"test_color_picker", // property name.
      L"This is what a ColorPicker property looks like", // description or resource id of the localized string.
      g_settings.test_color_prop // property value.
    );

    // Add a custom action property. When using this settings type, the "PowertoyModuleIface::call_custom_action()"
    // method should be overridden as well.
    settings.add_custom_action(
      L"test_custom_action", // action name.
      L"This is what a CustomAction property looks like", // label above the field.
      L"Call a custom action", // button text.
      L"Press the button to call a custom action in the Example PowerToy" // display values / extended info.
    );

    return settings.serialize_to_buffer(buffer, buffer_size);
  }

  // Called by the runner to pass the updated settings values as a serialized JSON.
  virtual void set_config(const wchar_t* config) override { 
    try {
      // Parse the input JSON string.
      PowerToysSettings::PowerToyValues values =
        PowerToysSettings::PowerToyValues::from_json_string(config);

      // Update the bool property.
      if (values.is_bool_value(L"test_bool_toggle")) {
        g_settings.test_bool_prop = values.get_bool_value(L"test_bool_toggle");
      }

      // Update the int property.
      if (values.is_int_value(L"test_int_spinner")) {
        g_settings.test_int_prop = values.get_int_value(L"test_int_spinner");
      }

      // Update the string property.
      if (values.is_string_value(L"test_string_text")) {
        g_settings.test_string_prop = values.get_string_value(L"test_string_text");
      }

      // Update the color property.
      if (values.is_string_value(L"test_color_picker")) {
        g_settings.test_color_prop = values.get_string_value(L"test_color_picker");
      }

      // If you don't need to do any custom processing of the settings, proceed
      // to persists the values calling:
      values.save_to_settings_file();
      // Otherwise call a custom function to process the settings before saving them to disk:
      // save_settings();
    }
    catch (std::exception& ex) {
      // Improper JSON.
    }
  }

Settings Informations

The PowerToys settings object supports adding additional information to a PowerToys Settings description:

name

The name of the PowerToy. Its a required information that's applied in the settings object constructor:

PowerToysSettings::Settings settings(hinstance, get_name());

description

A short description of the PowerToy.

settings.set_description(L"Serves as an example powertoy, with example settings.");

or

settings.set_description(description_resource_id);

where description_resource_id is the UINT index of a resource string in the project .rc file.

icon_key

The identifier of the PowerToy icon in the settings-web project. By default, a CircleRing icon from FabricUI is shown for the PowerToy if no icon is specified.

settings.set_icon_key(L"pt-shortcut-guide");

A link to an extended overview of the PowerToy.

settings.set_overview_link(L"https://github.com/microsoft/PowerToys");

A link to a video showcasing the PowerToy.

settings.set_video_link(L"https://www.youtube.com/watch?v=d3LHo2yXKoY&t=21462");

Setting Controls

bool_toggle

A boolean property, edited with a Toggle control.

It can be added to a Settings object by calling add_bool_toggle.

// Add a bool property with a toggle editor.
settings.add_bool_toggle(
  L"test_bool_toggle", // property name.
  L"This is what a BoolToggle property looks like", // description or resource id of the localized string.
  g_settings.test_bool_prop // property value.
);

It can be read from a PowerToyValues object by calling get_bool_value.

// Update the bool property.
if (values.is_bool_value(L"test_bool_toggle")) {
  g_settings.test_bool_prop = values.get_bool_value(L"test_bool_toggle");
}

int_spinner

An integer property, edited with a Spinner control.

It can be added to a Settings object by calling add_int_spinner.

// Add an integer property with a spinner editor.
settings.add_int_spinner(
  L"test_int_spinner", // property name
  L"This is what a IntSpinner property looks like", // description or resource id of the localized string.
  g_settings.test_int_prop, // property value.
  0, // min value.
  100, // max value.
  10 // incremental step.
);

It can be read from a PowerToyValues object by calling get_int_value.

// Update the int property.
if (values.is_int_value(L"test_int_spinner")) {
  g_settings.test_int_prop = values.get_int_value(L"test_int_spinner");
}

string

A string property, edited with a TextBox control.

It can be added to a Settings object by calling add_string.

// Add a string property with a textbox editor.
settings.add_string(
  L"test_string_text", // property name.
  L"This is what a String property looks like", // description or resource id of the localized string.
  g_settings.test_string_prop // property value.
);

It can be read from a PowerToyValues object by calling get_string_value.

// Update the string property.
if (values.is_string_value(L"test_string_text")) {
  g_settings.test_string_prop = values.get_string_value(L"test_string_text");
}

color_picker

A color property, edited with a ColorPicker control. Its value is a string with the '#RRGGBB' format, with two hexadecimal digits for each color component.

It can be added to a Settings object by calling add_color_picker.

// Add a string property with a color picker editor.
settings.add_color_picker(
  L"test_color_picker", // property name.
  L"This is what a ColorPicker property looks like", // description or resource id of the localized string.
  g_settings.test_color_prop // property value.
);

The '#RRGGBB'-format string can be read from a PowerToyValues object by calling get_string_value.

// Update the color property.
if (values.is_string_value(L"test_color_picker")) {
  g_settings.test_color_prop = values.get_string_value(L"test_color_picker");
}

custom_action

A custom action property, invoked from the settings by a Button control. This can be used to spawn a custom editor by the PowerToy.

It can be added to a Settings object by calling add_custom_action.

// Add a custom action property. When using this settings type, the "PowertoyModuleIface::call_custom_action()"
// method should be overridden as well.
settings.add_custom_action(
  L"test_custom_action", // action name.
  L"This is what a CustomAction property looks like", // label above the field: a string literal or a resource id
  L"Call a custom action", // button text: a string literal or a resource id
  L"Press the button to call a custom action in the Example PowerToy" // display values / extended info: a string literal or a resource id
);

When the custom action button is pressed, the PowerToy's call_custom_action() is called with a serialized PowerToysSettings::CustomActionObject object.

  // Signal from the Settings editor to call a custom action.
  // This can be used to spawn more complex editors.
  virtual void call_custom_action(const wchar_t* action) override {
    static UINT custom_action_num_calls = 0;
    try {
      // Parse the action values, including name.
      PowerToysSettings::CustomActionObject action_object =
        PowerToysSettings::CustomActionObject::from_json_string(action);

      if (action_object.get_name() == L"test_custom_action") {
        // Custom action code to increase and show a counter.
        ++custom_action_num_calls;
        std::wstring msg(L"I have been called ");
        msg += std::to_wstring(custom_action_num_calls);
        msg += L" time(s).";
        MessageBox(NULL, msg.c_str(), L"Custom action call.", MB_OK | MB_TOPMOST);
      }
    }
    catch (std::exception& ex) {
      // Improper JSON.
    }
  }

Settings Persistence

By default, the PowerToys settings are persisted in the User's %LocalAppData%\Microsoft\PowerToys path. Each PowerToy has its own folder for saving the persisted settings data.

Loading and saving the settings in the default location can be achieved through the use of a PowerToysSettings::PowerToyValues object.

Loading settings

The PowerToy can load the saved PowerToyValues object through the use of the load_from_settings_file method.

Here's an example:

// Load the settings file.
void ExamplePowertoy::init_settings() {
  try {
    // Load and parse the settings file for this PowerToy.
    PowerToysSettings::PowerToyValues settings =
      PowerToysSettings::PowerToyValues::load_from_settings_file(get_name());

    // Load the bool property.
    if (settings.is_bool_value(L"test_bool_toggle")) {
      g_settings.test_bool_prop = settings.get_bool_value(L"test_bool_toggle");
    }

    // Load the int property.
    if (settings.is_int_value(L"test_int_spinner")) {
      g_settings.test_int_prop = settings.get_int_value(L"test_int_spinner");
    }

    // Load the string property.
    if (settings.is_string_value(L"test_string_text")) {
      g_settings.test_string_prop = settings.get_string_value(L"test_string_text");
    }

    // Load the color property.
    if (settings.is_string_value(L"test_color_picker")) {
      g_settings.test_color_prop = settings.get_string_value(L"test_color_picker");
    }
  }
  catch (std::exception& ex) {
    // Error while loading from the settings file. Let default values stay as they are.
  }
}

Saving settings

The PowerToy can save the PowerToyValues object received in set_config through the use of the save_to_settings_file method.

Here's an example:

// Called by the runner to pass the updated settings values as a serialized JSON.
virtual void set_config(const wchar_t* config) override { 
  try {
    // Parse the input JSON string.
    PowerToysSettings::PowerToyValues values =
      PowerToysSettings::PowerToyValues::from_json_string(config);
...
    values.save_to_settings_file();
  }
  catch (std::exception& ex) {
    // Improper JSON.
  }
}

Alternatively, the PowerToyValues object can be built manually and then saved if more complex logic is needed:

// This method of saving the module settings is only required if you need to do any
// custom processing of the settings before saving them to disk.
void ExamplePowertoy::save_settings() {
  try {
    // Create a PowerToyValues object for this PowerToy
    PowerToysSettings::PowerToyValues values(get_name());

    // Save the bool property.
    values.add_property(
      L"test_bool_toggle", // property name
      g_settings.test_bool_prop // property value
    );

    // Save the int property.
    values.add_property(
      L"test_int_spinner", // property name
      g_settings.test_int_prop // property value
    );

    // Save the string property.
    values.add_property(
      L"test_string_text", // property name
      g_settings.test_string_prop // property value
    );

    // Save the color property.
    values.add_property(
      L"test_color_picker", // property name
      g_settings.test_color_prop // property value
    );

    // Save the PowerToyValues JSON to the power toy settings file.
    values.save_to_settings_file();
  }
  catch (std::exception& ex) {
    // Couldn't save the settings.
  }
}

Add a new PowerToy to the Installer

In the installer folder, open the PowerToysSetup.sln solution. Under the PowerToysSetup project, edit Product.wxs. You will need to add a component for your module DLL. Search for Module_ShortcutGuide to see where to add the component declaration and where to reference that declaration so the DLL is added to the installer. Each component requires a newly generated GUID (you can use the Visual Studio integrated tool to generate one). Repeat the process for each extra file your PowerToy module requires. If your PowerToy comes with a subfolder containing for example images, follow the example of the PowerToysSvgs component.