ViewAdornments.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. if (Border.Thickness == Thickness.Empty)
  95. {
  96. Border.Thickness = new (1);
  97. }
  98. }
  99. else
  100. {
  101. Border.Thickness = new (0);
  102. }
  103. Border.LineStyle = value;
  104. LayoutAdornments ();
  105. SetNeedsLayout ();
  106. }
  107. }
  108. /// <summary>
  109. /// The <see cref="Adornment"/> inside of the view that offsets the <see cref="Viewport"/>
  110. /// from the <see cref="Border"/>.
  111. /// </summary>
  112. /// <remarks>
  113. /// <para>
  114. /// The adornments (<see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>) are not part of the
  115. /// View's content and are not clipped by the View's Clip Area.
  116. /// </para>
  117. /// <para>
  118. /// Changing the size of a frame (<see cref="Margin"/>, <see cref="Border"/>, or <see cref="Padding"/>) will
  119. /// change the size of the <see cref="Frame"/> and trigger <see cref="LayoutSubviews"/> to update the layout of the
  120. /// <see cref="SuperView"/> and its <see cref="Subviews"/>.
  121. /// </para>
  122. /// </remarks>
  123. public Padding Padding { get; private set; }
  124. /// <summary>
  125. /// <para>Gets the thickness describing the sum of the Adornments' thicknesses.</para>
  126. /// </summary>
  127. /// <returns>A thickness that describes the sum of the Adornments' thicknesses.</returns>
  128. public Thickness GetAdornmentsThickness ()
  129. {
  130. if (Margin is null)
  131. {
  132. return Thickness.Empty;
  133. }
  134. return Margin.Thickness + Border.Thickness + Padding.Thickness;
  135. }
  136. /// <summary>Lays out the Adornments of the View.</summary>
  137. /// <remarks>
  138. /// Overriden by <see cref="Adornment"/> to do nothing, as <see cref="Adornment"/> does not have adornments.
  139. /// </remarks>
  140. internal virtual void LayoutAdornments ()
  141. {
  142. if (Margin is null)
  143. {
  144. return; // CreateAdornments () has not been called yet
  145. }
  146. if (Margin.Frame.Size != Frame.Size)
  147. {
  148. Margin.SetFrame (Rectangle.Empty with { Size = Frame.Size });
  149. Margin.X = 0;
  150. Margin.Y = 0;
  151. Margin.Width = Frame.Size.Width;
  152. Margin.Height = Frame.Size.Height;
  153. }
  154. Margin.SetNeedsLayout ();
  155. Margin.SetNeedsDisplay ();
  156. if (IsInitialized)
  157. {
  158. Margin.LayoutSubviews ();
  159. }
  160. Rectangle border = Margin.Thickness.GetInside (Margin.Frame);
  161. if (border != Border.Frame)
  162. {
  163. Border.SetFrame (border);
  164. Border.X = border.Location.X;
  165. Border.Y = border.Location.Y;
  166. Border.Width = border.Size.Width;
  167. Border.Height = border.Size.Height;
  168. }
  169. Border.SetNeedsLayout ();
  170. Border.SetNeedsDisplay ();
  171. if (IsInitialized)
  172. {
  173. Border.LayoutSubviews ();
  174. }
  175. Rectangle padding = Border.Thickness.GetInside (Border.Frame);
  176. if (padding != Padding.Frame)
  177. {
  178. Padding.SetFrame (padding);
  179. Padding.X = padding.Location.X;
  180. Padding.Y = padding.Location.Y;
  181. Padding.Width = padding.Size.Width;
  182. Padding.Height = padding.Size.Height;
  183. }
  184. Padding.SetNeedsLayout ();
  185. Padding.SetNeedsDisplay ();
  186. if (IsInitialized)
  187. {
  188. Padding.LayoutSubviews ();
  189. }
  190. }
  191. }