Adornment.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Adornments are a special form of <see cref="View"/> that appear outside the <see cref="View.Viewport"/>:
  4. /// <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>. They are defined using the
  5. /// <see cref="Thickness"/> class, which specifies the thickness of the sides of a rectangle.
  6. /// </summary>
  7. /// <remarsk>
  8. /// <para>
  9. /// Each of <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> has slightly different
  10. /// behavior relative to <see cref="ColorScheme"/>, <see cref="View.SetFocus"/>, keyboard input, and
  11. /// mouse input. Each can be customized by manipulating their Subviews.
  12. /// </para>
  13. /// </remarsk>
  14. public class Adornment : View
  15. {
  16. /// <inheritdoc/>
  17. public Adornment ()
  18. {
  19. /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  20. }
  21. /// <summary>Constructs a new adornment for the view specified by <paramref name="parent"/>.</summary>
  22. /// <param name="parent"></param>
  23. public Adornment (View parent)
  24. {
  25. CanFocus = true;
  26. Parent = parent;
  27. }
  28. /// <summary>The Parent of this Adornment (the View this Adornment surrounds).</summary>
  29. /// <remarks>
  30. /// Adornments are distinguished from typical View classes in that they are not sub-views, but have a parent/child
  31. /// relationship with their containing View.
  32. /// </remarks>
  33. public View Parent { get; set; }
  34. #region Thickness
  35. private Thickness _thickness = Thickness.Empty;
  36. /// <summary>Defines the rectangle that the <see cref="Adornment"/> will use to draw its content.</summary>
  37. public Thickness Thickness
  38. {
  39. get => _thickness;
  40. set
  41. {
  42. Thickness prev = _thickness;
  43. _thickness = value;
  44. if (prev != _thickness)
  45. {
  46. if (Parent?.IsInitialized == false)
  47. {
  48. // When initialized Parent.LayoutSubViews will cause a LayoutAdornments
  49. Parent?.LayoutAdornments ();
  50. }
  51. else
  52. {
  53. Parent?.SetNeedsLayout ();
  54. Parent?.LayoutSubviews ();
  55. }
  56. OnThicknessChanged (prev);
  57. }
  58. }
  59. }
  60. /// <summary>Fired whenever the <see cref="Thickness"/> property changes.</summary>
  61. public event EventHandler<ThicknessEventArgs> ThicknessChanged;
  62. /// <summary>Called whenever the <see cref="Thickness"/> property changes.</summary>
  63. public void OnThicknessChanged (Thickness previousThickness)
  64. {
  65. ThicknessChanged?.Invoke (
  66. this,
  67. new () { Thickness = Thickness, PreviousThickness = previousThickness }
  68. );
  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 (int x, int y) { return Parent.ScreenToFrame (x - Frame.X, y - Frame.Y); }
  119. /// <summary>Does nothing for Adornment</summary>
  120. /// <returns></returns>
  121. public override bool OnDrawAdornments () { return false; }
  122. /// <summary>Redraws the Adornments that comprise the <see cref="Adornment"/>.</summary>
  123. public override void OnDrawContent (Rectangle viewport)
  124. {
  125. if (Thickness == Thickness.Empty)
  126. {
  127. return;
  128. }
  129. Rectangle screen = ViewportToScreen (viewport);
  130. Attribute normalAttr = GetNormalColor ();
  131. Driver.SetAttribute (normalAttr);
  132. // This just draws/clears the thickness, not the insides.
  133. Thickness.Draw (screen, ToString ());
  134. if (!string.IsNullOrEmpty (TextFormatter.Text))
  135. {
  136. if (TextFormatter is { })
  137. {
  138. TextFormatter.Size = Frame.Size;
  139. TextFormatter.NeedsFormat = true;
  140. }
  141. }
  142. TextFormatter?.Draw (screen, normalAttr, normalAttr, Rectangle.Empty);
  143. if (Subviews.Count > 0)
  144. {
  145. base.OnDrawContent (viewport);
  146. }
  147. ClearLayoutNeeded ();
  148. ClearNeedsDisplay ();
  149. }
  150. /// <summary>Does nothing for Adornment</summary>
  151. /// <returns></returns>
  152. public override bool OnRenderLineCanvas () { return false; }
  153. /// <summary>
  154. /// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
  155. /// property throws an <see cref="InvalidOperationException"/>.
  156. /// </summary>
  157. public override bool SuperViewRendersLineCanvas
  158. {
  159. get => false;
  160. set => throw new InvalidOperationException (@"Adornment can only render to their Parent or Parent's Superview.");
  161. }
  162. #endregion View Overrides
  163. #region Mouse Support
  164. /// <summary>
  165. /// Indicates whether the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness.
  166. /// </summary>
  167. /// <remarks>
  168. /// The <paramref name="x"/> and <paramref name="x"/> are relative to the PARENT's SuperView.
  169. /// </remarks>
  170. /// <param name="x"></param>
  171. /// <param name="y"></param>
  172. /// <returns><see langword="true"/> if the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness. </returns>
  173. public override bool Contains (int x, int y)
  174. {
  175. if (Parent is null)
  176. {
  177. return false;
  178. }
  179. Rectangle frame = Frame;
  180. frame.Offset (Parent.Frame.Location);
  181. return Thickness.Contains (frame, x, y);
  182. }
  183. /// <inheritdoc/>
  184. protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
  185. {
  186. // Invert Normal
  187. if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
  188. {
  189. var cs = new ColorScheme (ColorScheme)
  190. {
  191. Normal = new (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground)
  192. };
  193. ColorScheme = cs;
  194. }
  195. return base.OnMouseEnter (mouseEvent);
  196. }
  197. /// <inheritdoc/>
  198. protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
  199. {
  200. // Invert Normal
  201. if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
  202. {
  203. var cs = new ColorScheme (ColorScheme)
  204. {
  205. Normal = new (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground)
  206. };
  207. ColorScheme = cs;
  208. }
  209. return base.OnMouseLeave (mouseEvent);
  210. }
  211. #endregion Mouse Support
  212. }