Browse Source

Added a set of immutable brushes.

Make `SolidColorBrush` immutable, but make the static `Brushes`
properties return immutable brushes. The immutable brushes will be
needed by the deferred renderer.
pull/920/head
Steven Kirk 9 years ago
parent
commit
65f9fe67f7
  1. 1
      src/Avalonia.Themes.Default/Accents/BaseLight.xaml
  2. 9
      src/Avalonia.Visuals/Avalonia.Visuals.csproj
  3. 3
      src/Avalonia.Visuals/Media/Brush.cs
  4. 566
      src/Avalonia.Visuals/Media/Brushes.cs
  5. 16
      src/Avalonia.Visuals/Media/IMutableBrush.cs
  6. 47
      src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs
  7. 58
      src/Avalonia.Visuals/Media/Immutable/ImmutableImageBrush.cs
  8. 51
      src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs
  9. 59
      src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs
  10. 59
      src/Avalonia.Visuals/Media/Immutable/ImmutableSolidColorBrush.cs
  11. 77
      src/Avalonia.Visuals/Media/Immutable/ImmutableTileBrush.cs
  12. 58
      src/Avalonia.Visuals/Media/Immutable/ImmutableVisualBrush.cs
  13. 35
      src/Avalonia.Visuals/Media/Mutable/SolidColorBrush.cs
  14. 36
      src/Avalonia.Visuals/Media/SolidColorBrush.cs
  15. 1
      src/Avalonia.Visuals/Properties/AssemblyInfo.cs
  16. 2
      src/Gtk/Avalonia.Cairo/Media/DrawingContext.cs
  17. 2
      src/Gtk/Avalonia.Cairo/Media/SolidColorBrushImpl.cs
  18. 9
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/Style2.xaml
  19. 20
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs

1
src/Avalonia.Themes.Default/Accents/BaseLight.xaml

@ -1,6 +1,5 @@
<Style xmlns="https://github.com/avaloniaui" <Style xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mut="https://github.com/avaloniaui/mutable"
xmlns:sys="clr-namespace:System;assembly=mscorlib"> xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style.Resources> <Style.Resources>
<SolidColorBrush x:Key="ThemeBackgroundBrush">#FFFFFFFF</SolidColorBrush> <SolidColorBrush x:Key="ThemeBackgroundBrush">#FFFFFFFF</SolidColorBrush>

9
src/Avalonia.Visuals/Avalonia.Visuals.csproj

@ -73,6 +73,14 @@
<Compile Include="Media\ILinearGradientBrush.cs" /> <Compile Include="Media\ILinearGradientBrush.cs" />
<Compile Include="Media\Imaging\IImageBrush.cs" /> <Compile Include="Media\Imaging\IImageBrush.cs" />
<Compile Include="Media\Imaging\WritableBitmap.cs" /> <Compile Include="Media\Imaging\WritableBitmap.cs" />
<Compile Include="Media\Immutable\ImmutableGradientBrush.cs" />
<Compile Include="Media\Immutable\ImmutableImageBrush.cs" />
<Compile Include="Media\Immutable\ImmutableLinearGradientBrush.cs" />
<Compile Include="Media\Immutable\ImmutableRadialGradientBrush.cs" />
<Compile Include="Media\Immutable\ImmutableSolidColorBrush.cs" />
<Compile Include="Media\Immutable\ImmutableTileBrush.cs" />
<Compile Include="Media\Immutable\ImmutableVisualBrush.cs" />
<Compile Include="Media\IMutableBrush.cs" />
<Compile Include="Media\IRadialGradientBrush.cs" /> <Compile Include="Media\IRadialGradientBrush.cs" />
<Compile Include="Media\ITileBrush.cs" /> <Compile Include="Media\ITileBrush.cs" />
<Compile Include="Media\IVisualBrush.cs" /> <Compile Include="Media\IVisualBrush.cs" />
@ -88,7 +96,6 @@
<Compile Include="Media\ISolidColorBrush.cs" /> <Compile Include="Media\ISolidColorBrush.cs" />
<Compile Include="Media\LineGeometry.cs" /> <Compile Include="Media\LineGeometry.cs" />
<Compile Include="Media\LineSegment.cs" /> <Compile Include="Media\LineSegment.cs" />
<Compile Include="Media\Mutable\SolidColorBrush.cs" />
<Compile Include="Media\PathGeometryCollections.cs" /> <Compile Include="Media\PathGeometryCollections.cs" />
<Compile Include="Media\PathFigure.cs" /> <Compile Include="Media\PathFigure.cs" />
<Compile Include="Media\PathGeometry.cs" /> <Compile Include="Media\PathGeometry.cs" />

