ColorScheme.Colors.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }