Adornment.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #nullable enable
  2. namespace Terminal.Gui.ViewBase;
  3. /// <summary>
  4. /// Adornments are a special form of <see cref="View"/> that appear outside the <see cref="View.Viewport"/>:
  5. /// <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>. They are defined using the
  6. /// <see cref="Thickness"/> class, which specifies the thickness of the sides of a rectangle.
  7. /// </summary>
  8. /// <remarsk>
  9. /// <para>
  10. /// Each of <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> has slightly different
  11. /// behavior relative to <see cref="Scheme"/>, <see cref="View.SetFocus()"/>, keyboard input, and
  12. /// mouse input. Each can be customized by manipulating their SubViews.
  13. /// </para>
  14. /// </remarsk>
  15. public class Adornment : View, IDesignable
  16. {
  17. /// <inheritdoc/>
  18. public Adornment ()
  19. {
  20. /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  21. }
  22. /// <summary>Constructs a new adornment for the view specified by <paramref name="parent"/>.</summary>
  23. /// <param name="parent"></param>
  24. public Adornment (View parent)
  25. {
  26. // By default, Adornments can't get focus; has to be enabled specifically.
  27. CanFocus = false;
  28. TabStop = TabBehavior.NoStop;
  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. /// <summary>
  39. /// Gets or sets whether the Adornment will draw diagnostic information. This is a bit-field of
  40. /// <see cref="ViewDiagnosticFlags"/>.
  41. /// </summary>
  42. /// <remarks>
  43. /// The <see cref="View.Diagnostics"/> static property is used as the default value for this property.
  44. /// </remarks>
  45. public new ViewDiagnosticFlags Diagnostics { get; set; } = View.Diagnostics;
  46. private Thickness _thickness = Thickness.Empty;
  47. /// <summary>Defines the rectangle that the <see cref="Adornment"/> will use to draw its content.</summary>
  48. public Thickness Thickness
  49. {
  50. get => _thickness;
  51. set
  52. {
  53. Thickness current = _thickness;
  54. _thickness = value;
  55. if (current != _thickness)
  56. {
  57. Parent?.SetAdornmentFrames ();
  58. SetNeedsLayout ();
  59. SetNeedsDraw ();
  60. OnThicknessChanged ();
  61. }
  62. }
  63. }
  64. /// <summary>Fired whenever the <see cref="Thickness"/> property changes.</summary>
  65. public event EventHandler? ThicknessChanged;
  66. /// <summary>Called whenever the <see cref="Thickness"/> property changes.</summary>
  67. public void OnThicknessChanged () { ThicknessChanged?.Invoke (this, EventArgs.Empty); }
  68. #endregion Thickness
  69. #region View Overrides
  70. // If a scheme is explicitly set, use that. Otherwise, use the scheme of the parent view.
  71. private Scheme? _scheme;
  72. /// <inheritdoc />
  73. protected override bool OnGettingScheme (out Scheme? scheme)
  74. {
  75. scheme = _scheme ?? Parent?.GetScheme () ?? SchemeManager.GetScheme (Schemes.Base);
  76. return true;
  77. }
  78. /// <param name="scheme"></param>
  79. /// <inheritdoc />
  80. protected override bool OnSettingScheme (in Scheme? scheme)
  81. {
  82. Parent?.SetNeedsDraw ();
  83. _scheme = scheme;
  84. return false;
  85. }
  86. /// <summary>
  87. /// Gets the rectangle that describes the area of the Adornment. The Location is always (0,0).
  88. /// The size is the size of the <see cref="View.Frame"/>.
  89. /// </summary>
  90. /// <remarks>
  91. /// The Viewport of an Adornment cannot be modified. Attempting to set this property will throw an
  92. /// <see cref="InvalidOperationException"/>.
  93. /// </remarks>
  94. public override Rectangle Viewport
  95. {
  96. get => base.Viewport;
  97. set => throw new InvalidOperationException (@"The Viewport of an Adornment cannot be modified.");
  98. }
  99. /// <inheritdoc/>
  100. public override Rectangle FrameToScreen ()
  101. {
  102. if (Parent is null)
  103. {
  104. // While there are no real use cases for an Adornment being a subview, we support it for
  105. // testing. E.g. in AllViewsTester.
  106. if (SuperView is { })
  107. {
  108. Point super = SuperView.ViewportToScreen (Frame.Location);
  109. return new (super, Frame.Size);
  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. View? parentOrSuperView = Parent;
  124. if (parentOrSuperView is null)
  125. {
  126. // While there are no real use cases for an Adornment being a subview, we support it for
  127. // testing. E.g. in AllViewsTester.
  128. parentOrSuperView = SuperView;
  129. if (parentOrSuperView is null)
  130. {
  131. return Point.Empty;
  132. }
  133. }
  134. return parentOrSuperView.ScreenToFrame (new (location.X - Frame.X, location.Y - Frame.Y));
  135. }
  136. /// <summary>
  137. /// Called when the <see cref="Thickness"/> of the Adornment is to be cleared.
  138. /// </summary>
  139. /// <returns><see langword="true"/> to stop further clearing.</returns>
  140. protected override bool OnClearingViewport ()
  141. {
  142. if (Thickness == Thickness.Empty)
  143. {
  144. return true;
  145. }
  146. // This just draws/clears the thickness, not the insides.
  147. Thickness.Draw (ViewportToScreen (Viewport), Diagnostics, ToString ());
  148. NeedsDraw = true;
  149. return true;
  150. }
  151. /// <inheritdoc/>
  152. protected override bool OnDrawingText () { return Thickness == Thickness.Empty; }
  153. /// <inheritdoc/>
  154. protected override bool OnDrawingSubViews () { 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. /// <summary>
  168. /// Indicates whether the specified Parent's SuperView-relative coordinates are within the Adornment's Thickness.
  169. /// </summary>
  170. /// <remarks>
  171. /// The <paramref name="location"/> is relative to the PARENT's SuperView.
  172. /// </remarks>
  173. /// <param name="location"></param>
  174. /// <returns>
  175. /// <see langword="true"/> if the specified Parent's SuperView-relative coordinates are within the Adornment's
  176. /// Thickness.
  177. /// </returns>
  178. public override bool Contains (in Point location)
  179. {
  180. View? parentOrSuperView = Parent;
  181. if (parentOrSuperView is null)
  182. {
  183. // While there are no real use cases for an Adornment being a subview, we support it for
  184. // testing. E.g. in AllViewsTester.
  185. parentOrSuperView = SuperView;
  186. if (parentOrSuperView is null)
  187. {
  188. return false;
  189. }
  190. }
  191. Rectangle outside = Frame;
  192. outside.Offset (parentOrSuperView.Frame.Location);
  193. return Thickness.Contains (outside, location);
  194. }
  195. /// <summary>
  196. /// INTERNAL: Gets all Views (Subviews and Adornments) in the of <see cref="Adornment"/> hierarchcy that are at <paramref name="screenLocation"/>,
  197. /// regardless of whether they will be drawn or see mouse events or not. Views with <see cref="View.Visible"/> set to <see langword="false"/> will not be included.
  198. /// The list is ordered by depth. The deepest View is at the end of the list (the topmost View is at element 0).
  199. /// </summary>
  200. /// <param name="adornment">The root Adornment from which the search for subviews begins.</param>
  201. /// <param name="screenLocation">The screen-relative location where the search for views is focused.</param>
  202. /// <returns>A list of views that are located under the specified point.</returns>
  203. internal static List<View?> GetViewsAtLocation (Adornment? adornment, in Point screenLocation)
  204. {
  205. List<View?> result = [];
  206. if (adornment is null || adornment.Thickness == Thickness.Empty)
  207. {
  208. return result;
  209. }
  210. Point superViewRelativeLocation = adornment.Parent!.SuperView?.ScreenToViewport (screenLocation) ?? screenLocation;
  211. if (adornment.Contains (superViewRelativeLocation))
  212. {
  213. List<View?> adornmentResult = GetViewsAtLocation (adornment as View, screenLocation);
  214. if (adornmentResult.Count > 0)
  215. {
  216. result.AddRange (adornmentResult);
  217. }
  218. }
  219. return result;
  220. }
  221. #endregion View Overrides
  222. /// <inheritdoc/>
  223. bool IDesignable.EnableForDesign ()
  224. {
  225. // This enables AllViewsTester to show something useful.
  226. Thickness = new (3);
  227. Frame = new (0, 0, 10, 10);
  228. Diagnostics = ViewDiagnosticFlags.Thickness;
  229. return true;
  230. }
  231. }