Adornment.cs 8.5 KB

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