From d566d0bdf0421a331b0c6b62707481632a5f038f Mon Sep 17 00:00:00 2001 From: Den Delimarsky <1389609+dend@users.noreply.github.com> Date: Tue, 11 May 2021 21:41:07 -0700 Subject: [PATCH] Simplify settings handling to not rely on complex properties --- .../espresso/Espresso/Core/APIHelper.cs | 8 +++---- .../espresso/Espresso/Core/TrayHelper.cs | 12 +++++------ src/modules/espresso/Espresso/Program.cs | 16 +++++++------- .../EspressoProperties.cs | 12 +++++------ .../ViewModels/EspressoViewModel.cs | 21 +++++++++---------- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/modules/espresso/Espresso/Core/APIHelper.cs b/src/modules/espresso/Espresso/Core/APIHelper.cs index d63e5266fa..03902740c4 100644 --- a/src/modules/espresso/Espresso/Core/APIHelper.cs +++ b/src/modules/espresso/Espresso/Core/APIHelper.cs @@ -104,7 +104,7 @@ namespace Espresso.Shell.Core .ContinueWith((result) => failureCallback, TaskContinuationOptions.NotOnRanToCompletion); } - public static void SetTimedKeepAwake(long seconds, Action callback, Action failureCallback, bool keepDisplayOn = true) + public static void SetTimedKeepAwake(uint seconds, Action callback, Action failureCallback, bool keepDisplayOn = true) { _tokenSource = new CancellationTokenSource(); _threadToken = _tokenSource.Token; @@ -153,7 +153,7 @@ namespace Espresso.Shell.Core } } - private static bool RunTimedLoop(long seconds, bool keepDisplayOn = true) + private static bool RunTimedLoop(uint seconds, bool keepDisplayOn = true) { bool success = false; @@ -168,7 +168,7 @@ namespace Espresso.Shell.Core { _log.Info("Timed keep-awake with display on."); var startTime = DateTime.UtcNow; - while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) + while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(Math.Abs(seconds))) { if (_threadToken.IsCancellationRequested) { @@ -191,7 +191,7 @@ namespace Espresso.Shell.Core { _log.Info("Timed keep-awake with display off."); var startTime = DateTime.UtcNow; - while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) + while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(Math.Abs(seconds))) { if (_threadToken.IsCancellationRequested) { diff --git a/src/modules/espresso/Espresso/Core/TrayHelper.cs b/src/modules/espresso/Espresso/Core/TrayHelper.cs index 3ec3e9aef6..0e163cef57 100644 --- a/src/modules/espresso/Espresso/Core/TrayHelper.cs +++ b/src/modules/espresso/Espresso/Core/TrayHelper.cs @@ -47,7 +47,7 @@ namespace Espresso.Shell.Core { SetTray( text, - settings.Properties.KeepDisplayOn.Value, + settings.Properties.KeepDisplayOn, settings.Properties.Mode, IndefiniteKeepAwakeCallback(text), TimedKeepAwakeCallback(text), @@ -69,21 +69,21 @@ namespace Espresso.Shell.Core { // Just changing the display mode. var currentSettings = ModuleSettings.GetSettings(moduleName); - currentSettings.Properties.KeepDisplayOn.Value = !currentSettings.Properties.KeepDisplayOn.Value; + currentSettings.Properties.KeepDisplayOn = !currentSettings.Properties.KeepDisplayOn; ModuleSettings.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); }; } - private static Action TimedKeepAwakeCallback(string moduleName) + private static Action TimedKeepAwakeCallback(string moduleName) { return (hours, minutes) => { // Set timed keep awake. var currentSettings = ModuleSettings.GetSettings(moduleName); currentSettings.Properties.Mode = EspressoMode.TIMED; - currentSettings.Properties.Hours.Value = hours; - currentSettings.Properties.Minutes.Value = minutes; + currentSettings.Properties.Hours = hours; + currentSettings.Properties.Minutes = minutes; ModuleSettings.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); }; @@ -101,7 +101,7 @@ namespace Espresso.Shell.Core }; } - public static void SetTray(string text, bool keepDisplayOn, EspressoMode mode, Action indefiniteKeepAwakeCallback, Action timedKeepAwakeCallback, Action keepDisplayOnCallback, Action exitCallback) + public static void SetTray(string text, bool keepDisplayOn, EspressoMode mode, Action indefiniteKeepAwakeCallback, Action timedKeepAwakeCallback, Action keepDisplayOnCallback, Action exitCallback) { var contextMenuStrip = new ContextMenuStrip(); diff --git a/src/modules/espresso/Espresso/Program.cs b/src/modules/espresso/Espresso/Program.cs index b7c2d79f16..31b45eebfa 100644 --- a/src/modules/espresso/Espresso/Program.cs +++ b/src/modules/espresso/Espresso/Program.cs @@ -79,12 +79,12 @@ namespace Espresso.Shell displayOption.Required = false; - var timeOption = new Option( + var timeOption = new Option( aliases: new[] { "--time-limit", "-t" }, getDefaultValue: () => 0, description: "Determines the interval, in seconds, during which the computer is kept awake.") { - Argument = new Argument(() => 0) + Argument = new Argument(() => 0) { Arity = ArgumentArity.ExactlyOne, }, @@ -115,7 +115,7 @@ namespace Espresso.Shell rootCommand.Description = AppName; - rootCommand.Handler = CommandHandler.Create(HandleCommandLineArguments); + rootCommand.Handler = CommandHandler.Create(HandleCommandLineArguments); return rootCommand.InvokeAsync(args).Result; } @@ -128,7 +128,7 @@ namespace Espresso.Shell Environment.Exit(exitCode); } - private static void HandleCommandLineArguments(bool usePtConfig, bool displayOn, long timeLimit, int pid) + private static void HandleCommandLineArguments(bool usePtConfig, bool displayOn, uint timeLimit, int pid) { if (pid == 0) { @@ -233,15 +233,15 @@ namespace Espresso.Shell case EspressoMode.INDEFINITE: { // Indefinite keep awake. - SetupIndefiniteKeepAwake(settings.Properties.KeepDisplayOn.Value); + SetupIndefiniteKeepAwake(settings.Properties.KeepDisplayOn); break; } case EspressoMode.TIMED: { // Timed keep-awake. - long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60); - SetupTimedKeepAwake(computedTime, settings.Properties.KeepDisplayOn.Value); + uint computedTime = (settings.Properties.Hours * 60 * 60) + (settings.Properties.Minutes * 60); + SetupTimedKeepAwake(computedTime, settings.Properties.KeepDisplayOn); break; } @@ -272,7 +272,7 @@ namespace Espresso.Shell } } - private static void SetupTimedKeepAwake(long time, bool displayOn) + private static void SetupTimedKeepAwake(uint time, bool displayOn) { _log.Info($"Timed keep-awake. Expected runtime: {time} seconds with display on setting set to {displayOn}."); diff --git a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/EspressoProperties.cs b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/EspressoProperties.cs index 13a8305cc1..f52be93315 100644 --- a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/EspressoProperties.cs +++ b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/EspressoProperties.cs @@ -10,23 +10,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library { public EspressoProperties() { - KeepDisplayOn = new BoolProperty(); + KeepDisplayOn = false; Mode = EspressoMode.INDEFINITE; - Hours = new IntProperty(); - Minutes = new IntProperty(); + Hours = 0; + Minutes = 0; } [JsonPropertyName("espresso_keep_display_on")] - public BoolProperty KeepDisplayOn { get; set; } + public bool KeepDisplayOn { get; set; } [JsonPropertyName("espresso_mode")] public EspressoMode Mode { get; set; } [JsonPropertyName("espresso_hours")] - public IntProperty Hours { get; set; } + public uint Hours { get; set; } [JsonPropertyName("espresso_minutes")] - public IntProperty Minutes { get; set; } + public uint Minutes { get; set; } } public enum EspressoMode diff --git a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/EspressoViewModel.cs b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/EspressoViewModel.cs index 599e12bb74..3483a35e82 100644 --- a/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/EspressoViewModel.cs +++ b/src/settings-ui/Microsoft.PowerToys.Settings.UI.Library/ViewModels/EspressoViewModel.cs @@ -36,10 +36,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels Settings = moduleSettingsRepository.SettingsConfig; _isEnabled = GeneralSettingsConfig.Enabled.Espresso; - _keepDisplayOn = Settings.Properties.KeepDisplayOn.Value; + _keepDisplayOn = Settings.Properties.KeepDisplayOn; _mode = Settings.Properties.Mode; - _hours = Settings.Properties.Hours.Value; - _minutes = Settings.Properties.Minutes.Value; + _hours = Settings.Properties.Hours; + _minutes = Settings.Properties.Minutes; // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; @@ -89,13 +89,12 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels _keepDisplayOn = value; OnPropertyChanged(nameof(KeepDisplayOn)); - Settings.Properties.KeepDisplayOn = new BoolProperty(value); - NotifyPropertyChanged(); + Settings.Properties.KeepDisplayOn = value; } } } - public int Hours + public uint Hours { get => _hours; set @@ -105,13 +104,13 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels _hours = value; OnPropertyChanged(nameof(Hours)); - Settings.Properties.Hours = new IntProperty(value); + Settings.Properties.Hours = value; NotifyPropertyChanged(); } } } - public int Minutes + public uint Minutes { get => _minutes; set @@ -121,7 +120,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels _minutes = value; OnPropertyChanged(nameof(Minutes)); - Settings.Properties.Minutes = new IntProperty(value); + Settings.Properties.Minutes = value; NotifyPropertyChanged(); } } @@ -141,8 +140,8 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels } private bool _isEnabled; - private int _hours; - private int _minutes; + private uint _hours; + private uint _minutes; private bool _keepDisplayOn; private EspressoMode _mode; }