Browse Source

Dont allow CommandDebugPopup to crash from Icon evaluation

CPKreuz 2 years ago
parent
commit
a6278b79b3

+ 17 - 3
src/PixiEditor/ViewModels/SubViewModels/Main/ClipboardViewModel.cs

@@ -108,10 +108,24 @@ internal class ClipboardViewModel : SubViewModel<ViewModelMain>
     }
 
     [Evaluator.Icon("PixiEditor.Clipboard.CopyColorIcon")]
-    public ImageSource GetCopyColorIcon(CommandSearchResult result)
+    public ImageSource GetCopyColorIcon(object data)
     {
-        var color = (CopyColor)((Models.Commands.Commands.Command.BasicCommand)result.Command).Parameter;
-
+        if (data is CopyColor color)
+        {
+        }
+        else if (data is Models.Commands.Commands.Command.BasicCommand command)
+        {
+            color = (CopyColor)command.Parameter;
+        }
+        else if (data is CommandSearchResult result)
+        {
+            color = (CopyColor)((Models.Commands.Commands.Command.BasicCommand)result.Command).Parameter;
+        }
+        else
+        {
+            throw new ArgumentException("data must be of type CopyColor, BasicCommand or CommandSearchResult");
+        }
+        
         var targetColor = color switch
         {
             CopyColor.PrimaryHEX or CopyColor.PrimaryRGB => Owner.ColorsSubViewModel.PrimaryColor,

+ 34 - 14
src/PixiEditor/Views/Dialogs/CommandDebugPopup.xaml.cs

@@ -33,10 +33,21 @@ public partial class CommandDebugPopup : Window
 
         foreach (var command in CommandController.Current.Commands)
         {
-            var comments = new TextBlock() { TextWrapping = TextWrapping.Wrap };
+            var comments = new TextBlock { TextWrapping = TextWrapping.Wrap };
 
-            var image = command.IconEvaluator.CallEvaluate(command, null);
-            var analysis = AnalyzeCommand(command, image, out int issues);
+            ImageSource image = null;
+            Exception imageException = null;
+
+            try
+            {
+                image = command.GetIcon();
+            }
+            catch (Exception e)
+            {
+                imageException = e;
+            }
+
+            var analysis = AnalyzeCommand(command, image, imageException, out int issues);
 
             foreach (var inline in analysis)
             {
@@ -51,23 +62,32 @@ public partial class CommandDebugPopup : Window
         InitializeComponent();
     }
 
-    private List<Inline> AnalyzeCommand(Command command, ImageSource image, out int issues)
+    private List<Inline> AnalyzeCommand(Command command, ImageSource? image, Exception? imageException, out int issues)
     {
         var inlines = new List<Inline>();
         issues = 0;
 
-        if (image == null && command.IconEvaluator == IconEvaluator.Default)
+        if (imageException != null)
         {
-            var expected = IconEvaluator.GetDefaultPath(command);
-
-            if (string.IsNullOrWhiteSpace(command.IconPath))
-            {
-                Info($"Default evaluator has not found a image (No icon path provided). Expected at '{expected}'\n");
-            }
-            else
+            Error($"Icon evaluator throws exception\n{imageException}\n");
+            issues++;
+        }
+        else
+        {
+            if (image == null && command.IconEvaluator == IconEvaluator.Default)
             {
-                Error($"Default evaluator has not found a image at icon path! Expected at '{expected}'.\n");
-                issues++;
+                var expected = IconEvaluator.GetDefaultPath(command);
+
+                if (string.IsNullOrWhiteSpace(command.IconPath))
+                {
+                    Info(
+                        $"Default evaluator has not found a image (No icon path provided). Expected at '{expected}'\n");
+                }
+                else
+                {
+                    Error($"Default evaluator has not found a image at icon path! Expected at '{expected}'.\n");
+                    issues++;
+                }
             }
         }