Adornment.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. namespace Terminal.Gui;
  2. // TODO: v2 - Missing 3D effect - 3D effects will be drawn by a mechanism separate from Adornments
  3. // TODO: v2 - If a Adornment has focus, navigation keys (e.g Command.NextView) should cycle through SubViews of the Adornments
  4. // QUESTION: How does a user navigate out of an Adornment to another Adornment, or back into the Parent's SubViews?
  5. /// <summary>
  6. /// Adornments are a special form of <see cref="View"/> that appear outside of the <see cref="View.Bounds"/>:
  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. /// There is no prevision for creating additional subclasses of Adornment. It is not abstract to enable unit
  13. /// testing.
  14. /// </para>
  15. /// <para>Each of <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> can be customized.</para>
  16. /// </remarsk>
  17. public class Adornment : View
  18. {
  19. private Thickness _thickness = Thickness.Empty;
  20. /// <inheritdoc/>
  21. public Adornment ()
  22. {
  23. /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  24. }
  25. /// <summary>Constructs a new adornment for the view specified by <paramref name="parent"/>.</summary>
  26. /// <param name="parent"></param>
  27. public Adornment (View parent) { Parent = parent; }
  28. /// <summary>Gets the rectangle that describes the inner area of the Adornment. The Location is always (0,0).</summary>
  29. public override Rectangle Bounds
  30. {
  31. get => Thickness?.GetInside (new Rectangle (Point.Empty, Frame.Size)) ?? new Rectangle (Point.Empty, Frame.Size);
  32. set => throw new InvalidOperationException ("It makes no sense to set Bounds of a Thickness.");
  33. }
  34. /// <summary>The Parent of this Adornment (the View this Adornment surrounds).</summary>
  35. /// <remarks>
  36. /// Adornments are distinguished from typical View classes in that they are not sub-views, but have a parent/child
  37. /// relationship with their containing View.
  38. /// </remarks>
  39. public View Parent { get; set; }
  40. /// <summary>
  41. /// Adornments cannot be used as sub-views (see <see cref="Parent"/>); this method always throws an
  42. /// <see cref="InvalidOperationException"/>. TODO: Are we sure?
  43. /// </summary>
  44. public override View SuperView
  45. {
  46. get => null;
  47. set => throw new NotImplementedException ();
  48. }
  49. /// <summary>
  50. /// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
  51. /// property throws an <see cref="InvalidOperationException"/>.
  52. /// </summary>
  53. public override bool SuperViewRendersLineCanvas
  54. {
  55. get => false; // throw new NotImplementedException ();
  56. set => throw new NotImplementedException ();
  57. }
  58. /// <summary>Defines the rectangle that the <see cref="Adornment"/> will use to draw its content.</summary>
  59. public Thickness Thickness
  60. {
  61. get => _thickness;
  62. set
  63. {
  64. Thickness prev = _thickness;
  65. _thickness = value;
  66. if (prev != _thickness)
  67. {
  68. Parent?.LayoutAdornments ();
  69. OnThicknessChanged (prev);
  70. }
  71. }
  72. }
  73. /// <inheritdoc/>
  74. public override void BoundsToScreen (int col, int row, out int rcol, out int rrow, bool clipped = true)
  75. {
  76. // Adornments are *Children* of a View, not SubViews. Thus View.BoundsToScreen will not work.
  77. // To get the screen-relative coordinates of a Adornment, we need to know who
  78. // the Parent is
  79. Rectangle parentFrame = Parent?.Frame ?? Frame;
  80. rrow = row + parentFrame.Y;
  81. rcol = col + parentFrame.X;
  82. // We now have rcol/rrow in coordinates relative to our View's SuperView. If our View's SuperView has
  83. // a SuperView, keep going...
  84. Parent?.SuperView?.BoundsToScreen (rcol, rrow, out rcol, out rrow, clipped);
  85. }
  86. /// <inheritdoc/>
  87. public override Rectangle FrameToScreen ()
  88. {
  89. // Adornments are *Children* of a View, not SubViews. Thus View.FrameToScreen will not work.
  90. // To get the screen-relative coordinates of a Adornment, we need to know who
  91. // the Parent is
  92. Rectangle ret = Parent?.Frame ?? Frame;
  93. ret.Size = Frame.Size;
  94. ret.Location = Parent?.FrameToScreen ().Location ?? ret.Location;
  95. // We now have coordinates relative to our View. If our View's SuperView has
  96. // a SuperView, keep going...
  97. return ret;
  98. }
  99. /// <summary>Does nothing for Adornment</summary>
  100. /// <returns></returns>
  101. public override bool OnDrawAdornments () { return false; }
  102. /// <summary>Redraws the Adornments that comprise the <see cref="Adornment"/>.</summary>
  103. public override void OnDrawContent (Rectangle contentArea)
  104. {
  105. if (Thickness == Thickness.Empty)
  106. {
  107. return;
  108. }
  109. Rectangle screenBounds = BoundsToScreen (Frame);
  110. Attribute normalAttr = GetNormalColor ();
  111. // This just draws/clears the thickness, not the insides.
  112. Driver.SetAttribute (normalAttr);
  113. Thickness.Draw (screenBounds, (string)(Data ?? string.Empty));
  114. if (!string.IsNullOrEmpty (TextFormatter.Text))
  115. {
  116. if (TextFormatter is { })
  117. {
  118. TextFormatter.Size = Frame.Size;
  119. TextFormatter.NeedsFormat = true;
  120. }
  121. }
  122. TextFormatter?.Draw (screenBounds, normalAttr, normalAttr, Rectangle.Empty);
  123. //base.OnDrawContent (contentArea);
  124. }
  125. /// <summary>Does nothing for Adornment</summary>
  126. /// <returns></returns>
  127. public override bool OnRenderLineCanvas () { return false; }
  128. /// <summary>Called whenever the <see cref="Thickness"/> property changes.</summary>
  129. public virtual void OnThicknessChanged (Thickness previousThickness)
  130. {
  131. ThicknessChanged?.Invoke (
  132. this,
  133. new ThicknessEventArgs { Thickness = Thickness, PreviousThickness = previousThickness }
  134. );
  135. }
  136. /// <summary>Fired whenever the <see cref="Thickness"/> property changes.</summary>
  137. public event EventHandler<ThicknessEventArgs> ThicknessChanged;
  138. internal override Adornment CreateAdornment (Type adornmentType)
  139. {
  140. /* Do nothing - Adornments do not have Adornments */
  141. return null;
  142. }
  143. internal override void LayoutAdornments ()
  144. {
  145. /* Do nothing - Adornments do not have Adornments */
  146. }
  147. }