ViewAdornments.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. namespace Terminal.Gui;
  2. public partial class View
  3. {
  4. private void CreateAdornments ()
  5. {
  6. //// TODO: Move this to Adornment as a static factory method
  7. if (this is not Adornment)
  8. {
  9. Margin = new (this);
  10. Border = new (this);
  11. Padding = new (this);
  12. }
  13. }
  14. private void BeginInitAdornments ()
  15. {
  16. Margin?.BeginInit ();
  17. Border?.BeginInit ();
  18. Padding?.BeginInit ();
  19. }
  20. private void EndInitAdornments ()
  21. {
  22. Margin?.EndInit ();
  23. Border?.EndInit ();
  24. Padding?.EndInit ();
  25. }
  26. private void DisposeAdornments ()
  27. {
  28. Margin?.Dispose ();
  29. Margin = null;
  30. Border?.Dispose ();
  31. Border = null;
  32. Padding?.Dispose ();
  33. Padding = null;
  34. }
  35. /// <summary>
  36. /// The <see cref="Adornment"/> that enables separation of a View from other SubViews of the same
  37. /// SuperView. The margin offsets the <see cref="Viewport"/> from the <see cref="Frame"/>.
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// The adornments (<see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>) are not part of the
  42. /// View's content and are not clipped by the View's Clip Area.
  43. /// </para>
  44. /// <para>
  45. /// Changing the size of an adornment (<see cref="Margin"/>, <see cref="Border"/>, or <see cref="Padding"/>) will
  46. /// change the size of <see cref="Frame"/> and trigger <see cref="LayoutSubviews"/> to update the layout of the
  47. /// <see cref="SuperView"/> and its <see cref="Subviews"/>.
  48. /// </para>
  49. /// </remarks>
  50. public Margin Margin { get; private set; }
  51. /// <summary>
  52. /// The <see cref="Adornment"/> that offsets the <see cref="Viewport"/> from the <see cref="Margin"/>.
  53. /// The Border provides the space for a visual border (drawn using
  54. /// line-drawing glyphs) and the Title. The Border expands inward; in other words if `Border.Thickness.Top == 2` the
  55. /// border and title will take up the first row and the second row will be filled with spaces.
  56. /// </summary>
  57. /// <remarks>
  58. /// <para><see cref="BorderStyle"/> provides a simple helper for turning a simple border frame on or off.</para>
  59. /// <para>
  60. /// The adornments (<see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>) are not part of the
  61. /// View's content and are not clipped by the View's Clip Area.
  62. /// </para>
  63. /// <para>
  64. /// Changing the size of a frame (<see cref="Margin"/>, <see cref="Border"/>, or <see cref="Padding"/>) will
  65. /// change the size of the <see cref="Frame"/> and trigger <see cref="LayoutSubviews"/> to update the layout of the
  66. /// <see cref="SuperView"/> and its <see cref="Subviews"/>.
  67. /// </para>
  68. /// </remarks>
  69. public Border Border { get; private set; }
  70. /// <summary>Gets or sets whether the view has a one row/col thick border.</summary>
  71. /// <remarks>
  72. /// <para>
  73. /// This is a helper for manipulating the view's <see cref="Border"/>. Setting this property to any value other
  74. /// than <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
  75. /// <see cref="Adornment.Thickness"/> to `1` and <see cref="BorderStyle"/> to the value.
  76. /// </para>
  77. /// <para>
  78. /// Setting this property to <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
  79. /// <see cref="Adornment.Thickness"/> to `0` and <see cref="BorderStyle"/> to <see cref="LineStyle.None"/>.
  80. /// </para>
  81. /// <para>For more advanced customization of the view's border, manipulate see <see cref="Border"/> directly.</para>
  82. /// </remarks>
  83. public LineStyle BorderStyle
  84. {
  85. get => Border?.LineStyle ?? LineStyle.Single;
  86. set
  87. {
  88. if (Border is null)
  89. {
  90. return;
  91. }
  92. if (value != LineStyle.None)
  93. {
  94. Border.Thickness = new (1);
  95. }
  96. else
  97. {
  98. Border.Thickness = new (0);
  99. }
  100. Border.LineStyle = value;
  101. LayoutAdornments ();
  102. SetNeedsLayout ();
  103. }
  104. }
  105. /// <summary>
  106. /// The <see cref="Adornment"/> inside of the view that offsets the <see cref="Viewport"/>
  107. /// from the <see cref="Border"/>.
  108. /// </summary>
  109. /// <remarks>
  110. /// <para>
  111. /// The adornments (<see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>) are not part of the
  112. /// View's content and are not clipped by the View's Clip Area.
  113. /// </para>
  114. /// <para>
  115. /// Changing the size of a frame (<see cref="Margin"/>, <see cref="Border"/>, or <see cref="Padding"/>) will
  116. /// change the size of the <see cref="Frame"/> and trigger <see cref="LayoutSubviews"/> to update the layout of the
  117. /// <see cref="SuperView"/> and its <see cref="Subviews"/>.
  118. /// </para>
  119. /// </remarks>
  120. public Padding Padding { get; private set; }
  121. /// <summary>
  122. /// <para>Gets the thickness describing the sum of the Adornments' thicknesses.</para>
  123. /// </summary>
  124. /// <returns>A thickness that describes the sum of the Adornments' thicknesses.</returns>
  125. public Thickness GetAdornmentsThickness () { return Margin.Thickness + Border.Thickness + Padding.Thickness; }
  126. /// <summary>Lays out the Adornments of the View.</summary>
  127. /// <remarks>
  128. /// Overriden by <see cref="Adornment"/> to do nothing, as <see cref="Adornment"/> does not have adornments.
  129. /// </remarks>
  130. internal virtual void LayoutAdornments ()
  131. {
  132. if (Margin is null)
  133. {
  134. return; // CreateAdornments () has not been called yet
  135. }
  136. if (Margin.Frame.Size != Frame.Size)
  137. {
  138. Margin._frame = Rectangle.Empty with { Size = Frame.Size };
  139. Margin.X = 0;
  140. Margin.Y = 0;
  141. Margin.Width = Frame.Size.Width;
  142. Margin.Height = Frame.Size.Height;
  143. }
  144. Margin.SetNeedsLayout ();
  145. Margin.SetNeedsDisplay ();
  146. if (IsInitialized)
  147. {
  148. Margin.LayoutSubviews ();
  149. }
  150. Rectangle border = Margin.Thickness.GetInside (Margin.Frame);
  151. if (border != Border.Frame)
  152. {
  153. Border._frame = border;
  154. Border.X = border.Location.X;
  155. Border.Y = border.Location.Y;
  156. Border.Width = border.Size.Width;
  157. Border.Height = border.Size.Height;
  158. }
  159. Border.SetNeedsLayout ();
  160. Border.SetNeedsDisplay ();
  161. if (IsInitialized)
  162. {
  163. Border.LayoutSubviews ();
  164. }
  165. Rectangle padding = Border.Thickness.GetInside (Border.Frame);
  166. if (padding != Padding.Frame)
  167. {
  168. Padding._frame = padding;
  169. Padding.X = padding.Location.X;
  170. Padding.Y = padding.Location.Y;
  171. Padding.Width = padding.Size.Width;
  172. Padding.Height = padding.Size.Height;
  173. }
  174. Padding.SetNeedsLayout ();
  175. Padding.SetNeedsDisplay ();
  176. if (IsInitialized)
  177. {
  178. Padding.LayoutSubviews ();
  179. }
  180. }
  181. }