View.Adornments.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. namespace Terminal.Gui;
  2. public partial class View // Adornments
  3. {
  4. /// <summary>
  5. /// Initializes the Adornments of the View. Called by the constructor.
  6. /// </summary>
  7. private void SetupAdornments ()
  8. {
  9. //// TODO: Move this to Adornment as a static factory method
  10. if (this is not Adornment)
  11. {
  12. Margin = new (this);
  13. Border = new (this);
  14. Padding = new (this);
  15. }
  16. }
  17. private void BeginInitAdornments ()
  18. {
  19. Margin?.BeginInit ();
  20. Border?.BeginInit ();
  21. Padding?.BeginInit ();
  22. }
  23. private void EndInitAdornments ()
  24. {
  25. Margin?.EndInit ();
  26. Border?.EndInit ();
  27. Padding?.EndInit ();
  28. }
  29. private void DisposeAdornments ()
  30. {
  31. Margin?.Dispose ();
  32. Margin = null;
  33. Border?.Dispose ();
  34. Border = null;
  35. Padding?.Dispose ();
  36. Padding = null;
  37. }
  38. /// <summary>
  39. /// The <see cref="Adornment"/> that enables separation of a View from other SubViews of the same
  40. /// SuperView. The margin offsets the <see cref="Viewport"/> from the <see cref="Frame"/>.
  41. /// </summary>
  42. /// <remarks>
  43. /// <para>
  44. /// Enabling <see cref="ShadowStyle"/> will change the Thickness of the Margin to include the shadow.
  45. /// </para>
  46. /// <para>
  47. /// The adornments (<see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>) are not part of the
  48. /// View's content and are not clipped by the View's Clip Area.
  49. /// </para>
  50. /// <para>
  51. /// Changing the size of an adornment (<see cref="Margin"/>, <see cref="Border"/>, or <see cref="Padding"/>) will
  52. /// change the size of <see cref="Frame"/> which will call <see cref="SetNeedsLayout"/> to update the layout of the
  53. /// <see cref="SuperView"/> and its <see cref="Subviews"/>.
  54. /// </para>
  55. /// </remarks>
  56. public Margin Margin { get; private set; }
  57. private ShadowStyle _shadowStyle;
  58. /// <summary>
  59. /// Gets or sets whether the View is shown with a shadow effect. The shadow is drawn on the right and bottom sides of
  60. /// the
  61. /// Margin.
  62. /// </summary>
  63. /// <remarks>
  64. /// Setting this property to <see langword="true"/> will add a shadow to the right and bottom sides of the Margin.
  65. /// The View 's <see cref="Frame"/> will be expanded to include the shadow.
  66. /// </remarks>
  67. public virtual ShadowStyle ShadowStyle
  68. {
  69. get => _shadowStyle;
  70. set
  71. {
  72. if (_shadowStyle == value)
  73. {
  74. return;
  75. }
  76. _shadowStyle = value;
  77. if (Margin is { })
  78. {
  79. Margin.ShadowStyle = value;
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// The <see cref="Adornment"/> that offsets the <see cref="Viewport"/> from the <see cref="Margin"/>.
  85. /// <para>
  86. /// The Border provides the space for a visual border (drawn using
  87. /// line-drawing glyphs) and the Title. The Border expands inward; in other words if `Border.Thickness.Top == 2`
  88. /// the
  89. /// border and title will take up the first row and the second row will be filled with spaces.
  90. /// </para>
  91. /// <para>
  92. /// The Border provides the UI for mouse and keyboard arrangement of the View. See <see cref="Arrangement"/>.
  93. /// </para>
  94. /// </summary>
  95. /// <remarks>
  96. /// <para><see cref="BorderStyle"/> provides a simple helper for turning a simple border frame on or off.</para>
  97. /// <para>
  98. /// The adornments (<see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>) are not part of the
  99. /// View's content and are not clipped by the View's Clip Area.
  100. /// </para>
  101. /// <para>
  102. /// Changing the size of an adornment (<see cref="Margin"/>, <see cref="Border"/>, or <see cref="Padding"/>) will
  103. /// change the size of <see cref="Frame"/> which will call <see cref="SetNeedsLayout"/> to update the layout of the
  104. /// <see cref="SuperView"/> and its <see cref="Subviews"/>.
  105. /// </para>
  106. /// </remarks>
  107. public Border Border { get; private set; }
  108. /// <summary>Gets or sets whether the view has a one row/col thick border.</summary>
  109. /// <remarks>
  110. /// <para>
  111. /// This is a helper for manipulating the view's <see cref="Border"/>. Setting this property to any value other
  112. /// than <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
  113. /// <see cref="Adornment.Thickness"/> to `1` and <see cref="BorderStyle"/> to the value.
  114. /// </para>
  115. /// <para>
  116. /// Setting this property to <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
  117. /// <see cref="Adornment.Thickness"/> to `0` and <see cref="BorderStyle"/> to <see cref="LineStyle.None"/>.
  118. /// </para>
  119. /// <para>For more advanced customization of the view's border, manipulate see <see cref="Border"/> directly.</para>
  120. /// </remarks>
  121. public LineStyle BorderStyle
  122. {
  123. get => Border?.LineStyle ?? LineStyle.Single;
  124. set
  125. {
  126. if (Border is null)
  127. {
  128. return;
  129. }
  130. LineStyle old = Border?.LineStyle ?? LineStyle.None;
  131. CancelEventArgs<LineStyle> e = new (ref old, ref value);
  132. if (OnBorderStyleChanging (e) || e.Cancel)
  133. {
  134. return;
  135. }
  136. SetBorderStyle (e.NewValue);
  137. SetAdornmentFrames ();
  138. SetNeedsLayout ();
  139. }
  140. }
  141. /// <summary>
  142. /// Called when the <see cref="BorderStyle"/> is changing. Invokes <see cref="BorderStyleChanging"/>, which allows the
  143. /// event to be cancelled.
  144. /// </summary>
  145. /// <remarks>
  146. /// Override <see cref="SetBorderStyle"/> to prevent the <see cref="BorderStyle"/> from changing.
  147. /// </remarks>
  148. /// <param name="e"></param>
  149. protected virtual bool OnBorderStyleChanging (CancelEventArgs<LineStyle> e)
  150. {
  151. if (Border is null)
  152. {
  153. return false;
  154. }
  155. BorderStyleChanging?.Invoke (this, e);
  156. return e.Cancel;
  157. }
  158. /// <summary>
  159. /// Sets the <see cref="BorderStyle"/> of the view to the specified value.
  160. /// </summary>
  161. /// <remarks>
  162. /// <para>
  163. /// <see cref="BorderStyle"/> is a helper for manipulating the view's <see cref="Border"/>. Setting this property
  164. /// to any value other
  165. /// than <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
  166. /// <see cref="Adornment.Thickness"/> to `1` and <see cref="BorderStyle"/> to the value.
  167. /// </para>
  168. /// <para>
  169. /// Setting this property to <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
  170. /// <see cref="Adornment.Thickness"/> to `0` and <see cref="BorderStyle"/> to <see cref="LineStyle.None"/>.
  171. /// </para>
  172. /// <para>For more advanced customization of the view's border, manipulate see <see cref="Border"/> directly.</para>
  173. /// </remarks>
  174. /// <param name="value"></param>
  175. public virtual void SetBorderStyle (LineStyle value)
  176. {
  177. if (value != LineStyle.None)
  178. {
  179. if (Border.Thickness == Thickness.Empty)
  180. {
  181. Border.Thickness = new (1);
  182. }
  183. }
  184. else
  185. {
  186. Border.Thickness = new (0);
  187. }
  188. Border.LineStyle = value;
  189. }
  190. /// <summary>
  191. /// Fired when the <see cref="BorderStyle"/> is changing. Allows the event to be cancelled.
  192. /// </summary>
  193. [CanBeNull]
  194. public event EventHandler<CancelEventArgs<LineStyle>> BorderStyleChanging;
  195. /// <summary>
  196. /// The <see cref="Adornment"/> inside of the view that offsets the <see cref="Viewport"/>
  197. /// from the <see cref="Border"/>.
  198. /// </summary>
  199. /// <remarks>
  200. /// <para>
  201. /// The adornments (<see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>) are not part of the
  202. /// View's content and are not clipped by the View's Clip Area.
  203. /// </para>
  204. /// <para>
  205. /// Changing the size of an adornment (<see cref="Margin"/>, <see cref="Border"/>, or <see cref="Padding"/>) will
  206. /// change the size of <see cref="Frame"/> which will call <see cref="SetNeedsLayout"/> to update the layout of the
  207. /// <see cref="SuperView"/> and its <see cref="Subviews"/>.
  208. /// </para>
  209. /// </remarks>
  210. public Padding Padding { get; private set; }
  211. /// <summary>
  212. /// <para>Gets the thickness describing the sum of the Adornments' thicknesses.</para>
  213. /// </summary>
  214. /// <remarks>
  215. /// <para>
  216. /// The <see cref="Viewport"/> is offset from the <see cref="Frame"/> by the thickness returned by this method.
  217. /// </para>
  218. /// </remarks>
  219. /// <returns>A thickness that describes the sum of the Adornments' thicknesses.</returns>
  220. public Thickness GetAdornmentsThickness ()
  221. {
  222. if (Margin is null)
  223. {
  224. return Thickness.Empty;
  225. }
  226. return Margin.Thickness + Border.Thickness + Padding.Thickness;
  227. }
  228. /// <summary>Sets the Frame's of the Margin, Border, and Padding.</summary>
  229. internal void SetAdornmentFrames ()
  230. {
  231. if (this is Adornment)
  232. {
  233. // Adornments do not have Adornments
  234. return;
  235. }
  236. if (Margin is null)
  237. {
  238. return; // CreateAdornments () has not been called yet
  239. }
  240. Margin.Frame = Rectangle.Empty with { Size = Frame.Size };
  241. Border.Frame = Margin.Thickness.GetInside (Margin.Frame);
  242. Padding.Frame = Border.Thickness.GetInside (Border.Frame);
  243. }
  244. }