3
src/Avalonia.Visuals/Media/Brush.cs

@ -46,7 +46,8 @@ namespace Avalonia.Media
if (member != null) if (member != null)
{ {
return (IBrush)member.GetValue(null); var brush = (ISolidColorBrush)member.GetValue(null);
return new SolidColorBrush(brush.Color, brush.Opacity);
} }
else else
{ {

566
src/Avalonia.Visuals/Media/Brushes.cs

File diff suppressed because it is too large

16
src/Avalonia.Visuals/Media/IMutableBrush.cs

@ -0,0 +1,16 @@
using System;
namespace Avalonia.Media
{
/// <summary>
/// Represents a mutable brush which can return an immutable clone of itself.
/// </summary>
public interface IMutableBrush : IBrush
{
/// <summary>
/// Creates an immutable clone of the brush.
/// </summary>
/// <returns>The immutable clone.</returns>
IBrush ToImmutable();
}
}

47
src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Avalonia.Media.Immutable
{
/// <summary>
/// A brush that draws with a gradient.
/// </summary>
public abstract class ImmutableGradientBrush : IGradientBrush
{
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableGradientBrush"/> class.
/// </summary>
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="spreadMethod">The spread method.</param>
protected ImmutableGradientBrush(
IReadOnlyList<GradientStop> gradientStops,
double opacity,
GradientSpreadMethod spreadMethod)
{
GradientStops = gradientStops;
Opacity = opacity;
SpreadMethod = spreadMethod;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableGradientBrush"/> class.
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
protected ImmutableGradientBrush(IGradientBrush source)
: this(source.GradientStops.ToList(), source.Opacity, source.SpreadMethod)
{
}
/// <inheritdoc/>
public IReadOnlyList<GradientStop> GradientStops { get; }
/// <inheritdoc/>
public double Opacity { get; }
/// <inheritdoc/>
public GradientSpreadMethod SpreadMethod { get; }
}
}

58
src/Avalonia.Visuals/Media/Immutable/ImmutableImageBrush.cs

@ -0,0 +1,58 @@
using System;
using Avalonia.Media.Imaging;
namespace Avalonia.Media.Immutable
{
/// <summary>
/// Paints an area with an <see cref="IBitmap"/>.
/// </summary>
internal class ImmutableImageBrush : ImmutableTileBrush, IImageBrush
{
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableImageBrush"/> class.
/// </summary>
/// <param name="source">The image to draw.</param>
/// <param name="alignmentX">The horizontal alignment of a tile in the destination.</param>
/// <param name="alignmentY">The vertical alignment of a tile in the destination.</param>
/// <param name="destinationRect">The rectangle on the destination in which to paint a tile.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="sourceRect">The rectangle of the source image that will be displayed.</param>
/// <param name="stretch">
/// How the source rectangle will be stretched to fill the destination rect.
/// </param>
/// <param name="tileMode">The tile mode.</param>
public ImmutableImageBrush(
IBitmap source,
AlignmentX alignmentX = AlignmentX.Center,
AlignmentY alignmentY = AlignmentY.Center,
RelativeRect? destinationRect = null,
double opacity = 1,
RelativeRect? sourceRect = null,
Stretch stretch = Stretch.Uniform,
TileMode tileMode = TileMode.None)
: base(
alignmentX,
alignmentY,
destinationRect ?? RelativeRect.Fill,
opacity,
sourceRect ?? RelativeRect.Fill,
stretch,
tileMode)
{
Source = source;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableImageBrush"/> class.
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
public ImmutableImageBrush(IImageBrush source)
: base(source)
{
Source = source.Source;
}
/// <inheritdoc/>
public IBitmap Source { get; }
}
}

51
src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Avalonia.Media.Immutable
{
/// <summary>
/// A brush that draws with a linear gradient.
/// </summary>
public class ImmutableLinearGradientBrush : ImmutableGradientBrush, ILinearGradientBrush
{
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableLinearGradientBrush"/> class.
/// </summary>
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="spreadMethod">The spread method.</param>
/// <param name="startPoint">The start point for the gradient.</param>
/// <param name="endPoint">The end point for the gradient.</param>
public ImmutableLinearGradientBrush(
IReadOnlyList<GradientStop> gradientStops,
double opacity = 1,
GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad,
RelativePoint? startPoint = null,
RelativePoint? endPoint = null)
: base(gradientStops, opacity, spreadMethod)
{
StartPoint = startPoint ?? RelativePoint.TopLeft;
EndPoint = endPoint ?? RelativePoint.BottomRight;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableLinearGradientBrush"/> class.
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
public ImmutableLinearGradientBrush(ILinearGradientBrush source)
: base(source)
{
StartPoint = source.StartPoint;
EndPoint = source.EndPoint;
}
/// <inheritdoc/>
public RelativePoint StartPoint { get; }
/// <inheritdoc/>
public RelativePoint EndPoint { get; }
}
}

59
src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
namespace Avalonia.Media.Immutable
{
/// <summary>
/// A brush that draws with a radial gradient.
/// </summary>
public class ImmutableRadialGradientBrush : ImmutableGradientBrush, IRadialGradientBrush
{
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableRadialGradientBrush"/> class.
/// </summary>
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="spreadMethod">The spread method.</param>
/// <param name="center">The start point for the gradient.</param>
/// <param name="gradientOrigin">
/// The location of the two-dimensional focal point that defines the beginning of the gradient.
/// </param>
/// <param name="radius">
/// The horizontal and vertical radius of the outermost circle of the radial gradient.
/// </param>
public ImmutableRadialGradientBrush(
IReadOnlyList<GradientStop> gradientStops,
double opacity = 1,
GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad,
RelativePoint? center = null,
RelativePoint? gradientOrigin = null,
double radius = 0.5)
: base(gradientStops, opacity, spreadMethod)
{
Center = center ?? RelativePoint.Center;
GradientOrigin = gradientOrigin ?? RelativePoint.Center;
Radius = radius;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableRadialGradientBrush"/> class.
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
public ImmutableRadialGradientBrush(IRadialGradientBrush source)
: base(source)
{
Center = source.Center;
GradientOrigin = source.GradientOrigin;
Radius = source.Radius;
}
/// <inheritdoc/>
public RelativePoint Center { get; }
/// <inheritdoc/>
public RelativePoint GradientOrigin { get; }
/// <inheritdoc/>
public double Radius { get; }
}
}

59
src/Avalonia.Visuals/Media/Immutable/ImmutableSolidColorBrush.cs

@ -0,0 +1,59 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
namespace Avalonia.Media.Immutable
{
/// <summary>
/// Fills an area with a solid color.
/// </summary>
public struct ImmutableSolidColorBrush : ISolidColorBrush
{
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableSolidColorBrush"/> class.
/// </summary>
/// <param name="color">The color to use.</param>
/// <param name="opacity">The opacity of the brush.</param>
public ImmutableSolidColorBrush(Color color, double opacity = 1)
{
Color = color;
Opacity = opacity;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableSolidColorBrush"/> class.
/// </summary>
/// <param name="color">The color to use.</param>
public ImmutableSolidColorBrush(uint color)
: this(Color.FromUInt32(color))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableSolidColorBrush"/> class.
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
public ImmutableSolidColorBrush(ISolidColorBrush source)
: this(source.Color, source.Opacity)
{
}
/// <summary>
/// Gets the color of the brush.
/// </summary>
public Color Color { get; }
/// <summary>
/// Gets the opacity of the brush.
/// </summary>
public double Opacity { get; }
/// <summary>
/// Returns a string representation of the brush.
/// </summary>
/// <returns>A string representation of the brush.</returns>
public override string ToString()
{
return Color.ToString();
}
}
}

77
src/Avalonia.Visuals/Media/Immutable/ImmutableTileBrush.cs

@ -0,0 +1,77 @@
using System;
namespace Avalonia.Media.Immutable
{
/// <summary>
/// A brush which displays a repeating image.
/// </summary>
public abstract class ImmutableTileBrush : ITileBrush
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageBrush"/> class.
/// </summary>
/// <param name="alignmentX">The horizontal alignment of a tile in the destination.</param>
/// <param name="alignmentY">The vertical alignment of a tile in the destination.</param>
/// <param name="destinationRect">The rectangle on the destination in which to paint a tile.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="sourceRect">The rectangle of the source image that will be displayed.</param>
/// <param name="stretch">
/// How the source rectangle will be stretched to fill the destination rect.
/// </param>
/// <param name="tileMode">The tile mode.</param>
protected ImmutableTileBrush(
AlignmentX alignmentX,
AlignmentY alignmentY,
RelativeRect destinationRect,
double opacity,
RelativeRect sourceRect,
Stretch stretch,
TileMode tileMode)
{
AlignmentX = alignmentX;
AlignmentY = alignmentY;
DestinationRect = destinationRect;
Opacity = opacity;
SourceRect = sourceRect;
Stretch = stretch;
TileMode = tileMode;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageBrush"/> class.
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
protected ImmutableTileBrush(ITileBrush source)
: this(
source.AlignmentX,
source.AlignmentY,
source.DestinationRect,
source.Opacity,
source.SourceRect,
source.Stretch,
source.TileMode)
{
}
/// <inheritdoc/>
public AlignmentX AlignmentX { get; }
/// <inheritdoc/>
public AlignmentY AlignmentY { get; }
/// <inheritdoc/>
public RelativeRect DestinationRect { get; }
/// <inheritdoc/>
public double Opacity { get; }
/// <inheritdoc/>
public RelativeRect SourceRect { get; }
/// <inheritdoc/>
public Stretch Stretch { get; }
/// <inheritdoc/>
public TileMode TileMode { get; }
}
}

58
src/Avalonia.Visuals/Media/Immutable/ImmutableVisualBrush.cs

@ -0,0 +1,58 @@
using System;
using Avalonia.VisualTree;
namespace Avalonia.Media.Immutable
{
/// <summary>
/// Paints an area with an <see cref="IVisual"/>.
/// </summary>
internal class ImmutableVisualBrush : ImmutableTileBrush, IVisualBrush
{
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableImageBrush"/> class.
/// </summary>
/// <param name="visual">The visual to draw.</param>
/// <param name="alignmentX">The horizontal alignment of a tile in the destination.</param>
/// <param name="alignmentY">The vertical alignment of a tile in the destination.</param>
/// <param name="destinationRect">The rectangle on the destination in which to paint a tile.</param>
/// <param name="opacity">The opacity of the brush.</param>
/// <param name="sourceRect">The rectangle of the source image that will be displayed.</param>
/// <param name="stretch">
/// How the source rectangle will be stretched to fill the destination rect.
/// </param>
/// <param name="tileMode">The tile mode.</param>
public ImmutableVisualBrush(
IVisual visual,
AlignmentX alignmentX = AlignmentX.Center,
AlignmentY alignmentY = AlignmentY.Center,
RelativeRect? destinationRect = null,
double opacity = 1,
RelativeRect? sourceRect = null,
Stretch stretch = Stretch.Uniform,
TileMode tileMode = TileMode.None)
: base(
alignmentX,
alignmentY,
destinationRect ?? RelativeRect.Fill,
opacity,
sourceRect ?? RelativeRect.Fill,
stretch,
tileMode)
{
Visual = visual;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImmutableVisualBrush"/> class.
/// </summary>
/// <param name="source">The brush from which this brush's properties should be copied.</param>
public ImmutableVisualBrush(IVisualBrush source)
: base(source)
{
Visual = source.Visual;
}
/// <inheritdoc/>
public IVisual Visual { get; }
}
}

35
src/Avalonia.Visuals/Media/Mutable/SolidColorBrush.cs

@ -1,35 +0,0 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
namespace Avalonia.Media.Mutable
{
/// <summary>
/// Fills an area with a solid color.
/// </summary>
/// <remarks>
/// This is a mutable version of the normal immutable <see cref="Avalonia.Media.SolidColorBrush"/>
/// for use in XAML. XAML really needs support for immutable data...
/// </remarks>
public class SolidColorBrush : Brush, ISolidColorBrush
{
public static readonly DirectProperty<SolidColorBrush, Color> ColorProperty =
AvaloniaProperty.RegisterDirect<SolidColorBrush, Color>(
"Color",
o => o.Color,
(o, v) => o.Color = v);
/// <summary>
/// Gets the color of the brush.
/// </summary>
public Color Color { get; set; }
/// <summary>
/// Returns a string representation of the brush.
/// </summary>
/// <returns>A string representation of the brush.</returns>
public override string ToString()
{
return Color.ToString();
}
}
}

36
src/Avalonia.Visuals/Media/SolidColorBrush.cs

@ -1,13 +1,28 @@
// Copyright (c) The Avalonia Project. All rights reserved. // Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
namespace Avalonia.Media namespace Avalonia.Media
{ {
/// <summary> /// <summary>
/// Fills an area with a solid color. /// Fills an area with a solid color.
/// </summary> /// </summary>
public class SolidColorBrush : ISolidColorBrush public class SolidColorBrush : Brush, ISolidColorBrush, IMutableBrush
{ {
/// <summary>
/// Defines the <see cref="Color"/> property.
/// </summary>
public static readonly StyledProperty<Color> ColorProperty =
AvaloniaProperty.Register<SolidColorBrush, Color>(nameof(Color));
/// <summary>
/// Initializes a new instance of the <see cref="SolidColorBrush"/> class.
/// </summary>
public SolidColorBrush()
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="SolidColorBrush"/> class. /// Initializes a new instance of the <see cref="SolidColorBrush"/> class.
/// </summary> /// </summary>
@ -29,14 +44,13 @@ namespace Avalonia.Media
} }
/// <summary> /// <summary>
/// Gets the color of the brush. /// Gets or sets the color of the brush.
/// </summary> /// </summary>
public Color Color { get; } public Color Color
{
/// <summary> get { return GetValue(ColorProperty); }
/// Gets the opacity of the brush. set { SetValue(ColorProperty, value); }
/// </summary> }
public double Opacity { get; }
/// <summary> /// <summary>
/// Returns a string representation of the brush. /// Returns a string representation of the brush.
@ -46,5 +60,11 @@ namespace Avalonia.Media
{ {
return Color.ToString(); return Color.ToString();
} }
/// <inheritdoc/>
IBrush IMutableBrush.ToImmutable()
{
return new Immutable.ImmutableSolidColorBrush(this);
}
} }
} }

1
src/Avalonia.Visuals/Properties/AssemblyInfo.cs

@ -7,4 +7,3 @@ using Avalonia.Metadata;
[assembly: AssemblyTitle("Avalonia.Visuals")] [assembly: AssemblyTitle("Avalonia.Visuals")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Animation")] [assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Animation")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Media")] [assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Media")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui/mutable", "Avalonia.Media.Mutable")]

2
src/Gtk/Avalonia.Cairo/Media/DrawingContext.cs

@ -294,7 +294,7 @@ namespace Avalonia.Cairo.Media
private BrushImpl CreateBrushImpl(IBrush brush, Size destinationSize) private BrushImpl CreateBrushImpl(IBrush brush, Size destinationSize)
{ {
var solid = brush as SolidColorBrush; var solid = brush as ISolidColorBrush;
var linearGradientBrush = brush as ILinearGradientBrush; var linearGradientBrush = brush as ILinearGradientBrush;
var radialGradientBrush = brush as IRadialGradientBrush; var radialGradientBrush = brush as IRadialGradientBrush;
var imageBrush = brush as IImageBrush; var imageBrush = brush as IImageBrush;

2
src/Gtk/Avalonia.Cairo/Media/SolidColorBrushImpl.cs

@ -5,7 +5,7 @@ namespace Avalonia.Cairo
{ {
public class SolidColorBrushImpl : BrushImpl public class SolidColorBrushImpl : BrushImpl
{ {
public SolidColorBrushImpl(Avalonia.Media.SolidColorBrush brush, double opacityOverride = 1.0f) public SolidColorBrushImpl(Avalonia.Media.ISolidColorBrush brush, double opacityOverride = 1.0f)
{ {
var color = brush?.Color.ToCairo() ?? new Color(); var color = brush?.Color.ToCairo() ?? new Color();

9
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/Style2.xaml

@ -1,9 +1,8 @@
<Style xmlns="https://github.com/avaloniaui" <Style xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:mut="https://github.com/avaloniaui/mutable">
<Style.Resources> <Style.Resources>
<mut:SolidColorBrush x:Key="RedBrush" Color="{StyleResource Red}"/> <SolidColorBrush x:Key="RedBrush" Color="{StyleResource Red}"/>
<mut:SolidColorBrush x:Key="GreenBrush" Color="{StyleResource Green}"/> <SolidColorBrush x:Key="GreenBrush" Color="{StyleResource Green}"/>
<mut:SolidColorBrush x:Key="BlueBrush" Color="{StyleResource Blue}"/> <SolidColorBrush x:Key="BlueBrush" Color="{StyleResource Blue}"/>
</Style.Resources> </Style.Resources>
</Style> </Style>

20
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs

@ -124,13 +124,12 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
{ {
var xaml = @" var xaml = @"
<Window xmlns='https://github.com/avaloniaui' <Window xmlns='https://github.com/avaloniaui'
xmlns:mut='https://github.com/avaloniaui/mutable'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Styles> <Window.Styles>
<Style> <Style>
<Style.Resources> <Style.Resources>
<Color x:Key='color'>#ff506070</Color> <Color x:Key='color'>#ff506070</Color>
<mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/> <SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
</Style.Resources> </Style.Resources>
</Style> </Style>
</Window.Styles> </Window.Styles>
@ -139,12 +138,12 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
var loader = new AvaloniaXamlLoader(); var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml); var window = (Window)loader.Load(xaml);
var brush = (Avalonia.Media.Mutable.SolidColorBrush)window.FindStyleResource("brush"); var brush = (ISolidColorBrush)window.FindStyleResource("brush");
var button = window.FindControl<Button>("button"); var button = window.FindControl<Button>("button");
DelayedBinding.ApplyBindings(button); DelayedBinding.ApplyBindings(button);
var buttonBrush = (Avalonia.Media.Mutable.SolidColorBrush)button.Background; var buttonBrush = (ISolidColorBrush)button.Background;
Assert.Equal(0xff506070, brush.Color.ToUint32()); Assert.Equal(0xff506070, brush.Color.ToUint32());
Assert.Equal(0xff506070, buttonBrush.Color.ToUint32()); Assert.Equal(0xff506070, buttonBrush.Color.ToUint32());
@ -156,19 +155,18 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
{ {
var xaml = @" var xaml = @"
<Styles xmlns='https://github.com/avaloniaui' <Styles xmlns='https://github.com/avaloniaui'
xmlns:mut='https://github.com/avaloniaui/mutable'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Style> <Style>
<Style.Resources> <Style.Resources>
<Color x:Key='color'>#ff506070</Color> <Color x:Key='color'>#ff506070</Color>
<mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/> <SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
</Style.Resources> </Style.Resources>
</Style> </Style>
</Styles>"; </Styles>";
var loader = new AvaloniaXamlLoader(); var loader = new AvaloniaXamlLoader();
var styles = (Styles)loader.Load(xaml); var styles = (Styles)loader.Load(xaml);
var brush = (Avalonia.Media.Mutable.SolidColorBrush)styles.FindResource("brush"); var brush = (ISolidColorBrush)styles.FindResource("brush");
Assert.Equal(0xff506070, brush.Color.ToUint32()); Assert.Equal(0xff506070, brush.Color.ToUint32());
} }
@ -178,7 +176,6 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
{ {
var xaml = @" var xaml = @"
<Styles xmlns='https://github.com/avaloniaui' <Styles xmlns='https://github.com/avaloniaui'
xmlns:mut='https://github.com/avaloniaui/mutable'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Style> <Style>
<Style.Resources> <Style.Resources>
@ -187,14 +184,14 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
</Style> </Style>
<Style> <Style>
<Style.Resources> <Style.Resources>
<mut:SolidColorBrush x:Key='brush' Color='{StyleResource color}'/> <SolidColorBrush x:Key='brush' Color='{StyleResource color}'/>
</Style.Resources> </Style.Resources>
</Style> </Style>
</Styles>"; </Styles>";
var loader = new AvaloniaXamlLoader(); var loader = new AvaloniaXamlLoader();
var styles = (Styles)loader.Load(xaml); var styles = (Styles)loader.Load(xaml);
var brush = (Avalonia.Media.Mutable.SolidColorBrush)styles.FindResource("brush"); var brush = (ISolidColorBrush)styles.FindResource("brush");
Assert.Equal(0xff506070, brush.Color.ToUint32()); Assert.Equal(0xff506070, brush.Color.ToUint32());
} }
@ -206,7 +203,6 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
{ {
var xaml = @" var xaml = @"
<Window xmlns='https://github.com/avaloniaui' <Window xmlns='https://github.com/avaloniaui'
xmlns:mut='https://github.com/avaloniaui/mutable'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Styles> <Window.Styles>
<StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style1.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/> <StyleInclude Source='resm:Avalonia.Markup.Xaml.UnitTests.Xaml.Style1.xaml?assembly=Avalonia.Markup.Xaml.UnitTests'/>
@ -218,7 +214,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
var loader = new AvaloniaXamlLoader(); var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml); var window = (Window)loader.Load(xaml);
var border = window.FindControl<Border>("border"); var border = window.FindControl<Border>("border");
var borderBrush = (Avalonia.Media.Mutable.SolidColorBrush)border.Background; var borderBrush = (ISolidColorBrush)border.Background;
Assert.NotNull(borderBrush); Assert.NotNull(borderBrush);
Assert.Equal(0xffff0000, borderBrush.Color.ToUint32()); Assert.Equal(0xffff0000, borderBrush.Color.ToUint32());

Loading…
Cancel
Save