1010using System . Runtime . CompilerServices ;
1111using System . Text . Json ;
1212using System . Windows . Input ;
13+ using global ::PowerToys . GPOWrapper ;
1314using ManagedCommon ;
1415using Microsoft . PowerToys . Settings . UI . Helpers ;
1516using Microsoft . PowerToys . Settings . UI . Library ;
1617using Microsoft . PowerToys . Settings . UI . Library . Helpers ;
18+ using Microsoft . PowerToys . Settings . UI . Library . Interfaces ;
1719using Microsoft . PowerToys . Settings . UI . SerializationContext ;
1820using Newtonsoft . Json . Linq ;
1921using Settings . UI . Library ;
@@ -27,10 +29,19 @@ public partial class LightSwitchViewModel : PageViewModelBase
2729
2830 private Func < string , int > SendConfigMSG { get ; }
2931
32+ private GeneralSettings GeneralSettingsConfig { get ; set ; }
33+
3034 public ObservableCollection < SearchLocation > SearchLocations { get ; } = new ( ) ;
3135
32- public LightSwitchViewModel ( LightSwitchSettings initialSettings = null , Func < string , int > ipcMSGCallBackFunc = null )
36+ public LightSwitchViewModel ( ISettingsRepository < GeneralSettings > settingsRepository , LightSwitchSettings initialSettings = null , Func < string , int > ipcMSGCallBackFunc = null )
3337 {
38+ // To obtain the general settings configurations of PowerToys Settings.
39+ ArgumentNullException . ThrowIfNull ( settingsRepository ) ;
40+
41+ GeneralSettingsConfig = settingsRepository . SettingsConfig ;
42+
43+ InitializeEnabledValue ( ) ;
44+
3445 _moduleSettings = initialSettings ?? new LightSwitchSettings ( ) ;
3546 SendConfigMSG = ipcMSGCallBackFunc ;
3647
@@ -46,6 +57,21 @@ public LightSwitchViewModel(LightSwitchSettings initialSettings = null, Func<str
4657 _toggleThemeHotkey = _moduleSettings . Properties . ToggleThemeHotkey . Value ;
4758 }
4859
60+ private void InitializeEnabledValue ( )
61+ {
62+ _enabledGpoRuleConfiguration = GPOWrapper . GetConfiguredLightSwitchEnabledValue ( ) ;
63+ if ( _enabledGpoRuleConfiguration == GpoRuleConfigured . Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured . Enabled )
64+ {
65+ // Get the enabled state from GPO.
66+ _enabledStateIsGPOConfigured = true ;
67+ _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured . Enabled ;
68+ }
69+ else
70+ {
71+ _isEnabled = GeneralSettingsConfig . Enabled . LightSwitch ;
72+ }
73+ }
74+
4975 public override Dictionary < string , HotkeySettings [ ] > GetAllHotkeySettings ( )
5076 {
5177 var hotkeysDict = new Dictionary < string , HotkeySettings [ ] >
@@ -91,61 +117,33 @@ public LightSwitchSettings ModuleSettings
91117
92118 public bool IsEnabled
93119 {
94- get
120+ get => _isEnabled ;
121+
122+ set
95123 {
96124 if ( _enabledStateIsGPOConfigured )
97125 {
98- return _enabledGPOConfiguration ;
99- }
100- else
101- {
102- return _isEnabled ;
126+ // If it's GPO configured, shouldn't be able to change this state.
127+ return ;
103128 }
104- }
105129
106- set
107- {
108- if ( _isEnabled != value )
130+ if ( value != _isEnabled )
109131 {
110- if ( _enabledStateIsGPOConfigured )
111- {
112- // If it's GPO configured, shouldn't be able to change this state.
113- return ;
114- }
115-
116132 _isEnabled = value ;
117133
118- RefreshEnabledState ( ) ;
134+ // Set the status in the general settings configuration
135+ GeneralSettingsConfig . Enabled . LightSwitch = value ;
136+ OutGoingGeneralSettings snd = new OutGoingGeneralSettings ( GeneralSettingsConfig ) ;
119137
120- NotifyPropertyChanged ( ) ;
138+ SendConfigMSG ( snd . ToString ( ) ) ;
139+ OnPropertyChanged ( nameof ( IsEnabled ) ) ;
121140 }
122141 }
123142 }
124143
125144 public bool IsEnabledGpoConfigured
126145 {
127146 get => _enabledStateIsGPOConfigured ;
128- set
129- {
130- if ( _enabledStateIsGPOConfigured != value )
131- {
132- _enabledStateIsGPOConfigured = value ;
133- NotifyPropertyChanged ( ) ;
134- }
135- }
136- }
137-
138- public bool EnabledGPOConfiguration
139- {
140- get => _enabledGPOConfiguration ;
141- set
142- {
143- if ( _enabledGPOConfiguration != value )
144- {
145- _enabledGPOConfiguration = value ;
146- NotifyPropertyChanged ( ) ;
147- }
148- }
149147 }
150148
151149 public string ScheduleMode
@@ -443,6 +441,7 @@ public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
443441
444442 public void RefreshEnabledState ( )
445443 {
444+ InitializeEnabledValue ( ) ;
446445 OnPropertyChanged ( nameof ( IsEnabled ) ) ;
447446 }
448447
@@ -507,8 +506,8 @@ public void InitializeScheduleMode()
507506 }
508507 }
509508
509+ private GpoRuleConfigured _enabledGpoRuleConfiguration ;
510510 private bool _enabledStateIsGPOConfigured ;
511- private bool _enabledGPOConfiguration ;
512511 private LightSwitchSettings _moduleSettings ;
513512 private bool _isEnabled ;
514513 private HotkeySettings _toggleThemeHotkey ;
0 commit comments