using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Layout; using OpenNest.Desktop; namespace OpenNest.Avalonia { /// /// What an SVG's numbers mean when the file does not state its real-world size, as the /// WinForms SVG Scale form asks, showing the size each choice gives. OK returns the chosen /// units per inch; Skip File, Esc or closing returns null and the file is not imported. /// internal sealed class SvgScaleDialog : Window { private SvgScaleDialog(string path, double width, double height, Units units) { Title = "SVG Scale"; Width = 440; SizeToContent = SizeToContent.Height; CanResize = false; ShowInTaskbar = false; WindowStartupLocation = WindowStartupLocation.CenterOwner; var size = new TextBlock(); var choices = new List<(RadioButton Button, double PerInch)>(); var options = new StackPanel { Spacing = 4 }; foreach (var (text, perInch) in SvgScale.Choices) { var button = new RadioButton { Content = text, GroupName = "SvgScale", IsChecked = choices.Count == 0 }; button.IsCheckedChanged += (_, _) => UpdateSize(); choices.Add((button, perInch)); options.Children.Add(button); } double Chosen() { foreach (var (button, perInch) in choices) { if (button.IsChecked == true) return perInch; } return choices[0].PerInch; } void UpdateSize() => size.Text = SvgScale.ImportedSizeText(width, height, Chosen(), units); UpdateSize(); var ok = new Button { Content = "OK", IsDefault = true, MinWidth = 80 }; ok.Click += (_, _) => Close(Chosen()); var skip = new Button { Content = "Skip File", IsCancel = true, MinWidth = 80 }; skip.Click += (_, _) => Close(null); Content = new StackPanel { Margin = new Thickness(18), Spacing = 12, Children = { new TextBlock { Text = SvgScale.Question(Path.GetFileName(path)), TextWrapping = global::Avalonia.Media.TextWrapping.Wrap }, options, size, new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Spacing = 8, Children = { ok, skip }, }, }, }; } /// /// Whether to import , and the SVG user units per inch the user /// chose: null when the file needs no scale (not an SVG, or one stating its size). /// Import is false when the user skipped the file. /// public static async Task<(bool Import, double? UserUnitsPerInch)> ResolveAsync( Window owner, string path, Units units) { if (SvgScale.SizeToAsk(path) is not { } size) return (true, null); var chosen = await new SvgScaleDialog(path, size.Width, size.Height, units) .ShowDialog(owner); return (chosen != null, chosen); } } }