Adornment.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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>
  29. /// Gets the rectangle that describes the area of the Adornment. The Location is always (0,0).
  30. /// The size is the size of the Frame
  31. /// </summary>
  32. public override Rectangle Bounds
  33. {
  34. get => Frame with { Location = Point.Empty };
  35. set => throw new InvalidOperationException ("It makes no sense to set Bounds of a Thickness.");
  36. }
  37. /// <summary>The Parent of this Adornment (the View this Adornment surrounds).</summary>
  38. /// <remarks>
  39. /// Adornments are distinguished from typical View classes in that they are not sub-views, but have a parent/child
  40. /// relationship with their containing View.
  41. /// </remarks>
  42. public View Parent { get; set; }
  43. /// <summary>
  44. /// Adornments cannot be used as sub-views (see <see cref="Parent"/>); this method always throws an
  45. /// <see cref="InvalidOperationException"/>. TODO: Are we sure?
  46. /// </summary>
  47. public override View SuperView
  48. {
  49. get => null;
  50. set => throw new NotImplementedException ();
  51. }
  52. /// <summary>
  53. /// Adornments only render to their <see cref="Parent"/>'s or Parent's SuperView's LineCanvas, so setting this
  54. /// property throws an <see cref="InvalidOperationException"/>.
  55. /// </summary>
  56. public override bool SuperViewRendersLineCanvas
  57. {
  58. get => false; // throw new NotImplementedException ();
  59. set => throw new NotImplementedException ();
  60. }
  61. /// <summary>Defines the rectangle that the <see cref="Adornment"/> will use to draw its content.</summary>
  62. public Thickness Thickness
  63. {
  64. get => _thickness;
  65. set
  66. {
  67. Thickness prev = _thickness;
  68. _thickness = value;
  69. if (prev != _thickness)
  70. {
  71. Parent?.LayoutAdornments ();
  72. OnThicknessChanged (prev);
  73. }
  74. }
  75. }
  76. /// <inheritdoc/>
  77. public override Rectangle FrameToScreen ()
  78. {
  79. if (Parent is null)
  80. {
  81. return Frame;
  82. }
  83. // Adornments are *Children* of a View, not SubViews. Thus View.FrameToScreen will not work.
  84. // To get the screen-relative coordinates of an Adornment, we need get the parent's Frame
  85. // in screen coords, and add our Frame location to it.
  86. Rectangle parent = Parent.FrameToScreen ();
  87. return new (new (parent.X + Frame.X, parent.Y + Frame.Y), Frame.Size);
  88. }
  89. /// <summary>Does nothing for Adornment</summary>
  90. /// <returns></returns>
  91. public override bool OnDrawAdornments () { return false; }
  92. /// <summary>Redraws the Adornments that comprise the <see cref="Adornment"/>.</summary>
  93. public override void OnDrawContent (Rectangle contentArea)
  94. {
  95. if (Thickness == Thickness.Empty)
  96. {
  97. return;
  98. }
  99. Rectangle screenBounds = BoundsToScreen (contentArea);
  100. Attribute normalAttr = GetNormalColor ();
  101. Driver.SetAttribute (normalAttr);
  102. // This just draws/clears the thickness, not the insides.
  103. Thickness.Draw (screenBounds, ToString ());
  104. if (!string.IsNullOrEmpty (TextFormatter.Text))
  105. {
  106. if (TextFormatter is { })
  107. {
  108. TextFormatter.Size = Frame.Size;
  109. TextFormatter.NeedsFormat = true;
  110. }
  111. }
  112. TextFormatter?.Draw (screenBounds, normalAttr, normalAttr, Rectangle.Empty);
  113. //base.OnDrawContent (contentArea);
  114. }
  115. /// <summary>Does nothing for Adornment</summary>
  116. /// <returns></returns>
  117. public override bool OnRenderLineCanvas () { return false; }
  118. /// <summary>Called whenever the <see cref="Thickness"/> property changes.</summary>
  119. public virtual void OnThicknessChanged (Thickness previousThickness)
  120. {
  121. ThicknessChanged?.Invoke (
  122. this,
  123. new() { Thickness = Thickness, PreviousThickness = previousThickness }
  124. );
  125. }
  126. /// <summary>Fired whenever the <see cref="Thickness"/> property changes.</summary>
  127. public event EventHandler<ThicknessEventArgs> ThicknessChanged;
  128. internal override Adornment CreateAdornment (Type adornmentType)
  129. {
  130. /* Do nothing - Adornments do not have Adornments */
  131. return null;
  132. }
  133. internal override void LayoutAdornments ()
  134. {
  135. /* Do nothing - Adornments do not have Adornments */
  136. }
  137. }