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