using System.Diagnostics; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using Microsoft.Win32; namespace XFEToolBox.QuickTools; public sealed partial class QuickToolPage : UserControl { public static Func? Confirmation { get; set; } public QuickToolKind Kind { get; } public QuickToolContext Context { get; } private readonly TextBlock status = new() { Text = "就绪 · 操作在本机完成", TextWrapping = TextWrapping.Wrap }; private readonly ProgressBar progress = new() { Height = 3, Minimum = 0, Maximum = 1, Value = 0, Margin = new Thickness(0, 8, 0, 0) }; private readonly StackPanel body = new() { Margin = new Thickness(0, 6, 0, 10) }; private readonly CancellationTokenSource cancellation = new(); private readonly List cleanup = []; private bool busy; public QuickToolPage() : this(QuickToolKind.Calculator) { } public QuickToolPage(QuickToolKind kind, QuickToolContext? context = null) { Kind = kind; Context = context ?? new(); var definition = QuickToolCatalog.Get(kind); Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/XFEToolBox.WpfCore;component/Resources/Style/ToolThemeResources.xaml") }); Background = new SolidColorBrush(Color.FromRgb(249, 249, 254)); Foreground = new SolidColorBrush(Color.FromRgb(73, 72, 95)); FontSize = 14; var layout = new Grid { Margin = new Thickness(20, 12, 20, 12) }; layout.RowDefinitions.Add(new() { Height = GridLength.Auto }); layout.RowDefinitions.Add(new()); layout.RowDefinitions.Add(new() { Height = GridLength.Auto }); var heading = new StackPanel(); var title = new DockPanel(); var glyph = new TextBlock { Text = definition.Glyph, FontFamily = new FontFamily("Segoe Fluent Icons, Segoe MDL2 Assets"), FontSize = 28, Margin = new Thickness(0, 2, 14, 0) }; DockPanel.SetDock(glyph, Dock.Left); title.Children.Add(glyph); title.Children.Add(new TextBlock { Text = definition.Name, FontFamily = new FontFamily("Microsoft YaHei UI"), FontSize = 24, FontWeight = FontWeights.SemiBold, TextWrapping = TextWrapping.Wrap }); heading.Children.Add(title); heading.Children.Add(Hint(definition.Description)); layout.Children.Add(heading); var scroll = new ScrollViewer { Content = body, VerticalScrollBarVisibility = ScrollBarVisibility.Auto, HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled, Margin = new Thickness(0, 4, 0, 6) }; Grid.SetRow(scroll, 1); layout.Children.Add(scroll); var footer = new StackPanel { Margin = new Thickness(2, 6, 2, 0) }; footer.Children.Add(status); footer.Children.Add(progress); Grid.SetRow(footer, 2); layout.Children.Add(footer); Content = layout; switch (kind) { case QuickToolKind.Calculator: BuildCalculator(); break; case QuickToolKind.Text: BuildText(); break; case QuickToolKind.Ports: BuildPorts(); break; case QuickToolKind.Rename: BuildRename(); break; case QuickToolKind.Qr: BuildQr(); break; case QuickToolKind.Clipboard: BuildClipboard(); break; case QuickToolKind.Screen: BuildScreen(); break; case QuickToolKind.System: BuildSystem(); break; } // Hiding for a screen capture also unloads the visual tree. Only cancel // when the owning window actually closes, not when it is temporarily hidden. Window? lifetimeWindow = null; Loaded += (_, _) => { if (lifetimeWindow is not null) return; lifetimeWindow = Window.GetWindow(this); if (lifetimeWindow is not null) lifetimeWindow.Closed += (_, _) => { cancellation.Cancel(); foreach (var action in cleanup) action(); cleanup.Clear(); }; }; } public async Task Run(Func action) { if (busy) return; busy = true; body.IsEnabled = false; progress.IsIndeterminate = true; status.Text = "正在处理…"; try { await action(); progress.Value = 1; if (status.Text == "正在处理…") status.Text = "操作完成。"; } catch (OperationCanceledException) { status.Text = "操作已取消。"; progress.Value = 0; } catch (Exception e) { status.Text = "未完成:" + e.Message; progress.Value = 0; } finally { busy = false; body.IsEnabled = true; progress.IsIndeterminate = false; } } private Button ActionButton(string title, Func action) { var b = new Button { Content = title, MinWidth = 94, FontSize = 14, HorizontalAlignment = HorizontalAlignment.Left, Margin = new Thickness(0, 0, 8, 8), Padding = new Thickness(14, 8, 14, 8) }; b.Click += async (_, _) => await Run(action); return b; } private Button ActionButton(string title, Action action) => ActionButton(title, () => { action(); return Task.CompletedTask; }); private static TextBox Box(string text = "", bool multiline = false, bool readOnly = false) => new() { Text = text, IsReadOnly = readOnly, AcceptsReturn = multiline, AcceptsTab = multiline, TextWrapping = multiline ? TextWrapping.Wrap : TextWrapping.NoWrap, VerticalScrollBarVisibility = multiline ? ScrollBarVisibility.Auto : ScrollBarVisibility.Disabled, HorizontalScrollBarVisibility = multiline ? ScrollBarVisibility.Disabled : ScrollBarVisibility.Auto, MinHeight = multiline ? 140 : 42, MaxHeight = multiline ? 300 : 80, FontSize = 14, VerticalContentAlignment = multiline ? VerticalAlignment.Top : VerticalAlignment.Center, Margin = new Thickness(0, 6, 0, 10), FontFamily = new FontFamily("Consolas") }; private static TextBlock Hint(string text) => new() { Text = text, TextWrapping = TextWrapping.Wrap, Foreground = new SolidColorBrush(Color.FromRgb(127, 125, 151)), Margin = new Thickness(0, 7, 0, 9) }; private static Border Card(UIElement child) => new() { Child = child, Background = Brushes.White, BorderBrush = new SolidColorBrush(Color.FromRgb(220, 219, 241)), BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(12), Padding = new Thickness(16), Margin = new Thickness(0, 0, 0, 8) }; private static WrapPanel Actions(params UIElement[] buttons) { var p = new WrapPanel { Margin = new Thickness(0, 6, 0, 0) }; foreach (var b in buttons) p.Children.Add(b); return p; } private static ComboBox Choices(IEnumerable choices, int selected = 0) { var c = new ComboBox { ItemsSource = choices.ToArray(), SelectedIndex = selected, FontSize = 14, MinWidth = 150, Margin = new Thickness(0, 4, 8, 8) }; return c; } private static ListBox List(string member = "Display") => new() { DisplayMemberPath = member, FontSize = 13, Height = 240, Margin = new Thickness(0, 6, 0, 10), HorizontalContentAlignment = HorizontalAlignment.Stretch }; private void Result(string text, TextBox output) { output.Text = text; status.Text = "已完成,可复制结果。"; } private Button Copy(TextBox box) => ActionButton("复制结果", () => { if (box.Text.Length == 0) throw new InvalidOperationException("没有可复制的内容。"); Clipboard.SetText(box.Text); status.Text = "已复制。"; }); private string? ChooseFile(string filter = "所有文件|*.*") { var dialog = new OpenFileDialog { Filter = filter }; return dialog.ShowDialog(Window.GetWindow(this)) == true ? dialog.FileName : null; } private static void Reveal(string path) { if (!File.Exists(path)) throw new FileNotFoundException("文件不存在或无权读取。", path); var start = new ProcessStartInfo("explorer.exe") { UseShellExecute = false }; start.ArgumentList.Add("/select," + Path.GetFullPath(path)); Process.Start(start); } private bool Confirm(string title, string message) { if (Confirmation is not null) return Confirmation(title, message, Window.GetWindow(this)); var panel = new StackPanel { Margin = new Thickness(20), Width = 440 }; panel.Children.Add(new TextBlock { Text = title, FontSize = 21, FontWeight = FontWeights.SemiBold }); panel.Children.Add(Hint(message)); var dialog = new Window { Title = title, Owner = Window.GetWindow(this), SizeToContent = SizeToContent.WidthAndHeight, ResizeMode = ResizeMode.NoResize, WindowStartupLocation = WindowStartupLocation.CenterOwner, Content = panel, Background = Brushes.White, ShowInTaskbar = false }; dialog.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/XFEToolBox.WpfCore;component/Resources/Style/ToolThemeResources.xaml") }); var yes = new Button { Content = "确认执行", Margin = new Thickness(0, 0, 10, 0), Padding = new Thickness(16, 8, 16, 8) }; var no = new Button { Content = "取消", IsCancel = true, Padding = new Thickness(16, 8, 16, 8) }; yes.Click += (_, _) => dialog.DialogResult = true; panel.Children.Add(Actions(yes, no)); return dialog.ShowDialog() == true; } }