Classes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Collections;
  4. using Avalonia.Utilities;
  5. namespace Avalonia.Controls
  6. {
  7. /// <summary>
  8. /// Holds a collection of style classes for an <see cref="StyledElement"/>.
  9. /// </summary>
  10. /// <remarks>
  11. /// Similar to CSS, each control may have any number of styling classes applied.
  12. /// </remarks>
  13. public class Classes : AvaloniaList<string>, IPseudoClasses
  14. {
  15. private SafeEnumerableList<IClassesChangedListener>? _listeners;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="Classes"/> class.
  18. /// </summary>
  19. public Classes()
  20. {
  21. }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="Classes"/> class.
  24. /// </summary>
  25. /// <param name="items">The initial items.</param>
  26. public Classes(IEnumerable<string> items)
  27. : base(items)
  28. {
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="Classes"/> class.
  32. /// </summary>
  33. /// <param name="items">The initial items.</param>
  34. public Classes(params string[] items)
  35. : base(items)
  36. {
  37. }
  38. /// <summary>
  39. /// Gets the number of listeners subscribed to this collection for unit testing purposes.
  40. /// </summary>
  41. internal int ListenerCount => _listeners?.Count ?? 0;
  42. /// <summary>
  43. /// Parses a classes string.
  44. /// </summary>
  45. /// <param name="s">The string.</param>
  46. /// <returns>The <see cref="Classes"/>.</returns>
  47. public static Classes Parse(string s) => new Classes(s.Split(' '));
  48. /// <summary>
  49. /// Adds a style class to the collection.
  50. /// </summary>
  51. /// <param name="name">The class name.</param>
  52. /// <remarks>
  53. /// Only standard classes may be added via this method. To add pseudoclasses (classes
  54. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  55. /// property.
  56. /// </remarks>
  57. public override void Add(string name)
  58. {
  59. ThrowIfPseudoclass(name, "added");
  60. if (!Contains(name))
  61. {
  62. base.Add(name);
  63. NotifyChanged();
  64. }
  65. }
  66. /// <summary>
  67. /// Adds a style classes to the collection.
  68. /// </summary>
  69. /// <param name="names">The class names.</param>
  70. /// <remarks>
  71. /// Only standard classes may be added via this method. To add pseudoclasses (classes
  72. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  73. /// property.
  74. /// </remarks>
  75. public override void AddRange(IEnumerable<string> names)
  76. {
  77. var c = new List<string>();
  78. foreach (var name in names)
  79. {
  80. ThrowIfPseudoclass(name, "added");
  81. if (!Contains(name))
  82. {
  83. c.Add(name);
  84. }
  85. }
  86. base.AddRange(c);
  87. NotifyChanged();
  88. }
  89. /// <summary>
  90. /// Removes all non-pseudoclasses from the collection.
  91. /// </summary>
  92. public override void Clear()
  93. {
  94. for (var i = Count - 1; i >= 0; --i)
  95. {
  96. if (!this[i].StartsWith(":"))
  97. {
  98. RemoveAt(i);
  99. }
  100. }
  101. NotifyChanged();
  102. }
  103. /// <summary>
  104. /// Inserts a style class into the collection.
  105. /// </summary>
  106. /// <param name="index">The index to insert the class at.</param>
  107. /// <param name="name">The class name.</param>
  108. /// <remarks>
  109. /// Only standard classes may be added via this method. To add pseudoclasses (classes
  110. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  111. /// property.
  112. /// </remarks>
  113. public override void Insert(int index, string name)
  114. {
  115. ThrowIfPseudoclass(name, "added");
  116. if (!Contains(name))
  117. {
  118. base.Insert(index, name);
  119. NotifyChanged();
  120. }
  121. }
  122. /// <summary>
  123. /// Inserts style classes into the collection.
  124. /// </summary>
  125. /// <param name="index">The index to insert the class at.</param>
  126. /// <param name="names">The class names.</param>
  127. /// <remarks>
  128. /// Only standard classes may be added via this method. To add pseudoclasses (classes
  129. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  130. /// property.
  131. /// </remarks>
  132. public override void InsertRange(int index, IEnumerable<string> names)
  133. {
  134. List<string>? toInsert = null;
  135. foreach (var name in names)
  136. {
  137. ThrowIfPseudoclass(name, "added");
  138. if (!Contains(name))
  139. {
  140. toInsert ??= new List<string>();
  141. toInsert.Add(name);
  142. }
  143. }
  144. if (toInsert != null)
  145. {
  146. base.InsertRange(index, toInsert);
  147. NotifyChanged();
  148. }
  149. }
  150. /// <summary>
  151. /// Removes a style class from the collection.
  152. /// </summary>
  153. /// <param name="name">The class name.</param>
  154. /// <remarks>
  155. /// Only standard classes may be removed via this method. To remove pseudoclasses (classes
  156. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  157. /// property.
  158. /// </remarks>
  159. public override bool Remove(string name)
  160. {
  161. ThrowIfPseudoclass(name, "removed");
  162. if (base.Remove(name))
  163. {
  164. NotifyChanged();
  165. return true;
  166. }
  167. return false;
  168. }
  169. /// <summary>
  170. /// Removes style classes from the collection.
  171. /// </summary>
  172. /// <param name="names">The class name.</param>
  173. /// <remarks>
  174. /// Only standard classes may be removed via this method. To remove pseudoclasses (classes
  175. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  176. /// property.
  177. /// </remarks>
  178. public override void RemoveAll(IEnumerable<string> names)
  179. {
  180. List<string>? toRemove = null;
  181. foreach (var name in names)
  182. {
  183. ThrowIfPseudoclass(name, "removed");
  184. toRemove ??= new List<string>();
  185. toRemove.Add(name);
  186. }
  187. if (toRemove != null)
  188. {
  189. base.RemoveAll(toRemove);
  190. NotifyChanged();
  191. }
  192. }
  193. /// <summary>
  194. /// Removes a style class from the collection.
  195. /// </summary>
  196. /// <param name="index">The index of the class in the collection.</param>
  197. /// <remarks>
  198. /// Only standard classes may be removed via this method. To remove pseudoclasses (classes
  199. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  200. /// property.
  201. /// </remarks>
  202. public override void RemoveAt(int index)
  203. {
  204. var name = this[index];
  205. ThrowIfPseudoclass(name, "removed");
  206. base.RemoveAt(index);
  207. NotifyChanged();
  208. }
  209. /// <summary>
  210. /// Removes style classes from the collection.
  211. /// </summary>
  212. /// <param name="index">The first index to remove.</param>
  213. /// <param name="count">The number of items to remove.</param>
  214. public override void RemoveRange(int index, int count)
  215. {
  216. base.RemoveRange(index, count);
  217. NotifyChanged();
  218. }
  219. /// <summary>
  220. /// Removes all non-pseudoclasses in the collection and adds a new set.
  221. /// </summary>
  222. /// <param name="source">The new contents of the collection.</param>
  223. public void Replace(IList<string> source)
  224. {
  225. List<string>? toRemove = null;
  226. foreach (var name in source)
  227. {
  228. ThrowIfPseudoclass(name, "added");
  229. }
  230. foreach (var name in this)
  231. {
  232. if (!name.StartsWith(":"))
  233. {
  234. toRemove ??= new List<string>();
  235. toRemove.Add(name);
  236. }
  237. }
  238. if (toRemove != null)
  239. {
  240. base.RemoveAll(toRemove);
  241. }
  242. base.AddRange(source);
  243. NotifyChanged();
  244. }
  245. /// <inheritdoc/>
  246. void IPseudoClasses.Add(string name)
  247. {
  248. if (!Contains(name))
  249. {
  250. base.Add(name);
  251. NotifyChanged();
  252. }
  253. }
  254. /// <inheritdoc/>
  255. bool IPseudoClasses.Remove(string name)
  256. {
  257. if (base.Remove(name))
  258. {
  259. NotifyChanged();
  260. return true;
  261. }
  262. return false;
  263. }
  264. internal void AddListener(IClassesChangedListener listener)
  265. {
  266. (_listeners ??= new()).Add(listener);
  267. }
  268. internal void RemoveListener(IClassesChangedListener listener)
  269. {
  270. _listeners?.Remove(listener);
  271. }
  272. private void NotifyChanged()
  273. {
  274. if (_listeners is null)
  275. return;
  276. foreach (var listener in _listeners)
  277. listener.Changed();
  278. }
  279. private static void ThrowIfPseudoclass(string name, string operation)
  280. {
  281. if (name.StartsWith(":"))
  282. {
  283. throw new ArgumentException(
  284. $"The pseudoclass '{name}' may only be {operation} by the control itself.");
  285. }
  286. }
  287. /// <summary>
  288. /// Adds a or removes a style class to/from the collection.
  289. /// </summary>
  290. /// <param name="name">The class names.</param>
  291. /// <param name="value">If true adds the class, if false, removes it.</param>
  292. /// <remarks>
  293. /// Only standard classes may be added or removed via this method. To add pseudoclasses (classes
  294. /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
  295. /// property.
  296. /// </remarks>
  297. public void Set(string name, bool value)
  298. {
  299. if (value)
  300. {
  301. if (!Contains(name))
  302. Add(name);
  303. }
  304. else
  305. Remove(name);
  306. }
  307. }
  308. }