diff --git a/src/common/GPOWrapperProjection/GPOWrapper.cs b/src/common/GPOWrapperProjection/GPOWrapper.cs index 6cb91a69ac..9793310277 100644 --- a/src/common/GPOWrapperProjection/GPOWrapper.cs +++ b/src/common/GPOWrapperProjection/GPOWrapper.cs @@ -66,5 +66,10 @@ namespace PowerToys.GPOWrapperProjection { return (GpoRuleConfigured)PowerToys.GPOWrapper.GPOWrapper.GetConfiguredWorkspacesEnabledValue(); } + + public static GpoRuleConfigured GetConfiguredLightSwitchEnabledValue() + { + return (GpoRuleConfigured)PowerToys.GPOWrapper.GPOWrapper.GetConfiguredLightSwitchEnabledValue(); + } } } diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml.cs index 1ee79a4010..bd6d01edde 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml.cs @@ -50,7 +50,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views var darkSettings = this.moduleSettingsRepository.SettingsConfig; // 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.LoadSettings(this.generalSettingsRepository, this.moduleSettingsRepository); @@ -185,7 +185,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views // need to save the values this.ViewModel.Latitude = latitude.ToString(CultureInfo.InvariantCulture); this.ViewModel.Longitude = longitude.ToString(CultureInfo.InvariantCulture); - this.ViewModel.SyncButtonInformation = $"{this.ViewModel.Latitude}°, {this.ViewModel.Longitude}°"; + this.ViewModel.SyncButtonInformation = $"{this.ViewModel.Latitude}°, {this.ViewModel.Longitude}°"; 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) { - var enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredLightSwitchEnabledValue(); - - 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; - } + ViewModel.RefreshEnabledState(); } private async void SyncLocationButton_Click(object sender, RoutedEventArgs e) diff --git a/src/settings-ui/Settings.UI/ViewModels/LightSwitchViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/LightSwitchViewModel.cs index e9e744705f..05aec49c9a 100644 --- a/src/settings-ui/Settings.UI/ViewModels/LightSwitchViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/LightSwitchViewModel.cs @@ -14,8 +14,10 @@ using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library.Helpers; +using Microsoft.PowerToys.Settings.UI.Library.Interfaces; using Microsoft.PowerToys.Settings.UI.SerializationContext; using Newtonsoft.Json.Linq; +using PowerToys.GPOWrapper; using Settings.UI.Library; using Settings.UI.Library.Helpers; @@ -27,10 +29,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func SendConfigMSG { get; } + private GeneralSettings GeneralSettingsConfig { get; set; } + public ObservableCollection SearchLocations { get; } = new(); - public LightSwitchViewModel(LightSwitchSettings initialSettings = null, Func ipcMSGCallBackFunc = null) + public LightSwitchViewModel(ISettingsRepository settingsRepository, LightSwitchSettings initialSettings = null, Func ipcMSGCallBackFunc = null) { + ArgumentNullException.ThrowIfNull(settingsRepository); + GeneralSettingsConfig = settingsRepository.SettingsConfig; + InitializeEnabledValue(); + _moduleSettings = initialSettings ?? new LightSwitchSettings(); SendConfigMSG = ipcMSGCallBackFunc; @@ -58,6 +66,21 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels 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() { Logger.LogInfo("Sending custom action: forceLight"); @@ -93,33 +116,26 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels public bool IsEnabled { - get - { - if (_enabledStateIsGPOConfigured) - { - return _enabledGPOConfiguration; - } - else - { - return _isEnabled; - } - } + get => _isEnabled; 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; - 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 { get => _enabledStateIsGPOConfigured; - set - { - if (_enabledStateIsGPOConfigured != value) - { - _enabledStateIsGPOConfigured = value; - NotifyPropertyChanged(); - } - } } - public bool EnabledGPOConfiguration + public GpoRuleConfigured EnabledGPOConfiguration { - get => _enabledGPOConfiguration; + get => _enabledGpoRuleConfiguration; set { - if (_enabledGPOConfiguration != value) + if (_enabledGpoRuleConfiguration != value) { - _enabledGPOConfiguration = value; + _enabledGpoRuleConfiguration = value; NotifyPropertyChanged(); } } @@ -575,7 +583,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } private bool _enabledStateIsGPOConfigured; - private bool _enabledGPOConfiguration; + private GpoRuleConfigured _enabledGpoRuleConfiguration; private LightSwitchSettings _moduleSettings; private bool _isEnabled; private HotkeySettings _toggleThemeHotkey;