| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- using System;
- using System.Collections.Generic;
- using Avalonia.Collections;
- using Avalonia.Utilities;
- namespace Avalonia.Controls
- {
- /// <summary>
- /// Holds a collection of style classes for an <see cref="StyledElement"/>.
- /// </summary>
- /// <remarks>
- /// Similar to CSS, each control may have any number of styling classes applied.
- /// </remarks>
- public class Classes : AvaloniaList<string>, IPseudoClasses
- {
- private SafeEnumerableList<IClassesChangedListener>? _listeners;
- /// <summary>
- /// Initializes a new instance of the <see cref="Classes"/> class.
- /// </summary>
- public Classes()
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Classes"/> class.
- /// </summary>
- /// <param name="items">The initial items.</param>
- public Classes(IEnumerable<string> items)
- : base(items)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Classes"/> class.
- /// </summary>
- /// <param name="items">The initial items.</param>
- public Classes(params string[] items)
- : base(items)
- {
- }
- /// <summary>
- /// Gets the number of listeners subscribed to this collection for unit testing purposes.
- /// </summary>
- internal int ListenerCount => _listeners?.Count ?? 0;
- /// <summary>
- /// Parses a classes string.
- /// </summary>
- /// <param name="s">The string.</param>
- /// <returns>The <see cref="Classes"/>.</returns>
- public static Classes Parse(string s) => new Classes(s.Split(' '));
- /// <summary>
- /// Adds a style class to the collection.
- /// </summary>
- /// <param name="name">The class name.</param>
- /// <remarks>
- /// Only standard classes may be added via this method. To add pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public override void Add(string name)
- {
- ThrowIfPseudoclass(name, "added");
- if (!Contains(name))
- {
- base.Add(name);
- NotifyChanged();
- }
- }
- /// <summary>
- /// Adds a style classes to the collection.
- /// </summary>
- /// <param name="names">The class names.</param>
- /// <remarks>
- /// Only standard classes may be added via this method. To add pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public override void AddRange(IEnumerable<string> names)
- {
- var c = new List<string>();
- foreach (var name in names)
- {
- ThrowIfPseudoclass(name, "added");
- if (!Contains(name))
- {
- c.Add(name);
- }
- }
- base.AddRange(c);
- NotifyChanged();
- }
- /// <summary>
- /// Removes all non-pseudoclasses from the collection.
- /// </summary>
- public override void Clear()
- {
- for (var i = Count - 1; i >= 0; --i)
- {
- if (!this[i].StartsWith(":"))
- {
- RemoveAt(i);
- }
- }
- NotifyChanged();
- }
- /// <summary>
- /// Inserts a style class into the collection.
- /// </summary>
- /// <param name="index">The index to insert the class at.</param>
- /// <param name="name">The class name.</param>
- /// <remarks>
- /// Only standard classes may be added via this method. To add pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public override void Insert(int index, string name)
- {
- ThrowIfPseudoclass(name, "added");
- if (!Contains(name))
- {
- base.Insert(index, name);
- NotifyChanged();
- }
- }
- /// <summary>
- /// Inserts style classes into the collection.
- /// </summary>
- /// <param name="index">The index to insert the class at.</param>
- /// <param name="names">The class names.</param>
- /// <remarks>
- /// Only standard classes may be added via this method. To add pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public override void InsertRange(int index, IEnumerable<string> names)
- {
- List<string>? toInsert = null;
- foreach (var name in names)
- {
- ThrowIfPseudoclass(name, "added");
- if (!Contains(name))
- {
- toInsert ??= new List<string>();
- toInsert.Add(name);
- }
- }
- if (toInsert != null)
- {
- base.InsertRange(index, toInsert);
- NotifyChanged();
- }
- }
- /// <summary>
- /// Removes a style class from the collection.
- /// </summary>
- /// <param name="name">The class name.</param>
- /// <remarks>
- /// Only standard classes may be removed via this method. To remove pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public override bool Remove(string name)
- {
- ThrowIfPseudoclass(name, "removed");
- if (base.Remove(name))
- {
- NotifyChanged();
- return true;
- }
- return false;
- }
- /// <summary>
- /// Removes style classes from the collection.
- /// </summary>
- /// <param name="names">The class name.</param>
- /// <remarks>
- /// Only standard classes may be removed via this method. To remove pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public override void RemoveAll(IEnumerable<string> names)
- {
- List<string>? toRemove = null;
- foreach (var name in names)
- {
- ThrowIfPseudoclass(name, "removed");
- toRemove ??= new List<string>();
- toRemove.Add(name);
- }
- if (toRemove != null)
- {
- base.RemoveAll(toRemove);
- NotifyChanged();
- }
- }
- /// <summary>
- /// Removes a style class from the collection.
- /// </summary>
- /// <param name="index">The index of the class in the collection.</param>
- /// <remarks>
- /// Only standard classes may be removed via this method. To remove pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public override void RemoveAt(int index)
- {
- var name = this[index];
- ThrowIfPseudoclass(name, "removed");
- base.RemoveAt(index);
- NotifyChanged();
- }
- /// <summary>
- /// Removes style classes from the collection.
- /// </summary>
- /// <param name="index">The first index to remove.</param>
- /// <param name="count">The number of items to remove.</param>
- public override void RemoveRange(int index, int count)
- {
- base.RemoveRange(index, count);
- NotifyChanged();
- }
- /// <summary>
- /// Removes all non-pseudoclasses in the collection and adds a new set.
- /// </summary>
- /// <param name="source">The new contents of the collection.</param>
- public void Replace(IList<string> source)
- {
- List<string>? toRemove = null;
- foreach (var name in source)
- {
- ThrowIfPseudoclass(name, "added");
- }
- foreach (var name in this)
- {
- if (!name.StartsWith(":"))
- {
- toRemove ??= new List<string>();
- toRemove.Add(name);
- }
- }
- if (toRemove != null)
- {
- base.RemoveAll(toRemove);
- }
- base.AddRange(source);
- NotifyChanged();
- }
- /// <inheritdoc/>
- void IPseudoClasses.Add(string name)
- {
- if (!Contains(name))
- {
- base.Add(name);
- NotifyChanged();
- }
- }
- /// <inheritdoc/>
- bool IPseudoClasses.Remove(string name)
- {
- if (base.Remove(name))
- {
- NotifyChanged();
- return true;
- }
- return false;
- }
- internal void AddListener(IClassesChangedListener listener)
- {
- (_listeners ??= new()).Add(listener);
- }
- internal void RemoveListener(IClassesChangedListener listener)
- {
- _listeners?.Remove(listener);
- }
- private void NotifyChanged()
- {
- if (_listeners is null)
- return;
- foreach (var listener in _listeners)
- listener.Changed();
- }
- private static void ThrowIfPseudoclass(string name, string operation)
- {
- if (name.StartsWith(":"))
- {
- throw new ArgumentException(
- $"The pseudoclass '{name}' may only be {operation} by the control itself.");
- }
- }
- /// <summary>
- /// Adds a or removes a style class to/from the collection.
- /// </summary>
- /// <param name="name">The class names.</param>
- /// <param name="value">If true adds the class, if false, removes it.</param>
- /// <remarks>
- /// Only standard classes may be added or removed via this method. To add pseudoclasses (classes
- /// beginning with a ':' character) use the protected <see cref="StyledElement.PseudoClasses"/>
- /// property.
- /// </remarks>
- public void Set(string name, bool value)
- {
- if (value)
- {
- if (!Contains(name))
- Add(name);
- }
- else
- Remove(name);
- }
- }
- }
|