2
0

Adornment.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, IDesignable
  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. TabStop = TabBehavior.NoStop;
  30. Parent = parent;
  31. }
  32. /// <summary>The Parent of this Adornment (the View this Adornment surrounds).</summary>
  33. /// <remarks>
  34. /// Adornments are distinguished from typical View classes in that they are not sub-views, but have a parent/child
  35. /// relationship with their containing View.
  36. /// </remarks>
  37. public View? Parent { get; set; }
  38. #region Thickness
  39. /// <summary>
  40. /// Gets or sets whether the Adornment will draw diagnostic information. This is a bit-field of
  41. /// <see cref="ViewDiagnosticFlags"/>.
  42. /// </summary>
  43. /// <remarks>
  44. /// The <see cref="View.Diagnostics"/> static property is used as the default value for this property.
  45. /// </remarks>
  46. public new ViewDiagnosticFlags Diagnostics { get; set; } = View.Diagnostics;
  47. private Thickness _thickness = Thickness.Empty;
  48. /// <summary>Defines the rectangle that the <see cref="Adornment"/> will use to draw its content.</summary>
  49. public Thickness Thickness
  50. {
  51. get => _thickness;
  52. set
  53. {
  54. Thickness current = _thickness;
  55. _thickness = value;
  56. if (current != _thickness)
  57. {
  58. Parent?.SetAdornmentFrames ();
  59. SetNeedsLayout ();
  60. SetNeedsDraw ();
  61. OnThicknessChanged ();
  62. }
  63. }
  64. }
  65. /// <summary>Fired whenever the <see cref="Thickness"/> property changes.</summary>
  66. public event EventHandler? ThicknessChanged;
  67. /// <summary>Called whenever the <see cref="Thickness"/> property changes.</summary>
  68. public void OnThicknessChanged () { ThicknessChanged?.Invoke (this, EventArgs.Empty); }
  69. #endregion Thickness
  70. #region View Overrides
  71. /// <summary>
  72. /// Adornments cannot be used as sub-views (see <see cref="Parent"/>); setting this property will throw
  73. /// <see cref="InvalidOperationException"/>.
  74. /// </summary>
  75. /// <remarks>
  76. /// While there are no real use cases for an Adornment being a subview, it is not explicitly dis-allowed to support
  77. /// testing. E.g. in AllViewsTester.
  78. /// </remarks>
  79. public override View? SuperView
  80. {
  81. get => base.SuperView!;
  82. set => throw new InvalidOperationException (@"Adornments can not be Subviews or have SuperViews. Use Parent instead.");
  83. }
  84. /// <summary>
  85. /// Gets the rectangle that describes the area of the Adornment. The Location is always (0,0).
  86. /// The size is the size of the <see cref="View.Frame"/>.
  87. /// </summary>
  88. /// <remarks>
  89. /// The Viewport of an Adornment cannot be modified. Attempting to set this property will throw an
  90. /// <see cref="InvalidOperationException"/>.
  91. /// </remarks>
  92. public override Rectangle Viewport
  93. {
  94. get => base.Viewport;
  95. set => throw new InvalidOperationException (@"The Viewport of an Adornment cannot be modified.");
  96. }
  97. /// <inheritdoc/>
  98. public override Rectangle FrameToScreen ()
  99. {
  100. if (Parent is null)
  101. {
  102. // While there are no real use cases for an Adornment being a subview, we support it for
  103. // testing. E.g. in AllViewsTester.
  104. if (SuperView is { })
  105. {
  106. Point super = SuperView.ViewportToScreen (Frame.Location);
  107. return new (super, Frame.Size);
  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. View? parentOrSuperView = Parent;
  122. if (parentOrSuperView is null)
  123. {
  124. // While there are no real use cases for an Adornment being a subview, we support it for
  125. // testing. E.g. in AllViewsTester.
  126. parentOrSuperView = SuperView;
  127. if (parentOrSuperView is null)
  128. {
  129. return Point.Empty;
  130. }
  131. }
  132. return parentOrSuperView.ScreenToFrame (new (location.X - Frame.X, location.Y - Frame.Y));
  133. }
  134. /// <summary>
  135. /// Called when the <see cref="Thickness"/> of the Adornment is to be cleared.
  136. /// </summary>
  137. /// <param name="viewport"></param>
  138. /// <returns><see langword="true"/> to stop further clearing.</returns>
  139. protected override bool OnClearingViewport (Rectangle viewport)
  140. {
  141. if (Thickness == Thickness.Empty)
  142. {
  143. return true;
  144. }
  145. Attribute normalAttr = GetNormalColor ();
  146. SetAttribute (normalAttr);
  147. // This just draws/clears the thickness, not the insides.
  148. Thickness.Draw (ViewportToScreen (viewport), Diagnostics, ToString ());
  149. return true;
  150. }
  151. /// <inheritdoc/>
  152. protected override bool OnDrawingText (Rectangle viewport) { return Thickness == Thickness.Empty; }
  153. /// <inheritdoc/>
  154. protected override bool OnDrawingSubviews (Rectangle viewport) { return Thickness == Thickness.Empty; }
  155. /// <summary>Does nothing for Adornment</summary>
  156. /// <returns></returns>
  157. protected override bool OnRenderingLineCanvas () { return true; }
  158. /// <summary>
  159. /// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
  160. /// property throws an <see cref="InvalidOperationException"/>.
  161. /// </summary>
  162. public override bool SuperViewRendersLineCanvas
  163. {
  164. get => false;
  165. set => throw new InvalidOperationException (@"Adornment can only render to their Parent or Parent's Superview.");
  166. }
  167. /// <inheritdoc/>
  168. protected override void OnDrawComplete () { }
  169. /// <summary>
  170. /// Indicates whether the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness.
  171. /// </summary>
  172. /// <remarks>
  173. /// The <paramref name="location"/> is relative to the PARENT's SuperView.
  174. /// </remarks>
  175. /// <param name="location"></param>
  176. /// <returns>
  177. /// <see langword="true"/> if the specified Parent's SuperView-relative coordinates are within the Adornment's
  178. /// Thickness.
  179. /// </returns>
  180. public override bool Contains (in Point location)
  181. {
  182. View? parentOrSuperView = Parent;
  183. if (parentOrSuperView is null)
  184. {
  185. // While there are no real use cases for an Adornment being a subview, we support it for
  186. // testing. E.g. in AllViewsTester.
  187. parentOrSuperView = SuperView;
  188. if (parentOrSuperView is null)
  189. {
  190. return false;
  191. }
  192. }
  193. Rectangle outside = Frame;
  194. outside.Offset (parentOrSuperView.Frame.Location);
  195. return Thickness.Contains (outside, location);
  196. }
  197. #endregion View Overrides
  198. /// <inheritdoc/>
  199. bool IDesignable.EnableForDesign ()
  200. {
  201. // This enables AllViewsTester to show something useful.
  202. Thickness = new (3);
  203. Frame = new (0, 0, 10, 10);
  204. Diagnostics = ViewDiagnosticFlags.Thickness;
  205. return true;
  206. }
  207. }