feat(drawing-library): confirm each add with a banner and list it at once

Adding from the Drawing Library now shows a slim green banner naming what
was added (or how many more, with the new required quantity). Each add or
In This Nest quantity edit raises NestChanged, so the main window lists the
drawing on its Drawings tab behind the dialog right away, brings that tab
forward and selects it, instead of waiting for the dialog to close.

The library form tests restore the WinForms synchronization context before
each add: DoEvents leaves a plain one behind, which sent the add's awaits to
the thread pool and created the banner's window there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-17 21:08:39 -05:00
1 parent 04c0e3bca3
commit 4f1779376b
8 files changed
+281 -18

No files matched your search

@@ -42,9 +42,9 @@ public class DrawingLibraryFormTests
Assert.Equal("Deburr both sides", notes.Text);
quantity.Value = 3;
Run(form.View.AddSelectedAsync());
Add(form);
quantity.Value = 2;
Run(form.View.AddSelectedAsync());
Add(form);
Assert.Empty(messages);
var drawing = Assert.Single(nest.Drawings);
@@ -57,6 +57,86 @@ public class DrawingLibraryFormTests
"The drawing library dialog test did not complete.");
}
[Fact]
public void EachAdd_ShowsTheBannerAndRaisesNestChangedWhileTheDialogIsOpen()
{
StaTestThread.Run(
() =>
{
var library = new FakeLibrary();
var record = library.Add("SYN-111", "Synthetic Steel", "Grade A", 0.25, Units.Inches, "");
var nest = MatchingNest();
var changed = new List<Drawing>();
using var form = new DrawingLibraryForm(library, Server, nest);
form.View.ShowMessage = (text, _, _) => throw new Xunit.Sdk.XunitException(text);
form.View.LoadPreview = (_, _) => Task.FromResult<Drawing>(null!);
form.NestChanged += (_, drawing) => changed.Add(drawing);
form.Show();
var grid = Field<DataGridView>(form, "drawingGrid");
var banner = Field<OpenNest.Controls.NoticeBanner>(form, "addedBanner");
var quantity = Field<NumericUpDown>(form, "quantityBox");
PumpUntilListed(grid, "the library page");
Assert.False(banner.Visible);
quantity.Value = 3;
Add(form);
var drawing = Assert.Single(nest.Drawings);
Assert.False(banner.InvokeRequired);
Assert.True(banner.Visible);
Assert.Equal($"Added 'SYN-111' revision {record.Revision} to the nest, quantity 3.", banner.Message);
Assert.Same(drawing, Assert.Single(changed));
quantity.Value = 2;
Add(form);
Assert.Equal("Added 2 more of 'SYN-111' to the nest (now 5 required).", banner.Message);
Assert.Equal(new[] { drawing, drawing }, changed);
// The In This Nest tab's quantity edits are listed behind the dialog too.
var nestGrid = Field<DataGridView>(form, "nestGrid");
Field<TabControl>(form, "tabControl").SelectedTab = Field<TabPage>(form, "nestTab");
nestGrid.CurrentCell = nestGrid.Rows[0].Cells["nestRequiredColumn"];
Assert.True(nestGrid.BeginEdit(true));
nestGrid.EditingControl!.Text = "7";
nestGrid.NotifyCurrentCellDirty(true);
Assert.True(nestGrid.EndEdit());
Assert.Equal(3, changed.Count);
Assert.Equal(7, drawing.Quantity.Required);
},
TestTimeout,
"The drawing library banner test did not complete.");
}
[Fact]
public void RefusedAdd_ShowsNoBannerAndRaisesNothing()
{
StaTestThread.Run(
() =>
{
var library = new FakeLibrary();
library.Add("SYN-313", "Synthetic Steel", "Grade A", 0.25, Units.Millimeters, "");
var nest = MatchingNest();
var changed = 0;
using var form = new DrawingLibraryForm(library, Server, nest);
form.View.ShowMessage = (_, _, _) => DialogResult.OK;
form.View.LoadPreview = (_, _) => Task.FromResult<Drawing>(null!);
form.NestChanged += (_, _) => changed++;
form.Show();
var grid = Field<DataGridView>(form, "drawingGrid");
PumpUntilListed(grid, "the library page");
Add(form);
Assert.Empty(nest.Drawings);
Assert.False(Field<OpenNest.Controls.NoticeBanner>(form, "addedBanner").Visible);
Assert.Equal(0, changed);
},
TestTimeout,
"The drawing library refused-add banner test did not complete.");
}
[Fact]
public void SelectingADrawing_ShowsItsDetailsAndPreviewsItsGeometry()
{
@@ -122,7 +202,7 @@ public class DrawingLibraryFormTests
PumpUntilListed(grid, "the library page");
Field<NumericUpDown>(form, "quantityBox").Value = 2;
Run(form.View.AddSelectedAsync());
Add(form);
Assert.Equal("In This Nest (2)", nestTab.Text);
Assert.Equal($"Revision {record.Revision}", nestGrid.Rows[1].Cells["nestLibraryColumn"].Value);
Assert.Equal(2, nestGrid.Rows[1].Cells["nestRequiredColumn"].Value);
@@ -183,9 +263,9 @@ public class DrawingLibraryFormTests
PumpUntilListed(grid, "the unfiltered page");
Assert.Equal(("", "", (double?)null), (library.Queries[^1].MaterialName, library.Queries[^1].MaterialGrade, library.Queries[^1].Thickness));
Run(form.View.AddSelectedAsync());
Add(form);
Assert.Empty(nest.Drawings);
Run(form.View.AddSelectedAsync());
Add(form);
Assert.Equal(2, messages.Count);
Assert.Contains("thickness 0.1875 (nest 0.25)", messages[0]);
@@ -213,7 +293,7 @@ public class DrawingLibraryFormTests
var grid = Field<DataGridView>(form, "drawingGrid");
PumpUntilListed(grid, "the library page");
Run(form.View.AddSelectedAsync());
Add(form);
Assert.Contains("millimeters", Assert.Single(messages));
Assert.Empty(nest.Drawings);
@@ -262,7 +342,7 @@ public class DrawingLibraryFormTests
Assert.Equal("Yes", grid.Rows[0].Cells["cutFirstColumn"].Value);
SelectFirstRow(grid);
Run(form.View.AddSelectedAsync());
Add(form);
Assert.True(Assert.Single(nest.Drawings).CutFirst);
},
TestTimeout,
@@ -524,7 +604,7 @@ public class DrawingLibraryFormTests
Assert.DoesNotContain(Field<TabPage>(form, "nestTab"), Field<TabControl>(form, "tabControl").TabPages.Cast<TabPage>());
Assert.False(Field<Button>(form, "addButton").Visible);
Assert.Equal("Edit Drawing...", Field<Button>(form, "editButton").Text);
Run(form.View.AddSelectedAsync());
Add(form);
Assert.Empty(library.Downloads);
typeof(DataGridView).GetMethod("OnCellDoubleClick", BindingFlags.Instance | BindingFlags.NonPublic)!
@@ -635,6 +715,17 @@ public class DrawingLibraryFormTests
Material = new Material("Synthetic Steel", "Grade A"),
};
/// <summary>
/// Adds the selected drawing and waits for it. Each DoEvents leaves a plain synchronization
/// context on the thread, so the WinForms one is put back first: as in the dialog's own
/// message loop, the add's awaits then return to the UI thread rather than the thread pool.
/// </summary>
private static void Add(DrawingLibraryForm form)
{
SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
Run(form.View.AddSelectedAsync());
}
// A faulted task rethrows here, so a failed step reports its own error instead of a later timeout.
private static void Run(Task task)
{
@@ -0,0 +1,48 @@
using System.Reflection;
using System.Windows.Forms;
using OpenNest.Controls;
using OpenNest.Forms;
using OpenNest.Shapes;
namespace OpenNest.WinForms.Tests.Forms;
[Collection("Fill operation lifetime")]
public class EditNestLibraryChangeTests
{
[Fact]
public void ALibraryAdd_IsListedOnTheDrawingsTabAndSelectedThere() => RunSta(() =>
{
var nest = new Nest("library add");
var existing = new RectangleShape { Name = "A-EXISTING", Length = 3, Width = 1 }.GetDrawing();
existing.Quantity.Required = 1;
nest.Drawings.Add(existing);
using var form = new EditNestForm(nest) { Width = 1100, Height = 700 };
form.PlateView.SetOverlapAutoCheck(null);
form.Show();
Application.DoEvents();
var tabs = Field<TabControl>(form, "tabControl1");
var drawingsTab = Field<TabPage>(form, "tabPage2");
var list = Field<DrawingListBox>(form, "drawingListBox1");
tabs.SelectedTab = Field<TabPage>(form, "tabPage1");
// What the Drawing Library dialog does to the nest before it raises NestChanged.
var added = new RectangleShape { Name = "B-LIBRARY", Length = 4, Width = 2 }.GetDrawing();
added.Quantity.Required = 3;
nest.Drawings.Add(added);
form.ShowLibraryChange(added);
Assert.Same(drawingsTab, tabs.SelectedTab);
Assert.Equal(new object[] { existing, added }, list.Items.Cast<object>());
Assert.Same(added, list.SelectedItem);
});
private static T Field<T>(EditNestForm form, string name) =>
(T)typeof(EditNestForm)
.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(form)!;
private static void RunSta(System.Action action) =>
StaTestThread.Run(action, TimeSpan.FromMinutes(3), "The STA test did not complete.");
}
+14 -3
View File
@@ -86,6 +86,7 @@ namespace OpenNest.Controls
editButton = new System.Windows.Forms.Button();
exportButton = new System.Windows.Forms.Button();
statusLabel = new System.Windows.Forms.Label();
addedBanner = new OpenNest.Controls.NoticeBanner();
searchTimer = new System.Windows.Forms.Timer(components);
previewTimer = new System.Windows.Forms.Timer(components);
drawingMenu = new System.Windows.Forms.ContextMenuStrip(components);
@@ -263,13 +264,15 @@ namespace OpenNest.Controls
layoutTable.ColumnCount = 1;
layoutTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
layoutTable.Controls.Add(filterTable, 0, 0);
layoutTable.Controls.Add(splitContainer, 0, 1);
layoutTable.Controls.Add(statusLabel, 0, 2);
layoutTable.Controls.Add(addedBanner, 0, 1);
layoutTable.Controls.Add(splitContainer, 0, 2);
layoutTable.Controls.Add(statusLabel, 0, 3);
layoutTable.Dock = System.Windows.Forms.DockStyle.Fill;
layoutTable.Margin = new System.Windows.Forms.Padding(0);
layoutTable.Padding = new System.Windows.Forms.Padding(8);
layoutTable.Name = "layoutTable";
layoutTable.RowCount = 3;
layoutTable.RowCount = 4;
layoutTable.RowStyles.Add(new System.Windows.Forms.RowStyle());
layoutTable.RowStyles.Add(new System.Windows.Forms.RowStyle());
layoutTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
layoutTable.RowStyles.Add(new System.Windows.Forms.RowStyle());
@@ -788,6 +791,13 @@ namespace OpenNest.Controls
statusLabel.Size = new System.Drawing.Size(100, 20);
statusLabel.TabIndex = 2;
//
// addedBanner
//
addedBanner.Dock = System.Windows.Forms.DockStyle.Top;
addedBanner.Margin = new System.Windows.Forms.Padding(0, 0, 0, 6);
addedBanner.Name = "addedBanner";
addedBanner.TabIndex = 3;
//
// searchTimer
//
searchTimer.Interval = 300;
@@ -988,6 +998,7 @@ namespace OpenNest.Controls
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button editButton;
private System.Windows.Forms.Label statusLabel;
private OpenNest.Controls.NoticeBanner addedBanner;
private System.Windows.Forms.Timer searchTimer;
private System.Windows.Forms.Timer previewTimer;
private System.Windows.Forms.ContextMenuStrip drawingMenu;
+16 -5
View File
@@ -20,8 +20,9 @@ namespace OpenNest.Controls
/// <see cref="DrawingLibraryBrowseSession"/>, filtered to the nest's material and thickness
/// unless Show all is ticked. Add downloads the listed revision's exact package, plans it
/// with <see cref="LibraryDrawingInsert"/>, shows any warning or refusal first and only then
/// changes the nest. Opened without a nest, it lists every active drawing and has no In This
/// Nest tab; as the Library tab of <see cref="DrawingSetsForm"/> a double-click, Enter or Add
/// changes the nest; a green banner confirms each add and <see cref="NestChanged"/> lets the
/// host list it behind the dialog at once. Opened without a nest, it lists every active
/// drawing and has no In This Nest tab; as the Library tab of <see cref="DrawingSetsForm"/> a double-click, Enter or Add
/// to Release adds the drawing on its own, at the entered quantity, to the window's
/// <see cref="ReleasePanel"/>, and elsewhere a double-click edits it. Edit Drawing changes the
/// selected drawing's details, notes and nesting settings (Cut first, priority, rotation,
@@ -116,6 +117,12 @@ namespace OpenNest.Controls
/// </summary>
public bool QuantitiesChanged { get; private set; }
/// <summary>
/// Raised each time an add or an In This Nest quantity edit changes the nest, while the
/// dialog is still open, so the host can show the change behind it at once.
/// </summary>
public event EventHandler<Drawing> NestChanged;
/// <summary>
/// The window that owns this view's dialogs. A dialog owned by a child control is not kept
/// above its top-level window, so it can fall behind it.
@@ -853,6 +860,7 @@ namespace OpenNest.Controls
adding = true;
UpdateCommands();
addedBanner.Hide();
statusLabel.Text = $"Downloading '{record.Name}' revision {record.Revision}...";
var quantity = (int)quantityBox.Value;
try
@@ -895,9 +903,11 @@ namespace OpenNest.Controls
if (!AddedDrawings.Contains(insert.Drawing))
AddedDrawings.Add(insert.Drawing);
RefreshNestTab();
statusLabel.Text = insert.Kind == LibraryInsertKind.AddQuantity
? $"Added {quantity} to '{insert.Drawing.Name}' (now {insert.Drawing.Quantity.Required})."
: $"Added '{insert.Drawing.Name}' revision {record.Revision}, quantity {quantity}.";
statusLabel.Text = "";
addedBanner.Show(insert.Kind == LibraryInsertKind.AddQuantity
? $"Added {quantity:N0} more of '{insert.Drawing.Name}' to the nest (now {insert.Drawing.Quantity.Required:N0} required)."
: $"Added '{insert.Drawing.Name}' revision {record.Revision} to the nest, quantity {quantity:N0}.");
NestChanged?.Invoke(this, insert.Drawing);
}
catch (ServerTooOldException ex)
{
@@ -1101,6 +1111,7 @@ namespace OpenNest.Controls
statusLabel.Text = $"'{drawing.Name}' now requires {value:N0} (was {before:N0}).";
UpdateNestSummary();
NestChanged?.Invoke(this, drawing);
}
private void UpdateNestSummary()
+75
View File
@@ -0,0 +1,75 @@
using System.Drawing;
using System.Windows.Forms;
namespace OpenNest.Controls
{
/// <summary>
/// A slim green confirmation strip, one line of text 30 pixels high (scaled with the form):
/// a pale tint with dark text and a border in the same hue, and a close button. A message
/// longer than the strip ends in an ellipsis. Hidden until <see cref="Show(string)"/>; it
/// stays until the next message replaces it or the operator closes it.
/// </summary>
public sealed class NoticeBanner : Panel
{
private static readonly Color Tint = Color.FromArgb(221, 240, 214);
private static readonly Color Ink = Color.FromArgb(28, 88, 28);
private readonly Label messageLabel;
private readonly Button closeButton;
private string message = "";
public NoticeBanner()
{
BackColor = Tint;
Height = 30;
Padding = new Padding(8, 1, 1, 1);
DoubleBuffered = true;
ResizeRedraw = true;
Visible = false;
messageLabel = new Label
{
AutoEllipsis = true,
AutoSize = false,
Dock = DockStyle.Fill,
ForeColor = Ink,
Name = "messageLabel",
TextAlign = ContentAlignment.MiddleLeft,
};
closeButton = new Button
{
Dock = DockStyle.Right,
FlatStyle = FlatStyle.Flat,
ForeColor = Ink,
AccessibleName = "Close",
Name = "closeButton",
TabStop = false,
Text = "✕",
Width = 26,
};
closeButton.FlatAppearance.BorderSize = 0;
closeButton.Click += (_, _) => Hide();
// Fill is laid out last, so the label takes what the button leaves.
Controls.Add(messageLabel);
Controls.Add(closeButton);
}
/// <summary>The message shown, or empty while hidden.</summary>
public string Message => Visible ? message : "";
/// <summary>Shows <paramref name="message"/>, replacing whatever was there.</summary>
public void Show(string message)
{
this.message = message ?? "";
messageLabel.Text = this.message;
Visible = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Ink, ButtonBorderStyle.Solid);
}
}
}
+7
View File
@@ -23,6 +23,13 @@ namespace OpenNest.Forms
/// <summary>True once the In This Nest tab changed a drawing's required quantity.</summary>
public bool QuantitiesChanged => view.QuantitiesChanged;
/// <summary>Raised with the drawing after each add or quantity edit, while the dialog is still open.</summary>
public event EventHandler<Drawing> NestChanged
{
add => view.NestChanged += value;
remove => view.NestChanged -= value;
}
/// <param name="nest">The nest to add drawings to.</param>
/// <param name="sets">Enables Used in Sets and Add to Set; null hides them.</param>
public DrawingLibraryForm(IDrawingLibraryStore library, string serverUrl, Nest nest, IDrawingSetStore sets = null)
+14
View File
@@ -1538,6 +1538,8 @@ namespace OpenNest.Forms
// The dialog adds drawings and sets quantities as the operator works in it.
using var change = BeginChange("Drawing Library");
var job = (Nest.Material, Nest.Thickness);
// Each add shows on the Drawings tab behind the dialog right away.
form.NestChanged += (_, drawing) => ShowLibraryChange(drawing);
form.ShowDialog(DialogOwner);
if (form.AddedDrawings.Count > 0 || form.QuantitiesChanged)
{
@@ -1555,6 +1557,18 @@ namespace OpenNest.Forms
change.Complete();
}
/// <summary>
/// Lists a drawing the open Drawing Library dialog just added or changed: brings the
/// Drawings tab forward and selects it there.
/// </summary>
internal void ShowLibraryChange(Drawing drawing)
{
UpdateDrawingList();
tabControl1.SelectedTab = tabPage2;
if (drawing != null && drawingListBox1.Items.Contains(drawing))
drawingListBox1.SelectedItem = drawing;
}
private void drawingListBox1_SaveToLibraryRequested(object sender, Drawing drawing)
{
SaveDrawingToLibrary(drawing);
+8 -2
View File
@@ -443,6 +443,12 @@ plans the insert (`OpenNest.Data.LibraryDrawingInsert`) before changing anything
revisions through Update Drawings.
- Another drawing with the same name: refused, naming both.
Each add shows a green banner above the list naming what was added (or how many
more of a drawing already in the nest, and the new required quantity); it stays
until the next add replaces it or it is closed. The dialog stays open for more
adds, and the main window lists the drawing on its Drawings tab behind the
dialog at once, brings that tab forward and selects it there.
The added drawing is an independent copy carrying a library link (server key,
id, revision and a snapshot of the library details) that is saved with the nest
(see [the nest file format](nest-file-format.md#drawing-library-links)). Editing it
@@ -454,8 +460,8 @@ library or not, cutoffs excluded: name, library revision ("Not from the library"
or "(other server)" when it is linked elsewhere), and the Required, Nested and
Remaining quantities, with totals below. It refreshes after every add. Required
is edited in place (0 to 99,999; one click starts editing, a bad number keeps
the editor open with an error) and applies to the nest at once, like an add;
the main window's drawing list refreshes when the dialog closes.
the editor open with an error) and applies to the nest at once, like an add,
and the main window's drawing list shows the change at once.
The browser's **Cut first** column shows the setting; **Size (in)** (longer x
shorter side) and **Holes** show the measured summary, blank until it is measured