ColorScheme.Colors.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #nullable enable
  2. using System.Globalization;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Holds the <see cref="ColorScheme"/>s that define the <see cref="Attribute"/>s that are used by views to render
  7. /// themselves.
  8. /// </summary>
  9. public static class Colors
  10. {
  11. static Colors ()
  12. {
  13. Reset ();
  14. }
  15. /// <summary>Gets a dictionary of defined <see cref="ColorScheme"/> objects.</summary>
  16. /// <remarks>
  17. /// <para>
  18. /// The <see cref="ColorSchemes"/> dictionary includes the following keys, by default:
  19. /// <list type="table">
  20. /// <listheader>
  21. /// <term>Built-in Color Scheme</term> <description>Description</description>
  22. /// </listheader>
  23. /// <item>
  24. /// <term>Base</term> <description>The base color scheme used for most Views.</description>
  25. /// </item>
  26. /// <item>
  27. /// <term>TopLevel</term>
  28. /// <description>The application Toplevel color scheme; used for the <see cref="Toplevel"/> View.</description>
  29. /// </item>
  30. /// <item>
  31. /// <term>Dialog</term>
  32. /// <description>
  33. /// The dialog color scheme; used for <see cref="Dialog"/>, <see cref="MessageBox"/>, and
  34. /// other views dialog-like views.
  35. /// </description>
  36. /// </item>
  37. /// <item>
  38. /// <term>Menu</term>
  39. /// <description>
  40. /// The menu color scheme; used for <see cref="MenuBar"/>, <see cref="ContextMenu"/>, and
  41. /// <see cref="StatusBar"/>.
  42. /// </description>
  43. /// </item>
  44. /// <item>
  45. /// <term>Error</term>
  46. /// <description>
  47. /// The color scheme for showing errors, such as in
  48. /// <see cref="MessageBox.ErrorQuery(string, string, string[])"/>.
  49. /// </description>
  50. /// </item>
  51. /// </list>
  52. /// </para>
  53. /// <para>Changing the values of an entry in this dictionary will affect all views that use the scheme.</para>
  54. /// <para>
  55. /// <see cref="ConfigurationManager"/> can be used to override the default values for these schemes and add
  56. /// additional schemes. See <see cref="ConfigurationManager.Themes"/>.
  57. /// </para>
  58. /// </remarks>
  59. [SerializableConfigurationProperty (Scope = typeof (ThemeScope), OmitClassName = true)]
  60. [JsonConverter (typeof (DictionaryJsonConverter<ColorScheme>))]
  61. public static Dictionary<string, ColorScheme>
  62. ColorSchemes { get; private set; } // Serialization requires this to have a setter (private set;)
  63. /// <summary>Resets the <see cref="ColorSchemes"/> dictionary to the default values.</summary>
  64. public static Dictionary<string, ColorScheme> Reset ()
  65. {
  66. ColorSchemes ??= new Dictionary<string, ColorScheme> (
  67. 5,
  68. CultureInfo.InvariantCulture.CompareInfo
  69. .GetStringComparer (
  70. CompareOptions.IgnoreCase
  71. )
  72. );
  73. ColorSchemes.Clear ();
  74. ColorSchemes.Add ("TopLevel", new ColorScheme ());
  75. ColorSchemes.Add ("Base", new ColorScheme ());
  76. ColorSchemes.Add ("Dialog", new ColorScheme ());
  77. ColorSchemes.Add ("Menu", new ColorScheme ());
  78. ColorSchemes.Add ("Error", new ColorScheme ());
  79. return ColorSchemes;
  80. }
  81. private class SchemeNameComparerIgnoreCase : IEqualityComparer<string>
  82. {
  83. public bool Equals (string x, string y)
  84. {
  85. if (x is { } && y is { })
  86. {
  87. return string.Equals (x, y, StringComparison.InvariantCultureIgnoreCase);
  88. }
  89. return false;
  90. }
  91. public int GetHashCode (string obj) { return obj.ToLowerInvariant ().GetHashCode (); }
  92. }
  93. }