mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-17 08:43:54 -05:00
fix(ui): preview the selected version in the database browser
Highlighting an earlier version on the Versions tab left the plates, drawings and plate preview on the current version, so there was no way to see what a historical version looked like without opening it. The embedded history now reports its selection and the browser downloads that version (debounced, cached per history view) and shows it, labelled as historical; selecting the current version returns to it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f1468e6f57
commit
379e3fd433
3 files changed
+185
-18
No files matched your search
@@ -59,6 +59,56 @@ public class SavedNestsFormTests
|
||||
"The saved-nest dialog test did not complete.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectingAHistoricalVersion_ShowsItsPlatesInTheDetailsAndPreview()
|
||||
{
|
||||
StaTestThread.Run(
|
||||
() =>
|
||||
{
|
||||
var job = Record("Versioned job");
|
||||
var current = NestFile((Quantity: 1, Parts: 1));
|
||||
var earlier = NestFile((Quantity: 1, Parts: 3), (Quantity: 1, Parts: 2));
|
||||
job.Revision = 3;
|
||||
job.FileSize = current.Length;
|
||||
var repository = new FakeRepository(job);
|
||||
repository.Files[job.Id] = current;
|
||||
repository.RevisionFiles[(job.Id, 2)] = earlier;
|
||||
repository.Revisions[job.Id] =
|
||||
[
|
||||
new NestRecord { Id = job.Id, Revision = 3, Name = job.Name, FileSize = current.Length },
|
||||
new NestRecord { Id = job.Id, Revision = 2, Name = job.Name, FileSize = earlier.Length },
|
||||
];
|
||||
|
||||
using var form = new SavedNestsForm(repository);
|
||||
form.Show();
|
||||
var plates = Field<DataGridView>(form, "platesGrid");
|
||||
var preview = Field<OpenNest.Controls.PlateView>(form, "platePreview");
|
||||
var status = Field<Label>(form, "detailsStatus");
|
||||
PumpUntil(() => plates.Rows.Count == 1, "the current version's plates");
|
||||
var currentPlate = preview.Plate;
|
||||
|
||||
Field<TabControl>(form, "detailsTabs").SelectedIndex = 2;
|
||||
NestHistoryForm? history = null;
|
||||
PumpUntil(
|
||||
() => (history = Field<NestHistoryForm>(form, "historyView")) != null
|
||||
&& Field<DataGridView>(history, "revisions").Rows.Count == 2,
|
||||
"the version history");
|
||||
var revisions = Field<DataGridView>(history!, "revisions");
|
||||
|
||||
revisions.CurrentCell = revisions.Rows[1].Cells[0];
|
||||
PumpUntil(() => plates.Rows.Count == 2 && status.Text.StartsWith("Version 2"), "version 2's plates");
|
||||
Assert.NotSame(currentPlate, preview.Plate);
|
||||
Assert.Equal(3, preview.Plate.Parts.Count);
|
||||
Assert.True(preview.Visible);
|
||||
|
||||
revisions.CurrentCell = revisions.Rows[0].Cells[0];
|
||||
PumpUntil(() => plates.Rows.Count == 1 && !status.Text.StartsWith("Version"), "the current version again");
|
||||
Assert.Single(preview.Plate.Parts);
|
||||
},
|
||||
TestTimeout,
|
||||
"The version preview test did not complete.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlatePreview_StepsThroughThePlates_AndFollowsThePlatesTable()
|
||||
{
|
||||
@@ -350,12 +400,18 @@ public class SavedNestsFormTests
|
||||
public int UpdateAttempts { get; private set; }
|
||||
public int ExpectedRevision { get; private set; }
|
||||
public Dictionary<Guid, byte[]> Files { get; } = new();
|
||||
public Dictionary<(Guid Id, int Revision), byte[]> RevisionFiles { get; } = new();
|
||||
public Dictionary<Guid, NestRecord[]> Revisions { get; } = new();
|
||||
|
||||
public Task<byte[]?> GetRevisionFileAsync(Guid id, int revisionNumber, CancellationToken cancellationToken = default) =>
|
||||
GetFileAsync(id, cancellationToken);
|
||||
RevisionFiles.TryGetValue((id, revisionNumber), out var file)
|
||||
? Task.FromResult<byte[]?>(file)
|
||||
: GetFileAsync(id, cancellationToken);
|
||||
|
||||
public Task<NestRevisionPage> GetRevisionsAsync(Guid id, int offset = 0, int limit = 100, CancellationToken cancellationToken = default) =>
|
||||
Task.FromResult(new NestRevisionPage());
|
||||
Task.FromResult(Revisions.TryGetValue(id, out var items)
|
||||
? new NestRevisionPage { Items = items, Total = items.Length, Offset = offset, Limit = limit }
|
||||
: new NestRevisionPage());
|
||||
|
||||
public async Task<NestRecord> UpdateFileAsync(Guid id, byte[] bytes, NestRecord metadata, int expectedRevision,
|
||||
CancellationToken cancellationToken = default)
|
||||
|
||||
@@ -39,6 +39,15 @@ public sealed class NestHistoryForm : Form
|
||||
public bool Changed { get; private set; }
|
||||
public event EventHandler HistoryChanged;
|
||||
|
||||
/// <summary>Raised when the highlighted version changes on a loaded history page.</summary>
|
||||
public event EventHandler RevisionSelected;
|
||||
|
||||
/// <summary>The highlighted version when it is not the current one; otherwise null.</summary>
|
||||
public NestRecord SelectedHistoricalRevision =>
|
||||
session.Current != null && SelectedRevision is NestRecord record && record.Revision != session.Current.Revision
|
||||
? record
|
||||
: null;
|
||||
|
||||
public NestHistoryForm(INestRepository repository, string serverUrl, Guid id = default, bool embedded = false)
|
||||
{
|
||||
this.repository = repository;
|
||||
@@ -74,7 +83,12 @@ public sealed class NestHistoryForm : Form
|
||||
await ReadAsync(0);
|
||||
}
|
||||
};
|
||||
revisions.SelectionChanged += (_, _) => UpdateButtons();
|
||||
revisions.SelectionChanged += (_, _) =>
|
||||
{
|
||||
UpdateButtons();
|
||||
if (!populating && session.Current != null)
|
||||
RevisionSelected?.Invoke(this, EventArgs.Empty);
|
||||
};
|
||||
var refresh = new Button { Text = "Refresh", AutoSize = true };
|
||||
refresh.Click += async (_, _) => { if (recovery) await ReadDeletedAsync(); else await ReadAsync(0); };
|
||||
previous.Click += async (_, _) => await ReadAsync(System.Math.Max(0, session.Page.Offset - NestHistorySession.PageSize));
|
||||
@@ -191,19 +205,28 @@ public sealed class NestHistoryForm : Form
|
||||
try
|
||||
{
|
||||
if (!await task || IsDisposed || run != readGeneration) return;
|
||||
foreach (var record in session.Page.Items)
|
||||
populating = true;
|
||||
try
|
||||
{
|
||||
var row = revisions.Rows.Add(record.Revision,
|
||||
(record.Revision == session.Current.Revision ? "Current" : "Historical") + (record.Deleted ? " (deleted)" : ""),
|
||||
record.SavedAt.ToString("g"), record.Name, record.Status, record.Comments, record.RestoredFromRevision, record.FileSize);
|
||||
revisions.Rows[row].Tag = record;
|
||||
foreach (var record in session.Page.Items)
|
||||
{
|
||||
var row = revisions.Rows.Add(record.Revision,
|
||||
(record.Revision == session.Current.Revision ? "Current" : "Historical") + (record.Deleted ? " (deleted)" : ""),
|
||||
record.SavedAt.ToString("g"), record.Name, record.Status, record.Comments, record.RestoredFromRevision, record.FileSize);
|
||||
revisions.Rows[row].Tag = record;
|
||||
}
|
||||
}
|
||||
finally { populating = false; }
|
||||
RevisionSelected?.Invoke(this, EventArgs.Empty);
|
||||
status.Text = $"{serverUrl} — Nest {selectedId} — Current version {session.Current.Revision}; {session.Page.Total} versions. Displaying {offset + 1}–{offset + session.Page.Items.Count}.";
|
||||
}
|
||||
catch (Exception ex) { if (!IsDisposed && run == readGeneration) status.Text = ex.Message; }
|
||||
finally { if (!IsDisposed) UpdateButtons(); }
|
||||
}
|
||||
|
||||
/// <summary>Downloads one version on the displayed page; null when the page changed meanwhile.</summary>
|
||||
public Task<byte[]> DownloadRevisionAsync(int revision) => session.DownloadAsync(revision, serverUrl, lifetime.Token);
|
||||
|
||||
private async Task InspectAsync()
|
||||
{
|
||||
if (SelectedRevision is not NestRecord record || busy) return;
|
||||
|
||||
@@ -49,6 +49,11 @@ public sealed class SavedNestsForm : Form
|
||||
private readonly TabPage versionsTab;
|
||||
private NestHistoryForm historyView;
|
||||
private Guid? historyId;
|
||||
// Historical version shown in the details and preview instead of the current one.
|
||||
private int? previewRevision;
|
||||
private int revisionGeneration;
|
||||
private readonly Dictionary<int, NestDetails> revisionDetails = new();
|
||||
private readonly Timer revisionTimer;
|
||||
private readonly SplitContainer detailsSplit;
|
||||
private readonly PlateView platePreview;
|
||||
private readonly Button previousPlateButton;
|
||||
@@ -281,6 +286,12 @@ public sealed class SavedNestsForm : Form
|
||||
if (SelectedNestId() is Guid id)
|
||||
await ShowDetailsAsync(id);
|
||||
};
|
||||
revisionTimer = new Timer { Interval = 250 };
|
||||
revisionTimer.Tick += async (_, _) =>
|
||||
{
|
||||
revisionTimer.Stop();
|
||||
await ShowSelectedRevisionAsync();
|
||||
};
|
||||
|
||||
var refreshButton = new Button { Text = "Refresh", AutoSize = true };
|
||||
refreshButton.Click += async (_, _) => await RunAsync(session.RefreshAsync);
|
||||
@@ -526,12 +537,16 @@ public sealed class SavedNestsForm : Form
|
||||
detailsTimer.Stop();
|
||||
var id = SelectedNestId();
|
||||
if (historyId != id)
|
||||
{
|
||||
historyView?.Dispose();
|
||||
historyView = null;
|
||||
historyId = null;
|
||||
}
|
||||
DropHistoryView();
|
||||
UpdateHistoryView();
|
||||
if (historyView?.SelectedHistoricalRevision != null)
|
||||
{
|
||||
// The Versions tab has an earlier version highlighted: show that one, not the current.
|
||||
revisionTimer.Stop();
|
||||
revisionTimer.Start();
|
||||
return;
|
||||
}
|
||||
|
||||
if (id == null)
|
||||
{
|
||||
details.Clear();
|
||||
@@ -565,7 +580,7 @@ public sealed class SavedNestsForm : Form
|
||||
{
|
||||
try
|
||||
{
|
||||
if (await details.LoadAsync(id) && !IsDisposed)
|
||||
if (await details.LoadAsync(id) && !IsDisposed && previewRevision == null)
|
||||
{
|
||||
PopulateDetails(details.Details);
|
||||
PrefetchNearby(id);
|
||||
@@ -713,9 +728,14 @@ public sealed class SavedNestsForm : Form
|
||||
{
|
||||
if (detailsTabs.SelectedTab != versionsTab || SelectedNestId() is not Guid id || historyId == id)
|
||||
return;
|
||||
historyView?.Dispose();
|
||||
DropHistoryView();
|
||||
historyView = new NestHistoryForm(repository, serverUrl, id, embedded: true);
|
||||
historyId = id;
|
||||
historyView.RevisionSelected += (_, _) =>
|
||||
{
|
||||
revisionTimer.Stop();
|
||||
revisionTimer.Start();
|
||||
};
|
||||
historyView.HistoryChanged += (_, _) =>
|
||||
{
|
||||
// Let the history operation finish before refreshing the browse selection.
|
||||
@@ -732,6 +752,75 @@ public sealed class SavedNestsForm : Form
|
||||
historyView.Show();
|
||||
}
|
||||
|
||||
/// <summary>Disposes the embedded history and returns the details to the current version.</summary>
|
||||
private void DropHistoryView()
|
||||
{
|
||||
historyView?.Dispose();
|
||||
historyView = null;
|
||||
historyId = null;
|
||||
revisionTimer.Stop();
|
||||
revisionGeneration++;
|
||||
previewRevision = null;
|
||||
revisionDetails.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the version highlighted in the Versions tab in the plates, drawings and preview.
|
||||
/// The current version comes from <see cref="NestDetailsSession"/>; earlier versions are
|
||||
/// downloaded once per embedded history view and kept, since saved versions never change.
|
||||
/// </summary>
|
||||
private async Task ShowSelectedRevisionAsync()
|
||||
{
|
||||
var view = historyView;
|
||||
var record = view?.SelectedHistoricalRevision;
|
||||
var run = ++revisionGeneration;
|
||||
if (record == null)
|
||||
{
|
||||
if (previewRevision != null)
|
||||
{
|
||||
previewRevision = null;
|
||||
ScheduleDetails();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
previewRevision = record.Revision;
|
||||
if (revisionDetails.TryGetValue(record.Revision, out var cached))
|
||||
{
|
||||
PopulateRevision(record, cached);
|
||||
return;
|
||||
}
|
||||
|
||||
ShowDetailsMessage($"Loading version {record.Revision}...");
|
||||
try
|
||||
{
|
||||
var bytes = await view.DownloadRevisionAsync(record.Revision);
|
||||
if (bytes == null || IsDisposed || run != revisionGeneration)
|
||||
return;
|
||||
var nestDetails = await Task.Run(() =>
|
||||
{
|
||||
using var stream = new MemoryStream(bytes);
|
||||
return NestDetails.FromNest(new NestReader(stream) { BestFitStore = BestFitCacheStore.Shared }.Read());
|
||||
});
|
||||
if (IsDisposed || run != revisionGeneration)
|
||||
return;
|
||||
revisionDetails[record.Revision] = nestDetails;
|
||||
PopulateRevision(record, nestDetails);
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!IsDisposed && run == revisionGeneration)
|
||||
ShowDetailsMessage($"Could not load version {record.Revision}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void PopulateRevision(NestRecord record, NestDetails nestDetails)
|
||||
{
|
||||
PopulateDetails(nestDetails);
|
||||
detailsStatus.Text = $"Version {record.Revision} (historical) — {detailsStatus.Text}";
|
||||
}
|
||||
|
||||
private async Task ShowDeletedAsync()
|
||||
{
|
||||
using var dialog = new NestHistoryForm(repository, serverUrl);
|
||||
@@ -804,9 +893,7 @@ public sealed class SavedNestsForm : Form
|
||||
// Drop archive previews and embedded history, including prefetched snapshots.
|
||||
details.Dispose();
|
||||
details = new NestDetailsSession(LoadDetailsAsync);
|
||||
historyView?.Dispose();
|
||||
historyView = null;
|
||||
historyId = null;
|
||||
DropHistoryView();
|
||||
ShowDetailsMessage("");
|
||||
await RunAsync(session.RefreshAsync);
|
||||
foreach (DataGridViewRow row in nestGrid.Rows)
|
||||
@@ -887,6 +974,7 @@ public sealed class SavedNestsForm : Form
|
||||
{
|
||||
searchTimer.Dispose();
|
||||
detailsTimer.Dispose();
|
||||
revisionTimer.Dispose();
|
||||
session.Dispose();
|
||||
details.Dispose();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user