mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
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>
This commit is contained in:
1 parent
10651618bf
commit
99c440b215
3 files changed
+80
-9
No files matched your search
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using OpenNest.Forms;
|
||||
|
||||
namespace OpenNest.Desktop.Tests.Forms;
|
||||
|
||||
public class OperationProgressTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0, false, "Elapsed 0:00")]
|
||||
[InlineData(65.9, false, "Elapsed 1:05")]
|
||||
[InlineData(3601, true, "Canceling... waiting for the current step to stop (60:01)")]
|
||||
public void Status_text_shows_the_elapsed_minutes_and_seconds(double seconds, bool canceling, string expected) =>
|
||||
Assert.Equal(expected, OperationProgress.StatusText(TimeSpan.FromSeconds(seconds), canceling));
|
||||
|
||||
[Fact]
|
||||
public void A_finished_or_faulted_run_finishes_within_the_delay()
|
||||
{
|
||||
Assert.True(OperationProgress.FinishesWithin(Task.CompletedTask, TimeSpan.Zero));
|
||||
Assert.True(OperationProgress.FinishesWithin(Task.FromException(new InvalidOperationException()), TimeSpan.Zero));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_run_still_going_does_not_finish_within_the_delay()
|
||||
{
|
||||
using var release = new ManualResetEventSlim();
|
||||
var task = Task.Run(release.Wait);
|
||||
|
||||
Assert.False(OperationProgress.FinishesWithin(task, TimeSpan.FromMilliseconds(20)));
|
||||
|
||||
release.Set();
|
||||
Assert.True(OperationProgress.FinishesWithin(task, TimeSpan.FromSeconds(10)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ namespace OpenNest.Forms;
|
||||
internal sealed class OperationProgressForm : Form
|
||||
{
|
||||
/// <summary>Runs that finish within this delay never show the window.</summary>
|
||||
internal static readonly TimeSpan DefaultShowDelay = TimeSpan.FromMilliseconds(300);
|
||||
internal static readonly TimeSpan DefaultShowDelay = OperationProgress.DefaultShowDelay;
|
||||
|
||||
private readonly Task task;
|
||||
private readonly CancellationTokenSource cts;
|
||||
@@ -78,7 +78,7 @@ internal sealed class OperationProgressForm : Form
|
||||
CancelButton = cancelButton;
|
||||
cancelButton.DialogResult = DialogResult.None;
|
||||
|
||||
elapsedTimer = new System.Windows.Forms.Timer { Interval = 500 };
|
||||
elapsedTimer = new System.Windows.Forms.Timer { Interval = (int)OperationProgress.StatusInterval.TotalMilliseconds };
|
||||
elapsedTimer.Tick += (_, _) => UpdateStatus();
|
||||
UpdateStatus();
|
||||
|
||||
@@ -112,8 +112,7 @@ internal sealed class OperationProgressForm : Form
|
||||
var token = cts.Token;
|
||||
var task = Task.Run(() => work(token));
|
||||
|
||||
// A wait handle, unlike Task.Wait, does not throw for a faulted run.
|
||||
if (!((IAsyncResult)task).AsyncWaitHandle.WaitOne(showDelay ?? DefaultShowDelay))
|
||||
if (!OperationProgress.FinishesWithin(task, showDelay ?? DefaultShowDelay))
|
||||
{
|
||||
using var form = new OperationProgressForm(title, message, task, cts);
|
||||
form.ShowDialog(owner);
|
||||
@@ -185,10 +184,6 @@ internal sealed class OperationProgressForm : Form
|
||||
|
||||
private void UpdateStatus()
|
||||
{
|
||||
var elapsed = stopwatch.Elapsed;
|
||||
var time = $"{(int)elapsed.TotalMinutes}:{elapsed.Seconds:00}";
|
||||
statusLabel.Text = cts.IsCancellationRequested
|
||||
? $"Canceling... waiting for the current step to stop ({time})"
|
||||
: $"Elapsed {time}";
|
||||
statusLabel.Text = OperationProgress.StatusText(stopwatch.Elapsed, cts.IsCancellationRequested);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user