Browse Source

Merge pull request #1019 from PixiEditor/fixes/03.08.2025

Lot of gotchas and safety checks
Krzysztof Krysiński 1 week ago
parent
commit
ddaaf62384

+ 1 - 1
src/Drawie

@@ -1 +1 @@
-Subproject commit 6844357c31e686ad30f255a4bfab93b37e5bfde8
+Subproject commit 0145d4583f4ac2e20a4658e451e841a9f8397a9e

+ 21 - 1
src/PixiEditor.Desktop/Program.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 using Avalonia;
 using Avalonia.Logging;
 using Drawie.Interop.Avalonia;
@@ -19,7 +20,26 @@ public class Program
     // Avalonia configuration, don't remove; also used by visual designer.
     public static AppBuilder BuildAvaloniaApp()
     {
-        bool openGlPreferred = string.Equals(RenderApiPreferenceManager.TryReadRenderApiPreference(), "opengl", StringComparison.OrdinalIgnoreCase);
+        bool openGlPreferred = false;
+        try
+        {
+            openGlPreferred = string.Equals(RenderApiPreferenceManager.TryReadRenderApiPreference(), "opengl",
+                StringComparison.OrdinalIgnoreCase);
+
+            if (!openGlPreferred)
+            {
+                var cmdArgs = Environment.GetCommandLineArgs();
+                if (cmdArgs is { Length: > 0 })
+                {
+                    openGlPreferred = cmdArgs.Any(arg =>
+                        string.Equals(arg, "--opengl", StringComparison.OrdinalIgnoreCase));
+                }
+            }
+        }
+        catch (Exception ex)
+        {
+        }
+
         return AppBuilder.Configure<App>()
             .UsePlatformDetect()
             .With(new Win32PlatformOptions()

+ 6 - 0
src/PixiEditor/ViewModels/SubViewModels/LayersViewModel.cs

@@ -557,6 +557,12 @@ internal class LayersViewModel : SubViewModel<ViewModelMain>
             NoticeDialog.Show(title: "ERROR", message: e.Message);
             return;
         }
+        catch (Exception e)
+        {
+            CrashHelper.SendExceptionInfo(e);
+            NoticeDialog.Show(title: "ERROR", message: e.Message);
+            return;
+        }
 
         byte[] bytes = bitmap.ToByteArray();
 

+ 1 - 1
src/PixiEditor/Views/Overlays/LineToolOverlay/LineToolOverlay.cs

@@ -174,7 +174,7 @@ internal class LineToolOverlay : Overlay
         isDraggingHandle = false;
         IsSizeBoxEnabled = false;
         
-        AddToUndoCommand.Execute((LineStart, LineEnd));
+        AddToUndoCommand?.Execute((LineStart, LineEnd));
     }
 
     protected override void ZoomChanged(double newZoom)

+ 162 - 99
src/PixiEditor/Views/Rendering/Scene.cs

@@ -358,18 +358,25 @@ internal class Scene : Zoombox.Zoombox, ICustomHitTest
         {
             foreach (Overlay overlay in AllOverlays)
             {
-                if (!overlay.IsVisible || overlay.OverlayRenderSorting != sorting)
+                try
                 {
-                    continue;
-                }
+                    if (!overlay.IsVisible || overlay.OverlayRenderSorting != sorting)
+                    {
+                        continue;
+                    }
 
-                overlay.PointerPosition = lastMousePosition;
+                    overlay.PointerPosition = lastMousePosition;
 
-                overlay.ZoomScale = Scale;
+                    overlay.ZoomScale = Scale;
 
-                if (!overlay.CanRender()) continue;
+                    if (!overlay.CanRender()) continue;
 
-                overlay.RenderOverlay(renderSurface.Canvas, dirtyBounds);
+                    overlay.RenderOverlay(renderSurface.Canvas, dirtyBounds);
+                }
+                catch (Exception ex)
+                {
+                    CrashHelper.SendExceptionInfo(ex);
+                }
             }
         }
     }
