dllmain.cpp
1 #include "pch.h" 2 #include <interface/powertoy_module_interface.h> 3 #include <common/SettingsAPI/settings_objects.h> 4 #include "trace.h" 5 6 extern "C" IMAGE_DOS_HEADER __ImageBase; 7 8 BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 9 { 10 switch (ul_reason_for_call) 11 { 12 case DLL_PROCESS_ATTACH: 13 Trace::RegisterProvider(); 14 break; 15 case DLL_THREAD_ATTACH: 16 case DLL_THREAD_DETACH: 17 break; 18 case DLL_PROCESS_DETACH: 19 Trace::UnregisterProvider(); 20 break; 21 } 22 return TRUE; 23 } 24 25 // The PowerToy name that will be shown in the settings. 26 const static wchar_t* MODULE_NAME = L"$projectname$"; 27 // Add a description that will we shown in the module settings page. 28 const static wchar_t* MODULE_DESC = L"<no description>"; 29 30 // These are the properties shown in the Settings page. 31 struct ModuleSettings 32 { 33 // Add the PowerToy module properties with default values. 34 // Currently available types: 35 // - int 36 // - bool 37 // - string 38 39 //bool bool_prop = true; 40 //int int_prop = 10; 41 //std::wstring string_prop = L"The quick brown fox jumps over the lazy dog"; 42 //std::wstring color_prop = L"#1212FF"; 43 44 } g_settings; 45 46 // Implement the PowerToy Module Interface and all the required methods. 47 class $safeprojectname$ : public PowertoyModuleIface 48 { 49 private: 50 // The PowerToy state. 51 bool m_enabled = false; 52 53 // Load initial settings from the persisted values. 54 void init_settings(); 55 56 public: 57 // Constructor 58 $safeprojectname$() 59 { 60 init_settings(); 61 }; 62 63 // Destroy the powertoy and free memory 64 virtual void destroy() override 65 { 66 delete this; 67 } 68 69 // Return the localized display name of the powertoy 70 virtual const wchar_t* get_name() override 71 { 72 return MODULE_NAME; 73 } 74 75 // Return the non localized key of the powertoy, this will be cached by the runner 76 virtual const wchar_t* get_key() override 77 { 78 return MODULE_NAME; 79 } 80 81 // Return JSON with the configuration options. 82 virtual bool get_config(wchar_t* buffer, int* buffer_size) override 83 { 84 HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase); 85 86 // Create a Settings object. 87 PowerToysSettings::Settings settings(hinstance, get_name()); 88 settings.set_description(MODULE_DESC); 89 90 // Show an overview link in the Settings page 91 //settings.set_overview_link(L"https://"); 92 93 // Show a video link in the Settings page. 94 //settings.set_video_link(L"https://"); 95 96 // A bool property with a toggle editor. 97 //settings.add_bool_toggle( 98 // L"bool_toggle_1", // property name. 99 // L"This is what a BoolToggle property looks like", // description or resource id of the localized string. 100 // g_settings.bool_prop // property value. 101 //); 102 103 // An integer property with a spinner editor. 104 //settings.add_int_spinner( 105 // L"int_spinner_1", // property name 106 // L"This is what a IntSpinner property looks like", // description or resource id of the localized string. 107 // g_settings.int_prop, // property value. 108 // 0, // min value. 109 // 100, // max value. 110 // 10 // incremental step. 111 //); 112 113 // A string property with a textbox editor. 114 //settings.add_string( 115 // L"string_text_1", // property name. 116 // L"This is what a String property looks like", // description or resource id of the localized string. 117 // g_settings.string_prop // property value. 118 //); 119 120 // A string property with a color picker editor. 121 //settings.add_color_picker( 122 // L"color_picker_1", // property name. 123 // L"This is what a ColorPicker property looks like", // description or resource id of the localized string. 124 // g_settings.color_prop // property value. 125 //); 126 127 // A custom action property. When using this settings type, the "PowertoyModuleIface::call_custom_action()" 128 // method should be overridden as well. 129 //settings.add_custom_action( 130 // L"custom_action_id", // action name. 131 // L"This is what a CustomAction property looks like", // label above the field. 132 // L"Call a custom action", // button text. 133 // L"Press the button to call a custom action." // display values / extended info. 134 //); 135 136 return settings.serialize_to_buffer(buffer, buffer_size); 137 } 138 139 // Signal from the Settings editor to call a custom action. 140 // This can be used to spawn more complex editors. 141 virtual void call_custom_action(const wchar_t* action) override 142 { 143 static UINT custom_action_num_calls = 0; 144 try 145 { 146 // Parse the action values, including name. 147 PowerToysSettings::CustomActionObject action_object = 148 PowerToysSettings::CustomActionObject::from_json_string(action); 149 150 //if (action_object.get_name() == L"custom_action_id") { 151 // // Execute your custom action 152 //} 153 } 154 catch (std::exception&) 155 { 156 // Improper JSON. 157 } 158 } 159 160 // Called by the runner to pass the updated settings values as a serialized JSON. 161 virtual void set_config(const wchar_t* config) override 162 { 163 try 164 { 165 // Parse the input JSON string. 166 PowerToysSettings::PowerToyValues values = 167 PowerToysSettings::PowerToyValues::from_json_string(config, get_key()); 168 169 // Update a bool property. 170 //if (auto v = values.get_bool_value(L"bool_toggle_1")) { 171 // g_settings.bool_prop = *v; 172 //} 173 174 // Update an int property. 175 //if (auto v = values.get_int_value(L"int_spinner_1")) { 176 // g_settings.int_prop = *v; 177 //} 178 179 // Update a string property. 180 //if (auto v = values.get_string_value(L"string_text_1")) { 181 // g_settings.string_prop = *v; 182 //} 183 184 // Update a color property. 185 //if (auto v = values.get_string_value(L"color_picker_1")) { 186 // g_settings.color_prop = *v; 187 //} 188 189 // If you don't need to do any custom processing of the settings, proceed 190 // to persists the values calling: 191 values.save_to_settings_file(); 192 // Otherwise call a custom function to process the settings before saving them to disk: 193 // save_settings(); 194 } 195 catch (std::exception&) 196 { 197 // Improper JSON. 198 } 199 } 200 201 // Enable the powertoy 202 virtual void enable() 203 { 204 m_enabled = true; 205 } 206 207 // Disable the powertoy 208 virtual void disable() 209 { 210 m_enabled = false; 211 } 212 213 // Returns if the powertoys is enabled 214 virtual bool is_enabled() override 215 { 216 return m_enabled; 217 } 218 }; 219 220 // Load the settings file. 221 void $safeprojectname$::init_settings() 222 { 223 try 224 { 225 // Load and parse the settings file for this PowerToy. 226 PowerToysSettings::PowerToyValues settings = 227 PowerToysSettings::PowerToyValues::load_from_settings_file($safeprojectname$::get_key()); 228 229 // Load a bool property. 230 //if (auto v = settings.get_bool_value(L"bool_toggle_1")) { 231 // g_settings.bool_prop = *v; 232 //} 233 234 // Load an int property. 235 //if (auto v = settings.get_int_value(L"int_spinner_1")) { 236 // g_settings.int_prop = *v; 237 //} 238 239 // Load a string property. 240 //if (auto v = settings.get_string_value(L"string_text_1")) { 241 // g_settings.string_prop = *v; 242 //} 243 244 // Load a color property. 245 //if (auto v = settings.get_string_value(L"color_picker_1")) { 246 // g_settings.color_prop = *v; 247 //} 248 } 249 catch (std::exception&) 250 { 251 // Error while loading from the settings file. Let default values stay as they are. 252 } 253 } 254 255 // This method of saving the module settings is only required if you need to do any 256 // custom processing of the settings before saving them to disk. 257 //void $projectname$::save_settings() { 258 // try { 259 // // Create a PowerToyValues object for this PowerToy 260 // PowerToysSettings::PowerToyValues values(get_name()); 261 // 262 // // Save a bool property. 263 // //values.add_property( 264 // // L"bool_toggle_1", // property name 265 // // g_settings.bool_prop // property value 266 // //); 267 // 268 // // Save an int property. 269 // //values.add_property( 270 // // L"int_spinner_1", // property name 271 // // g_settings.int_prop // property value 272 // //); 273 // 274 // // Save a string property. 275 // //values.add_property( 276 // // L"string_text_1", // property name 277 // // g_settings.string_prop // property value 278 // ); 279 // 280 // // Save a color property. 281 // //values.add_property( 282 // // L"color_picker_1", // property name 283 // // g_settings.color_prop // property value 284 // //); 285 // 286 // // Save the PowerToyValues JSON to the power toy settings file. 287 // values.save_to_settings_file(); 288 // } 289 // catch (std::exception& ex) { 290 // // Couldn't save the settings. 291 // } 292 //} 293 294 extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() 295 { 296 return new $safeprojectname$(); 297 }