Browse Source

Merge pull request #6032 from MarchingCube/use-immutable-data

Use immutable data to limit allocations.
Jumar Macato 4 years ago
parent
commit
bda473070f

+ 2 - 1
src/Avalonia.Controls/ExperimentalAcrylicBorder.cs

@@ -3,6 +3,7 @@ using Avalonia.Layout;
 using Avalonia.Media;
 using Avalonia.Platform;
 using System;
+using Avalonia.Media.Immutable;
 
 namespace Avalonia.Controls
 {
@@ -90,7 +91,7 @@ namespace Avalonia.Controls
             }
             else
             {
-                _borderRenderHelper.Render(context, Bounds.Size, new Thickness(), CornerRadius, new SolidColorBrush(Material.FallbackColor), null, default);
+                _borderRenderHelper.Render(context, Bounds.Size, new Thickness(), CornerRadius, new ImmutableSolidColorBrush(Material.FallbackColor), null, default);
             }
         }
 

+ 6 - 3
src/Avalonia.Controls/Presenters/TextPresenter.cs

@@ -6,6 +6,7 @@ using Avalonia.Metadata;
 using Avalonia.Threading;
 using Avalonia.VisualTree;
 using Avalonia.Layout;
+using Avalonia.Media.Immutable;
 
 namespace Avalonia.Controls.Presenters
 {
@@ -366,24 +367,26 @@ namespace Avalonia.Controls.Presenters
 
                 if (caretBrush is null)
                 {
-                    var backgroundColor = (Background as SolidColorBrush)?.Color;
+                    var backgroundColor = (Background as ISolidColorBrush)?.Color;
                     if (backgroundColor.HasValue)
                     {
                         byte red = (byte)~(backgroundColor.Value.R);
                         byte green = (byte)~(backgroundColor.Value.G);
                         byte blue = (byte)~(backgroundColor.Value.B);
 
-                        caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
+                        caretBrush = new ImmutableSolidColorBrush(Color.FromRgb(red, green, blue));
                     }
                     else
+                    {
                         caretBrush = Brushes.Black;
+                    }
                 }
 
                 if (_caretBlink)
                 {
                     var (p1, p2) = GetCaretPoints();
                     context.DrawLine(
-                        new Pen(caretBrush, 1),
+                        new ImmutablePen(caretBrush, 1),
                         p1, p2);
                 }
             }

+ 2 - 1
src/Avalonia.Controls/TickBar.cs

@@ -1,6 +1,7 @@
 using Avalonia.Collections;
 using Avalonia.Layout;
 using Avalonia.Media;
+using Avalonia.Media.Immutable;
 using Avalonia.Utilities;
 
 namespace Avalonia.Controls
@@ -295,7 +296,7 @@ namespace Avalonia.Controls
                 endPoint = pt;
             }
 
-            var pen = new Pen(Fill, 1.0d);
+            var pen = new ImmutablePen(Fill?.ToImmutable(), 1.0d);
 
             // Is it Vertical?
             if (Placement == TickBarPlacement.Left || Placement == TickBarPlacement.Right)

+ 3 - 2
src/Avalonia.Controls/Utils/BorderRenderHelper.cs

@@ -1,5 +1,6 @@
 using System;
 using Avalonia.Media;
+using Avalonia.Media.Immutable;
 using Avalonia.Platform;
 using Avalonia.Utilities;
 
@@ -114,9 +115,9 @@ namespace Avalonia.Controls.Utils
                 var borderThickness = _borderThickness.Top;
                 IPen pen = null;
 
-                if (borderThickness > 0)
+                if (borderBrush != null && borderThickness > 0)
                 {
-                    pen = new Pen(borderBrush, borderThickness);
+                    pen = new ImmutablePen(borderBrush.ToImmutable(), borderThickness);
                 }
 
                 var rect = new Rect(_size);