@@ -417,161 +424,203 @@ internal class Scene : Zoombox.Zoombox, ICustomHitTest
     protected override void OnPointerMoved(PointerEventArgs e)
     {
         base.OnPointerMoved(e);
-        if (AllOverlays != null)
+        try
         {
-            OverlayPointerArgs args = ConstructPointerArgs(e);
-            lastMousePosition = args.Point;
+            if (AllOverlays != null)
+            {
+                OverlayPointerArgs args = ConstructPointerArgs(e);
+                lastMousePosition = args.Point;
 
-            Cursor finalCursor = DefaultCursor;
+                Cursor finalCursor = DefaultCursor;
 
-            if (capturedOverlay != null)
-            {
-                capturedOverlay.MovePointer(args);
-                if (capturedOverlay.IsHitTestVisible)
+                if (capturedOverlay != null)
                 {
-                    finalCursor = capturedOverlay.Cursor ?? DefaultCursor;
+                    capturedOverlay.MovePointer(args);
+                    if (capturedOverlay.IsHitTestVisible)
+                    {
+                        finalCursor = capturedOverlay.Cursor ?? DefaultCursor;
+                    }
                 }
-            }
-            else
-            {
-                foreach (Overlay overlay in AllOverlays)
+                else
                 {
-                    if (!overlay.IsVisible) continue;
-
-                    if (overlay.TestHit(args.Point))
+                    foreach (Overlay overlay in AllOverlays)
                     {
-                        if (!mouseOverOverlays.Contains(overlay))
+                        if (!overlay.IsVisible) continue;
+
+                        if (overlay.TestHit(args.Point))
                         {
-                            overlay.EnterPointer(args);
-                            mouseOverOverlays.Add(overlay);
+                            if (!mouseOverOverlays.Contains(overlay))
+                            {
+                                overlay.EnterPointer(args);
+                                mouseOverOverlays.Add(overlay);
+                            }
                         }
-                    }
-                    else
-                    {
-                        if (mouseOverOverlays.Contains(overlay))
+                        else
                         {
-                            overlay.ExitPointer(args);
-                            mouseOverOverlays.Remove(overlay);
-
-                            e.Handled = args.Handled;
-                            return;
+                            if (mouseOverOverlays.Contains(overlay))
+                            {
+                                overlay.ExitPointer(args);
+                                mouseOverOverlays.Remove(overlay);
+
+                                e.Handled = args.Handled;
+                                return;
+                            }
                         }
-                    }
 
-                    overlay.MovePointer(args);
-                    if (overlay.IsHitTestVisible)
-                    {
-                        finalCursor = overlay.Cursor ?? DefaultCursor;
+                        overlay.MovePointer(args);
+                        if (overlay.IsHitTestVisible)
+                        {
+                            finalCursor = overlay.Cursor ?? DefaultCursor;
+                        }
                     }
                 }
-            }
 
-            if (Cursor.ToString() != finalCursor.ToString())
-                Cursor = finalCursor;
-            e.Handled = args.Handled;
+                if (Cursor.ToString() != finalCursor.ToString())
+                    Cursor = finalCursor;
+                e.Handled = args.Handled;
+            }
+        }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
         }
     }
 
     protected override void OnPointerPressed(PointerPressedEventArgs e)
     {
         base.OnPointerPressed(e);
-        if (AllOverlays != null)
+        try
         {
-            OverlayPointerArgs args = ConstructPointerArgs(e);
-            if (capturedOverlay != null)
-            {
-                capturedOverlay?.PressPointer(args);
-            }
-            else
+            if (AllOverlays != null)
             {
-                foreach (var overlay in AllOverlays)
+                OverlayPointerArgs args = ConstructPointerArgs(e);
+                if (capturedOverlay != null)
                 {
-                    if (args.Handled) break;
-                    if (!overlay.IsVisible) continue;
+                    capturedOverlay?.PressPointer(args);
+                }
+                else
+                {
+                    foreach (var overlay in AllOverlays)
+                    {
+                        if (args.Handled) break;
+                        if (!overlay.IsVisible) continue;
 
-                    if (!overlay.IsHitTestVisible || !overlay.TestHit(args.Point)) continue;
+                        if (!overlay.IsHitTestVisible || !overlay.TestHit(args.Point)) continue;
 
-                    overlay.PressPointer(args);
+                        overlay.PressPointer(args);
+                    }
                 }
-            }
 
-            e.Handled = args.Handled;
+                e.Handled = args.Handled;
+            }
+        }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
         }
     }
 
     protected override void OnPointerExited(PointerEventArgs e)
     {
         base.OnPointerExited(e);
-        if (AllOverlays != null)
+        try
         {
-            OverlayPointerArgs args = ConstructPointerArgs(e);
-            for (var i = 0; i < mouseOverOverlays.Count; i++)
+            if (AllOverlays != null)
             {
-                var overlay = mouseOverOverlays[i];
-                if (args.Handled) break;
-                if (!overlay.IsVisible) continue;
+                OverlayPointerArgs args = ConstructPointerArgs(e);
+                for (var i = 0; i < mouseOverOverlays.Count; i++)
+                {
+                    var overlay = mouseOverOverlays[i];
+                    if (args.Handled) break;
+                    if (!overlay.IsVisible) continue;
 
-                overlay.ExitPointer(args);
-                mouseOverOverlays.Remove(overlay);
-                i--;
-            }
+                    overlay.ExitPointer(args);
+                    mouseOverOverlays.Remove(overlay);
+                    i--;
+                }
 
-            e.Handled = args.Handled;
+                e.Handled = args.Handled;
+            }
+        }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
         }
     }
 
     protected override void OnPointerReleased(PointerReleasedEventArgs e)
     {
         base.OnPointerExited(e);
-        if (AllOverlays != null)
+        try
         {
-            OverlayPointerArgs args = ConstructPointerArgs(e);
-
-            if (capturedOverlay != null)
-            {
-                capturedOverlay.ReleasePointer(args);
-                capturedOverlay = null;
-            }
-            else
+            if (AllOverlays != null)
             {
-                foreach (Overlay overlay in AllOverlays)
+                OverlayPointerArgs args = ConstructPointerArgs(e);
+
+                if (capturedOverlay != null)
                 {
-                    if (args.Handled) break;
-                    if (!overlay.IsVisible) continue;
+                    capturedOverlay.ReleasePointer(args);
+                    capturedOverlay = null;
+                }
+                else
+                {
+                    foreach (Overlay overlay in AllOverlays)
+                    {
+                        if (args.Handled) break;
+                        if (!overlay.IsVisible) continue;
 
-                    if (!overlay.IsHitTestVisible || !overlay.TestHit(args.Point)) continue;
+                        if (!overlay.IsHitTestVisible || !overlay.TestHit(args.Point)) continue;
 
-                    overlay.ReleasePointer(args);
+                        overlay.ReleasePointer(args);
+                    }
                 }
             }
         }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
+        }
     }
 
     protected override void OnKeyDown(KeyEventArgs e)
     {
         base.OnKeyDown(e);
-        if (AllOverlays != null)
+        try
         {
-            foreach (Overlay overlay in AllOverlays)
+            if (AllOverlays != null)
             {
-                if (!overlay.IsVisible) continue;
+                foreach (Overlay overlay in AllOverlays)
+                {
+                    if (!overlay.IsVisible) continue;
 
-                overlay.KeyPressed(e);
+                    overlay.KeyPressed(e);
+                }
             }
         }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
+        }
     }
 
     protected override void OnKeyUp(KeyEventArgs e)
     {
         base.OnKeyUp(e);
-        if (AllOverlays != null)
+        try
         {
-            foreach (Overlay overlay in AllOverlays)
+            if (AllOverlays != null)
             {
-                if (!overlay.IsVisible) continue;
-                overlay.KeyReleased(e);
+                foreach (Overlay overlay in AllOverlays)
+                {
+                    if (!overlay.IsVisible) continue;
+                    overlay.KeyReleased(e);
+                }
             }
         }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
+        }
     }
 
     private OverlayPointerArgs ConstructPointerArgs(PointerEventArgs e)
