Fixed incorrect countdown action desplay for restart.

Also partially improved documentation
This commit is contained in:
Kamil118 committed 2026-08-30 23:29:04 +02:00
1 parent 1ce52c801d
commit 733d0ceede
2 files changed
+92 -6

No files matched your search

+27 -4
View File
@@ -11,24 +11,30 @@ namespace GoToSleep
{
Shutdown,
Suspend,
Hibernate
Hibernate,
Restart
};
public PowerDownType actionType { get; }
public DateTime when { get; }
public delegate void Action();
public Action onCountdownEnd { get; }
// Private constructor to enforce actionType and Action consistency
private PowerDownData(PowerDownType _type, DateTime _when, Action action)
{
actionType = _type;
when = _when;
onCountdownEnd = action;
}
/// <summary>
/// Creates PowerDownData for Shutdown action scheduled at specified time
/// </summary>
/// <param name="when">The date and time when the action is supposed to trigger</param>
/// <returns>A new PowerDownData configured for Shutdown</returns>
public static PowerDownData Shutdown(DateTime when)
{
return new PowerDownData(PowerDownType.Shutdown, when, () => {
@@ -39,21 +45,38 @@ namespace GoToSleep
});
}
/// <summary>
/// Creates PowerDownData for Restart action scheduled at specified time
/// </summary>
/// <param name="when">The date and time when the action is supposed to trigger</param>
/// <returns>A new PowerDownData configured for Restart</returns>
public static PowerDownData Restart(DateTime when)
{
return new PowerDownData(PowerDownType.Shutdown, when, () => {
return new PowerDownData(PowerDownType.Restart, when, () => {
var psi = new ProcessStartInfo("shutdown", "/r /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
});
}
/// <summary>
/// Creates PowerDownData for Suspend action scheduled at specified time
/// </summary>
/// <param name="when">The date and time when the action is supposed to trigger</param>
/// <returns>A new PowerDownData configured for Suspend</returns>
public static PowerDownData Suspend(DateTime when)
{
return new PowerDownData(PowerDownType.Suspend, when, () => {
Application.SetSuspendState(PowerState.Suspend, false, false);
});
}
/// <summary>
/// Creates PowerDownData for Hibernate action scheduled at specified time
/// </summary>
/// <param name="when">The date and time when the action is supposed to trigger</param>
/// <returns>A new PowerDownData configured for Hibernate</returns>
public static PowerDownData Hibernate(DateTime when)
{
return new PowerDownData(PowerDownType.Hibernate, when, () => {
+65 -2
View File
@@ -13,16 +13,32 @@ namespace GoToSleep
{
internal class SystemPowerManager
{
private static int TIMER_WARNING_SECONDS = 15;
/// <summary>
/// Static value specifying the minimum delay (in seconds) of action execution without a warning.
/// </summary>
/// <remarks>
/// If user specifies time that's delayed by less than this number of seconds, ask for confirmation.
/// Exists to protect user from typos or interpreter errors that would schedule an action
/// that executes before user can notice the mistake and cancel it.
///
/// Selected unilaterally by a committee of 1 developers.</remarks>
private const int TIMER_WARNING_SECONDS = 15;
/// <summary>
/// Base exception for when the string can't be converted into a valid <see cref="DateTime"/>object.
/// </summary>
[Serializable]
class DatetimeStringException : Exception
abstract class DatetimeStringException : Exception
{
public DatetimeStringException(string message) : base(message) { }
public DatetimeStringException(Exception e) : base("Conversion from string to time failed.", e) { }
}
/// <summary>
/// The exception that is thrown when the time string is converted into a <see cref="DateTime"/> object that references
/// a point of time in the past.
/// </summary>
[Serializable]
class DateTimeBeforeException : DatetimeStringException
{
@@ -31,12 +47,20 @@ namespace GoToSleep
}
}
/// <summary>
/// The exception that's thrown when the time string couldn't be converted into any <see cref="DateTime"/>.
/// </summary>
[Serializable]
class ParseFailedException : DatetimeStringException
{
public ParseFailedException(Exception e) : base(e) { }
}
/// <summary>
/// Initializes a new instance of the <see cref="SystemPowerManager"/> class with a reference to the main window.
/// </summary>
/// <param name="pf"><see cref="GoToSleep"/> main window instance.</param>
public SystemPowerManager(GoToSleep pf)
{
parentForm = pf;
@@ -45,6 +69,12 @@ namespace GoToSleep
CountdownForm? progressPopup;
/// <summary>
/// Prepares an input time string to improve compatibility with Microsoft.Recognizers.Text
/// </summary>
/// <param name="s">Time string to be preprocessed</param>
/// <returns>Time string modified for better compatibility with Microsoft.Recognizers.Text parser</returns>
string prepareTimeString(string s)
{
// make sure there are spaces between numbers and units, for the parser
@@ -61,6 +91,14 @@ namespace GoToSleep
return s;
}
/// <summary>
/// Converts natural language time string to Datetime object
/// </summary>
/// <param name="timeString">String containing natural language time definition</param>
/// <returns>new Datetime object referencing the time specified in <paramref name="timeString"/></returns>
/// <exception cref="ParseFailedException">Thrown when general parsing failure</exception>
/// <exception cref="DateTimeBeforeException">Thrown when the input string references point of time in the past</exception>
DateTime getTimeOffsetFromString(string timeString)
{
DateTime result;
@@ -109,6 +147,15 @@ namespace GoToSleep
return result;
}
/// <summary>
/// Prompts user to confirm the scheduling of action if it's scheduled to happen less than
/// <see cref="TIMER_WARNING_SECONDS"/> in the future.
/// </summary>
/// <param name="target">The Datetime the action is scheduled to execute.</param>
/// <param name="action">The name of action that is about to be scheduled.</param>
/// <returns><see langword="true""/> if the date doesn't meet warning requirements, or the user confirmed the intent.
/// <see langword="false"/> if the user clicked cancel or closed the prompt window.</returns>
bool confirmDate(DateTime target, string action)
{
if (target <= DateTime.Now.AddSeconds(TIMER_WARNING_SECONDS))
@@ -124,6 +171,10 @@ namespace GoToSleep
return true;
}
/// <summary>
/// Displays a new <see cref="CountdownForm"/> window for scheduled shutdown action.
/// </summary>
/// <param name="timeString">Time string refrencing when the action should be executed.</param>
public void shutdown(string timeString)
{
DateTime target;
@@ -145,6 +196,10 @@ namespace GoToSleep
progressPopup.Show();
}
/// <summary>
/// Displays a new <see cref="CountdownForm"/> window for scheduled suspend action.
/// </summary>
/// <param name="timeString">Time string refrencing when the action should be executed.</param>
public void suspend(string timeString)
{
DateTime target;
@@ -166,6 +221,10 @@ namespace GoToSleep
progressPopup.Show();
}
/// <summary>
/// Displays a new <see cref="CountdownForm"/> window for scheduled hibernate action.
/// </summary>
/// <param name="timeString">Time string refrencing when the action should be executed.</param>
public void hibernate(string timeString)
{
DateTime target;
@@ -187,6 +246,10 @@ namespace GoToSleep
progressPopup.Show();
}
/// <summary>
/// Displays a new <see cref="CountdownForm"/> window for scheduled restart action.
/// </summary>
/// <param name="timeString">Time string refrencing when the action should be executed.</param>
public void restart(string timeString)
{
DateTime target;