Adornment.cs 8.4 KB

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