Simplify settings handling to not rely on complex properties

This commit is contained in:
Den Delimarsky 2021-05-11 21:41:07 -07:00
parent 65ecfd2424
commit d566d0bdf0
No known key found for this signature in database
GPG Key ID: E1BE1355085F0BCF
5 changed files with 34 additions and 35 deletions

View File

@ -104,7 +104,7 @@ namespace Espresso.Shell.Core
.ContinueWith((result) => failureCallback, TaskContinuationOptions.NotOnRanToCompletion); .ContinueWith((result) => failureCallback, TaskContinuationOptions.NotOnRanToCompletion);
} }
public static void SetTimedKeepAwake(long seconds, Action<bool> callback, Action failureCallback, bool keepDisplayOn = true) public static void SetTimedKeepAwake(uint seconds, Action<bool> callback, Action failureCallback, bool keepDisplayOn = true)
{ {
_tokenSource = new CancellationTokenSource(); _tokenSource = new CancellationTokenSource();
_threadToken = _tokenSource.Token; _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; bool success = false;
@ -168,7 +168,7 @@ namespace Espresso.Shell.Core
{ {
_log.Info("Timed keep-awake with display on."); _log.Info("Timed keep-awake with display on.");
var startTime = DateTime.UtcNow; var startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(Math.Abs(seconds)))
{ {
if (_threadToken.IsCancellationRequested) if (_threadToken.IsCancellationRequested)
{ {
@ -191,7 +191,7 @@ namespace Espresso.Shell.Core
{ {
_log.Info("Timed keep-awake with display off."); _log.Info("Timed keep-awake with display off.");
var startTime = DateTime.UtcNow; var startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(Math.Abs(seconds)))
{ {
if (_threadToken.IsCancellationRequested) if (_threadToken.IsCancellationRequested)
{ {

View File

@ -47,7 +47,7 @@ namespace Espresso.Shell.Core
{ {
SetTray( SetTray(
text, text,
settings.Properties.KeepDisplayOn.Value, settings.Properties.KeepDisplayOn,
settings.Properties.Mode, settings.Properties.Mode,
IndefiniteKeepAwakeCallback(text), IndefiniteKeepAwakeCallback(text),
TimedKeepAwakeCallback(text), TimedKeepAwakeCallback(text),
@ -69,21 +69,21 @@ namespace Espresso.Shell.Core
{ {
// Just changing the display mode. // Just changing the display mode.
var currentSettings = ModuleSettings.GetSettings<EspressoSettings>(moduleName); var currentSettings = ModuleSettings.GetSettings<EspressoSettings>(moduleName);
currentSettings.Properties.KeepDisplayOn.Value = !currentSettings.Properties.KeepDisplayOn.Value; currentSettings.Properties.KeepDisplayOn = !currentSettings.Properties.KeepDisplayOn;
ModuleSettings.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); ModuleSettings.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName);
}; };
} }
private static Action<int, int> TimedKeepAwakeCallback(string moduleName) private static Action<uint, uint> TimedKeepAwakeCallback(string moduleName)
{ {
return (hours, minutes) => return (hours, minutes) =>
{ {
// Set timed keep awake. // Set timed keep awake.
var currentSettings = ModuleSettings.GetSettings<EspressoSettings>(moduleName); var currentSettings = ModuleSettings.GetSettings<EspressoSettings>(moduleName);
currentSettings.Properties.Mode = EspressoMode.TIMED; currentSettings.Properties.Mode = EspressoMode.TIMED;
currentSettings.Properties.Hours.Value = hours; currentSettings.Properties.Hours = hours;
currentSettings.Properties.Minutes.Value = minutes; currentSettings.Properties.Minutes = minutes;
ModuleSettings.SaveSettings(JsonSerializer.Serialize(currentSettings), moduleName); 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<int, int> timedKeepAwakeCallback, Action keepDisplayOnCallback, Action exitCallback) public static void SetTray(string text, bool keepDisplayOn, EspressoMode mode, Action indefiniteKeepAwakeCallback, Action<uint, uint> timedKeepAwakeCallback, Action keepDisplayOnCallback, Action exitCallback)
{ {
var contextMenuStrip = new ContextMenuStrip(); var contextMenuStrip = new ContextMenuStrip();

View File

@ -79,12 +79,12 @@ namespace Espresso.Shell
displayOption.Required = false; displayOption.Required = false;
var timeOption = new Option<long>( var timeOption = new Option<uint>(
aliases: new[] { "--time-limit", "-t" }, aliases: new[] { "--time-limit", "-t" },
getDefaultValue: () => 0, getDefaultValue: () => 0,
description: "Determines the interval, in seconds, during which the computer is kept awake.") description: "Determines the interval, in seconds, during which the computer is kept awake.")
{ {
Argument = new Argument<long>(() => 0) Argument = new Argument<uint>(() => 0)
{ {
Arity = ArgumentArity.ExactlyOne, Arity = ArgumentArity.ExactlyOne,
}, },
@ -115,7 +115,7 @@ namespace Espresso.Shell
rootCommand.Description = AppName; rootCommand.Description = AppName;
rootCommand.Handler = CommandHandler.Create<bool, bool, long, int>(HandleCommandLineArguments); rootCommand.Handler = CommandHandler.Create<bool, bool, uint, int>(HandleCommandLineArguments);
return rootCommand.InvokeAsync(args).Result; return rootCommand.InvokeAsync(args).Result;
} }
@ -128,7 +128,7 @@ namespace Espresso.Shell
Environment.Exit(exitCode); 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) if (pid == 0)
{ {
@ -233,15 +233,15 @@ namespace Espresso.Shell
case EspressoMode.INDEFINITE: case EspressoMode.INDEFINITE:
{ {
// Indefinite keep awake. // Indefinite keep awake.
SetupIndefiniteKeepAwake(settings.Properties.KeepDisplayOn.Value); SetupIndefiniteKeepAwake(settings.Properties.KeepDisplayOn);
break; break;
} }
case EspressoMode.TIMED: case EspressoMode.TIMED:
{ {
// Timed keep-awake. // Timed keep-awake.
long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60); uint computedTime = (settings.Properties.Hours * 60 * 60) + (settings.Properties.Minutes * 60);
SetupTimedKeepAwake(computedTime, settings.Properties.KeepDisplayOn.Value); SetupTimedKeepAwake(computedTime, settings.Properties.KeepDisplayOn);
break; 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}."); _log.Info($"Timed keep-awake. Expected runtime: {time} seconds with display on setting set to {displayOn}.");

View File

@ -10,23 +10,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{ {
public EspressoProperties() public EspressoProperties()
{ {
KeepDisplayOn = new BoolProperty(); KeepDisplayOn = false;
Mode = EspressoMode.INDEFINITE; Mode = EspressoMode.INDEFINITE;
Hours = new IntProperty(); Hours = 0;
Minutes = new IntProperty(); Minutes = 0;
} }
[JsonPropertyName("espresso_keep_display_on")] [JsonPropertyName("espresso_keep_display_on")]
public BoolProperty KeepDisplayOn { get; set; } public bool KeepDisplayOn { get; set; }
[JsonPropertyName("espresso_mode")] [JsonPropertyName("espresso_mode")]
public EspressoMode Mode { get; set; } public EspressoMode Mode { get; set; }
[JsonPropertyName("espresso_hours")] [JsonPropertyName("espresso_hours")]
public IntProperty Hours { get; set; } public uint Hours { get; set; }
[JsonPropertyName("espresso_minutes")] [JsonPropertyName("espresso_minutes")]
public IntProperty Minutes { get; set; } public uint Minutes { get; set; }
} }
public enum EspressoMode public enum EspressoMode

View File

@ -36,10 +36,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
Settings = moduleSettingsRepository.SettingsConfig; Settings = moduleSettingsRepository.SettingsConfig;
_isEnabled = GeneralSettingsConfig.Enabled.Espresso; _isEnabled = GeneralSettingsConfig.Enabled.Espresso;
_keepDisplayOn = Settings.Properties.KeepDisplayOn.Value; _keepDisplayOn = Settings.Properties.KeepDisplayOn;
_mode = Settings.Properties.Mode; _mode = Settings.Properties.Mode;
_hours = Settings.Properties.Hours.Value; _hours = Settings.Properties.Hours;
_minutes = Settings.Properties.Minutes.Value; _minutes = Settings.Properties.Minutes;
// set the callback functions value to hangle outgoing IPC message. // set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc; SendConfigMSG = ipcMSGCallBackFunc;
@ -89,13 +89,12 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
_keepDisplayOn = value; _keepDisplayOn = value;
OnPropertyChanged(nameof(KeepDisplayOn)); OnPropertyChanged(nameof(KeepDisplayOn));
Settings.Properties.KeepDisplayOn = new BoolProperty(value); Settings.Properties.KeepDisplayOn = value;
NotifyPropertyChanged();
} }
} }
} }
public int Hours public uint Hours
{ {
get => _hours; get => _hours;
set set
@ -105,13 +104,13 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
_hours = value; _hours = value;
OnPropertyChanged(nameof(Hours)); OnPropertyChanged(nameof(Hours));
Settings.Properties.Hours = new IntProperty(value); Settings.Properties.Hours = value;
NotifyPropertyChanged(); NotifyPropertyChanged();
} }
} }
} }
public int Minutes public uint Minutes
{ {
get => _minutes; get => _minutes;
set set
@ -121,7 +120,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
_minutes = value; _minutes = value;
OnPropertyChanged(nameof(Minutes)); OnPropertyChanged(nameof(Minutes));
Settings.Properties.Minutes = new IntProperty(value); Settings.Properties.Minutes = value;
NotifyPropertyChanged(); NotifyPropertyChanged();
} }
} }
@ -141,8 +140,8 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
} }
private bool _isEnabled; private bool _isEnabled;
private int _hours; private uint _hours;
private int _minutes; private uint _minutes;
private bool _keepDisplayOn; private bool _keepDisplayOn;
private EspressoMode _mode; private EspressoMode _mode;
} }