Skip to content

Commit 01fb831

Browse files
[Light Switch] Light Switch should detect changes in Windows Settings and treat as manual override (same as using shortcut) (#42882)
<!-- 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 This PR ensures that Light Switch detects changes to app/system theme from Windows Settings. This PR also introduces new behavior where switching the schedule will cause an instant update to the theme if necessary. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #42878 - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx
1 parent d197af3 commit 01fb831

File tree

8 files changed

+262
-101
lines changed

8 files changed

+262
-101
lines changed

‎src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp‎

Lines changed: 200 additions & 99 deletions
Large diffs are not rendered by default.

‎src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
</ItemDefinitionGroup>
7575
<ItemGroup>
7676
<ClCompile Include="LightSwitchService.cpp" />
77+
<ClCompile Include="LightSwitchServiceObserver.cpp" />
7778
<ClCompile Include="LightSwitchSettings.cpp" />
7879
<ClCompile Include="SettingsConstants.cpp" />
7980
<ClCompile Include="ThemeHelper.cpp" />
@@ -84,6 +85,7 @@
8485
<ResourceCompile Include="LightSwitchService.rc" />
8586
</ItemGroup>
8687
<ItemGroup>
88+
<ClInclude Include="LightSwitchServiceObserver.h" />
8789
<ClInclude Include="LightSwitchSettings.h" />
8890
<ClInclude Include="SettingsConstants.h" />
8991
<ClInclude Include="SettingsObserver.h" />

‎src/modules/LightSwitch/LightSwitchService/LightSwitchService.vcxproj.filters‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<ClCompile Include="WinHookEventIDs.cpp">
3434
<Filter>Source Files</Filter>
3535
</ClCompile>
36+
<ClCompile Include="LightSwitchServiceObserver.cpp">
37+
<Filter>Source Files</Filter>
38+
</ClCompile>
3639
</ItemGroup>
3740
<ItemGroup>
3841
<ClInclude Include="ThemeScheduler.h">
@@ -53,6 +56,9 @@
5356
<ClInclude Include="WinHookEventIDs.h">
5457
<Filter>Header Files</Filter>
5558
</ClInclude>
59+
<ClInclude Include="LightSwitchServiceObserver.h">
60+
<Filter>Header Files</Filter>
61+
</ClInclude>
5662
</ItemGroup>
5763
<ItemGroup>
5864
<Natvis Include="$(MSBuildThisFileDirectory)..\..\natvis\wil.natvis" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "LightSwitchServiceObserver.h"
2+
#include <logger.h>
3+
#include "LightSwitchSettings.h"
4+
5+
// These are defined elsewhere in your service module (ServiceWorkerThread.cpp)
6+
extern int g_lastUpdatedDay;
7+
void ApplyThemeNow();
8+
9+
void LightSwitchServiceObserver::SettingsUpdate(SettingId id)
10+
{
11+
Logger::info(L"[LightSwitchService] Setting changed: {}", static_cast<int>(id));
12+
g_lastUpdatedDay = -1;
13+
ApplyThemeNow();
14+
}
15+
16+
bool LightSwitchServiceObserver::WantsToBeNotified(SettingId id) const noexcept
17+
{
18+
switch (id)
19+
{
20+
case SettingId::LightTime:
21+
case SettingId::DarkTime:
22+
case SettingId::ScheduleMode:
23+
case SettingId::Sunrise_Offset:
24+
case SettingId::Sunset_Offset:
25+
return true;
26+
default:
27+
return false;
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include "SettingsObserver.h"
4+
5+
// The LightSwitchServiceObserver reacts when LightSwitchSettings changes.
6+
class LightSwitchServiceObserver : public SettingsObserver
7+
{
8+
public:
9+
explicit LightSwitchServiceObserver(std::unordered_set<SettingId> observedSettings) :
10+
SettingsObserver(std::move(observedSettings))
11+
{
12+
}
13+
14+
void SettingsUpdate(SettingId id) override;
15+
bool WantsToBeNotified(SettingId id) const noexcept override;
16+
};

‎src/modules/LightSwitch/LightSwitchService/LightSwitchSettings.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void LightSwitchSettings::InitFileWatcher()
3939
GetSettingsFileName(),
4040
[this]() {
4141
Logger::info(L"[LightSwitchSettings] Settings file changed, signaling event.");
42+
LoadSettings();
4243
SetEvent(m_settingsChangedEvent);
4344
});
4445
}
@@ -65,6 +66,11 @@ void LightSwitchSettings::NotifyObservers(SettingId id) const
6566
}
6667
}
6768

69+
HANDLE LightSwitchSettings::GetSettingsChangedEvent() const
70+
{
71+
return m_settingsChangedEvent;
72+
}
73+
6874
void LightSwitchSettings::LoadSettings()
6975
{
7076
try

‎src/modules/LightSwitch/LightSwitchService/LightSwitchSettings.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class LightSwitchSettings
7979

8080
void LoadSettings();
8181

82-
HANDLE GetSettingsChangedEvent() const { return m_settingsChangedEvent; }
82+
HANDLE GetSettingsChangedEvent() const;
8383

8484
private:
8585
LightSwitchSettings();

‎src/modules/LightSwitch/LightSwitchService/SettingsObserver.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <unordered_set>
44
#include "SettingsConstants.h"
5+
#include "LightSwitchSettings.h"
56

67
class LightSwitchSettings;
78

@@ -22,7 +23,7 @@ class SettingsObserver
2223
// Override this in your class to respond to updates
2324
virtual void SettingsUpdate(SettingId type) {}
2425

25-
bool WantsToBeNotified(SettingId type) const noexcept
26+
virtual bool WantsToBeNotified(SettingId type) const noexcept
2627
{
2728
return m_observedSettings.contains(type);
2829
}

0 commit comments

Comments
 (0)