Style.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reactive.Linq;
  6. using Avalonia.Controls;
  7. using Avalonia.Metadata;
  8. namespace Avalonia.Styling
  9. {
  10. /// <summary>
  11. /// Defines a style.
  12. /// </summary>
  13. public class Style : IStyle
  14. {
  15. private static Dictionary<IStyleable, List<IDisposable>> _applied =
  16. new Dictionary<IStyleable, List<IDisposable>>();
  17. private IResourceDictionary _resources;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="Style"/> class.
  20. /// </summary>
  21. public Style()
  22. {
  23. }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="Style"/> class.
  26. /// </summary>
  27. /// <param name="selector">The style selector.</param>
  28. public Style(Func<Selector, Selector> selector)
  29. {
  30. Selector = selector(null);
  31. }
  32. /// <summary>
  33. /// Gets or sets a dictionary of style resources.
  34. /// </summary>
  35. public IResourceDictionary Resources
  36. {
  37. get
  38. {
  39. if (_resources == null)
  40. {
  41. _resources = new ResourceDictionary();
  42. }
  43. return _resources;
  44. }
  45. }
  46. /// <summary>
  47. /// Gets or sets the style's selector.
  48. /// </summary>
  49. public Selector Selector { get; set; }
  50. /// <summary>
  51. /// Gets or sets the style's setters.
  52. /// </summary>
  53. [Content]
  54. public IList<ISetter> Setters { get; set; } = new List<ISetter>();
  55. /// <summary>
  56. /// Attaches the style to a control if the style's selector matches.
  57. /// </summary>
  58. /// <param name="control">The control to attach to.</param>
  59. /// <param name="container">
  60. /// The control that contains this style. May be null.
  61. /// </param>
  62. public void Attach(IStyleable control, IStyleHost container)
  63. {
  64. if (Selector != null)
  65. {
  66. var match = Selector.Match(control);
  67. if (match.ImmediateResult != false)
  68. {
  69. var subs = GetSubscriptions(control);
  70. foreach (var setter in Setters)
  71. {
  72. var sub = setter.Apply(this, control, match.ObservableResult);
  73. subs.Add(sub);
  74. }
  75. }
  76. }
  77. else if (control == container)
  78. {
  79. var subs = GetSubscriptions(control);
  80. foreach (var setter in Setters)
  81. {
  82. var sub = setter.Apply(this, control, null);
  83. subs.Add(sub);
  84. }
  85. }
  86. }
  87. /// <inheritdoc/>
  88. public bool TryGetResource(string key, out object result)
  89. {
  90. result = null;
  91. return _resources?.TryGetResource(key, out result) ?? false;
  92. }
  93. /// <summary>
  94. /// Returns a string representation of the style.
  95. /// </summary>
  96. /// <returns>A string representation of the style.</returns>
  97. public override string ToString()
  98. {
  99. if (Selector != null)
  100. {
  101. return "Style: " + Selector.ToString();
  102. }
  103. else
  104. {
  105. return "Style";
  106. }
  107. }
  108. private static List<IDisposable> GetSubscriptions(IStyleable control)
  109. {
  110. List<IDisposable> subscriptions;
  111. if (!_applied.TryGetValue(control, out subscriptions))
  112. {
  113. subscriptions = new List<IDisposable>(2);
  114. subscriptions.Add(control.StyleDetach.Subscribe(ControlDetach));
  115. _applied.Add(control, subscriptions);
  116. }
  117. return subscriptions;
  118. }
  119. /// <summary>
  120. /// Called when a control's <see cref="IStyleable.StyleDetach"/> is signalled to remove
  121. /// all applied styles.
  122. /// </summary>
  123. /// <param name="control">The control.</param>
  124. private static void ControlDetach(IStyleable control)
  125. {
  126. var subscriptions = _applied[control];
  127. foreach (var subscription in subscriptions)
  128. {
  129. subscription.Dispose();
  130. }
  131. _applied.Remove(control);
  132. }
  133. }
  134. }