Adornment.cs 8.5 KB

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