StyleBase.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Animation;
  4. using Avalonia.Controls;
  5. using Avalonia.Metadata;
  6. using Avalonia.PropertyStore;
  7. using Avalonia.Styling.Activators;
  8. namespace Avalonia.Styling
  9. {
  10. /// <summary>
  11. /// Base class for <see cref="Style"/> and <see cref="ControlTheme"/>.
  12. /// </summary>
  13. public abstract class StyleBase : AvaloniaObject, IStyle, IResourceProvider
  14. {
  15. private IResourceHost? _owner;
  16. private StyleChildren? _children;
  17. private IResourceDictionary? _resources;
  18. private List<ISetter>? _setters;
  19. private List<IAnimation>? _animations;
  20. private StyleInstance? _sharedInstance;
  21. public IList<IStyle> Children => _children ??= new(this);
  22. public IResourceHost? Owner
  23. {
  24. get => _owner;
  25. private set
  26. {
  27. if (_owner != value)
  28. {
  29. _owner = value;
  30. OwnerChanged?.Invoke(this, EventArgs.Empty);
  31. }
  32. }
  33. }
  34. public IStyle? Parent { get; private set; }
  35. public IResourceDictionary Resources
  36. {
  37. get => _resources ?? (Resources = new ResourceDictionary());
  38. set
  39. {
  40. value = value ?? throw new ArgumentNullException(nameof(value));
  41. var hadResources = _resources?.HasResources ?? false;
  42. _resources = value;
  43. if (Owner is object)
  44. {
  45. _resources.AddOwner(Owner);
  46. if (hadResources || _resources.HasResources)
  47. {
  48. Owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
  49. }
  50. }
  51. }
  52. }
  53. public IList<ISetter> Setters => _setters ??= new List<ISetter>();
  54. public IList<IAnimation> Animations => _animations ??= new List<IAnimation>();
  55. bool IResourceNode.HasResources => _resources?.Count > 0;
  56. IReadOnlyList<IStyle> IStyle.Children => (IReadOnlyList<IStyle>?)_children ?? Array.Empty<IStyle>();
  57. internal bool HasChildren => _children?.Count > 0;
  58. internal bool HasSettersOrAnimations => _setters?.Count > 0 || _animations?.Count > 0;
  59. public void Add(ISetter setter) => Setters.Add(setter);
  60. public void Add(IStyle style) => Children.Add(style);
  61. public event EventHandler? OwnerChanged;
  62. public bool TryGetResource(object key, out object? result)
  63. {
  64. if (_resources is not null && _resources.TryGetResource(key, out result))
  65. return true;
  66. if (_children is not null)
  67. {
  68. for (var i = 0; i < _children.Count; ++i)
  69. {
  70. if (_children[i].TryGetResource(key, out result))
  71. return true;
  72. }
  73. }
  74. result= null;
  75. return false;
  76. }
  77. internal abstract SelectorMatchResult TryAttach(IStyleable target, object? host);
  78. internal ValueFrame Attach(IStyleable target, IStyleActivator? activator)
  79. {
  80. if (target is not AvaloniaObject ao)
  81. throw new InvalidOperationException("Styles can only be applied to AvaloniaObjects.");
  82. StyleInstance instance;
  83. if (_sharedInstance is not null)
  84. {
  85. instance = _sharedInstance;
  86. }
  87. else
  88. {
  89. var canShareInstance = activator is null;
  90. instance = new StyleInstance(this, activator);
  91. if (_setters is not null)
  92. {
  93. foreach (var setter in _setters)
  94. {
  95. var setterInstance = setter.Instance(instance, target);
  96. instance.Add(setterInstance);
  97. canShareInstance &= setterInstance == setter;
  98. }
  99. }
  100. if (_animations is not null)
  101. instance.Add(_animations);
  102. if (canShareInstance)
  103. {
  104. instance.MakeShared();
  105. _sharedInstance = instance;
  106. }
  107. }
  108. ao.GetValueStore().AddFrame(instance);
  109. instance.ApplyAnimations(ao);
  110. return instance;
  111. }
  112. internal virtual void SetParent(StyleBase? parent) => Parent = parent;
  113. void IResourceProvider.AddOwner(IResourceHost owner)
  114. {
  115. owner = owner ?? throw new ArgumentNullException(nameof(owner));
  116. if (Owner != null)
  117. {
  118. throw new InvalidOperationException("The Style already has a parent.");
  119. }
  120. Owner = owner;
  121. _resources?.AddOwner(owner);
  122. }
  123. void IResourceProvider.RemoveOwner(IResourceHost owner)
  124. {
  125. owner = owner ?? throw new ArgumentNullException(nameof(owner));
  126. if (Owner == owner)
  127. {
  128. Owner = null;
  129. _resources?.RemoveOwner(owner);
  130. }
  131. }
  132. }
  133. }