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

@@ -86,7 +86,7 @@ public:
GetZoneHighlightColor() noexcept
{
// Skip the leading # and convert to long
const auto color = m_settings->GetSettings().zoneHightlightColor;
const auto color = m_settings->GetSettings()->zoneHightlightColor;
const auto tmp = std::stol(color.substr(1), nullptr, 16);
const auto nR = (tmp & 0xFF0000) >> 16;
const auto nG = (tmp & 0xFF00) >> 8;
@@ -108,7 +108,7 @@ public:
IFACEMETHODIMP_(int)
GetZoneHighlightOpacity() noexcept
{
return m_settings->GetSettings().zoneHighlightOpacity;
return m_settings->GetSettings()->zoneHighlightOpacity;
}
LRESULT WndProc(HWND, UINT, WPARAM, LPARAM) noexcept;
@@ -217,7 +217,7 @@ FancyZones::Run() noexcept
if (!m_window)
return;
RegisterHotKey(m_window, 1, m_settings->GetSettings().editorHotkey.get_modifiers(), m_settings->GetSettings().editorHotkey.get_code());
RegisterHotKey(m_window, 1, m_settings->GetSettings()->editorHotkey.get_modifiers(), m_settings->GetSettings()->editorHotkey.get_code());
VirtualDesktopInitialize();
@@ -309,7 +309,7 @@ FancyZones::VirtualDesktopInitialize() noexcept
IFACEMETHODIMP_(void)
FancyZones::WindowCreated(HWND window) noexcept
{
if (m_settings->GetSettings().appLastZone_moveWindows && IsInterestingWindow(window))
if (m_settings->GetSettings()->appLastZone_moveWindows && IsInterestingWindow(window))
{
for (const auto& [monitor, zoneWindow] : m_zoneWindowMap)
{
@@ -355,7 +355,7 @@ FancyZones::OnKeyDown(PKBDLLHOOKSTRUCT info) noexcept
}
else if ((info->vkCode == VK_RIGHT) || (info->vkCode == VK_LEFT))
{
if (m_settings->GetSettings().overrideSnapHotkeys)
if (m_settings->GetSettings()->overrideSnapHotkeys)
{
// Win+Left, Win+Right will cycle through Zones in the active ZoneSet
Trace::FancyZones::OnKeyDown(info->vkCode, win, ctrl, false /*inMoveSize*/);
@@ -400,7 +400,7 @@ void FancyZones::ToggleEditor() noexcept
UINT dpi_x = DPIAware::DEFAULT_DPI;
UINT dpi_y = DPIAware::DEFAULT_DPI;
const bool use_cursorpos_editor_startupscreen = m_settings->GetSettings().use_cursorpos_editor_startupscreen;
const bool use_cursorpos_editor_startupscreen = m_settings->GetSettings()->use_cursorpos_editor_startupscreen;
POINT currentCursorPos{};
if (use_cursorpos_editor_startupscreen)
{
@@ -515,14 +515,14 @@ void FancyZones::SettingsChanged() noexcept
std::shared_lock readLock(m_lock);
// Update the hotkey
UnregisterHotKey(m_window, 1);
RegisterHotKey(m_window, 1, m_settings->GetSettings().editorHotkey.get_modifiers(), m_settings->GetSettings().editorHotkey.get_code());
RegisterHotKey(m_window, 1, m_settings->GetSettings()->editorHotkey.get_modifiers(), m_settings->GetSettings()->editorHotkey.get_code());
}
// IZoneWindowHost
IFACEMETHODIMP_(void)
FancyZones::MoveWindowsOnActiveZoneSetChange() noexcept
{
if (m_settings->GetSettings().zoneSetChange_moveWindows)
if (m_settings->GetSettings()->zoneSetChange_moveWindows)
{
MoveWindowsOnDisplayChange();
}
@@ -616,21 +616,21 @@ void FancyZones::OnDisplayChange(DisplayChangeType changeType) noexcept
if ((changeType == DisplayChangeType::WorkArea) || (changeType == DisplayChangeType::DisplayChange))
{
if (m_settings->GetSettings().displayChange_moveWindows)
if (m_settings->GetSettings()->displayChange_moveWindows)
{
MoveWindowsOnDisplayChange();
}
}
else if (changeType == DisplayChangeType::VirtualDesktop)
{
if (m_settings->GetSettings().virtualDesktopChange_moveWindows)
if (m_settings->GetSettings()->virtualDesktopChange_moveWindows)
{
MoveWindowsOnDisplayChange();
}
}
else if (changeType == DisplayChangeType::Editor)
{
if (m_settings->GetSettings().zoneSetChange_moveWindows)
if (m_settings->GetSettings()->zoneSetChange_moveWindows)
{
MoveWindowsOnDisplayChange();
}
@@ -647,7 +647,7 @@ void FancyZones::AddZoneWindow(HMONITOR monitor, PCWSTR deviceId) noexcept
JSONHelpers::FancyZonesDataInstance().SetActiveDeviceId(uniqueId);
const bool newWorkArea = IsNewWorkArea(m_currentVirtualDesktopId, monitor);
const bool flash = m_settings->GetSettings().zoneSetChange_flashZones && newWorkArea;
const bool flash = m_settings->GetSettings()->zoneSetChange_flashZones && newWorkArea;
auto zoneWindow = MakeZoneWindow(this, m_hinstance, monitor, uniqueId, flash);
if (zoneWindow)
@@ -706,7 +706,7 @@ bool FancyZones::IsInterestingWindow(HWND window) noexcept
CharUpperBuffW(filtered.process_path.data(), (DWORD)filtered.process_path.length());
if (m_settings)
{
for (const auto& excluded : m_settings->GetSettings().excludedAppsArray)
for (const auto& excluded : m_settings->GetSettings()->excludedAppsArray)
{
if (filtered.process_path.find(excluded) != std::wstring::npos)
{
@@ -795,7 +795,7 @@ void FancyZones::UpdateDragState(require_write_lock) noexcept
mouse |= mouseR;
}
if (m_settings->GetSettings().shiftDrag)
if (m_settings->GetSettings()->shiftDrag)
{
m_dragEnabled = (shift | mouse);
}

View File

@@ -19,7 +19,7 @@ public:
IFACEMETHODIMP_(bool) GetConfig(_Out_ PWSTR buffer, _Out_ int *buffer_sizeg) noexcept;
IFACEMETHODIMP_(void) SetConfig(PCWSTR config) noexcept;
IFACEMETHODIMP_(void) CallCustomAction(PCWSTR action) noexcept;
IFACEMETHODIMP_(Settings) GetSettings() noexcept { return m_settings; }
IFACEMETHODIMP_(const Settings*) GetSettings() const noexcept { return &m_settings; }
private:
void LoadSettings(PCWSTR config, bool fromFile) noexcept;

View File

@@ -28,7 +28,7 @@ interface __declspec(uuid("{BA4E77C4-6F44-4C5D-93D3-CBDE880495C2}")) IFancyZones
IFACEMETHOD_(bool, GetConfig)(_Out_ PWSTR buffer, _Out_ int *buffer_size) = 0;
IFACEMETHOD_(void, SetConfig)(PCWSTR serializedPowerToysSettingsJson) = 0;
IFACEMETHOD_(void, CallCustomAction)(PCWSTR action) = 0;
IFACEMETHOD_(Settings, GetSettings)() = 0;
IFACEMETHOD_(const Settings*, GetSettings)() const = 0;
};
winrt::com_ptr<IFancyZonesSettings> MakeFancyZonesSettings(HINSTANCE hinstance, PCWSTR config) noexcept;

View File

@@ -71,7 +71,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(m_defaultSettings, actualSettings);
compareSettings(m_defaultSettings, *actualSettings);
}
TEST_METHOD(CreateWithHinstanceNullptr)
@@ -80,7 +80,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(m_defaultSettings, actualSettings);
compareSettings(m_defaultSettings, *actualSettings);
}
TEST_METHOD(CreateWithNameEmpty)
@@ -89,7 +89,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(m_defaultSettings, actualSettings);
compareSettings(m_defaultSettings, *actualSettings);
}
TEST_METHOD(Create)
@@ -131,7 +131,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(expected, actualSettings);
compareSettings(expected, *actualSettings);
}
TEST_METHOD(CreateWithMultipleApps)
@@ -173,7 +173,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(expected, actualSettings);
compareSettings(expected, *actualSettings);
}
TEST_METHOD(CreateWithBoolValuesMissed)
@@ -206,7 +206,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(expected, actualSettings);
compareSettings(expected, *actualSettings);
}
TEST_METHOD(CreateColorMissed)
@@ -247,7 +247,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(expected, actualSettings);
compareSettings(expected, *actualSettings);
}
TEST_METHOD(CreateOpacityMissed)
@@ -288,7 +288,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(expected, actualSettings);
compareSettings(expected, *actualSettings);
}
TEST_METHOD(CreateHotkeyMissed)
@@ -329,7 +329,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(expected, actualSettings);
compareSettings(expected, *actualSettings);
}
TEST_METHOD(CreateAppsMissed)
@@ -370,7 +370,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(expected, actualSettings);
compareSettings(expected, *actualSettings);
}
TEST_METHOD(CreateWithEmptyJson)
@@ -380,7 +380,7 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual != nullptr);
auto actualSettings = actual->GetSettings();
compareSettings(m_defaultSettings, actualSettings);
compareSettings(m_defaultSettings, *actualSettings);
}
};
@@ -706,7 +706,7 @@ namespace FancyZonesUnitTests
m_settings->SetConfig(config.c_str());
auto actual = m_settings->GetSettings();
compareSettings(expected, actual);
compareSettings(expected, *actual);
Assert::IsTrue(std::filesystem::exists(settingsFile));
}

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());
}
}

View File

@@ -1,4 +1,5 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#pragma push_macro("GetCurrentTime")
#undef GetCurrentTime