Brush.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.ComponentModel;
  3. using Avalonia.Animation;
  4. using Avalonia.Animation.Animators;
  5. using Avalonia.Media.Immutable;
  6. using Avalonia.Reactive;
  7. namespace Avalonia.Media
  8. {
  9. /// <summary>
  10. /// Describes how an area is painted.
  11. /// </summary>
  12. [TypeConverter(typeof(BrushConverter))]
  13. public abstract class Brush : Animatable
  14. {
  15. /// <summary>
  16. /// Defines the <see cref="Opacity"/> property.
  17. /// </summary>
  18. public static readonly StyledProperty<double> OpacityProperty =
  19. AvaloniaProperty.Register<Brush, double>(nameof(Opacity), 1.0);
  20. /// <summary>
  21. /// Defines the <see cref="Transform"/> property.
  22. /// </summary>
  23. public static readonly StyledProperty<ITransform?> TransformProperty =
  24. AvaloniaProperty.Register<Brush, ITransform?>(nameof(Transform));
  25. /// <summary>
  26. /// Defines the <see cref="TransformOrigin"/> property
  27. /// </summary>
  28. public static readonly StyledProperty<RelativePoint> TransformOriginProperty =
  29. AvaloniaProperty.Register<Brush, RelativePoint>(nameof(TransformOrigin));
  30. /// <inheritdoc/>
  31. public event EventHandler? Invalidated;
  32. static Brush()
  33. {
  34. Animation.Animation.RegisterAnimator<BaseBrushAnimator>(prop => typeof(IBrush).IsAssignableFrom(prop.PropertyType));
  35. AffectsRender<Brush>(OpacityProperty, TransformProperty);
  36. }
  37. /// <summary>
  38. /// Gets or sets the opacity of the brush.
  39. /// </summary>
  40. public double Opacity
  41. {
  42. get { return GetValue(OpacityProperty); }
  43. set { SetValue(OpacityProperty, value); }
  44. }
  45. /// <summary>
  46. /// Gets or sets the transform of the brush.
  47. /// </summary>
  48. public ITransform? Transform
  49. {
  50. get { return GetValue(TransformProperty); }
  51. set { SetValue(TransformProperty, value); }
  52. }
  53. /// <summary>
  54. /// Gets or sets the origin of the brush <see cref="Transform"/>
  55. /// </summary>
  56. public RelativePoint TransformOrigin
  57. {
  58. get => GetValue(TransformOriginProperty);
  59. set => SetValue(TransformOriginProperty, value);
  60. }
  61. /// <summary>
  62. /// Parses a brush string.
  63. /// </summary>
  64. /// <param name="s">The brush string.</param>
  65. /// <returns>The <see cref="Color"/>.</returns>
  66. public static IBrush Parse(string s)
  67. {
  68. _ = s ?? throw new ArgumentNullException(nameof(s));
  69. if (s.Length > 0)
  70. {
  71. if (s[0] == '#')
  72. {
  73. return new ImmutableSolidColorBrush(Color.Parse(s));
  74. }
  75. var brush = KnownColors.GetKnownBrush(s);
  76. if (brush != null)
  77. {
  78. return brush;
  79. }
  80. }
  81. throw new FormatException($"Invalid brush string: '{s}'.");
  82. }
  83. /// <summary>
  84. /// Marks a property as affecting the brush's visual representation.
  85. /// </summary>
  86. /// <param name="properties">The properties.</param>
  87. /// <remarks>
  88. /// After a call to this method in a brush's static constructor, any change to the
  89. /// property will cause the <see cref="Invalidated"/> event to be raised on the brush.
  90. /// </remarks>
  91. protected static void AffectsRender<T>(params AvaloniaProperty[] properties)
  92. where T : Brush
  93. {
  94. static void Invalidate(AvaloniaPropertyChangedEventArgs e)
  95. {
  96. (e.Sender as T)?.RaiseInvalidated(EventArgs.Empty);
  97. }
  98. foreach (var property in properties)
  99. {
  100. property.Changed.Subscribe(e => Invalidate(e));
  101. }
  102. }
  103. /// <summary>
  104. /// Raises the <see cref="Invalidated"/> event.
  105. /// </summary>
  106. /// <param name="e">The event args.</param>
  107. protected void RaiseInvalidated(EventArgs e) => Invalidated?.Invoke(this, e);
  108. }
  109. }