runner: simplify powertoy_module interface (#1560)

This commit is contained in:
Andrey Nekrasov
2020-03-13 12:55:15 +03:00
committed by GitHub
parent 4c1dfbaddd
commit 0ac6c01d65
10 changed files with 62 additions and 106 deletions

View File

@@ -101,7 +101,7 @@ GeneralSettings get_settings()
for (auto& [name, powertoy] : modules())
{
settings.isModulesEnabledMap[name] = powertoy.is_enabled();
settings.isModulesEnabledMap[name] = powertoy->is_enabled();
}
return settings;
@@ -153,7 +153,7 @@ void apply_general_settings(const json::JsonObject& general_configs)
{
continue;
}
const bool module_inst_enabled = modules().at(name).is_enabled();
const bool module_inst_enabled = modules().at(name)->is_enabled();
const bool target_enabled = value.GetBoolean();
if (module_inst_enabled == target_enabled)
{
@@ -161,11 +161,11 @@ void apply_general_settings(const json::JsonObject& general_configs)
}
if (target_enabled)
{
modules().at(name).enable();
modules().at(name)->enable();
}
else
{
modules().at(name).disable();
modules().at(name)->disable();
}
}
}
@@ -215,12 +215,12 @@ void start_initial_powertoys()
{
if (powertoys_to_enable.find(name) != powertoys_to_enable.end())
{
powertoy.enable();
powertoy->enable();
}
}
else
{
powertoy.enable();
powertoy->enable();
}
}
}

View File

@@ -199,7 +199,7 @@ int runner(bool isProcessElevated)
try
{
auto module = load_powertoy(file.path().wstring());
modules().emplace(module.get_name(), std::move(module));
modules().emplace(module->get_name(), std::move(module));
}
catch (...)
{

View File

@@ -37,3 +37,24 @@ json::JsonObject PowertoyModule::json_config() const
module->get_config(result.data(), &size);
return json::JsonObject::Parse(result);
}
PowertoyModule::PowertoyModule(PowertoyModuleIface* module, HMODULE handle) :
handle(handle), module(module)
{
if (!module)
{
throw std::runtime_error("Module not initialized");
}
auto want_signals = module->get_events();
if (want_signals)
{
for (; *want_signals; ++want_signals)
{
powertoys_events().register_receiver(*want_signals, module);
}
}
if (SystemMenuHelperInstace().HasCustomConfig(module))
{
powertoys_events().register_system_menu_action(module);
}
}

View File

@@ -8,7 +8,6 @@
#include <vector>
#include <functional>
class PowertoyModule;
#include <common/json.h>
struct PowertoyModuleDeleter
@@ -36,83 +35,18 @@ struct PowertoyModuleDLLDeleter
class PowertoyModule
{
public:
PowertoyModule(PowertoyModuleIface* module, HMODULE handle) :
handle(handle), module(module)
{
if (!module)
{
throw std::runtime_error("Module not initialized");
}
name = module->get_name();
auto want_signals = module->get_events();
if (want_signals)
{
for (; *want_signals; ++want_signals)
{
powertoys_events().register_receiver(*want_signals, module);
}
}
if (SystemMenuHelperInstace().HasCustomConfig(module))
{
powertoys_events().register_system_menu_action(module);
}
}
PowertoyModule(PowertoyModuleIface* module, HMODULE handle);
const std::wstring& get_name() const
inline PowertoyModuleIface* operator->()
{
return name;
return module.get();
}
json::JsonObject json_config() const;
const std::wstring get_config() const
{
std::wstring result;
int size = 0;
module->get_config(nullptr, &size);
wchar_t* buffer = new wchar_t[size];
if (module->get_config(buffer, &size))
{
result.assign(buffer);
}
delete[] buffer;
return result;
}
void set_config(const std::wstring& config)
{
module->set_config(config.c_str());
}
void call_custom_action(const std::wstring& action)
{
module->call_custom_action(action.c_str());
}
intptr_t signal_event(const std::wstring& signal_event, intptr_t data)
{
return module->signal_event(signal_event.c_str(), data);
}
bool is_enabled()
{
return module->is_enabled();
}
void enable()
{
module->enable();
}
void disable()
{
module->disable();
}
private:
std::unique_ptr<HMODULE, PowertoyModuleDLLDeleter> handle;
std::unique_ptr<PowertoyModuleIface, PowertoyModuleDeleter> module;
std::wstring name;
};
PowertoyModule load_powertoy(const std::wstring& filename);

View File

@@ -68,7 +68,7 @@ void dispatch_json_action_to_module(const json::JsonObject& powertoys_configs)
else if (modules().find(name) != modules().end())
{
const auto element = powertoy_element.Value().Stringify();
modules().at(name).call_custom_action(element.c_str());
modules().at(name)->call_custom_action(element.c_str());
}
}
}
@@ -77,7 +77,7 @@ void send_json_config_to_module(const std::wstring& module_key, const std::wstri
{
if (modules().find(module_key) != modules().end())
{
modules().at(module_key).set_config(settings.c_str());
modules().at(module_key)->set_config(settings.c_str());
}
}