Adornment.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Adornments are a special form of <see cref="View"/> that appear outside the <see cref="View.Bounds"/>:
  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 NotImplementedException ();
  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. public override Rectangle Bounds
  95. {
  96. get => Frame with { Location = Point.Empty };
  97. set => throw new InvalidOperationException ("It makes no sense to set Bounds of a Thickness.");
  98. }
  99. /// <inheritdoc/>
  100. public override Rectangle FrameToScreen ()
  101. {
  102. if (Parent is null)
  103. {
  104. return Frame;
  105. }
  106. // Adornments are *Children* of a View, not SubViews. Thus View.FrameToScreen will not work.
  107. // To get the screen-relative coordinates of an Adornment, we need get the parent's Frame
  108. // in screen coords, and add our Frame location to it.
  109. Rectangle parent = Parent.FrameToScreen ();
  110. return new (new (parent.X + Frame.X, parent.Y + Frame.Y), Frame.Size);
  111. }
  112. /// <inheritdoc/>
  113. public override Point ScreenToFrame (int x, int y) { return Parent.ScreenToFrame (x - Frame.X, y - Frame.Y); }
  114. /// <summary>Does nothing for Adornment</summary>
  115. /// <returns></returns>
  116. public override bool OnDrawAdornments () { return false; }
  117. /// <summary>Redraws the Adornments that comprise the <see cref="Adornment"/>.</summary>
  118. public override void OnDrawContent (Rectangle contentArea)
  119. {
  120. if (Thickness == Thickness.Empty)
  121. {
  122. return;
  123. }
  124. Rectangle screenBounds = BoundsToScreen (contentArea);
  125. Attribute normalAttr = GetNormalColor ();
  126. Driver.SetAttribute (normalAttr);
  127. // This just draws/clears the thickness, not the insides.
  128. Thickness.Draw (screenBounds, ToString ());
  129. if (!string.IsNullOrEmpty (TextFormatter.Text))
  130. {
  131. if (TextFormatter is { })
  132. {
  133. TextFormatter.Size = Frame.Size;
  134. TextFormatter.NeedsFormat = true;
  135. }
  136. }
  137. TextFormatter?.Draw (screenBounds, normalAttr, normalAttr, Rectangle.Empty);
  138. if (Subviews.Count > 0)
  139. {
  140. base.OnDrawContent (contentArea);
  141. }
  142. ClearLayoutNeeded ();
  143. ClearNeedsDisplay ();
  144. }
  145. /// <summary>Does nothing for Adornment</summary>
  146. /// <returns></returns>
  147. public override bool OnRenderLineCanvas () { return false; }
  148. /// <summary>
  149. /// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
  150. /// property throws an <see cref="InvalidOperationException"/>.
  151. /// </summary>
  152. public override bool SuperViewRendersLineCanvas
  153. {
  154. get => false; // throw new NotImplementedException ();
  155. set => throw new NotImplementedException ();
  156. }
  157. #endregion View Overrides
  158. #region Mouse Support
  159. /// <summary>
  160. /// Indicates whether the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness.
  161. /// </summary>
  162. /// <remarks>
  163. /// The <paramref name="x"/> and <paramref name="x"/> are relative to the PARENT's SuperView.
  164. /// </remarks>
  165. /// <param name="x"></param>
  166. /// <param name="y"></param>
  167. /// <returns><see langword="true"/> if the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness. </returns>
  168. public override bool Contains (int x, int y)
  169. {
  170. Rectangle frame = Frame;
  171. frame.Offset (Parent.Frame.Location);
  172. return Thickness.Contains (frame, x, y);
  173. }
  174. /// <inheritdoc/>
  175. protected internal override bool OnMouseEnter (MouseEvent mouseEvent)
  176. {
  177. // Invert Normal
  178. if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
  179. {
  180. var cs = new ColorScheme (ColorScheme)
  181. {
  182. Normal = new (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground)
  183. };
  184. ColorScheme = cs;
  185. }
  186. return base.OnMouseEnter (mouseEvent);
  187. }
  188. /// <summary>Called when a mouse event occurs within the Adornment.</summary>
  189. /// <remarks>
  190. /// <para>
  191. /// The coordinates are relative to <see cref="View.Bounds"/>.
  192. /// </para>
  193. /// <para>
  194. /// A mouse click on the Adornment will cause the Parent to focus.
  195. /// </para>
  196. /// <para>
  197. /// A mouse drag on the Adornment will cause the Parent to move.
  198. /// </para>
  199. /// </remarks>
  200. /// <param name="mouseEvent"></param>
  201. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  202. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  203. {
  204. var args = new MouseEventEventArgs (mouseEvent);
  205. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
  206. {
  207. if (Parent.CanFocus && !Parent.HasFocus)
  208. {
  209. Parent.SetFocus ();
  210. Parent.SetNeedsDisplay ();
  211. }
  212. return OnMouseClick (args);
  213. }
  214. return false;
  215. }
  216. /// <inheritdoc/>
  217. protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
  218. {
  219. // Invert Normal
  220. if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
  221. {
  222. var cs = new ColorScheme (ColorScheme)
  223. {
  224. Normal = new (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground)
  225. };
  226. ColorScheme = cs;
  227. }
  228. return base.OnMouseLeave (mouseEvent);
  229. }
  230. #endregion Mouse Support
  231. }