mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request Fixes for the light switch module. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #42878 + another bug that was discovered. - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
184 lines
4.7 KiB
C++
184 lines
4.7 KiB
C++
#include "LightSwitchSettings.h"
|
|
#include <common/utils/json.h>
|
|
#include <common/SettingsAPI/settings_helpers.h>
|
|
#include "SettingsObserver.h"
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <WinHookEventIDs.h>
|
|
#include <logger.h>
|
|
|
|
using namespace std;
|
|
|
|
LightSwitchSettings& LightSwitchSettings::instance()
|
|
{
|
|
static LightSwitchSettings inst;
|
|
return inst;
|
|
}
|
|
|
|
LightSwitchSettings::LightSwitchSettings()
|
|
{
|
|
LoadSettings();
|
|
}
|
|
|
|
std::wstring LightSwitchSettings::GetSettingsFileName()
|
|
{
|
|
return PTSettingsHelper::get_module_save_file_location(L"LightSwitch");
|
|
}
|
|
|
|
void LightSwitchSettings::InitFileWatcher()
|
|
{
|
|
if (!m_settingsChangedEvent)
|
|
{
|
|
m_settingsChangedEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
}
|
|
|
|
if (!m_settingsFileWatcher)
|
|
{
|
|
m_settingsFileWatcher = std::make_unique<FileWatcher>(
|
|
GetSettingsFileName(),
|
|
[this]() {
|
|
Logger::info(L"[LightSwitchSettings] Settings file changed, signaling event.");
|
|
LoadSettings();
|
|
SetEvent(m_settingsChangedEvent);
|
|
});
|
|
}
|
|
}
|
|
|
|
void LightSwitchSettings::AddObserver(SettingsObserver& observer)
|
|
{
|
|
m_observers.insert(&observer);
|
|
}
|
|
|
|
void LightSwitchSettings::RemoveObserver(SettingsObserver& observer)
|
|
{
|
|
m_observers.erase(&observer);
|
|
}
|
|
|
|
void LightSwitchSettings::NotifyObservers(SettingId id) const
|
|
{
|
|
for (auto observer : m_observers)
|
|
{
|
|
if (observer->WantsToBeNotified(id))
|
|
{
|
|
observer->SettingsUpdate(id);
|
|
}
|
|
}
|
|
}
|
|
|
|
HANDLE LightSwitchSettings::GetSettingsChangedEvent() const
|
|
{
|
|
return m_settingsChangedEvent;
|
|
}
|
|
|
|
void LightSwitchSettings::LoadSettings()
|
|
{
|
|
try
|
|
{
|
|
PowerToysSettings::PowerToyValues values =
|
|
PowerToysSettings::PowerToyValues::load_from_settings_file(L"LightSwitch");
|
|
|
|
|
|
if (const auto jsonVal = values.get_string_value(L"scheduleMode"))
|
|
{
|
|
auto val = *jsonVal;
|
|
auto newMode = FromString(val);
|
|
if (m_settings.scheduleMode != newMode)
|
|
{
|
|
m_settings.scheduleMode = newMode;
|
|
NotifyObservers(SettingId::ScheduleMode);
|
|
}
|
|
}
|
|
|
|
// Latitude
|
|
if (const auto jsonVal = values.get_string_value(L"latitude"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.latitude != val)
|
|
{
|
|
m_settings.latitude = val;
|
|
NotifyObservers(SettingId::Latitude);
|
|
}
|
|
}
|
|
|
|
// Longitude
|
|
if (const auto jsonVal = values.get_string_value(L"longitude"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.longitude != val)
|
|
{
|
|
m_settings.longitude = val;
|
|
NotifyObservers(SettingId::Longitude);
|
|
}
|
|
}
|
|
|
|
// LightTime
|
|
if (const auto jsonVal = values.get_int_value(L"lightTime"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.lightTime != val)
|
|
{
|
|
m_settings.lightTime = val;
|
|
NotifyObservers(SettingId::LightTime);
|
|
}
|
|
}
|
|
|
|
// DarkTime
|
|
if (const auto jsonVal = values.get_int_value(L"darkTime"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.darkTime != val)
|
|
{
|
|
m_settings.darkTime = val;
|
|
NotifyObservers(SettingId::DarkTime);
|
|
}
|
|
}
|
|
|
|
// Offset
|
|
if (const auto jsonVal = values.get_int_value(L"sunrise_offset"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.sunrise_offset != val)
|
|
{
|
|
m_settings.sunrise_offset = val;
|
|
NotifyObservers(SettingId::Sunrise_Offset);
|
|
}
|
|
}
|
|
|
|
if (const auto jsonVal = values.get_int_value(L"sunset_offset"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.sunset_offset != val)
|
|
{
|
|
m_settings.sunset_offset = val;
|
|
NotifyObservers(SettingId::Sunset_Offset);
|
|
}
|
|
}
|
|
|
|
// ChangeSystem
|
|
if (const auto jsonVal = values.get_bool_value(L"changeSystem"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.changeSystem != val)
|
|
{
|
|
m_settings.changeSystem = val;
|
|
NotifyObservers(SettingId::ChangeSystem);
|
|
}
|
|
}
|
|
|
|
// ChangeApps
|
|
if (const auto jsonVal = values.get_bool_value(L"changeApps"))
|
|
{
|
|
auto val = *jsonVal;
|
|
if (m_settings.changeApps != val)
|
|
{
|
|
m_settings.changeApps = val;
|
|
NotifyObservers(SettingId::ChangeApps);
|
|
}
|
|
}
|
|
}
|
|
catch (...)
|
|
{
|
|
// Keeps defaults if load fails
|
|
}
|
|
} |