From 8e87c0881a9c7e820122ba4550d9e954d4a3f416 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 11 Nov 2026 17:22:48 -0500 Subject: [PATCH] feat(desktop): save a nest tab to a file or export a copy NestTabs.SaveAs writes the tab's nest, makes the file its file and name, and clears its unsaved mark; a write that fails throws and changes nothing. ExportCopy writes the nest without touching its file, name or unsaved mark. Both refuse, before writing anything, a file another open tab shows (so one file never has two tabs) and a tab that has been closed; Export also refuses the tab's own file, which only Save may write. Refusals are a SaveTargetRefusedException whose message says what to do. SavePath is the file Save writes without asking: only one saved to this session, as in the WinForms app, so a nest that was only opened asks for a file on its first Save. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Desktop.Tests/NestTabsSaveTests.cs | 195 ++++++++++++++++++++ OpenNest.Desktop/Document.cs | 11 ++ OpenNest.Desktop/NestTabs.cs | 68 ++++++- 3 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 OpenNest.Desktop.Tests/NestTabsSaveTests.cs diff --git a/OpenNest.Desktop.Tests/NestTabsSaveTests.cs b/OpenNest.Desktop.Tests/NestTabsSaveTests.cs new file mode 100644 index 00000000..ae315cf5 --- /dev/null +++ b/OpenNest.Desktop.Tests/NestTabsSaveTests.cs @@ -0,0 +1,195 @@ +using OpenNest.IO; + +namespace OpenNest.Desktop.Tests; + +public sealed class NestTabsSaveTests : IDisposable +{ + private readonly string directory = + Directory.CreateTempSubdirectory("opennest-desktop-tabs-save-").FullName; + + public void Dispose() => Directory.Delete(directory, recursive: true); + + private string InDir(string name) => Path.Combine(directory, name); + + // Opens a real nest file in a new tab, as File > Open does. + private NestTab OpenSaved(NestTabs tabs, string name) + { + var path = InDir(name); + var nest = TestNests.OneSquare(); + nest.Name = Path.GetFileNameWithoutExtension(name); + Assert.True(new NestWriter(nest).Write(path)); + return tabs.Open(path, file => new Document { Nest = new NestReader(file).Read() }); + } + + [Fact] + public void SavePath_IsNullForANestOnlyOpened_SoTheFirstSaveAsks() + { + var tabs = new NestTabs(); + + var tab = OpenSaved(tabs, "opened.nest"); + + Assert.Null(tab.SavePath); + } + + [Fact] + public void SaveAs_WritesTheFileAndMakesItTheTabsFileNameAndSavePath() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "opened.nest"); + tab.MarkModified(); + var changed = 0; + tabs.Changed += (_, _) => changed++; + var target = InDir("Renamed Job.nest"); + + tabs.SaveAs(tab, target); + + Assert.Equal(target, tab.Path); + Assert.Equal(target, tab.SavePath); + Assert.Equal("Renamed Job", tab.Name); + Assert.False(tab.Modified); + Assert.Equal(1, changed); + var read = new NestReader(target).Read(); + Assert.Equal("Renamed Job", read.Name); + Assert.Single(read.Plates[0].Parts); + } + + [Fact] + public void SaveAs_ThenOpeningTheSavedFile_SwitchesToTheTabInsteadOfOpeningItTwice() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "opened.nest"); + OpenSaved(tabs, "other.nest"); + var target = InDir("saved.nest"); + tabs.SaveAs(tab, target); + + var again = tabs.Open(target, _ => throw new InvalidOperationException("loaded again")); + + Assert.Same(tab, again); + Assert.Equal(2, tabs.Tabs.Count); + } + + [Fact] + public void SaveAs_AFileAnotherTabShows_IsRefusedAndWritesNothing() + { + var tabs = new NestTabs(); + var a = OpenSaved(tabs, "a.nest"); + var b = OpenSaved(tabs, "b.nest"); + a.MarkModified(); + var before = File.ReadAllBytes(b.Path); + + var ex = Assert.Throws(() => tabs.SaveAs(a, b.Path)); + + Assert.Contains("b.nest is open in the b tab", ex.Message); + Assert.Equal(before, File.ReadAllBytes(b.Path)); + Assert.Equal(InDir("a.nest"), a.Path); + Assert.Equal("a", a.Name); + Assert.True(a.Modified); + } + + [Fact] + public void SaveAs_OntoItsOwnFile_IsAllowed() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "own.nest"); + tab.MarkModified(); + + tabs.SaveAs(tab, tab.Path); + + Assert.False(tab.Modified); + Assert.Equal(InDir("own.nest"), tab.SavePath); + } + + [Fact] + public void SaveAs_AWriteThatFails_KeepsTheFileNameAndUnsavedMark() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "kept.nest"); + tab.MarkModified(); + var changed = 0; + tabs.Changed += (_, _) => changed++; + + Assert.ThrowsAny( + () => tabs.SaveAs(tab, InDir(Path.Combine("missing folder", "x.nest")))); + + Assert.Equal(InDir("kept.nest"), tab.Path); + Assert.Equal("kept", tab.Name); + Assert.Null(tab.Document.LastSavePath); + Assert.True(tab.Modified); + Assert.Equal(0, changed); + } + + [Fact] + public void ExportCopy_WritesTheNestButKeepsTheTabsFileNameAndUnsavedMark() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "job.nest"); + tab.MarkModified(); + var target = InDir("copy.nest"); + + tabs.ExportCopy(tab, target); + + Assert.Equal(InDir("job.nest"), tab.Path); + Assert.Equal("job", tab.Name); + Assert.Null(tab.SavePath); + Assert.True(tab.Modified); + var read = new NestReader(target).Read(); + Assert.Equal("job", read.Name); + Assert.Single(read.Plates[0].Parts); + } + + [Fact] + public void SaveAs_AFailedWriteAfterAnEarlierSave_KeepsTheEarlierSavePath() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "first.nest"); + var earlier = InDir("earlier.nest"); + tabs.SaveAs(tab, earlier); + + Assert.ThrowsAny( + () => tabs.SaveAs(tab, InDir(Path.Combine("missing folder", "x.nest")))); + + Assert.Equal(earlier, tab.SavePath); + Assert.Equal(earlier, tab.Path); + } + + [Fact] + public void SaveAs_ATabThatWasClosed_IsRefusedAndWritesNothing() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "closed.nest"); + tabs.Close(tab); + var target = InDir("after-close.nest"); + + Assert.Throws(() => tabs.SaveAs(tab, target)); + Assert.Throws(() => tabs.ExportCopy(tab, target)); + + Assert.False(File.Exists(target)); + } + + [Fact] + public void ExportCopy_OntoTheTabsOwnFile_IsRefusedAndWritesNothing() + { + var tabs = new NestTabs(); + var tab = OpenSaved(tabs, "own.nest"); + tab.Document.Nest.Notes = "not saved"; + var before = File.ReadAllBytes(tab.Path); + + var ex = Assert.Throws(() => tabs.ExportCopy(tab, tab.Path)); + + Assert.Contains("File > Save", ex.Message); + Assert.Equal(before, File.ReadAllBytes(tab.Path)); + } + + [Fact] + public void ExportCopy_ToAFileAnotherTabShows_IsRefusedAndWritesNothing() + { + var tabs = new NestTabs(); + var a = OpenSaved(tabs, "a.nest"); + var b = OpenSaved(tabs, "b.nest"); + var before = File.ReadAllBytes(b.Path); + + Assert.Throws(() => tabs.ExportCopy(a, b.Path)); + + Assert.Equal(before, File.ReadAllBytes(b.Path)); + } +} diff --git a/OpenNest.Desktop/Document.cs b/OpenNest.Desktop/Document.cs index 29a6a1d1..47b49709 100644 --- a/OpenNest.Desktop/Document.cs +++ b/OpenNest.Desktop/Document.cs @@ -77,6 +77,17 @@ namespace OpenNest History.MarkSaved(); } + /// + /// Writes a copy of the nest to . The nest's name, save path, + /// server identity and undo save point stay as they were (the writer still stamps its + /// last-modified date, as every save does). + /// + public void ExportCopy(string path) + { + // A write that fails throws and leaves any file already at the path as it was. + new NestWriter(Nest) { BestFitStore = BestFitCacheStore.Shared }.Write(path); + } + /// Serializes the current nest to a .nest archive in memory. public byte[] Serialize() { diff --git a/OpenNest.Desktop/NestTabs.cs b/OpenNest.Desktop/NestTabs.cs index a4395f00..98e30330 100644 --- a/OpenNest.Desktop/NestTabs.cs +++ b/OpenNest.Desktop/NestTabs.cs @@ -16,8 +16,14 @@ namespace OpenNest.Desktop public Document Document { get; } - /// The full path of the file it was opened from, else null. - public string Path { get; } + /// The full path of the file it was opened from or saved to, else null. + public string Path { get; internal set; } + + /// + /// The file Save writes without asking: the one saved to this session, if it still + /// exists. Null for a nest only opened, as in the WinForms app, so the first Save asks. + /// + public string SavePath => Document.HasSavePath ? Document.LastSavePath : null; /// True after a change this session that is not saved. public bool Modified { get; private set; } @@ -32,6 +38,18 @@ namespace OpenNest.Desktop public void MarkSaved() => Modified = false; } + /// + /// A save or export refused before anything was written; the message says why and what to + /// do instead. + /// + public sealed class SaveTargetRefusedException : InvalidOperationException + { + public SaveTargetRefusedException(string message) + : base(message) + { + } + } + /// /// The nests open in the desktop app, one tab each, and which one is active. Opening a file /// that is already open activates its tab; closing the active tab activates the one to its @@ -104,6 +122,52 @@ namespace OpenNest.Desktop Changed?.Invoke(this, EventArgs.Empty); } + /// + /// Saves the tab's nest to , which becomes its file and name, and + /// clears its unsaved mark. Refuses a file another open tab shows, so one file never has + /// two tabs. A failed write throws and changes nothing. + /// + public void SaveAs(NestTab tab, string path) + { + var fullPath = CheckTarget(tab, path, export: false); + tab.Document.SaveAs(fullPath); + tab.Path = fullPath; + tab.MarkSaved(); + Changed?.Invoke(this, EventArgs.Empty); + } + + /// + /// Writes a copy of the tab's nest to without changing its file, + /// name or unsaved mark. Refuses the tab's own file, which only Save may write, and a + /// file another open tab shows. + /// + public void ExportCopy(NestTab tab, string path) + { + var fullPath = CheckTarget(tab, path, export: true); + tab.Document.ExportCopy(fullPath); + } + + private string CheckTarget(NestTab tab, string path, bool export) + { + ArgumentNullException.ThrowIfNull(tab); + ArgumentNullException.ThrowIfNull(path); + if (!tabs.Contains(tab)) + throw new SaveTargetRefusedException( + $"{tab.Name} was closed before it could be written. Open it again to save it."); + var fullPath = System.IO.Path.GetFullPath(path); + if (export && tab.Path != null && SamePath(tab.Path, fullPath)) + throw new SaveTargetRefusedException( + $"{System.IO.Path.GetFileName(fullPath)} is this nest's own file. Use " + + "File > Save to save it there, or export the copy under another name."); + var other = tabs.FirstOrDefault( + t => t != tab && t.Path != null && SamePath(t.Path, fullPath)); + if (other != null) + throw new SaveTargetRefusedException( + $"{System.IO.Path.GetFileName(fullPath)} is open in the {other.Name} tab. " + + "Close that tab first, or choose another file name."); + return fullPath; + } + // Windows paths ignore case; Linux and macOS paths do not. private static bool SamePath(string a, string b) => string.Equals(a, b, OperatingSystem.IsWindows()