AwakeViewModel.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Runtime.CompilerServices; 7 8 using ManagedCommon; 9 using Microsoft.PowerToys.Settings.UI.Library; 10 using Microsoft.PowerToys.Settings.UI.Library.Helpers; 11 12 namespace Microsoft.PowerToys.Settings.UI.ViewModels 13 { 14 public partial class AwakeViewModel : Observable 15 { 16 public AwakeViewModel() 17 { 18 } 19 20 public AwakeSettings ModuleSettings 21 { 22 get => _moduleSettings; 23 set 24 { 25 if (_moduleSettings != value) 26 { 27 _moduleSettings = value; 28 RefreshModuleSettings(); 29 RefreshEnabledState(); 30 } 31 } 32 } 33 34 public bool IsEnabled 35 { 36 get 37 { 38 if (_enabledStateIsGPOConfigured) 39 { 40 return _enabledGPOConfiguration; 41 } 42 else 43 { 44 return _isEnabled; 45 } 46 } 47 48 set 49 { 50 if (_isEnabled != value) 51 { 52 if (_enabledStateIsGPOConfigured) 53 { 54 // If it's GPO configured, shouldn't be able to change this state. 55 return; 56 } 57 58 _isEnabled = value; 59 60 RefreshEnabledState(); 61 62 NotifyPropertyChanged(); 63 } 64 } 65 } 66 67 public bool IsEnabledGpoConfigured 68 { 69 get => _enabledStateIsGPOConfigured; 70 set 71 { 72 if (_enabledStateIsGPOConfigured != value) 73 { 74 _enabledStateIsGPOConfigured = value; 75 NotifyPropertyChanged(); 76 } 77 } 78 } 79 80 public bool EnabledGPOConfiguration 81 { 82 get => _enabledGPOConfiguration; 83 set 84 { 85 if (_enabledGPOConfiguration != value) 86 { 87 _enabledGPOConfiguration = value; 88 NotifyPropertyChanged(); 89 } 90 } 91 } 92 93 public bool IsExpirationConfigurationEnabled => ModuleSettings.Properties.Mode == AwakeMode.EXPIRABLE && IsEnabled; 94 95 public bool IsTimeConfigurationEnabled => ModuleSettings.Properties.Mode == AwakeMode.TIMED && IsEnabled; 96 97 public bool IsScreenConfigurationPossibleEnabled => ModuleSettings.Properties.Mode != AwakeMode.PASSIVE && IsEnabled; 98 99 public AwakeMode Mode 100 { 101 get => ModuleSettings.Properties.Mode; 102 set 103 { 104 if (ModuleSettings.Properties.Mode != value) 105 { 106 ModuleSettings.Properties.Mode = value; 107 108 if (value == AwakeMode.TIMED && IntervalMinutes == 0 && IntervalHours == 0) 109 { 110 // Handle the special case where both hours and minutes are zero. 111 // Otherwise, this will reset to passive very quickly in the UI. 112 ModuleSettings.Properties.IntervalMinutes = 1; 113 OnPropertyChanged(nameof(IntervalMinutes)); 114 } 115 else if (value == AwakeMode.EXPIRABLE && ExpirationDateTime <= DateTimeOffset.Now) 116 { 117 // To make sure that we're not tracking expirable keep-awake in the past, 118 // let's make sure that every time it's enabled from the settings UI, it's 119 // five (5) minutes into the future. 120 ExpirationDateTime = DateTimeOffset.Now.AddMinutes(5); 121 122 // The expiration date/time is updated and will send the notification 123 // but we need to do this manually for the expiration time that is 124 // bound to the time control on the settings page. 125 OnPropertyChanged(nameof(ExpirationTime)); 126 } 127 128 OnPropertyChanged(nameof(IsTimeConfigurationEnabled)); 129 OnPropertyChanged(nameof(IsScreenConfigurationPossibleEnabled)); 130 OnPropertyChanged(nameof(IsExpirationConfigurationEnabled)); 131 132 NotifyPropertyChanged(); 133 } 134 } 135 } 136 137 public bool KeepDisplayOn 138 { 139 get => ModuleSettings.Properties.KeepDisplayOn; 140 set 141 { 142 if (ModuleSettings.Properties.KeepDisplayOn != value) 143 { 144 ModuleSettings.Properties.KeepDisplayOn = value; 145 NotifyPropertyChanged(); 146 } 147 } 148 } 149 150 public uint IntervalHours 151 { 152 get => ModuleSettings.Properties.IntervalHours; 153 set 154 { 155 if (ModuleSettings.Properties.IntervalHours != value) 156 { 157 ModuleSettings.Properties.IntervalHours = value; 158 NotifyPropertyChanged(); 159 } 160 } 161 } 162 163 public uint IntervalMinutes 164 { 165 get => ModuleSettings.Properties.IntervalMinutes; 166 set 167 { 168 if (ModuleSettings.Properties.IntervalMinutes != value) 169 { 170 ModuleSettings.Properties.IntervalMinutes = value; 171 NotifyPropertyChanged(); 172 } 173 } 174 } 175 176 public DateTimeOffset ExpirationDateTime 177 { 178 get => ModuleSettings.Properties.ExpirationDateTime; 179 set 180 { 181 if (ModuleSettings.Properties.ExpirationDateTime != value) 182 { 183 ModuleSettings.Properties.ExpirationDateTime = value; 184 NotifyPropertyChanged(); 185 } 186 } 187 } 188 189 public TimeSpan ExpirationTime 190 { 191 get => ExpirationDateTime.TimeOfDay; 192 set 193 { 194 if (ExpirationDateTime.TimeOfDay != value) 195 { 196 ExpirationDateTime = new DateTime(ExpirationDateTime.Year, ExpirationDateTime.Month, ExpirationDateTime.Day, value.Hours, value.Minutes, value.Seconds); 197 } 198 } 199 } 200 201 public void NotifyPropertyChanged([CallerMemberName] string propertyName = null) 202 { 203 Logger.LogInfo($"Changed the property {propertyName}"); 204 OnPropertyChanged(propertyName); 205 } 206 207 public void RefreshEnabledState() 208 { 209 OnPropertyChanged(nameof(IsEnabled)); 210 OnPropertyChanged(nameof(IsTimeConfigurationEnabled)); 211 OnPropertyChanged(nameof(IsScreenConfigurationPossibleEnabled)); 212 OnPropertyChanged(nameof(IsExpirationConfigurationEnabled)); 213 } 214 215 public void RefreshModuleSettings() 216 { 217 OnPropertyChanged(nameof(Mode)); 218 OnPropertyChanged(nameof(KeepDisplayOn)); 219 OnPropertyChanged(nameof(IntervalHours)); 220 OnPropertyChanged(nameof(IntervalMinutes)); 221 OnPropertyChanged(nameof(ExpirationDateTime)); 222 } 223 224 private bool _enabledStateIsGPOConfigured; 225 private bool _enabledGPOConfiguration; 226 private AwakeSettings _moduleSettings; 227 private bool _isEnabled; 228 } 229 }