mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
The toolbar's Engine list shows each engine's description beside its name and its tooltip, and saves the choice through EngineSelection. The status bar follows the WinForms strip: the tool's name and hint with the last notice, the location (a click switches between following the mouse and showing left clicks, saved for both apps), the selection, the nest's machine (either button lists the machines to assign), material, plate utilization and the version. Nest > Machine assigns a machine as one undoable step, refused while Auto Nest works on the nest or the plate's tool is busy; Auto Nest is enabled only with a plate to nest on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.4 KiB
C#
121 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using OpenNest.Data;
|
|
using OpenNest.Desktop;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>
|
|
/// The machines, nest defaults and post processors this app works with, from the shared
|
|
/// desktop library: in Database mode (storage.json, shared with the WinForms app) the nest
|
|
/// server's copy once it holds one, otherwise this PC's files, with the user settings both
|
|
/// apps keep in user-settings.json.
|
|
/// </summary>
|
|
internal static class DesktopSettings
|
|
{
|
|
/// <summary>The storage mode and nest server from storage.json; Tools > Options can change the server.</summary>
|
|
private static NestStorageSettings storage = NestStorageSettings.Load(NestStorageSettings.DefaultPath);
|
|
|
|
public static SharedSettingsSource Shared { get; } = new(() => storage);
|
|
|
|
public static MachineSettingsSource Machines { get; } =
|
|
new(Shared, new MachineUserSettingsFile(UserSettingsFile.DefaultPath));
|
|
|
|
private static RemoteNestRepository repository;
|
|
private static string repositoryUrl;
|
|
|
|
/// <summary>
|
|
/// File > New nests, numbered by the nest server in Database mode and otherwise by the
|
|
/// counter both apps keep in user-settings.json.
|
|
/// </summary>
|
|
public static NewNestSource NewNests { get; } = new(
|
|
Machines,
|
|
new NewNestUserSettingsFile(UserSettingsFile.DefaultPath),
|
|
() => storage.IsDatabaseMode ? Repository(storage.ServerUrl) : null);
|
|
|
|
/// <summary>The nest server URL saved in storage.json (blank when none).</summary>
|
|
public static string ServerUrl => storage.ServerUrl;
|
|
|
|
/// <summary>
|
|
/// Saves <paramref name="serverUrl"/> as the nest server in storage.json, keeping the
|
|
/// storage mode, as the WinForms Options form does; the next request uses it.
|
|
/// </summary>
|
|
public static void SaveServerUrl(string serverUrl) =>
|
|
storage = NestServerUrlOption.SaveToFile(NestStorageSettings.DefaultPath, serverUrl);
|
|
|
|
// One client per server, recreated when the URL changes.
|
|
private static RemoteNestRepository Repository(string url)
|
|
{
|
|
if (repository == null || repositoryUrl != url)
|
|
{
|
|
repository?.Dispose();
|
|
repository = new RemoteNestRepository(url);
|
|
repositoryUrl = url;
|
|
}
|
|
return repository;
|
|
}
|
|
|
|
/// <summary>The post processors loaded from Posts/ beside the app, by load order.</summary>
|
|
public static IReadOnlyList<IPostProcessor> PostProcessors { get; private set; } = [];
|
|
|
|
/// <summary>The readable machine configurations, once <see cref="LoadMachinesAsync"/> has run.</summary>
|
|
public static IReadOnlyList<MachineConfig> MachineConfigs { get; private set; } = [];
|
|
|
|
/// <summary>
|
|
/// Loads the post processors in <paramref name="directory"/> (each DLL on its own, so one
|
|
/// that fails does not stop the others) and registers their materials. Returns the
|
|
/// failures to show, or null when every post loaded.
|
|
/// </summary>
|
|
public static string LoadPosts(string directory)
|
|
{
|
|
var result = PostProcessorLoader.Load(directory);
|
|
PostProcessors = result.PostProcessors;
|
|
return result.Failures.Count == 0
|
|
? null
|
|
: "These post processors could not be loaded: " + string.Join("; ", result.Failures);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the machines off the UI thread (the nest server can take seconds to answer), then
|
|
/// copies the legacy clamps onto the default or only machine, as the WinForms app does at
|
|
/// startup (with several machines and no default it waits for the WinForms app to ask).
|
|
/// Returns what to tell the user: the machines' source when the server could not be
|
|
/// reached, or why the clamps were not copied; null when there is nothing to say.
|
|
/// </summary>
|
|
public static Task<string> LoadMachinesAsync() => Task.Run(() =>
|
|
{
|
|
MachineConfigs = Machines.LoadMachines();
|
|
var clamps = Machines.MigrateLegacyClamps(askMachine: null);
|
|
var offline = Shared.TakeOfflineWarning();
|
|
var notices = new[] { offline, clamps }
|
|
.Where(text => !string.IsNullOrEmpty(text))
|
|
.Select(OneLine)
|
|
.ToList();
|
|
return notices.Count == 0 ? null : string.Join(" ", notices);
|
|
});
|
|
|
|
/// <summary>
|
|
/// Reads the machines again off the UI thread, as the WinForms machine lists read them
|
|
/// each time they open; a failed read keeps the last list. Returns the list.
|
|
/// </summary>
|
|
public static Task<IReadOnlyList<MachineConfig>> RefreshMachinesAsync() => Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
MachineConfigs = Machines.LoadMachines();
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
|
{
|
|
// The list read at startup (or last time) stays.
|
|
}
|
|
return MachineConfigs;
|
|
});
|
|
|
|
private static string OneLine(string text) =>
|
|
string.Join(" ", text.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
|
|
}
|
|
}
|