@@ -597,27 +646,41 @@ internal class Scene : Zoombox.Zoombox, ICustomHitTest
     protected override void OnGotFocus(GotFocusEventArgs e)
     {
         base.OnGotFocus(e);
-        if (AllOverlays != null)
+        try
         {
-            foreach (Overlay overlay in AllOverlays)
+            if (AllOverlays != null)
             {
-                if (!overlay.IsVisible) continue;
-                overlay.FocusChanged(true);
+                foreach (Overlay overlay in AllOverlays)
+                {
+                    if (!overlay.IsVisible) continue;
+                    overlay.FocusChanged(true);
+                }
             }
         }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
+        }
     }
 
     protected override void OnLostFocus(RoutedEventArgs e)
     {
         base.OnLostFocus(e);
-        if (AllOverlays != null)
+        try
         {
-            foreach (Overlay overlay in AllOverlays)
+            if (AllOverlays != null)
             {
-                if (!overlay.IsVisible) continue;
-                overlay.FocusChanged(false);
+                foreach (Overlay overlay in AllOverlays)
+                {
+                    if (!overlay.IsVisible) continue;
+                    overlay.FocusChanged(false);
+                }
             }
         }
+        catch (Exception ex)
+        {
+            CrashHelper.SendExceptionInfo(ex);
+        }
     }
 
     private VecD ToCanvasSpace(Point scenePosition)