Files
OpenNest/OpenNest.Desktop/Forms/OperationProgress.cs
T
ajandClaude Opus 5.5 99c440b215 feat(desktop): share the plate command progress window's rules
OperationProgress holds what the WinForms Cancel window for Expand and
Fix Spacing does apart from drawing itself: a run that finishes within
300 ms never shows it, the caller blocks until then, the elapsed time
updates every half second, and after Cancel it reads "Canceling...
waiting for the current step to stop". The WinForms window now uses it
with unchanged behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:11:35 -05:00

42 lines
1.9 KiB
C#

using System;
using System.Threading.Tasks;
namespace OpenNest.Forms
{
/// <summary>
/// The rules of the small "working" window both desktop apps show with a Cancel button while
/// a long plate command (Expand Spacing, Fix Spacing) runs on a worker: the window appears only
/// once the run outlasts <see cref="DefaultShowDelay"/>, shows the elapsed time, and after
/// Cancel says it is waiting for the worker to stop. Cancel, Escape and closing only request
/// cancellation; the window closes once the worker has actually stopped.
/// </summary>
public static class OperationProgress
{
/// <summary>Runs that finish within this delay never show the window.</summary>
public static readonly TimeSpan DefaultShowDelay = TimeSpan.FromMilliseconds(300);
/// <summary>How often the window updates its elapsed time.</summary>
public static readonly TimeSpan StatusInterval = TimeSpan.FromMilliseconds(500);
/// <summary>
/// Waits up to <paramref name="delay"/> for <paramref name="task"/>, blocking the caller as
/// the window's owner is blocked; true when it finished, faulted or was canceled in time.
/// </summary>
public static bool FinishesWithin(Task task, TimeSpan delay)
{
ArgumentNullException.ThrowIfNull(task);
// A wait handle, unlike Task.Wait, does not throw for a faulted run.
return ((IAsyncResult)task).AsyncWaitHandle.WaitOne(delay);
}
/// <summary>"Elapsed 1:05", or after Cancel "Canceling... waiting for the current step to stop (1:05)".</summary>
public static string StatusText(TimeSpan elapsed, bool canceling)
{
var time = $"{(int)elapsed.TotalMinutes}:{elapsed.Seconds:00}";
return canceling
? $"Canceling... waiting for the current step to stop ({time})"
: $"Elapsed {time}";
}
}
}