LightSwitchSettings.h
1 #pragma once 2 3 #include <unordered_set> 4 #include <string> 5 #include <vector> 6 #include <memory> 7 #include <windows.h> 8 #include <mutex> 9 #include <atomic> 10 #include <thread> 11 #include <chrono> 12 #include <common/SettingsAPI/FileWatcher.h> 13 #include <common/SettingsAPI/settings_objects.h> 14 #include <SettingsConstants.h> 15 16 class SettingsObserver; 17 18 enum class ScheduleMode 19 { 20 Off, 21 FixedHours, 22 SunsetToSunrise, 23 FollowNightLight, 24 // Add more in the future 25 }; 26 27 inline std::wstring ToString(ScheduleMode mode) 28 { 29 switch (mode) 30 { 31 case ScheduleMode::FixedHours: 32 return L"FixedHours"; 33 case ScheduleMode::SunsetToSunrise: 34 return L"SunsetToSunrise"; 35 case ScheduleMode::FollowNightLight: 36 return L"FollowNightLight"; 37 default: 38 return L"Off"; 39 } 40 } 41 42 inline ScheduleMode FromString(const std::wstring& str) 43 { 44 if (str == L"SunsetToSunrise") 45 return ScheduleMode::SunsetToSunrise; 46 if (str == L"FixedHours") 47 return ScheduleMode::FixedHours; 48 if (str == L"FollowNightLight") 49 return ScheduleMode::FollowNightLight; 50 else 51 return ScheduleMode::Off; 52 } 53 54 struct LightSwitchConfig 55 { 56 ScheduleMode scheduleMode = ScheduleMode::FixedHours; 57 58 std::wstring latitude = L"0.0"; 59 std::wstring longitude = L"0.0"; 60 61 // Stored as minutes since midnight 62 int lightTime = 8 * 60; // 08:00 default 63 int darkTime = 20 * 60; // 20:00 default 64 65 int sunrise_offset = 0; 66 int sunset_offset = 0; 67 68 bool changeSystem = false; 69 bool changeApps = false; 70 71 bool enableDarkModeProfile = false; 72 bool enableLightModeProfile = false; 73 std::wstring darkModeProfile = L""; 74 std::wstring lightModeProfile = L""; 75 }; 76 77 class LightSwitchSettings 78 { 79 public: 80 static LightSwitchSettings& instance(); 81 82 static inline const LightSwitchConfig& settings() 83 { 84 return instance().m_settings; 85 } 86 87 void InitFileWatcher(); 88 static std::wstring GetSettingsFileName(); 89 90 void AddObserver(SettingsObserver& observer); 91 void RemoveObserver(SettingsObserver& observer); 92 93 void LoadSettings(); 94 95 HANDLE GetSettingsChangedEvent() const; 96 97 private: 98 LightSwitchSettings(); 99 ~LightSwitchSettings(); 100 101 LightSwitchConfig m_settings; 102 std::unique_ptr<FileWatcher> m_settingsFileWatcher; 103 std::unordered_set<SettingsObserver*> m_observers; 104 105 void NotifyObservers(SettingId id) const; 106 107 HANDLE m_settingsChangedEvent = nullptr; 108 mutable std::mutex m_settingsMutex; 109 110 // Debounce state 111 std::atomic_bool m_debouncePending{ false }; 112 std::mutex m_debounceMutex; 113 std::chrono::steady_clock::time_point m_lastChangeTime{}; 114 std::jthread m_debounceThread; 115 };