Adornment.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #nullable enable
  2. using System.ComponentModel;
  3. using Terminal.Gui;
  4. using static Terminal.Gui.SpinnerStyle;
  5. using Attribute = Terminal.Gui.Attribute;
  6. /// <summary>
  7. /// Adornments are a special form of <see cref="View"/> that appear outside the <see cref="View.Viewport"/>:
  8. /// <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>. They are defined using the
  9. /// <see cref="Thickness"/> class, which specifies the thickness of the sides of a rectangle.
  10. /// </summary>
  11. /// <remarsk>
  12. /// <para>
  13. /// Each of <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> has slightly different
  14. /// behavior relative to <see cref="ColorScheme"/>, <see cref="View.SetFocus()"/>, keyboard input, and
  15. /// mouse input. Each can be customized by manipulating their Subviews.
  16. /// </para>
  17. /// </remarsk>
  18. public class Adornment : View, IDesignable
  19. {
  20. /// <inheritdoc/>
  21. public Adornment ()
  22. {
  23. /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  24. }
  25. /// <summary>Constructs a new adornment for the view specified by <paramref name="parent"/>.</summary>
  26. /// <param name="parent"></param>
  27. public Adornment (View parent)
  28. {
  29. // By default Adornments can't get focus; has to be enabled specifically.
  30. CanFocus = false;
  31. TabStop = TabBehavior.NoStop;
  32. Parent = parent;
  33. }
  34. /// <summary>The Parent of this Adornment (the View this Adornment surrounds).</summary>
  35. /// <remarks>
  36. /// Adornments are distinguished from typical View classes in that they are not sub-views, but have a parent/child
  37. /// relationship with their containing View.
  38. /// </remarks>
  39. public View? Parent { get; set; }
  40. #region Thickness
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. public ViewDiagnosticFlags Diagnostics { get; set; } = View.Diagnostics;
  45. private Thickness _thickness = Thickness.Empty;
  46. /// <summary>Defines the rectangle that the <see cref="Adornment"/> will use to draw its content.</summary>
  47. public Thickness Thickness
  48. {
  49. get => _thickness;
  50. set
  51. {
  52. Thickness current = _thickness;
  53. _thickness = value;
  54. if (current != _thickness)
  55. {
  56. Parent?.SetAdornmentFrames ();
  57. SetLayoutNeeded ();
  58. SetNeedsDisplay ();
  59. //Parent?.SetLayoutNeeded ();
  60. OnThicknessChanged ();
  61. }
  62. }
  63. }
  64. /// <summary>Fired whenever the <see cref="Thickness"/> property changes.</summary>
  65. [CanBeNull]
  66. public event EventHandler? ThicknessChanged;
  67. /// <summary>Called whenever the <see cref="Thickness"/> property changes.</summary>
  68. public void OnThicknessChanged ()
  69. {
  70. ThicknessChanged?.Invoke (this, EventArgs.Empty);
  71. }
  72. #endregion Thickness
  73. #region View Overrides
  74. /// <summary>
  75. /// Adornments cannot be used as sub-views (see <see cref="Parent"/>); setting this property will throw
  76. /// <see cref="InvalidOperationException"/>.
  77. /// </summary>
  78. /// <remarks>
  79. /// While there are no real use cases for an Adornment being a subview, it is not explicitly dis-allowed to support testing. E.g. in AllViewsTester.
  80. /// </remarks>
  81. public override View? SuperView
  82. {
  83. get => base.SuperView!;
  84. set => throw new InvalidOperationException (@"Adornments can not be Subviews or have SuperViews. Use Parent instead.");
  85. }
  86. //internal override Adornment CreateAdornment (Type adornmentType)
  87. //{
  88. // /* Do nothing - Adornments do not have Adornments */
  89. // return null;
  90. //}
  91. //internal override void LayoutAdornments ()
  92. //{
  93. // /* Do nothing - Adornments do not have Adornments */
  94. //}
  95. /// <summary>
  96. /// Gets the rectangle that describes the area of the Adornment. The Location is always (0,0).
  97. /// The size is the size of the <see cref="View.Frame"/>.
  98. /// </summary>
  99. /// <remarks>
  100. /// The Viewport of an Adornment cannot be modified. Attempting to set this property will throw an
  101. /// <see cref="InvalidOperationException"/>.
  102. /// </remarks>
  103. public override Rectangle Viewport
  104. {
  105. get => base.Viewport;
  106. set => throw new InvalidOperationException (@"The Viewport of an Adornment cannot be modified.");
  107. }
  108. /// <inheritdoc/>
  109. public override Rectangle FrameToScreen ()
  110. {
  111. if (Parent is null)
  112. {
  113. // While there are no real use cases for an Adornment being a subview, we support it for
  114. // testing. E.g. in AllViewsTester.
  115. if (SuperView is { })
  116. {
  117. Point super = SuperView.ViewportToScreen (Frame.Location);
  118. return new (super, Frame.Size);
  119. }
  120. return Frame;
  121. }
  122. // Adornments are *Children* of a View, not SubViews. Thus View.FrameToScreen will not work.
  123. // To get the screen-relative coordinates of an Adornment, we need get the parent's Frame
  124. // in screen coords, ...
  125. Rectangle parentScreen = Parent.FrameToScreen ();
  126. // ...and add our Frame location to it.
  127. return new (new (parentScreen.X + Frame.X, parentScreen.Y + Frame.Y), Frame.Size);
  128. }
  129. /// <inheritdoc/>
  130. public override Point ScreenToFrame (in Point location)
  131. {
  132. return Parent!.ScreenToFrame (new (location.X - Frame.X, location.Y - Frame.Y));
  133. }
  134. /// <summary>Does nothing for Adornment</summary>
  135. /// <returns></returns>
  136. public override bool OnDrawAdornments () { return false; }
  137. /// <summary>Redraws the Adornments that comprise the <see cref="Adornment"/>.</summary>
  138. public override void OnDrawContent (Rectangle viewport)
  139. {
  140. if (Thickness == Thickness.Empty)
  141. {
  142. return;
  143. }
  144. Rectangle prevClip = SetClip ();
  145. Rectangle screen = ViewportToScreen (viewport);
  146. Attribute normalAttr = GetNormalColor ();
  147. Driver?.SetAttribute (normalAttr);
  148. // This just draws/clears the thickness, not the insides.
  149. Thickness.Draw (screen, Diagnostics, ToString ());
  150. if (!string.IsNullOrEmpty (TextFormatter.Text))
  151. {
  152. TextFormatter.ConstrainToSize = Frame.Size;
  153. TextFormatter.NeedsFormat = true;
  154. }
  155. TextFormatter?.Draw (screen, normalAttr, normalAttr, Rectangle.Empty);
  156. if (Subviews.Count > 0)
  157. {
  158. base.OnDrawContent (viewport);
  159. }
  160. if (Driver is { })
  161. {
  162. Driver.Clip = prevClip;
  163. }
  164. ClearNeedsDisplay ();
  165. }
  166. /// <summary>Does nothing for Adornment</summary>
  167. /// <returns></returns>
  168. public override bool OnRenderLineCanvas () { return false; }
  169. /// <summary>
  170. /// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
  171. /// property throws an <see cref="InvalidOperationException"/>.
  172. /// </summary>
  173. public override bool SuperViewRendersLineCanvas
  174. {
  175. get => false;
  176. set => throw new InvalidOperationException (@"Adornment can only render to their Parent or Parent's Superview.");
  177. }
  178. #endregion View Overrides
  179. #region Mouse Support
  180. /// <summary>
  181. /// Indicates whether the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness.
  182. /// </summary>
  183. /// <remarks>
  184. /// The <paramref name="location"/> is relative to the PARENT's SuperView.
  185. /// </remarks>
  186. /// <param name="location"></param>
  187. /// <returns><see langword="true"/> if the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness. </returns>
  188. public override bool Contains (in Point location)
  189. {
  190. if (Parent is null)
  191. {
  192. return false;
  193. }
  194. Rectangle outside = Frame;
  195. outside.Offset (Parent.Frame.Location);
  196. return Thickness.Contains (outside, location);
  197. }
  198. ///// <inheritdoc/>
  199. //protected override bool OnMouseEnter (CancelEventArgs mouseEvent)
  200. //{
  201. // // Invert Normal
  202. // if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
  203. // {
  204. // var cs = new ColorScheme (ColorScheme)
  205. // {
  206. // Normal = new (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground)
  207. // };
  208. // ColorScheme = cs;
  209. // }
  210. // return false;
  211. //}
  212. ///// <inheritdoc/>
  213. //protected override void OnMouseLeave ()
  214. //{
  215. // // Invert Normal
  216. // if (Diagnostics.FastHasFlags (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
  217. // {
  218. // var cs = new ColorScheme (ColorScheme)
  219. // {
  220. // Normal = new (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground)
  221. // };
  222. // ColorScheme = cs;
  223. // }
  224. //}
  225. #endregion Mouse Support
  226. /// <inheritdoc/>
  227. bool IDesignable.EnableForDesign ()
  228. {
  229. Thickness = new (3);
  230. Frame = new (0, 0, 10, 10);
  231. Diagnostics = ViewDiagnosticFlags.Padding;
  232. return true;
  233. }
  234. }