Adornment.cs 8.1 KB

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