SchemeManager.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #nullable enable
  2. using System.Collections.Immutable;
  3. using System.Diagnostics;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Text.Json.Serialization;
  6. namespace Terminal.Gui.Configuration;
  7. /// <summary>
  8. /// Holds the <see cref="Drawing.Scheme"/>s that define the <see cref="System.Attribute"/>s that are used by views to render
  9. /// themselves. A Scheme is a mapping from <see cref="Drawing.VisualRole"/>s (such as <see cref="Drawing.VisualRole.Focus"/>) to <see cref="System.Attribute"/>s.
  10. /// A Scheme defines how a `View` should look based on its purpose (e.g. Menu or Dialog).
  11. /// </summary>
  12. public sealed class SchemeManager// : INotifyCollectionChanged, IDictionary<string, Scheme?>
  13. {
  14. #pragma warning disable IDE1006 // Naming Styles
  15. private static readonly object _schemesLock = new ();
  16. #pragma warning restore IDE1006 // Naming Styles
  17. /// <summary>
  18. /// INTERNAL: Gets the hard-coded schemes defined by <see cref="View"/>. These are not loaded from the configuration files,
  19. /// but are hard-coded in the source code. Used for unit testing when ConfigurationManager is not initialized.
  20. /// </summary>
  21. /// <returns></returns>
  22. internal static ImmutableSortedDictionary<string, Scheme?>? GetHardCodedSchemes () { return Scheme.GetHardCodedSchemes ()!; }
  23. /// <summary>
  24. /// Use <see cref="AddScheme"/>, <see cref="GetScheme(Drawing.Schemes)"/>, <see cref="GetSchemeNames"/>, <see cref="GetSchemesForCurrentTheme"/>, etc... instead.
  25. /// </summary>
  26. [ConfigurationProperty (Scope = typeof (ThemeScope), OmitClassName = true)]
  27. [JsonConverter (typeof (DictionaryJsonConverter<Scheme?>))]
  28. [UsedImplicitly]
  29. public static Dictionary<string, Scheme?>? Schemes
  30. {
  31. get => GetSchemes ();
  32. [RequiresUnreferencedCode ("Calls Terminal.Gui.SchemeManager.SetSchemes(Dictionary<String, Scheme>)")]
  33. [RequiresDynamicCode ("Calls Terminal.Gui.SchemeManager.SetSchemes(Dictionary<String, Scheme>)")]
  34. private set => SetSchemes (value);
  35. }
  36. /// <summary>INTERNAL: Gets the dictionary of defined <see cref="Scheme"/>s. The get method for <see cref="Schemes"/>.</summary>
  37. internal static Dictionary<string, Scheme?> GetSchemes ()
  38. {
  39. if (!ConfigurationManager.IsInitialized ())
  40. {
  41. // We're being called from the module initializer.
  42. // Hard coded default value
  43. return GetHardCodedSchemes ()!.ToDictionary (StringComparer.InvariantCultureIgnoreCase);
  44. }
  45. return GetSchemesForCurrentTheme ();
  46. }
  47. /// <summary>INTERNAL: The set method for <see cref="Schemes"/>.</summary>
  48. [RequiresUnreferencedCode ("Calls Terminal.Gui.ConfigProperty.UpdateFrom(Object)")]
  49. [RequiresDynamicCode ("Calls Terminal.Gui.ConfigProperty.UpdateFrom(Object)")]
  50. private static void SetSchemes (Dictionary<string, Scheme?>? value)
  51. {
  52. lock (_schemesLock)
  53. {
  54. if (!ConfigurationManager.IsInitialized ())
  55. {
  56. throw new InvalidOperationException ("Schemes cannot be set before ConfigurationManager is initialized.");
  57. }
  58. Debug.Assert (value is { });
  59. // Update the backing store
  60. ThemeManager.GetCurrentTheme () ["Schemes"].UpdateFrom (value);
  61. }
  62. //Instance.OnThemeChanged (prevousValue);
  63. }
  64. /// <summary>
  65. /// Adds a new <see cref="Scheme"/> to <see cref="SchemeManager"/>. If the Scheme has already been added,
  66. /// it will be updated to <paramref name="scheme"/>.
  67. /// </summary>
  68. /// <param name="schemeName">The name of the Scheme. This must be unique.</param>
  69. /// <param name="scheme"></param>
  70. /// <returns></returns>
  71. public static void AddScheme (string schemeName, Scheme scheme)
  72. {
  73. if (!GetSchemes ()!.TryAdd (schemeName, scheme))
  74. {
  75. GetSchemes () [schemeName] = scheme;
  76. }
  77. }
  78. /// <summary>
  79. /// Removes a Scheme from <see cref="SchemeManager"/>.
  80. /// </summary>
  81. /// <param name="schemeName"></param>
  82. /// <exception cref="InvalidOperationException">If the scheme is a built-in Scheme or was not previously added.</exception>
  83. public static void RemoveScheme (string schemeName)
  84. {
  85. if (SchemeNameToSchemes (schemeName) is { })
  86. {
  87. throw new InvalidOperationException ($@"{schemeName}: Cannot remove a built-in Scheme.");
  88. }
  89. if (!GetSchemes ().TryGetValue (schemeName, out _))
  90. {
  91. throw new InvalidOperationException ($@"{schemeName}: Does not exist in Schemes.");
  92. }
  93. GetSchemes ().Remove (schemeName);
  94. }
  95. /// <summary>
  96. /// Gets the <see cref="Scheme"/> for the specified <see cref="Drawing.Schemes"/>.
  97. /// </summary>
  98. /// <param name="schemeName"></param>
  99. /// <returns></returns>
  100. /// <exception cref="ArgumentException"></exception>
  101. public static Scheme GetScheme (Schemes schemeName)
  102. {
  103. // Convert schemeName to string via Enum api
  104. string? schemeNameString = SchemesToSchemeName (schemeName);
  105. if (schemeNameString is null)
  106. {
  107. throw new ArgumentException ($"Invalid scheme name: {schemeName}");
  108. }
  109. return GetSchemesForCurrentTheme ()! [schemeNameString]!;
  110. }
  111. /// <summary>
  112. /// Gets the <see cref="Scheme"/> for the specified string.
  113. /// </summary>
  114. /// <param name="schemeName"></param>
  115. /// <returns></returns>
  116. /// <exception cref="ArgumentException"></exception>
  117. public static Scheme GetScheme (string schemeName)
  118. {
  119. return GetSchemesForCurrentTheme ()! [schemeName]!;
  120. }
  121. /// <summary>
  122. /// Gets the name of the specified <see cref="Schemes"/>. Will throw an exception if <paramref name="schemeName"/>
  123. /// is not a built-in Scheme.
  124. /// </summary>
  125. /// <param name="schemeName"></param>
  126. /// <returns>The name of scheme.</returns>
  127. public static string? SchemesToSchemeName (Schemes schemeName)
  128. {
  129. return Enum.GetName (typeof (Schemes), schemeName);
  130. }
  131. /// <summary>
  132. /// Converts a string to a <see cref="Schemes"/> enum value.
  133. /// </summary>
  134. /// <param name="schemeName"><see langword="null"/> if the schemeName is not a built-in Scheme name.</param>
  135. /// <returns><see langword="null"/> if <paramref name="schemeName"/> is not the name of a built-in Scheme.</returns>
  136. public static string? SchemeNameToSchemes (string schemeName)
  137. {
  138. if (Enum.TryParse (typeof (Schemes), schemeName, out object? value))
  139. {
  140. return value?.ToString ();
  141. }
  142. return null;
  143. }
  144. /// <summary>
  145. /// Get the dictionary schemes from the selected theme loaded from configuration.
  146. /// </summary>
  147. /// <returns></returns>
  148. public static Dictionary<string, Scheme?> GetSchemesForCurrentTheme ()
  149. {
  150. lock (_schemesLock)
  151. {
  152. if (!ConfigurationManager.IsInitialized ())
  153. {
  154. throw new InvalidOperationException ("CM Must be Initialized");
  155. }
  156. if (ThemeManager.GetCurrentTheme () ["Schemes"].PropertyValue is not Dictionary<string, Scheme?> schemes)
  157. {
  158. // Most likely because "Schemes": was left out of the config
  159. throw new InvalidOperationException ("Current Theme does not have a Scheme.");
  160. }
  161. return schemes!;
  162. }
  163. }
  164. /// <summary>
  165. /// Convenience method to get the names of the schemes.
  166. /// </summary>
  167. /// <returns></returns>
  168. public static ImmutableList<string> GetSchemeNames ()
  169. {
  170. lock (_schemesLock)
  171. {
  172. return GetSchemes ()!.Keys.ToImmutableList ();
  173. }
  174. }
  175. }