View.Drawing.Scheme.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui.ViewBase;
  4. public partial class View
  5. {
  6. private string? _schemeName;
  7. /// <summary>
  8. /// Gets or sets the name of the Scheme to use for this View. If set, it will override the scheme inherited from the
  9. /// SuperView. If a Scheme was explicitly set (<see cref="HasScheme"/> is <see langword="true"/>),
  10. /// this property will be ignored.
  11. /// </summary>
  12. public string? SchemeName
  13. {
  14. get => _schemeName;
  15. set
  16. {
  17. if (_schemeName == value)
  18. {
  19. return;
  20. }
  21. if (OnSettingSchemeName (in _schemeName, ref value))
  22. {
  23. _schemeName = value;
  24. return;
  25. }
  26. StringPropertyEventArgs args = new (in _schemeName, ref value);
  27. SettingSchemeName?.Invoke (this, args);
  28. if (args.Cancel)
  29. {
  30. _schemeName = args.NewString;
  31. return;
  32. }
  33. _schemeName = value;
  34. }
  35. }
  36. /// <summary>
  37. /// Called when the <see cref="Scheme"/> for the View is to be set.
  38. /// </summary>
  39. /// <param name="currentName"></param>
  40. /// <param name="newName"></param>
  41. /// <returns><see langword="true"/> to stop default behavior.</returns>
  42. protected virtual bool OnSettingSchemeName (in string? currentName, ref string? newName) { return false; }
  43. /// <summary>Raised when the <see cref="Scheme"/> for the View is to be set.</summary>
  44. /// <returns>
  45. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop default behavior.
  46. /// </returns>
  47. public event EventHandler<StringPropertyEventArgs>? SettingSchemeName;
  48. // Both holds the set Scheme and is used to determine if a Scheme has been set or not
  49. private Scheme? _scheme;
  50. /// <summary>
  51. /// Gets whether a Scheme has been explicitly set for this View, or if it will inherit the Scheme from its
  52. /// <see cref="SuperView"/>.
  53. /// </summary>
  54. public bool HasScheme => _scheme is { };
  55. /// <summary>
  56. /// Gets the Scheme for the View. If the Scheme has not been explicitly set (see <see cref="HasScheme"/>), gets
  57. /// <see cref="SuperView"/>'s Scheme.
  58. /// </summary>
  59. /// <returns></returns>
  60. public Scheme GetScheme ()
  61. {
  62. if (OnGettingScheme (out Scheme? newScheme))
  63. {
  64. return newScheme!;
  65. }
  66. var args = new SchemeEventArgs (in _scheme, ref newScheme);
  67. GettingScheme?.Invoke (this, args);
  68. if (args.Cancel)
  69. {
  70. return args.NewScheme!;
  71. }
  72. if (!HasScheme && !string.IsNullOrEmpty (SchemeName))
  73. {
  74. return SchemeManager.GetScheme (SchemeName);
  75. }
  76. if (!HasScheme)
  77. {
  78. return SuperView?.GetScheme () ?? SchemeManager.GetScheme (Schemes.Base);
  79. }
  80. return _scheme!;
  81. }
  82. /// <summary>
  83. /// Called when the <see cref="Scheme"/> for the View is being retrieved. Overrides can return <see langword="true"/>
  84. /// to
  85. /// stop further processing and optionally set <paramref name="scheme"/> to a different value.
  86. /// </summary>
  87. /// <returns><see langword="true"/> to stop default behavior.</returns>
  88. protected virtual bool OnGettingScheme (out Scheme? scheme)
  89. {
  90. scheme = null;
  91. return false;
  92. }
  93. /// <summary>
  94. /// Raised when the <see cref="Scheme"/> for the View is being retrieved. Overrides can return <see langword="true"/>
  95. /// to
  96. /// stop further processing and optionally set the <see cref="Scheme"/> in the event args to a different value.
  97. /// </summary>
  98. /// <returns>
  99. /// Set `Cancel` to <see langword="true"/> to stop default behavior.
  100. /// </returns>
  101. public event EventHandler<SchemeEventArgs>? GettingScheme;
  102. /// <summary>
  103. /// Sets the Scheme for the View. Raises <see cref="SettingScheme"/> event before setting the scheme.
  104. /// </summary>
  105. /// <param name="scheme">
  106. /// The scheme to set. If <see langword="null"/> <see cref="HasScheme"/> will be
  107. /// <see langword="false"/>.
  108. /// </param>
  109. /// <returns><see langword="true"/> if the scheme was set.</returns>
  110. public bool SetScheme (Scheme? scheme)
  111. {
  112. if (_scheme == scheme)
  113. {
  114. return false;
  115. }
  116. if (OnSettingScheme (in scheme))
  117. {
  118. return false;
  119. }
  120. var args = new CancelEventArgs ();
  121. SettingScheme?.Invoke (this, args);
  122. if (args.Cancel)
  123. {
  124. return false;
  125. }
  126. _scheme = scheme;
  127. // BUGBUG: This should be in Border.cs somehow
  128. if (Border is { } && Border.LineStyle != LineStyle.None && Border.HasScheme)
  129. {
  130. Border.SetScheme (_scheme);
  131. }
  132. SetNeedsDraw ();
  133. return true;
  134. }
  135. /// <summary>
  136. /// Called when the <see cref="Scheme"/> for the View is to be set.
  137. /// </summary>
  138. /// <param name="scheme"></param>
  139. /// <returns><see langword="true"/> to stop default behavior.</returns>
  140. protected virtual bool OnSettingScheme (in Scheme? scheme) { return false; }
  141. /// <summary>Raised when the <see cref="Scheme"/> for the View is to be set.</summary>
  142. /// <returns>
  143. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop default behavior.
  144. /// </returns>
  145. public event EventHandler<CancelEventArgs>? SettingScheme;
  146. }