Skip to content

Commit 99e1b80

Browse files
Copilotnoraa-junker
andcommitted
Implement GPO support for Light Switch utility
Co-authored-by: noraa-junker <[email protected]>
1 parent 4b47082 commit 99e1b80

File tree

3 files changed

+50
-56
lines changed

3 files changed

+50
-56
lines changed

src/common/GPOWrapperProjection/GPOWrapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,10 @@ public static GpoRuleConfigured GetConfiguredWorkspacesEnabledValue()
6666
{
6767
return (GpoRuleConfigured)PowerToys.GPOWrapper.GPOWrapper.GetConfiguredWorkspacesEnabledValue();
6868
}
69+
70+
public static GpoRuleConfigured GetConfiguredLightSwitchEnabledValue()
71+
{
72+
return (GpoRuleConfigured)PowerToys.GPOWrapper.GPOWrapper.GetConfiguredLightSwitchEnabledValue();
73+
}
6974
}
7075
}

src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public LightSwitchPage()
5252
var darkSettings = _moduleSettingsRepository.SettingsConfig;
5353

5454
// Pass them into the ViewModel
55-
ViewModel = new LightSwitchViewModel(darkSettings, ShellPage.SendDefaultIPCMessage);
55+
ViewModel = new LightSwitchViewModel(_generalSettingsRepository, darkSettings, ShellPage.SendDefaultIPCMessage);
5656
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
5757

5858
LoadSettings(_generalSettingsRepository, _moduleSettingsRepository);
@@ -128,7 +128,7 @@ private async Task GetGeoLocation()
128128
ViewModel.SelectedCity = null;
129129

130130
// CityAutoSuggestBox.Text = string.Empty;
131-
ViewModel.SyncButtonInformation = $"{ViewModel.Latitude}°, {ViewModel.Longitude}°";
131+
ViewModel.SyncButtonInformation = $"{ViewModel.Latitude}, {ViewModel.Longitude}";
132132

133133
// ViewModel.CityTimesText = $"Sunrise: {result.SunriseHour}:{result.SunriseMinute:D2}\n" + $"Sunset: {result.SunsetHour}:{result.SunsetMinute:D2}";
134134
SyncButton.IsEnabled = true;
@@ -153,7 +153,7 @@ private void LocationDialog_PrimaryButtonClick(object sender, ContentDialogButto
153153
}
154154
else if (ViewModel.ScheduleMode == "SunriseToSunsetGeo")
155155
{
156-
ViewModel.SyncButtonInformation = $"{ViewModel.Latitude}°, {ViewModel.Longitude}°";
156+
ViewModel.SyncButtonInformation = $"{ViewModel.Latitude}, {ViewModel.Longitude}";
157157
}
158158

159159
SunriseModeChartState();
@@ -248,18 +248,8 @@ private void Settings_Changed(object sender, FileSystemEventArgs e)
248248

249249
private void UpdateEnabledState(bool recommendedState)
250250
{
251-
var enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredLightSwitchEnabledValue();
252-
253-
if (enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
254-
{
255-
// Get the enabled state from GPO.
256-
ViewModel.IsEnabledGpoConfigured = true;
257-
ViewModel.EnabledGPOConfiguration = enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
258-
}
259-
else
260-
{
261-
ViewModel.IsEnabled = recommendedState;
262-
}
251+
// The ViewModel now handles GPO configuration internally via InitializeEnabledValue
252+
ViewModel.RefreshEnabledState();
263253
}
264254

265255
private async void SyncLocationButton_Click(object sender, RoutedEventArgs e)

src/settings-ui/Settings.UI/ViewModels/LightSwitchViewModel.cs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
using System.Runtime.CompilerServices;
1111
using System.Text.Json;
1212
using System.Windows.Input;
13+
using global::PowerToys.GPOWrapper;
1314
using ManagedCommon;
1415
using Microsoft.PowerToys.Settings.UI.Helpers;
1516
using Microsoft.PowerToys.Settings.UI.Library;
1617
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
18+
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
1719
using Microsoft.PowerToys.Settings.UI.SerializationContext;
1820
using Newtonsoft.Json.Linq;
1921
using 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

Comments
 (0)