mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
Format shortcut_example_powertoy according to .clang-format
This commit is contained in:
@@ -7,8 +7,10 @@
|
|||||||
|
|
||||||
extern "C" IMAGE_DOS_HEADER __ImageBase;
|
extern "C" IMAGE_DOS_HEADER __ImageBase;
|
||||||
|
|
||||||
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
|
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
||||||
switch (ul_reason_for_call) {
|
{
|
||||||
|
switch (ul_reason_for_call)
|
||||||
|
{
|
||||||
case DLL_PROCESS_ATTACH:
|
case DLL_PROCESS_ATTACH:
|
||||||
Trace::RegisterProvider();
|
Trace::RegisterProvider();
|
||||||
break;
|
break;
|
||||||
@@ -23,7 +25,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReser
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PowerToy sample settings.
|
// PowerToy sample settings.
|
||||||
struct SampleSettings {
|
struct SampleSettings
|
||||||
|
{
|
||||||
bool test_bool_prop = true;
|
bool test_bool_prop = true;
|
||||||
int test_int_prop = 10;
|
int test_int_prop = 10;
|
||||||
std::wstring test_string_prop = L"The quick brown fox jumps over the lazy dog";
|
std::wstring test_string_prop = L"The quick brown fox jumps over the lazy dog";
|
||||||
@@ -31,7 +34,8 @@ struct SampleSettings {
|
|||||||
} g_settings;
|
} g_settings;
|
||||||
|
|
||||||
// Implement the PowerToy Module Interface and all the required methods.
|
// Implement the PowerToy Module Interface and all the required methods.
|
||||||
class ExamplePowertoy : public PowertoyModuleIface {
|
class ExamplePowertoy : public PowertoyModuleIface
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
// The PowerToy state.
|
// The PowerToy state.
|
||||||
bool m_enabled = false;
|
bool m_enabled = false;
|
||||||
@@ -42,17 +46,20 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
ExamplePowertoy() {
|
ExamplePowertoy()
|
||||||
|
{
|
||||||
init_settings();
|
init_settings();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Destroy the powertoy and free memory
|
// Destroy the powertoy and free memory
|
||||||
virtual void destroy() override {
|
virtual void destroy() override
|
||||||
|
{
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the display name of the powertoy, this will be cached
|
// Return the display name of the powertoy, this will be cached
|
||||||
virtual const wchar_t* get_name() override {
|
virtual const wchar_t* get_name() override
|
||||||
|
{
|
||||||
return L"Example Powertoy";
|
return L"Example Powertoy";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +67,8 @@ public:
|
|||||||
// nullptr as the last element of the array. Nullptr can also be retured for empty
|
// nullptr as the last element of the array. Nullptr can also be retured for empty
|
||||||
// list.
|
// list.
|
||||||
// Right now there is only lowlevel keyboard hook event
|
// Right now there is only lowlevel keyboard hook event
|
||||||
virtual const wchar_t** get_events() override {
|
virtual const wchar_t** get_events() override
|
||||||
|
{
|
||||||
static const wchar_t* events[] = { ll_keyboard,
|
static const wchar_t* events[] = { ll_keyboard,
|
||||||
win_hook_event,
|
win_hook_event,
|
||||||
nullptr };
|
nullptr };
|
||||||
@@ -68,7 +76,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return JSON with the configuration options.
|
// Return JSON with the configuration options.
|
||||||
virtual bool get_config(wchar_t* buffer, int* buffer_size) override {
|
virtual bool get_config(wchar_t* buffer, int* buffer_size) override
|
||||||
|
{
|
||||||
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
|
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
|
||||||
|
|
||||||
// Create a Settings object.
|
// Create a Settings object.
|
||||||
@@ -126,14 +135,17 @@ public:
|
|||||||
|
|
||||||
// Signal from the Settings editor to call a custom action.
|
// Signal from the Settings editor to call a custom action.
|
||||||
// This can be used to spawn more complex editors.
|
// This can be used to spawn more complex editors.
|
||||||
virtual void call_custom_action(const wchar_t* action) override {
|
virtual void call_custom_action(const wchar_t* action) override
|
||||||
|
{
|
||||||
static UINT custom_action_num_calls = 0;
|
static UINT custom_action_num_calls = 0;
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
// Parse the action values, including name.
|
// Parse the action values, including name.
|
||||||
PowerToysSettings::CustomActionObject action_object =
|
PowerToysSettings::CustomActionObject action_object =
|
||||||
PowerToysSettings::CustomActionObject::from_json_string(action);
|
PowerToysSettings::CustomActionObject::from_json_string(action);
|
||||||
|
|
||||||
if (action_object.get_name() == L"test_custom_action") {
|
if (action_object.get_name() == L"test_custom_action")
|
||||||
|
{
|
||||||
// Custom action code to increase and show a counter.
|
// Custom action code to increase and show a counter.
|
||||||
++custom_action_num_calls;
|
++custom_action_num_calls;
|
||||||
std::wstring msg(L"I have been called ");
|
std::wstring msg(L"I have been called ");
|
||||||
@@ -142,35 +154,42 @@ public:
|
|||||||
MessageBox(NULL, msg.c_str(), L"Custom action call.", MB_OK | MB_TOPMOST);
|
MessageBox(NULL, msg.c_str(), L"Custom action call.", MB_OK | MB_TOPMOST);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& ex) {
|
catch (std::exception& ex)
|
||||||
|
{
|
||||||
// Improper JSON.
|
// Improper JSON.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called by the runner to pass the updated settings values as a serialized JSON.
|
// Called by the runner to pass the updated settings values as a serialized JSON.
|
||||||
virtual void set_config(const wchar_t* config) override {
|
virtual void set_config(const wchar_t* config) override
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
// Parse the input JSON string.
|
// Parse the input JSON string.
|
||||||
PowerToysSettings::PowerToyValues values =
|
PowerToysSettings::PowerToyValues values =
|
||||||
PowerToysSettings::PowerToyValues::from_json_string(config);
|
PowerToysSettings::PowerToyValues::from_json_string(config);
|
||||||
|
|
||||||
// Update the bool property.
|
// Update the bool property.
|
||||||
if (values.is_bool_value(L"test_bool_toggle")) {
|
if (values.is_bool_value(L"test_bool_toggle"))
|
||||||
|
{
|
||||||
g_settings.test_bool_prop = values.get_bool_value(L"test_bool_toggle");
|
g_settings.test_bool_prop = values.get_bool_value(L"test_bool_toggle");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the int property.
|
// Update the int property.
|
||||||
if (values.is_int_value(L"test_int_spinner")) {
|
if (values.is_int_value(L"test_int_spinner"))
|
||||||
|
{
|
||||||
g_settings.test_int_prop = values.get_int_value(L"test_int_spinner");
|
g_settings.test_int_prop = values.get_int_value(L"test_int_spinner");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the string property.
|
// Update the string property.
|
||||||
if (values.is_string_value(L"test_string_text")) {
|
if (values.is_string_value(L"test_string_text"))
|
||||||
|
{
|
||||||
g_settings.test_string_prop = values.get_string_value(L"test_string_text");
|
g_settings.test_string_prop = values.get_string_value(L"test_string_text");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the color property.
|
// Update the color property.
|
||||||
if (values.is_string_value(L"test_color_picker")) {
|
if (values.is_string_value(L"test_color_picker"))
|
||||||
|
{
|
||||||
g_settings.test_color_prop = values.get_string_value(L"test_color_picker");
|
g_settings.test_color_prop = values.get_string_value(L"test_color_picker");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,34 +199,42 @@ public:
|
|||||||
// Otherwise call a custom function to process the settings before saving them to disk:
|
// Otherwise call a custom function to process the settings before saving them to disk:
|
||||||
// save_settings();
|
// save_settings();
|
||||||
}
|
}
|
||||||
catch (std::exception& ex) {
|
catch (std::exception& ex)
|
||||||
|
{
|
||||||
// Improper JSON.
|
// Improper JSON.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable the powertoy
|
// Enable the powertoy
|
||||||
virtual void enable() {
|
virtual void enable()
|
||||||
|
{
|
||||||
m_enabled = true;
|
m_enabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable the powertoy
|
// Disable the powertoy
|
||||||
virtual void disable() {
|
virtual void disable()
|
||||||
|
{
|
||||||
m_enabled = false;
|
m_enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns if the powertoys is enabled
|
// Returns if the powertoys is enabled
|
||||||
virtual bool is_enabled() override {
|
virtual bool is_enabled() override
|
||||||
|
{
|
||||||
return m_enabled;
|
return m_enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle incoming event, data is event-specific
|
// Handle incoming event, data is event-specific
|
||||||
virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override {
|
virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override
|
||||||
if (wcscmp(name, ll_keyboard) == 0) {
|
{
|
||||||
|
if (wcscmp(name, ll_keyboard) == 0)
|
||||||
|
{
|
||||||
auto& event = *(reinterpret_cast<LowlevelKeyboardEvent*>(data));
|
auto& event = *(reinterpret_cast<LowlevelKeyboardEvent*>(data));
|
||||||
// Return 1 if the keypress is to be suppressed (not forwarded to Windows),
|
// Return 1 if the keypress is to be suppressed (not forwarded to Windows),
|
||||||
// otherwise return 0.
|
// otherwise return 0.
|
||||||
return 0;
|
return 0;
|
||||||
} else if (wcscmp(name, win_hook_event) == 0) {
|
}
|
||||||
|
else if (wcscmp(name, win_hook_event) == 0)
|
||||||
|
{
|
||||||
auto& event = *(reinterpret_cast<WinHookEvent*>(data));
|
auto& event = *(reinterpret_cast<WinHookEvent*>(data));
|
||||||
// Return value is ignored
|
// Return value is ignored
|
||||||
return 0;
|
return 0;
|
||||||
@@ -215,46 +242,55 @@ public:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) override { }
|
virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) override {}
|
||||||
virtual void signal_system_menu_action(const wchar_t* name) override { }
|
virtual void signal_system_menu_action(const wchar_t* name) override {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Load the settings file.
|
// Load the settings file.
|
||||||
void ExamplePowertoy::init_settings() {
|
void ExamplePowertoy::init_settings()
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
// Load and parse the settings file for this PowerToy.
|
// Load and parse the settings file for this PowerToy.
|
||||||
PowerToysSettings::PowerToyValues settings =
|
PowerToysSettings::PowerToyValues settings =
|
||||||
PowerToysSettings::PowerToyValues::load_from_settings_file(ExamplePowertoy::get_name());
|
PowerToysSettings::PowerToyValues::load_from_settings_file(ExamplePowertoy::get_name());
|
||||||
|
|
||||||
// Load the bool property.
|
// Load the bool property.
|
||||||
if (settings.is_bool_value(L"test_bool_toggle")) {
|
if (settings.is_bool_value(L"test_bool_toggle"))
|
||||||
|
{
|
||||||
g_settings.test_bool_prop = settings.get_bool_value(L"test_bool_toggle");
|
g_settings.test_bool_prop = settings.get_bool_value(L"test_bool_toggle");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the int property.
|
// Load the int property.
|
||||||
if (settings.is_int_value(L"test_int_spinner")) {
|
if (settings.is_int_value(L"test_int_spinner"))
|
||||||
|
{
|
||||||
g_settings.test_int_prop = settings.get_int_value(L"test_int_spinner");
|
g_settings.test_int_prop = settings.get_int_value(L"test_int_spinner");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the string property.
|
// Load the string property.
|
||||||
if (settings.is_string_value(L"test_string_text")) {
|
if (settings.is_string_value(L"test_string_text"))
|
||||||
|
{
|
||||||
g_settings.test_string_prop = settings.get_string_value(L"test_string_text");
|
g_settings.test_string_prop = settings.get_string_value(L"test_string_text");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the color property.
|
// Load the color property.
|
||||||
if (settings.is_string_value(L"test_color_picker")) {
|
if (settings.is_string_value(L"test_color_picker"))
|
||||||
|
{
|
||||||
g_settings.test_color_prop = settings.get_string_value(L"test_color_picker");
|
g_settings.test_color_prop = settings.get_string_value(L"test_color_picker");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& ex) {
|
catch (std::exception& ex)
|
||||||
|
{
|
||||||
// Error while loading from the settings file. Let default values stay as they are.
|
// Error while loading from the settings file. Let default values stay as they are.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method of saving the module settings is only required if you need to do any
|
// 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.
|
// custom processing of the settings before saving them to disk.
|
||||||
void ExamplePowertoy::save_settings() {
|
void ExamplePowertoy::save_settings()
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
// Create a PowerToyValues object for this PowerToy
|
// Create a PowerToyValues object for this PowerToy
|
||||||
PowerToysSettings::PowerToyValues values(get_name());
|
PowerToysSettings::PowerToyValues values(get_name());
|
||||||
|
|
||||||
@@ -285,11 +321,13 @@ void ExamplePowertoy::save_settings() {
|
|||||||
// Save the PowerToyValues JSON to the power toy settings file.
|
// Save the PowerToyValues JSON to the power toy settings file.
|
||||||
values.save_to_settings_file();
|
values.save_to_settings_file();
|
||||||
}
|
}
|
||||||
catch (std::exception& ex) {
|
catch (std::exception& ex)
|
||||||
|
{
|
||||||
// Couldn't save the settings.
|
// Couldn't save the settings.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() {
|
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
|
||||||
|
{
|
||||||
return new ExamplePowertoy();
|
return new ExamplePowertoy();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,15 +8,18 @@ TRACELOGGING_DEFINE_PROVIDER(
|
|||||||
(0x38e8889b, 0x9731, 0x53f5, 0xe9, 0x01, 0xe8, 0xa7, 0xc1, 0x75, 0x30, 0x74),
|
(0x38e8889b, 0x9731, 0x53f5, 0xe9, 0x01, 0xe8, 0xa7, 0xc1, 0x75, 0x30, 0x74),
|
||||||
TraceLoggingOptionProjectTelemetry());
|
TraceLoggingOptionProjectTelemetry());
|
||||||
|
|
||||||
void Trace::RegisterProvider() {
|
void Trace::RegisterProvider()
|
||||||
|
{
|
||||||
TraceLoggingRegister(g_hProvider);
|
TraceLoggingRegister(g_hProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Trace::UnregisterProvider() {
|
void Trace::UnregisterProvider()
|
||||||
|
{
|
||||||
TraceLoggingUnregister(g_hProvider);
|
TraceLoggingUnregister(g_hProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Trace::MyEvent() {
|
void Trace::MyEvent()
|
||||||
|
{
|
||||||
TraceLoggingWrite(
|
TraceLoggingWrite(
|
||||||
g_hProvider,
|
g_hProvider,
|
||||||
"PowerToyName_MyEvent",
|
"PowerToyName_MyEvent",
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class Trace {
|
class Trace
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
static void RegisterProvider();
|
static void RegisterProvider();
|
||||||
static void UnregisterProvider();
|
static void UnregisterProvider();
|
||||||
|
|||||||
Reference in New Issue
Block a user