CommandDebugPopup.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Documents;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using PixiEditor.Models.Commands;
  7. using PixiEditor.Models.Commands.Commands;
  8. using PixiEditor.Models.Commands.Evaluators;
  9. namespace PixiEditor.Views.Dialogs.DebugDialogs;
  10. public partial class CommandDebugPopup : Window
  11. {
  12. private static Brush infoBrush = new SolidColorBrush(Color.FromRgb(129, 143, 156));
  13. private static Brush warningBrush = new SolidColorBrush(Color.FromRgb(222, 130, 55));
  14. private static Brush errorBrush = new SolidColorBrush(Color.FromRgb(230, 34, 57));
  15. public static readonly DependencyProperty CommandsProperty =
  16. DependencyProperty.Register(nameof(Commands), typeof(IEnumerable<CommandDebug>), typeof(CommandDebugPopup));
  17. internal IEnumerable<CommandDebug> Commands
  18. {
  19. get => (IEnumerable<CommandDebug>)GetValue(CommandsProperty);
  20. set => SetValue(CommandsProperty, value);
  21. }
  22. public CommandDebugPopup()
  23. {
  24. var debugCommands = new List<CommandDebug>();
  25. foreach (var command in CommandController.Current.Commands)
  26. {
  27. var comments = new TextBlock { TextWrapping = TextWrapping.Wrap };
  28. ImageSource image = null;
  29. Exception imageException = null;
  30. try
  31. {
  32. image = command.IconEvaluator.CallEvaluate(command, null);
  33. }
  34. catch (Exception e)
  35. {
  36. imageException = e;
  37. }
  38. var analysis = AnalyzeCommand(command, image, imageException, out int issues);
  39. foreach (var inline in analysis)
  40. {
  41. comments.Inlines.Add(inline);
  42. }
  43. debugCommands.Add(new CommandDebug(command, comments, image, issues));
  44. }
  45. Commands = debugCommands.OrderByDescending(x => x.Issues).ThenBy(x => x.Command.InternalName).ToArray();
  46. InitializeComponent();
  47. }
  48. private List<Inline> AnalyzeCommand(Command command, ImageSource? image, Exception? imageException, out int issues)
  49. {
  50. var inlines = new List<Inline>();
  51. issues = 0;
  52. if (imageException != null)
  53. {
  54. Error($"Icon evaluator throws exception\n{imageException}\n");
  55. issues++;
  56. }
  57. else
  58. {
  59. if (image == null && command.IconEvaluator == IconEvaluator.Default)
  60. {
  61. var expected = IconEvaluator.GetDefaultPath(command);
  62. if (string.IsNullOrWhiteSpace(command.IconPath))
  63. {
  64. Info(
  65. $"Default evaluator has not found a image (No icon path provided). Expected at '{expected}'\n");
  66. }
  67. else
  68. {
  69. Error($"Default evaluator has not found a image at icon path! Expected at '{expected}'.\n");
  70. issues++;
  71. }
  72. }
  73. }
  74. if (command.IconEvaluator == null)
  75. {
  76. Warning("Icon evaluator is null");
  77. }
  78. else if (command.IconEvaluator != IconEvaluator.Default)
  79. {
  80. Info($"Uses custom icon evaluator ({command.IconEvaluator.Name})\n");
  81. }
  82. if (!string.IsNullOrWhiteSpace(command.IconPath))
  83. {
  84. Info($"Has custom icon path: '{command.IconPath}'\n");
  85. }
  86. return inlines;
  87. void Info(string text) => inlines.Add(new Run(text) { Foreground = infoBrush });
  88. void Warning(string text) => inlines.Add(new Run(text) { Foreground = warningBrush });
  89. void Error(string text) => inlines.Add(new Run(text) { Foreground = errorBrush });
  90. }
  91. private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  92. {
  93. e.CanExecute = true;
  94. }
  95. private void CommandBinding_Executed_Close(object sender, ExecutedRoutedEventArgs e)
  96. {
  97. SystemCommands.CloseWindow(this);
  98. }
  99. internal class CommandDebug
  100. {
  101. public Command Command { get; }
  102. public TextBlock Comments { get; }
  103. public ImageSource Image { get; }
  104. public int Issues { get; }
  105. public CommandDebug(Command command, TextBlock comments, ImageSource image, int issues)
  106. {
  107. Command = command;
  108. Comments = comments;
  109. Image = image;
  110. Issues = issues;
  111. }
  112. }
  113. }