GPO configured correctly and tested (#44567)

Ensure that Light Switch respects GPO configuration. 
In "Not configured" --> No message, everything is under the user's
control
In "Enabled" --> Module is forced enabled, policy message shows, able to
change settings
In "Disabled" --> Module is forced disabled, policy message shows,
unable to change settings
This commit is contained in:
Jaylyn Barbee
2026-01-09 10:29:17 -05:00
committed by GitHub
parent 5f61057b38
commit a6b8cea7cd
3 changed files with 49 additions and 47 deletions

View File

@@ -66,5 +66,10 @@ namespace PowerToys.GPOWrapperProjection
{ {
return (GpoRuleConfigured)PowerToys.GPOWrapper.GPOWrapper.GetConfiguredWorkspacesEnabledValue(); return (GpoRuleConfigured)PowerToys.GPOWrapper.GPOWrapper.GetConfiguredWorkspacesEnabledValue();
} }
public static GpoRuleConfigured GetConfiguredLightSwitchEnabledValue()
{
return (GpoRuleConfigured)PowerToys.GPOWrapper.GPOWrapper.GetConfiguredLightSwitchEnabledValue();
}
} }
} }

View File

@@ -50,7 +50,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
var darkSettings = this.moduleSettingsRepository.SettingsConfig; var darkSettings = this.moduleSettingsRepository.SettingsConfig;
// Pass them into the ViewModel // Pass them into the ViewModel
this.ViewModel = new LightSwitchViewModel(darkSettings, this.sendConfigMsg); this.ViewModel = new LightSwitchViewModel(this.generalSettingsRepository, darkSettings, ShellPage.SendDefaultIPCMessage);
this.ViewModel.PropertyChanged += ViewModel_PropertyChanged; this.ViewModel.PropertyChanged += ViewModel_PropertyChanged;
this.LoadSettings(this.generalSettingsRepository, this.moduleSettingsRepository); this.LoadSettings(this.generalSettingsRepository, this.moduleSettingsRepository);
@@ -185,7 +185,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
// need to save the values // need to save the values
this.ViewModel.Latitude = latitude.ToString(CultureInfo.InvariantCulture); this.ViewModel.Latitude = latitude.ToString(CultureInfo.InvariantCulture);
this.ViewModel.Longitude = longitude.ToString(CultureInfo.InvariantCulture); this.ViewModel.Longitude = longitude.ToString(CultureInfo.InvariantCulture);
this.ViewModel.SyncButtonInformation = $"{this.ViewModel.Latitude}<EFBFBD>, {this.ViewModel.Longitude}<EFBFBD>"; this.ViewModel.SyncButtonInformation = $"{this.ViewModel.Latitude}°, {this.ViewModel.Longitude}°";
var result = SunCalc.CalculateSunriseSunset(latitude, longitude, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); var result = SunCalc.CalculateSunriseSunset(latitude, longitude, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
@@ -293,18 +293,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
private void UpdateEnabledState(bool recommendedState) private void UpdateEnabledState(bool recommendedState)
{ {
var enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredLightSwitchEnabledValue(); ViewModel.RefreshEnabledState();
if (enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
{
// Get the enabled state from GPO.
this.ViewModel.IsEnabledGpoConfigured = true;
this.ViewModel.EnabledGPOConfiguration = enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
}
else
{
this.ViewModel.IsEnabled = recommendedState;
}
} }
private async void SyncLocationButton_Click(object sender, RoutedEventArgs e) private async void SyncLocationButton_Click(object sender, RoutedEventArgs e)

View File

@@ -14,8 +14,10 @@ using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers; using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.SerializationContext; using Microsoft.PowerToys.Settings.UI.SerializationContext;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using PowerToys.GPOWrapper;
using Settings.UI.Library; using Settings.UI.Library;
using Settings.UI.Library.Helpers; using Settings.UI.Library.Helpers;
@@ -27,10 +29,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private Func<string, int> SendConfigMSG { get; } private Func<string, int> SendConfigMSG { get; }
private GeneralSettings GeneralSettingsConfig { get; set; }
public ObservableCollection<SearchLocation> SearchLocations { get; } = new(); public ObservableCollection<SearchLocation> SearchLocations { get; } = new();
public LightSwitchViewModel(LightSwitchSettings initialSettings = null, Func<string, int> ipcMSGCallBackFunc = null) public LightSwitchViewModel(ISettingsRepository<GeneralSettings> settingsRepository, LightSwitchSettings initialSettings = null, Func<string, int> ipcMSGCallBackFunc = null)
{ {
ArgumentNullException.ThrowIfNull(settingsRepository);
GeneralSettingsConfig = settingsRepository.SettingsConfig;
InitializeEnabledValue();
_moduleSettings = initialSettings ?? new LightSwitchSettings(); _moduleSettings = initialSettings ?? new LightSwitchSettings();
SendConfigMSG = ipcMSGCallBackFunc; SendConfigMSG = ipcMSGCallBackFunc;
@@ -58,6 +66,21 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
return hotkeysDict; return hotkeysDict;
} }
private void InitializeEnabledValue()
{
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredLightSwitchEnabledValue();
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true;
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
}
else
{
_isEnabled = GeneralSettingsConfig.Enabled.LightSwitch;
}
}
private void ForceLightNow() private void ForceLightNow()
{ {
Logger.LogInfo("Sending custom action: forceLight"); Logger.LogInfo("Sending custom action: forceLight");
@@ -93,33 +116,26 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public bool IsEnabled public bool IsEnabled
{ {
get get => _isEnabled;
{
if (_enabledStateIsGPOConfigured)
{
return _enabledGPOConfiguration;
}
else
{
return _isEnabled;
}
}
set set
{ {
if (_isEnabled != value) if (_enabledStateIsGPOConfigured)
{ {
if (_enabledStateIsGPOConfigured) // If it's GPO configured, shouldn't be able to change this state.
{ return;
// If it's GPO configured, shouldn't be able to change this state. }
return;
}
if (value != _isEnabled)
{
_isEnabled = value; _isEnabled = value;
RefreshEnabledState(); // Set the status in the general settings configuration
GeneralSettingsConfig.Enabled.LightSwitch = value;
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig);
NotifyPropertyChanged(); SendConfigMSG(snd.ToString());
OnPropertyChanged(nameof(IsEnabled));
} }
} }
} }
@@ -127,24 +143,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public bool IsEnabledGpoConfigured public bool IsEnabledGpoConfigured
{ {
get => _enabledStateIsGPOConfigured; get => _enabledStateIsGPOConfigured;
set
{
if (_enabledStateIsGPOConfigured != value)
{
_enabledStateIsGPOConfigured = value;
NotifyPropertyChanged();
}
}
} }
public bool EnabledGPOConfiguration public GpoRuleConfigured EnabledGPOConfiguration
{ {
get => _enabledGPOConfiguration; get => _enabledGpoRuleConfiguration;
set set
{ {
if (_enabledGPOConfiguration != value) if (_enabledGpoRuleConfiguration != value)
{ {
_enabledGPOConfiguration = value; _enabledGpoRuleConfiguration = value;
NotifyPropertyChanged(); NotifyPropertyChanged();
} }
} }
@@ -575,7 +583,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
} }
private bool _enabledStateIsGPOConfigured; private bool _enabledStateIsGPOConfigured;
private bool _enabledGPOConfiguration; private GpoRuleConfigured _enabledGpoRuleConfiguration;
private LightSwitchSettings _moduleSettings; private LightSwitchSettings _moduleSettings;
private bool _isEnabled; private bool _isEnabled;
private HotkeySettings _toggleThemeHotkey; private HotkeySettings _toggleThemeHotkey;