/ src / modules / LightSwitch / LightSwitchService / LightSwitchStateManager.h
LightSwitchStateManager.h
 1  #pragma once
 2  #include "LightSwitchSettings.h"
 3  #include <optional>
 4  
 5  // Represents runtime-only information (not saved in settings.json)
 6  struct LightSwitchState
 7  {
 8      ScheduleMode lastAppliedMode = ScheduleMode::Off;
 9      bool isManualOverride = false;
10      bool isSystemLightActive = false;
11      bool isAppsLightActive = false;
12      bool isNightLightActive = false;
13      int lastEvaluatedDay = -1;
14      int lastTickMinutes = -1;
15  
16      // Derived, runtime-resolved times
17      int effectiveLightMinutes = 0; // the boundary we actually act on
18      int effectiveDarkMinutes = 0; // includes offsets if needed
19  };
20  
21  // The controller that reacts to settings changes, time ticks, and manual overrides.
22  class LightSwitchStateManager
23  {
24  public:
25      LightSwitchStateManager();
26  
27      // Called when settings.json changes or stabilizes.
28      void OnSettingsChanged();
29  
30      // Called every minute (from service worker tick).
31      void OnTick();
32  
33      // Called when manual override is toggled (via shortcut or system change).
34      void OnManualOverride();
35  
36      // Called when night light changes in windows settings
37      void OnNightLightChange();
38  
39      // Initial sync at startup to align internal state with system theme
40      void SyncInitialThemeState();
41  
42      // Accessor for current state (optional, for debugging or telemetry)
43      const LightSwitchState& GetState() const { return _state; }
44  
45  private:
46      LightSwitchState _state;
47      std::mutex _stateMutex;
48  
49      void EvaluateAndApplyIfNeeded();
50      bool CoordinatesAreValid(const std::wstring& lat, const std::wstring& lon);
51  
52      // Notify PowerDisplay module about theme change to apply display profiles
53      void NotifyPowerDisplay(bool isLight);
54  };