Awake vNext - NOBLE_SIX_02162023 (#24183)

* Initial scaffolding for expiration configuration

* Simplifying the code and adding support for expiration events

* Bit more cleanup

* Initial support for expirable keep-awake

* Update some of the threading logic

* Logging and timing consistency

* Initial UI scaffolding

* Fix pathing issue for the icon when using config file

* Add missing definitions

* Update with basic interface

* Cleanup redundant calls

* Update name per convention

* Simplify declaration

* Proper binding to secondary Time property

* Cleanup the terminology use

* Standardize naming conventions.

* More Awake cleanup

* Ability to update the UI when the tray icon updates

* Small tweaks before ViewModel refactor

* Refactor the view model logic

* Some consistency fixes

* Remove the build props change

* Add settings scaffolding when a file does not exist

* Update expect.txt

* Fix typos

* Update build in logs

* Updating based on discussion in #24183.
This specifically addresses the fact that the `ExpirationDateTime` property was incorrectly auto-initialized to `DateTime.MinValue` when it should've been set to `DateTimeOffset.MinValue` to be consistent with the underlying type and assumptions around date/time.

---------

Co-authored-by: Clint Rutkas <clint@rutkas.com>
This commit is contained in:
Den
2023-03-15 01:42:47 -07:00
committed by GitHub
parent 13cb52763d
commit 466252745d
20 changed files with 664 additions and 323 deletions

View File

@@ -4,62 +4,29 @@
using System;
using System.Runtime.CompilerServices;
using global::PowerToys.GPOWrapper;
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.Library.Utilities;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class AwakeViewModel : Observable
{
private GeneralSettings GeneralSettingsConfig { get; set; }
private AwakeSettings Settings { get; set; }
private Func<string, int> SendConfigMSG { get; }
public AwakeViewModel(ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<AwakeSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc)
public AwakeViewModel()
{
// To obtain the general settings configurations of PowerToys Settings.
if (settingsRepository == null)
{
throw new ArgumentNullException(nameof(settingsRepository));
}
GeneralSettingsConfig = settingsRepository.SettingsConfig;
// To obtain the settings configurations of Fancy zones.
if (moduleSettingsRepository == null)
{
throw new ArgumentNullException(nameof(moduleSettingsRepository));
}
Settings = moduleSettingsRepository.SettingsConfig;
InitializeEnabledValue();
_keepDisplayOn = Settings.Properties.KeepDisplayOn;
_mode = Settings.Properties.Mode;
_hours = Settings.Properties.Hours;
_minutes = Settings.Properties.Minutes;
// set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
}
private void InitializeEnabledValue()
public AwakeSettings ModuleSettings
{
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredAwakeEnabledValue();
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
get => _moduleSettings;
set
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true;
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
}
else
{
_isEnabled = GeneralSettingsConfig.Enabled.Awake;
if (_moduleSettings != value)
{
_moduleSettings = value;
RefreshModuleSettings();
RefreshEnabledState();
}
}
}
@@ -78,13 +45,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_isEnabled = value;
GeneralSettingsConfig.Enabled.Awake = value;
OnPropertyChanged(nameof(IsEnabled));
OnPropertyChanged(nameof(IsTimeConfigurationEnabled));
OnPropertyChanged(nameof(IsScreenConfigurationPossibleEnabled));
RefreshEnabledState();
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(outgoing.ToString());
NotifyPropertyChanged();
}
}
@@ -93,31 +55,44 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public bool IsEnabledGpoConfigured
{
get => _enabledStateIsGPOConfigured;
set
{
if (_enabledStateIsGPOConfigured != value)
{
_enabledStateIsGPOConfigured = value;
NotifyPropertyChanged();
}
}
}
public bool IsExpirationConfigurationEnabled
{
get => ModuleSettings.Properties.Mode == AwakeMode.EXPIRABLE && IsEnabled;
}
public bool IsTimeConfigurationEnabled
{
get => _mode == AwakeMode.TIMED && _isEnabled;
get => ModuleSettings.Properties.Mode == AwakeMode.TIMED && IsEnabled;
}
public bool IsScreenConfigurationPossibleEnabled
{
get => _mode != AwakeMode.PASSIVE && _isEnabled;
get => ModuleSettings.Properties.Mode != AwakeMode.PASSIVE && IsEnabled;
}
public AwakeMode Mode
{
get => _mode;
get => ModuleSettings.Properties.Mode;
set
{
if (_mode != value)
if (ModuleSettings.Properties.Mode != value)
{
_mode = value;
OnPropertyChanged(nameof(Mode));
ModuleSettings.Properties.Mode = value;
OnPropertyChanged(nameof(IsTimeConfigurationEnabled));
OnPropertyChanged(nameof(IsScreenConfigurationPossibleEnabled));
OnPropertyChanged(nameof(IsExpirationConfigurationEnabled));
Settings.Properties.Mode = value;
NotifyPropertyChanged();
}
}
@@ -125,79 +100,93 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
public bool KeepDisplayOn
{
get => _keepDisplayOn;
get => ModuleSettings.Properties.KeepDisplayOn;
set
{
if (_keepDisplayOn != value)
if (ModuleSettings.Properties.KeepDisplayOn != value)
{
_keepDisplayOn = value;
OnPropertyChanged(nameof(KeepDisplayOn));
Settings.Properties.KeepDisplayOn = value;
ModuleSettings.Properties.KeepDisplayOn = value;
NotifyPropertyChanged();
}
}
}
public uint Hours
public uint IntervalHours
{
get => _hours;
get => ModuleSettings.Properties.IntervalHours;
set
{
if (_hours != value)
if (ModuleSettings.Properties.IntervalHours != value)
{
_hours = value;
OnPropertyChanged(nameof(Hours));
Settings.Properties.Hours = value;
ModuleSettings.Properties.IntervalHours = value;
NotifyPropertyChanged();
}
}
}
public uint Minutes
public uint IntervalMinutes
{
get => _minutes;
get => ModuleSettings.Properties.IntervalMinutes;
set
{
if (_minutes != value)
if (ModuleSettings.Properties.IntervalMinutes != value)
{
_minutes = value;
OnPropertyChanged(nameof(Minutes));
Settings.Properties.Minutes = 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);
if (SendConfigMSG != null)
{
SndAwakeSettings outsettings = new SndAwakeSettings(Settings);
SndModuleSettings<SndAwakeSettings> ipcMessage = new SndModuleSettings<SndAwakeSettings>(outsettings);
string targetMessage = ipcMessage.ToJsonString();
SendConfigMSG(targetMessage);
}
}
public void RefreshEnabledState()
{
InitializeEnabledValue();
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 GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
private AwakeSettings _moduleSettings;
private bool _isEnabled;
private uint _hours;
private uint _minutes;
private bool _keepDisplayOn;
private AwakeMode _mode;
}
}