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