using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Layout; using Avalonia.Media; using OpenNest.Avalonia.Rendering; using OpenNest.Desktop; using DrawingColor = System.Drawing.Color; namespace OpenNest.Avalonia { /// /// The Drawings tab's double-click, as the WinForms Drawing Properties dialog: the drawing's /// picture beside its name, customer and formed-after-cutting answer, its quantity (with how /// many are nested and remain), priority, cut first and allow common line, its color (picked, /// or Automatic from the color scheme) and its size, area and source file. OK returns the /// edited (the caller saves them as one undo step); Cancel or /// Esc returns null and changes nothing. /// internal sealed class DrawingPropertiesDialog : Window { private const int PreviewSize = 240; private readonly DrawingProperties properties; private readonly TextBox name = new() { Name = "DrawingName", MinWidth = 260 }; private readonly TextBox customer = new() { Name = "Customer", MinWidth = 260 }; private readonly CheckBox formed = new() { Name = "Formed", Content = "Formed after cutting" }; private readonly NumericUpDown quantity = new() { Name = "Quantity", Minimum = 0, Maximum = DrawingProperties.MaxQuantity, Increment = 1, FormatString = "0", ClipValueToMinMax = true, MinWidth = 140, }; private readonly NumericUpDown priority = new() { Name = "Priority", Minimum = DrawingProperties.MinPriority, Maximum = DrawingProperties.MaxPriority, Increment = 1, FormatString = "0", ClipValueToMinMax = true, MinWidth = 140, }; private readonly CheckBox cutFirst = new() { Name = "CutFirst", Content = "Cut first" }; private readonly CheckBox allowCommonLine = new() { Name = "AllowCommonLine", Content = "Allow common line" }; private readonly TextBlock nested = new() { Name = "Nested", VerticalAlignment = VerticalAlignment.Center }; private readonly Border colorSwatch = new() { Name = "ColorSwatch", Width = 28, Height = 20, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1), Cursor = new Cursor(StandardCursorType.Hand), }; private readonly TextBlock colorMode = new() { Name = "ColorMode", VerticalAlignment = VerticalAlignment.Center }; private readonly Button automaticColor = new() { Name = "AutomaticColor", Content = "Automatic" }; private readonly Image preview = new() { Width = PreviewSize, Height = PreviewSize }; private DrawingPropertiesDialog(Drawing drawing, Nest nest) { properties = new DrawingProperties(drawing, nest); Title = properties.Title; SizeToContent = SizeToContent.WidthAndHeight; CanResize = false; ShowInTaskbar = false; WindowStartupLocation = WindowStartupLocation.CenterOwner; name.Text = properties.Name; customer.Text = properties.Customer; formed.IsChecked = properties.Formed; quantity.Value = properties.Quantity; priority.Value = properties.Priority; cutFirst.IsChecked = properties.CutFirst; allowCommonLine.IsChecked = properties.AllowCommonLine; quantity.ValueChanged += (_, _) => UpdateNested(); var changeColor = new Button { Name = "ChangeColor", Content = "Change Color..." }; changeColor.Click += async (_, _) => await ChangeColorAsync(); colorSwatch.PointerReleased += async (_, _) => await ChangeColorAsync(); automaticColor.Click += (_, _) => { properties.UseAutomaticColor(); UpdateColor(); }; var drawingGroup = Fields( ("Name", name, null), ("Customer", customer, null), (null, formed, "Bent or rolled; marked F on the nest report")); var nestingGroup = Fields( ("Quantity", Row(quantity, nested), null), ("Priority", priority, "Higher numbers nest first"), (null, cutFirst, "Cut before other parts on the plate"), (null, allowCommonLine, "Copies may share a cut, one kerf apart")); var colorGroup = new StackPanel { Spacing = 6, Children = { Row(colorSwatch, colorMode), Row(changeColor, automaticColor), }, }; var source = new TextBlock { Name = "Source", Text = properties.SourceText }; if (properties.SourcePath != null) ToolTip.SetTip(source, properties.SourcePath); var detailsGroup = Fields( ("Size", new TextBlock { Name = "DrawingSize", Text = properties.SizeText }, null), ("Area", new TextBlock { Name = "Area", Text = properties.AreaText }, null), ("Source", source, null)); var ok = new Button { Name = "Ok", Content = "OK", IsDefault = true, MinWidth = 90 }; ok.Click += (_, _) => Close(Values()); var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 }; cancel.Click += (_, _) => Close(null); var fields = new StackPanel { Spacing = 14, Width = 420, Children = { Group("Drawing", drawingGroup), Group("Nesting", nestingGroup), Group("Color", colorGroup), Group("Details", detailsGroup), }, }; var previewBox = new Border { Background = Brushes.White, BorderBrush = Brush.Parse("#C8CDD2"), BorderThickness = new Thickness(1), Padding = new Thickness(12), VerticalAlignment = VerticalAlignment.Top, Child = preview, }; Content = new StackPanel { Margin = new Thickness(18), Spacing = 18, Children = { new StackPanel { Orientation = Orientation.Horizontal, Spacing = 18, Children = { previewBox, fields }, }, new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Spacing = 8, Children = { ok, cancel }, }, }, }; AddHandler(KeyDownEvent, (_, e) => { if (e.Key != Key.Escape) return; e.Handled = true; Close(null); }, RoutingStrategies.Tunnel); Opened += (_, _) => { UpdateColor(); name.SelectAll(); name.Focus(); }; UpdateNested(); } /// The edited values, or null when the user cancelled. public static Task ShowAsync(Window owner, Drawing drawing, Nest nest) => new DrawingPropertiesDialog(drawing, nest).ShowDialog(owner); private DrawingProperties Values() { properties.Name = name.Text ?? ""; properties.Customer = customer.Text ?? ""; properties.Quantity = (int)(quantity.Value ?? 0); properties.Priority = (int)(priority.Value ?? 0); properties.CutFirst = cutFirst.IsChecked == true; properties.AllowCommonLine = allowCommonLine.IsChecked == true; properties.Formed = formed.IsChecked == true; return properties; } private void UpdateNested() { properties.Quantity = (int)(quantity.Value ?? 0); nested.Text = properties.NestedText; } private void UpdateColor() { var color = properties.Color; colorSwatch.Background = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B)); colorMode.Text = properties.ColorModeText; automaticColor.IsEnabled = properties.CanUseAutomaticColor; preview.Source = DrawingThumbnail.Preview(properties.Drawing, color, PreviewSize, PreviewSize, RenderScaling); } private async Task ChangeColorAsync() { var picked = await ColorChoiceDialog.ShowAsync(this, properties.Color); if (picked is not { } color) return; // A picked color cancels a pending Automatic. properties.PickColor(color); UpdateColor(); } private static StackPanel Row(params Control[] controls) { var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8 }; foreach (var control in controls) row.Children.Add(control); return row; } // Labeled fields, each with an optional gray hint below it. private static Grid Fields(params (string Label, Control Field, string Hint)[] rows) { var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("Auto,*"), ColumnSpacing = 10, RowSpacing = 6 }; var row = 0; foreach (var (label, field, hint) in rows) { grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); if (label != null) { 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); row++; if (hint == null) continue; grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); var note = new TextBlock { Text = hint, Foreground = Brushes.Gray, FontSize = 12, Margin = new Thickness(26, -4, 0, 0) }; Grid.SetRow(note, row); Grid.SetColumn(note, 1); grid.Children.Add(note); row++; } return grid; } // A titled box, as the WinForms group boxes. private static Control Group(string header, Control content) => new StackPanel { Spacing = 4, Children = { new TextBlock { Text = header, FontWeight = FontWeight.SemiBold }, new Border { BorderBrush = Brush.Parse("#C8CDD2"), BorderThickness = new Thickness(1), Padding = new Thickness(10), Child = content, }, }, }; } /// /// Change Color's chooser: the basic colors of the Windows color dialog the WinForms app /// shows, and a "#RRGGBB" box for any other. OK returns the color; Cancel or Esc returns null. /// internal sealed class ColorChoiceDialog : Window { private static readonly string[] BasicColors = { "#FF8080", "#FFFF80", "#80FF80", "#00FF80", "#80FFFF", "#0080FF", "#FF80C0", "#FF80FF", "#FF0000", "#FFFF00", "#80FF00", "#00FF40", "#00FFFF", "#0080C0", "#8080C0", "#FF00FF", "#804040", "#FF8040", "#00FF00", "#008080", "#004080", "#8080FF", "#800040", "#FF0080", "#800000", "#FF8000", "#008000", "#008040", "#0000FF", "#0000A0", "#800080", "#8000FF", "#400000", "#804000", "#004000", "#004040", "#000080", "#000040", "#400040", "#400080", "#000000", "#808000", "#808040", "#808080", "#408080", "#C0C0C0", "#404040", "#FFFFFF", }; private readonly TextBox hex = new() { Name = "Hex", Width = 110 }; private readonly Border sample = new() { Width = 40, Height = 24, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1) }; private readonly Button ok = new() { Content = "OK", IsDefault = true, MinWidth = 90 }; private ColorChoiceDialog(DrawingColor current) { Title = "Color"; SizeToContent = SizeToContent.WidthAndHeight; CanResize = false; ShowInTaskbar = false; WindowStartupLocation = WindowStartupLocation.CenterOwner; var swatches = new UniformGrid { Columns = 8 }; foreach (var text in BasicColors) { var color = Color.Parse(text); var swatch = new Button { Width = 26, Height = 22, Margin = new Thickness(2), Padding = new Thickness(0), Background = new SolidColorBrush(color), BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1), }; ToolTip.SetTip(swatch, text); swatch.Click += (_, _) => hex.Text = text; swatch.DoubleTapped += (_, _) => Close(Parse(text)); swatches.Children.Add(swatch); } hex.Text = $"#{current.R:X2}{current.G:X2}{current.B:X2}"; hex.TextChanged += (_, _) => UpdateSample(); ok.Click += (_, _) => Close(Parse(hex.Text)); var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 }; cancel.Click += (_, _) => Close(null); Content = new StackPanel { Margin = new Thickness(18), Spacing = 14, Children = { new TextBlock { Text = "Basic colors" }, swatches, new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8, Children = { new TextBlock { Text = "Color", VerticalAlignment = VerticalAlignment.Center }, hex, sample }, }, new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Spacing = 8, Children = { ok, cancel }, }, }, }; AddHandler(KeyDownEvent, (_, e) => { if (e.Key != Key.Escape) return; e.Handled = true; Close(null); }, RoutingStrategies.Tunnel); UpdateSample(); } /// The chosen color, or null when the user cancelled. public static Task ShowAsync(Window owner, DrawingColor current) => new ColorChoiceDialog(current).ShowDialog(owner); // "#RRGGBB" (the # optional) as an opaque color; null when it is not one. internal static DrawingColor? Parse(string text) { var value = text?.Trim().TrimStart('#'); if (value is not { Length: 6 } || !int.TryParse(value, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out var rgb)) return null; return DrawingColor.FromArgb(255, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF); } private void UpdateSample() { var color = Parse(hex.Text); ok.IsEnabled = color != null; sample.Background = color is { } c ? new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B)) : Brushes.Transparent; } } }