Adornment.cs 10.0 KB

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