ColorScheme.Colors.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #nullable enable
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Text.Json.Serialization;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// Holds the <see cref="ColorScheme"/>s that define the <see cref="Attribute"/>s that are used by views to render
  8. /// themselves.
  9. /// </summary>
  10. public sealed class Colors : INotifyCollectionChanged, IDictionary<string, ColorScheme?>
  11. {
  12. static Colors ()
  13. {
  14. ColorSchemes = new (5, StringComparer.InvariantCultureIgnoreCase);
  15. Reset ();
  16. }
  17. /// <summary>Gets a dictionary of defined <see cref="ColorScheme"/> objects.</summary>
  18. /// <remarks>
  19. /// <para>
  20. /// The <see cref="ColorSchemes"/> dictionary includes the following keys, by default:
  21. /// <list type="table">
  22. /// <listheader>
  23. /// <term>Built-in Color Scheme</term> <description>Description</description>
  24. /// </listheader>
  25. /// <item>
  26. /// <term>Base</term> <description>The base color scheme used for most Views.</description>
  27. /// </item>
  28. /// <item>
  29. /// <term>TopLevel</term>
  30. /// <description>The application Toplevel color scheme; used for the <see cref="Toplevel"/> View.</description>
  31. /// </item>
  32. /// <item>
  33. /// <term>Dialog</term>
  34. /// <description>
  35. /// The dialog color scheme; used for <see cref="Dialog"/>, <see cref="MessageBox"/>, and
  36. /// other views dialog-like views.
  37. /// </description>
  38. /// </item>
  39. /// <item>
  40. /// <term>Menu</term>
  41. /// <description>
  42. /// The menu color scheme; used for <see cref="MenuBar"/>, <see cref="ContextMenu"/>, and
  43. /// <see cref="StatusBar"/>.
  44. /// </description>
  45. /// </item>
  46. /// <item>
  47. /// <term>Error</term>
  48. /// <description>
  49. /// The color scheme for showing errors, such as in
  50. /// <see cref="MessageBox.ErrorQuery(string, string, string[])"/>.
  51. /// </description>
  52. /// </item>
  53. /// </list>
  54. /// </para>
  55. /// <para>Changing the values of an entry in this dictionary will affect all views that use the scheme.</para>
  56. /// <para>
  57. /// <see cref="ConfigurationManager"/> can be used to override the default values for these schemes and add
  58. /// additional schemes. See <see cref="ConfigurationManager.Themes"/>.
  59. /// </para>
  60. /// </remarks>
  61. [SerializableConfigurationProperty (Scope = typeof (ThemeScope), OmitClassName = true)]
  62. [JsonConverter (typeof (DictionaryJsonConverter<ColorScheme?>))]
  63. [UsedImplicitly]
  64. public static Dictionary<string, ColorScheme?> ColorSchemes { get; private set; }
  65. /// <inheritdoc/>
  66. public IEnumerator<KeyValuePair<string, ColorScheme?>> GetEnumerator () { return ColorSchemes.GetEnumerator (); }
  67. /// <inheritdoc/>
  68. IEnumerator IEnumerable.GetEnumerator () { return GetEnumerator (); }
  69. /// <inheritdoc/>
  70. public void Add (KeyValuePair<string, ColorScheme?> item)
  71. {
  72. ColorSchemes.Add (item.Key, item.Value);
  73. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, item));
  74. }
  75. /// <inheritdoc/>
  76. public void Clear ()
  77. {
  78. ColorSchemes.Clear ();
  79. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Reset));
  80. }
  81. /// <inheritdoc/>
  82. public bool Contains (KeyValuePair<string, ColorScheme?> item) { return ColorSchemes.Contains (item); }
  83. /// <inheritdoc/>
  84. public void CopyTo (KeyValuePair<string, ColorScheme?> [] array, int arrayIndex) { ((ICollection)ColorSchemes).CopyTo (array, arrayIndex); }
  85. /// <inheritdoc/>
  86. public bool Remove (KeyValuePair<string, ColorScheme?> item)
  87. {
  88. if (ColorSchemes.Remove (item.Key))
  89. {
  90. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, item));
  91. return true;
  92. }
  93. return false;
  94. }
  95. /// <inheritdoc/>
  96. public int Count => ColorSchemes.Count;
  97. /// <inheritdoc/>
  98. public bool IsReadOnly => false;
  99. /// <inheritdoc/>
  100. public void Add (string key, ColorScheme? value) { Add (new (key, value)); }
  101. /// <inheritdoc/>
  102. public bool ContainsKey (string key) { return ColorSchemes.ContainsKey (key); }
  103. /// <inheritdoc/>
  104. public bool Remove (string key)
  105. {
  106. if (ColorSchemes.Remove (key))
  107. {
  108. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, key));
  109. return true;
  110. }
  111. return false;
  112. }
  113. /// <inheritdoc/>
  114. public bool TryGetValue (string key, out ColorScheme? value) { return ColorSchemes.TryGetValue (key, out value); }
  115. /// <inheritdoc/>
  116. public ColorScheme? this [string key]
  117. {
  118. get => ColorSchemes [key];
  119. set
  120. {
  121. if (ColorSchemes.TryAdd (key, value))
  122. {
  123. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, new KeyValuePair<string, ColorScheme?> (key, value)));
  124. }
  125. else
  126. {
  127. ColorScheme? oldValue = ColorSchemes [key];
  128. ColorSchemes [key] = value;
  129. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Replace, value, oldValue));
  130. }
  131. }
  132. }
  133. /// <inheritdoc/>
  134. public ICollection<string> Keys => ColorSchemes.Keys;
  135. /// <inheritdoc/>
  136. public ICollection<ColorScheme?> Values => ColorSchemes.Values;
  137. /// <inheritdoc/>
  138. public event NotifyCollectionChangedEventHandler? CollectionChanged;
  139. /// <summary>Resets the <see cref="ColorSchemes"/> dictionary to the default values.</summary>
  140. public static Dictionary<string, ColorScheme?> Reset ()
  141. {
  142. ColorSchemes.Clear ();
  143. ColorSchemes.Add ("TopLevel", new ());
  144. ColorSchemes.Add ("Base", new ());
  145. ColorSchemes.Add ("Dialog", new ());
  146. ColorSchemes.Add ("Menu", new ());
  147. ColorSchemes.Add ("Error", new ());
  148. return ColorSchemes;
  149. }
  150. }