ColorScheme.Colors.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. public static Dictionary<string, ColorScheme?> ColorSchemes { get; }
  64. /// <inheritdoc/>
  65. public IEnumerator<KeyValuePair<string, ColorScheme?>> GetEnumerator () { return ColorSchemes.GetEnumerator (); }
  66. /// <inheritdoc/>
  67. IEnumerator IEnumerable.GetEnumerator () { return GetEnumerator (); }
  68. /// <inheritdoc/>
  69. public void Add (KeyValuePair<string, ColorScheme?> item)
  70. {
  71. ColorSchemes.Add (item.Key, item.Value);
  72. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, item));
  73. }
  74. /// <inheritdoc/>
  75. public void Clear ()
  76. {
  77. ColorSchemes.Clear ();
  78. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Reset));
  79. }
  80. /// <inheritdoc/>
  81. public bool Contains (KeyValuePair<string, ColorScheme?> item) { return ColorSchemes.Contains (item); }
  82. /// <inheritdoc/>
  83. public void CopyTo (KeyValuePair<string, ColorScheme?> [] array, int arrayIndex) { ((ICollection)ColorSchemes).CopyTo (array, arrayIndex); }
  84. /// <inheritdoc/>
  85. public bool Remove (KeyValuePair<string, ColorScheme?> item)
  86. {
  87. if (ColorSchemes.Remove (item.Key))
  88. {
  89. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, item));
  90. return true;
  91. }
  92. return false;
  93. }
  94. /// <inheritdoc/>
  95. public int Count => ColorSchemes.Count;
  96. /// <inheritdoc/>
  97. public bool IsReadOnly => false;
  98. /// <inheritdoc/>
  99. public void Add (string key, ColorScheme? value) { Add (new (key, value)); }
  100. /// <inheritdoc/>
  101. public bool ContainsKey (string key) { return ColorSchemes.ContainsKey (key); }
  102. /// <inheritdoc/>
  103. public bool Remove (string key)
  104. {
  105. if (ColorSchemes.Remove (key))
  106. {
  107. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Remove, key));
  108. return true;
  109. }
  110. return false;
  111. }
  112. /// <inheritdoc/>
  113. public bool TryGetValue (string key, out ColorScheme? value) { return ColorSchemes.TryGetValue (key, out value); }
  114. /// <inheritdoc/>
  115. public ColorScheme? this [string key]
  116. {
  117. get => ColorSchemes [key];
  118. set
  119. {
  120. if (ColorSchemes.TryAdd (key, value))
  121. {
  122. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Add, new KeyValuePair<string, ColorScheme?> (key, value)));
  123. }
  124. else
  125. {
  126. ColorScheme? oldValue = ColorSchemes [key];
  127. ColorSchemes [key] = value;
  128. CollectionChanged?.Invoke (this, new (NotifyCollectionChangedAction.Replace, value, oldValue));
  129. }
  130. }
  131. }
  132. /// <inheritdoc/>
  133. public ICollection<string> Keys => ColorSchemes.Keys;
  134. /// <inheritdoc/>
  135. public ICollection<ColorScheme?> Values => ColorSchemes.Values;
  136. /// <inheritdoc/>
  137. public event NotifyCollectionChangedEventHandler? CollectionChanged;
  138. /// <summary>Resets the <see cref="ColorSchemes"/> dictionary to the default values.</summary>
  139. public static Dictionary<string, ColorScheme?> Reset ()
  140. {
  141. ColorSchemes.Clear ();
  142. ColorSchemes.Add ("TopLevel", new ());
  143. ColorSchemes.Add ("Base", new ());
  144. ColorSchemes.Add ("Dialog", new ());
  145. ColorSchemes.Add ("Menu", new ());
  146. ColorSchemes.Add ("Error", new ());
  147. return ColorSchemes;
  148. }
  149. }