diff --git a/src/Avalonia.Visuals/Media/BrushExtensions.cs b/src/Avalonia.Visuals/Media/BrushExtensions.cs new file mode 100644 index 0000000000..cd351071dd --- /dev/null +++ b/src/Avalonia.Visuals/Media/BrushExtensions.cs @@ -0,0 +1,51 @@ +using System; + +namespace Avalonia.Media +{ + /// + /// Extension methods for brush classes. + /// + public static class BrushExtensions + { + /// + /// Converts a brush to an immutable brush. + /// + /// The brush. + /// + /// The result of calling if the brush is mutable, + /// otherwise . + /// + public static IBrush ToImmutable(this IBrush brush) + { + Contract.Requires(brush != null); + + return (brush as IMutableBrush)?.ToImmutable() ?? brush; + } + + /// + /// Converts a pen to a pen with an immutable brush + /// + /// The pen. + /// + /// A copy of the pen with an immutable brush, or if the pen's brush + /// is already immutable or null. + /// + public static Pen ToImmutable(this Pen pen) + { + Contract.Requires(pen != null); + + var brush = pen?.Brush?.ToImmutable(); + return pen == null || ReferenceEquals(pen?.Brush, brush) ? + pen : + new Pen( + brush, + thickness: pen.Thickness, + dashStyle: pen.DashStyle, + dashCap: pen.DashCap, + startLineCap: pen.StartLineCap, + endLineCap: pen.EndLineCap, + lineJoin: pen.LineJoin, + miterLimit: pen.MiterLimit); + } + } +} diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/BrushDrawOperation.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/BrushDrawOperation.cs index 023a2965ab..59a895a22f 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/BrushDrawOperation.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/BrushDrawOperation.cs @@ -27,36 +27,5 @@ namespace Avalonia.Rendering.SceneGraph /// public abstract void Render(IDrawingContextImpl context); - - /// - /// Converts a possibly mutable brush to an immutable brush. - /// - /// The brush. - /// An immutable brush - protected IBrush ToImmutable(IBrush brush) - { - return (brush as IMutableBrush)?.ToImmutable() ?? brush; - } - - /// - /// Converts pen with a possibly mutable brush to a pen with an immutable brush. - /// - /// The pen. - /// A pen with an immutable brush - protected Pen ToImmutable(Pen pen) - { - var brush = pen?.Brush != null ? ToImmutable(pen.Brush) : null; - return pen == null || ReferenceEquals(pen?.Brush, brush) ? - pen : - new Pen( - brush, - thickness: pen.Thickness, - dashStyle: pen.DashStyle, - dashCap: pen.DashCap, - startLineCap: pen.StartLineCap, - endLineCap: pen.EndLineCap, - lineJoin: pen.LineJoin, - miterLimit: pen.MiterLimit); - } } } diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs index 077a753b8d..b884c42d99 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs @@ -31,8 +31,8 @@ namespace Avalonia.Rendering.SceneGraph { Bounds = geometry.GetRenderBounds(pen?.Thickness ?? 0).TransformToAABB(transform); Transform = transform; - Brush = ToImmutable(brush); - Pen = ToImmutable(pen); + Brush = brush?.ToImmutable(); + Pen = pen?.ToImmutable(); Geometry = geometry; ChildScenes = childScenes; } diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs index 46da8c7900..7ed188de94 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs @@ -31,7 +31,7 @@ namespace Avalonia.Rendering.SceneGraph { Bounds = new Rect(P1, P2); Transform = transform; - Pen = ToImmutable(pen); + Pen = pen?.ToImmutable(); P1 = p1; P2 = p2; ChildScenes = childScenes; diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/OpacityMaskNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/OpacityMaskNode.cs index 8238e0b479..c40869724f 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/OpacityMaskNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/OpacityMaskNode.cs @@ -20,7 +20,7 @@ namespace Avalonia.Rendering.SceneGraph /// Child scenes for drawing visual brushes. public OpacityMaskNode(IBrush mask, Rect bounds, IDictionary childScenes = null) { - Mask = ToImmutable(mask); + Mask = mask?.ToImmutable(); MaskBounds = bounds; ChildScenes = childScenes; } diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/RectangleNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/RectangleNode.cs index eaaaec171a..2affc454b5 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/RectangleNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/RectangleNode.cs @@ -33,8 +33,8 @@ namespace Avalonia.Rendering.SceneGraph { Bounds = rect.TransformToAABB(transform).Inflate(pen?.Thickness ?? 0); Transform = transform; - Brush = ToImmutable(brush); - Pen = ToImmutable(pen); + Brush = brush?.ToImmutable(); + Pen = pen?.ToImmutable(); Rect = rect; CornerRadius = cornerRadius; ChildScenes = childScenes; diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs index fa80845cfd..afdf488b31 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs @@ -326,7 +326,7 @@ namespace Avalonia.Rendering.SceneGraph if (node.Visual.OpacityMask != null) { - layer.OpacityMask = ToImmutable(node.Visual.OpacityMask); + layer.OpacityMask = node.Visual.OpacityMask?.ToImmutable(); layer.OpacityMaskRect = node.ClipBounds; } else @@ -380,10 +380,5 @@ namespace Avalonia.Rendering.SceneGraph return result; } - - private static IBrush ToImmutable(IBrush brush) - { - return (brush as IMutableBrush)?.ToImmutable() ?? brush; - } } } diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/TextNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/TextNode.cs index 3dc1500af0..058f3b1c22 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/TextNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/TextNode.cs @@ -31,7 +31,7 @@ namespace Avalonia.Rendering.SceneGraph { Bounds = new Rect(origin, text.Size).TransformToAABB(transform); Transform = transform; - Foreground = ToImmutable(foreground); + Foreground = foreground?.ToImmutable(); Origin = origin; Text = text; ChildScenes = childScenes; diff --git a/src/Skia/Avalonia.Skia/FormattedTextImpl.cs b/src/Skia/Avalonia.Skia/FormattedTextImpl.cs index d200b28ecb..c73e1dbcbe 100644 --- a/src/Skia/Avalonia.Skia/FormattedTextImpl.cs +++ b/src/Skia/Avalonia.Skia/FormattedTextImpl.cs @@ -615,7 +615,7 @@ namespace Avalonia.Skia if (brush != null) { - brush = (brush as IMutableBrush)?.ToImmutable() ?? brush; + brush = brush.ToImmutable(); _foregroundBrushes.Insert(0, new KeyValuePair(key, brush)); } }