using System; using System.Collections.Generic; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Layout; using OpenNest.Data; using OpenNest.Desktop; namespace OpenNest.Avalonia { /// /// The cut-time values Calculate Cut Time asks for, as the WinForms Cut Parameters form: /// outside feed, rapid, pierce time, hole feed, the machine whose acceleration times the /// moves (or full speed) and whether etch-only contours are skipped. It opens with the values /// last used here, in the WinForms app or in either simulator, and saves them however it /// closes, as WinForms does. /// internal sealed class CutTimeDialog : Window { private readonly NumberField feed; private readonly NumberField rapid; private readonly NumberField pierce; private readonly NumberField holeFeed; private readonly ComboBox machineBox; private readonly CheckBox skipEtch; private readonly Units units; private readonly bool saveSettings; private CutTimeDialog(Units units, IReadOnlyList machines, bool saveSettings) { this.units = units; this.saveSettings = saveSettings; Title = "Cut Parameters"; SizeToContent = SizeToContent.WidthAndHeight; MinWidth = 380; CanResize = false; ShowInTaskbar = false; WindowStartupLocation = WindowStartupLocation.CenterOwner; var saved = CutTimeSettingsFile.Read(); var choices = CutTimeMachineChoice.For(machines); var loaded = saved.ToCutParameters(units, SavedCutTimeSettings.ChooseMachine(machines, saved.Machine)); var rate = UnitsHelper.GetShortTimeUnitPair(units); feed = new NumberField("Feed", loaded.Feedrate, CutTimeEntry.MinimumRate, CutTimeEntry.MaximumRate, 0, rate); rapid = new NumberField("Rapid", loaded.RapidTravelRate, CutTimeEntry.MinimumRate, CutTimeEntry.MaximumRate, 0, rate); pierce = new NumberField("Pierce", loaded.PierceTime.TotalSeconds, 0, CutTimeEntry.DialogMaximumPierce, 1, "seconds"); holeFeed = new NumberField("HoleFeed", loaded.HoleFeedrate ?? loaded.Feedrate, CutTimeEntry.MinimumRate, CutTimeEntry.MaximumRate, 0, rate); machineBox = new ComboBox { Name = "Machine", ItemsSource = choices, SelectedIndex = CutTimeMachineChoice.SavedIndex(choices, saved.Machine), HorizontalAlignment = HorizontalAlignment.Stretch, }; skipEtch = new CheckBox { Name = "SkipEtch", Content = "Skip etch-only contours", IsChecked = loaded.SkipScribe }; var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("Auto,*"), RowDefinitions = new RowDefinitions("Auto,Auto,Auto,Auto,Auto,Auto"), ColumnSpacing = 10, RowSpacing = 8, }; AddRow(grid, 0, "Outside feed :", feed.View); AddRow(grid, 1, "Rapid Feedrate :", rapid.View); AddRow(grid, 2, "Pierce Time :", pierce.View); AddRow(grid, 3, "Hole feed :", holeFeed.View); AddRow(grid, 4, "Acceleration :", machineBox); Grid.SetRow(skipEtch, 5); Grid.SetColumn(skipEtch, 1); grid.Children.Add(skipEtch); var accept = new Button { Name = "Accept", Content = "Accept", IsDefault = true, MinWidth = 80 }; accept.Click += (_, _) => Close(GetCutParameters()); var cancel = new Button { Name = "Cancel", Content = "Cancel", IsCancel = true, MinWidth = 80 }; cancel.Click += (_, _) => Close(null); var buttons = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8, HorizontalAlignment = HorizontalAlignment.Right, }; buttons.Children.Add(accept); buttons.Children.Add(cancel); var panel = new StackPanel { Margin = new Thickness(12), Spacing = 14 }; panel.Children.Add(grid); panel.Children.Add(buttons); Content = panel; AddHandler(KeyDownEvent, (_, e) => { if (e.Key != Key.Escape) return; e.Handled = true; Close(null); }, RoutingStrategies.Tunnel); Opened += (_, _) => feed.Box.Focus(); // As WinForms: the values are remembered whether the dialog was accepted or not. Closing += (_, _) => { if (this.saveSettings) CutTimeSettingsFile.Save(GetCutParameters(), Machine); }; } private MachineConfig Machine => (machineBox.SelectedItem as CutTimeMachineChoice)?.Machine; private CutParameters GetCutParameters() => CutTimeEntry.Parameters(feed.Value, holeFeed.Value, rapid.Value, pierce.Value, skipEtch.IsChecked == true, units, Machine); private static void AddRow(Grid grid, int row, string label, Control field) { var text = new TextBlock { Text = label, VerticalAlignment = VerticalAlignment.Center }; Grid.SetRow(text, row); grid.Children.Add(text); Grid.SetRow(field, row); Grid.SetColumn(field, 1); grid.Children.Add(field); } /// /// The parameters entered, in , or null when the dialog was /// cancelled. false leaves the remembered values alone. /// public static Task ShowAsync( Window owner, Units units, IReadOnlyList machines, bool saveSettings = true) => new CutTimeDialog(units, machines, saveSettings).ShowDialog(owner); } /// /// A number box with its unit beside it, holding the last value typed when emptied and /// keeping values within its range, as the WinForms boxes do. /// internal sealed class NumberField { private readonly decimal minimum; private readonly decimal maximum; private decimal last; public NumberField(string name, double value, decimal minimum, decimal maximum, int decimals, string unit, double width = 120) { this.minimum = minimum; this.maximum = maximum; last = CutTimeEntry.Clamp(value, minimum, maximum); Box = new NumericUpDown { Name = name, Minimum = minimum, Maximum = maximum, Increment = decimals == 0 ? 10 : 0.1m, ClipValueToMinMax = true, FormatString = decimals == 0 ? "0" : "0.0", Value = last, Width = width, }; Box.ValueChanged += (_, e) => { if (e.NewValue is not { } v) return; last = v; Changed?.Invoke(this, EventArgs.Empty); }; var panel = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 6 }; panel.Children.Add(Box); panel.Children.Add(new TextBlock { Text = unit, VerticalAlignment = VerticalAlignment.Center }); View = panel; } public NumericUpDown Box { get; } /// The box with its unit. public Control View { get; } public decimal Value => System.Math.Clamp(last, minimum, maximum); /// Raised when a new value is entered. public event EventHandler Changed; } /// /// The cut-time values last used, in the user settings both apps share (WinForms names and /// text, see ). /// internal static class CutTimeSettingsFile { public static SavedCutTimeSettings Read() => SavedCutTimeSettings.Read(UserSettingsFile.Read(UserSettingsFile.DefaultPath)); /// /// Remembers the feeds, pierce and etch choice, leaving the remembered machine alone (the /// simulator times with the nest's machine, not a choice of its own). /// public static void Save(CutParameters parameters) => UserSettingsFile.Update(UserSettingsFile.DefaultPath, settings => SavedCutTimeSettings.Read(settings).With(parameters).WriteTo(settings)); /// Remembers the feeds, pierce, etch choice and machine (null for full speed). public static void Save(CutParameters parameters, MachineConfig machine) => UserSettingsFile.Update(UserSettingsFile.DefaultPath, settings => (SavedCutTimeSettings.Read(settings).With(parameters) with { Machine = SavedCutTimeSettings.MachineChoice(machine), }).WriteTo(settings)); } }