mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-17 01:15:34 -05:00
feat(desktop): share the start-up template migration and new-nest choice
DesktopStartup holds the WinForms main window's start-up steps in its order for the Avalonia app: first the one-time conversion of the legacy nest template into the nest defaults file, then whether Tools > Options > Create a new nest on startup asks for a new nest. Run reads the shared user settings but never writes them, so it can run off the UI thread; it reports whether to clear NestTemplatePath, and ClearTemplateSetting empties that setting alone on the caller's (UI) thread, as the window's other settings writers run there.
This commit is contained in:
1 parent
c55187909d
commit
7bd44ec4c4
2 files changed
+192
No files matched your search
@@ -0,0 +1,144 @@
|
||||
using OpenNest.Data;
|
||||
using OpenNest.Engine.BestFit;
|
||||
using OpenNest.IO;
|
||||
|
||||
namespace OpenNest.Desktop.Tests;
|
||||
|
||||
public sealed class DesktopStartupTests : IDisposable
|
||||
{
|
||||
private readonly string directory =
|
||||
Directory.CreateTempSubdirectory("opennest-desktop-startup-").FullName;
|
||||
|
||||
public void Dispose() => Directory.Delete(directory, recursive: true);
|
||||
|
||||
private string SettingsPath => Path.Combine(directory, "user-settings.json");
|
||||
|
||||
private string DefaultsPath => Path.Combine(directory, "defaults.json");
|
||||
|
||||
// BestFitCacheStore has no public constructor: Shared wraps the process-wide cache.
|
||||
private DesktopStartupResult Run() =>
|
||||
DesktopStartup.Run(SettingsPath, DefaultsPath, BestFitCacheStore.Shared);
|
||||
|
||||
private void WriteSettings(Dictionary<string, string> values) =>
|
||||
Assert.True(UserSettingsFile.Write(SettingsPath, values));
|
||||
|
||||
private string WriteTemplate()
|
||||
{
|
||||
var path = Path.Combine(directory, "template.nstdot");
|
||||
var nest = TestNests.OneSquare();
|
||||
nest.Units = Units.Millimeters;
|
||||
Assert.True(new NestWriter(nest).Write(path));
|
||||
return path;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoSettings_OpensANewNest_AsTheOptionDefaultsToOn()
|
||||
{
|
||||
var result = Run();
|
||||
|
||||
Assert.True(result.CreateNewNest);
|
||||
Assert.False(result.ClearTemplateSetting);
|
||||
Assert.Null(result.TemplateWarning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoSettings_LeavesTheSettingsFileUnwritten()
|
||||
{
|
||||
Run();
|
||||
|
||||
Assert.False(File.Exists(SettingsPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Run_NeverWritesTheSettingsFile_EvenWhenATemplateConverts()
|
||||
{
|
||||
WriteSettings(new()
|
||||
{
|
||||
[NestTemplateMigration.SettingName] = WriteTemplate(),
|
||||
["SomethingElse"] = "kept",
|
||||
});
|
||||
var before = File.ReadAllBytes(SettingsPath);
|
||||
|
||||
Run();
|
||||
|
||||
Assert.Equal(before, File.ReadAllBytes(SettingsPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OptionOff_OpensNoNewNest()
|
||||
{
|
||||
WriteSettings(new() { [DesktopOptions.CreateNewNestOnOpenName] = "False" });
|
||||
|
||||
Assert.False(Run().CreateNewNest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OptionOn_OpensANewNest()
|
||||
{
|
||||
WriteSettings(new() { [DesktopOptions.CreateNewNestOnOpenName] = "True" });
|
||||
|
||||
Assert.True(Run().CreateNewNest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ATemplate_IsConverted_AndItsSettingIsToBeCleared()
|
||||
{
|
||||
WriteSettings(new() { [NestTemplateMigration.SettingName] = WriteTemplate() });
|
||||
|
||||
var result = Run();
|
||||
|
||||
Assert.True(result.ClearTemplateSetting);
|
||||
Assert.Null(result.TemplateWarning);
|
||||
Assert.Equal(Units.Millimeters, NestDefaults.Load(DefaultsPath).Units);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExistingDefaults_AreKept_AndTheTemplateSettingIsToBeCleared()
|
||||
{
|
||||
File.WriteAllText(DefaultsPath, "{ \"kept\": true }");
|
||||
WriteSettings(new() { [NestTemplateMigration.SettingName] = WriteTemplate() });
|
||||
|
||||
var result = Run();
|
||||
|
||||
Assert.True(result.ClearTemplateSetting);
|
||||
Assert.Equal("{ \"kept\": true }", File.ReadAllText(DefaultsPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnAlreadyClearedSetting_IsNotClearedAgain()
|
||||
{
|
||||
WriteSettings(new() { [NestTemplateMigration.SettingName] = "" });
|
||||
|
||||
Assert.False(Run().ClearTemplateSetting);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnUnreadableTemplate_Warns_KeepsTheSetting_AndStillOpensANewNest()
|
||||
{
|
||||
var path = Path.Combine(directory, "broken.nstdot");
|
||||
File.WriteAllText(path, "not a nest");
|
||||
WriteSettings(new() { [NestTemplateMigration.SettingName] = path });
|
||||
|
||||
var result = Run();
|
||||
|
||||
Assert.Contains(path, result.TemplateWarning);
|
||||
Assert.False(result.ClearTemplateSetting);
|
||||
Assert.True(result.CreateNewNest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClearTemplateSetting_EmptiesOnlyThatSetting()
|
||||
{
|
||||
WriteSettings(new()
|
||||
{
|
||||
[NestTemplateMigration.SettingName] = "old.nstdot",
|
||||
["SomethingElse"] = "kept",
|
||||
});
|
||||
|
||||
Assert.True(DesktopStartup.ClearTemplateSetting(SettingsPath));
|
||||
|
||||
var saved = UserSettingsFile.Read(SettingsPath);
|
||||
Assert.Equal("", saved[NestTemplateMigration.SettingName]);
|
||||
Assert.Equal("kept", saved["SomethingElse"]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using OpenNest.BestFit;
|
||||
using OpenNest.Data;
|
||||
|
||||
namespace OpenNest.Desktop;
|
||||
|
||||
/// <summary>What the desktop app's start-up steps found.</summary>
|
||||
/// <param name="TemplateWarning">Why the legacy nest template could not be converted, to show
|
||||
/// the user; null when there was nothing to report.</param>
|
||||
/// <param name="ClearTemplateSetting">The template was converted or is gone: clear its setting
|
||||
/// (<see cref="DesktopStartup.ClearTemplateSetting"/>) so it is never consulted again.</param>
|
||||
/// <param name="CreateNewNest">Open a new nest, as Tools > Options > Create a new nest on
|
||||
/// startup asks; the window adds none beside nests already open.</param>
|
||||
public sealed record DesktopStartupResult(
|
||||
string TemplateWarning, bool ClearTemplateSetting, bool CreateNewNest);
|
||||
|
||||
/// <summary>
|
||||
/// The desktop app's start-up steps, in the WinForms main window's order: first the one-time
|
||||
/// conversion of the legacy nest template into the nest defaults file (a new nest reads that
|
||||
/// file), then whether to open a new nest.
|
||||
/// </summary>
|
||||
public static class DesktopStartup
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts the legacy nest template and decides on the new nest. Reads the user settings
|
||||
/// but never writes them, so it can run off the UI thread while the window's own settings
|
||||
/// writers run on it; the caller clears the setting on the UI thread when asked to.
|
||||
/// </summary>
|
||||
/// <param name="settingsPath">The shared user settings file.</param>
|
||||
/// <param name="defaultsPath">The nest defaults file the template converts into.</param>
|
||||
/// <param name="bestFitStore">Best-fit cache the template's drawings read from.</param>
|
||||
public static DesktopStartupResult Run(
|
||||
string settingsPath, string defaultsPath, IBestFitStore bestFitStore)
|
||||
{
|
||||
var settings = UserSettingsFile.Read(settingsPath);
|
||||
settings.TryGetValue(NestTemplateMigration.SettingName, out var templatePath);
|
||||
var migration = NestTemplateMigration.Run(templatePath, defaultsPath, bestFitStore);
|
||||
return new DesktopStartupResult(migration.Warning, migration.ClearSetting,
|
||||
DesktopOptions.Read(settings).CreateNewNestOnOpen);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the template setting to the empty text WinForms stores, keeping every other
|
||||
/// setting. Returns false when the file could not be written; the next start then finds
|
||||
/// the defaults file already there and clears it again.
|
||||
/// </summary>
|
||||
public static bool ClearTemplateSetting(string settingsPath) =>
|
||||
UserSettingsFile.Update(settingsPath, s => s[NestTemplateMigration.SettingName] = "");
|
||||
}
|
||||
Reference in new issue
Block a user