mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-05 04:39:08 +08:00
Simplify settings handling to not rely on complex properties
This commit is contained in:
parent
65ecfd2424
commit
d566d0bdf0
@ -104,7 +104,7 @@ namespace Espresso.Shell.Core
|
||||
.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();
|
||||
_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)
|
||||
{
|
||||
|
@ -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<EspressoSettings>(moduleName);
|
||||
currentSettings.Properties.KeepDisplayOn.Value = !currentSettings.Properties.KeepDisplayOn.Value;
|
||||
currentSettings.Properties.KeepDisplayOn = !currentSettings.Properties.KeepDisplayOn;
|
||||
|
||||
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) =>
|
||||
{
|
||||
// Set timed keep awake.
|
||||
var currentSettings = ModuleSettings.GetSettings<EspressoSettings>(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<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();
|
||||
|
||||
|
@ -79,12 +79,12 @@ namespace Espresso.Shell
|
||||
|
||||
displayOption.Required = false;
|
||||
|
||||
var timeOption = new Option<long>(
|
||||
var timeOption = new Option<uint>(
|
||||
aliases: new[] { "--time-limit", "-t" },
|
||||
getDefaultValue: () => 0,
|
||||
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,
|
||||
},
|
||||
@ -115,7 +115,7 @@ namespace Espresso.Shell
|
||||
|
||||
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;
|
||||
}
|
||||
@ -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}.");
|
||||
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user