Dim.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// <para>
  6. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  7. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  8. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  9. /// </para>
  10. /// <para>
  11. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  12. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  13. /// subtraction operators.
  14. /// </para>
  15. /// </summary>
  16. /// <remarks>
  17. /// <para>
  18. /// <list type="table">
  19. /// <listheader>
  20. /// <term>Dim Object</term> <description>Description</description>
  21. /// </listheader>
  22. /// <item>
  23. /// <term>
  24. /// <see cref="Dim.Auto"/>
  25. /// </term>
  26. /// <description>
  27. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit
  28. /// the view's Text, SubViews, or ContentArea.
  29. /// </description>
  30. /// </item>
  31. /// <item>
  32. /// <term>
  33. /// <see cref="Func"/>
  34. /// </term>
  35. /// <description>
  36. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  37. /// function. The function will be called every time the dimension is needed.
  38. /// </description>
  39. /// </item>
  40. /// <item>
  41. /// <term>
  42. /// <see cref="Dim.Percent(int, DimPercentMode)"/>
  43. /// </term>
  44. /// <description>
  45. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  46. /// SuperView.
  47. /// </description>
  48. /// </item>
  49. /// <item>
  50. /// <term>
  51. /// <see cref="Dim.Fill(int)"/>
  52. /// </term>
  53. /// <description>
  54. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  55. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  56. /// </description>
  57. /// </item>
  58. /// <item>
  59. /// <term>
  60. /// <see cref="Dim.Width(View)"/>
  61. /// </term>
  62. /// <description>
  63. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  64. /// <see cref="View"/>.
  65. /// </description>
  66. /// </item>
  67. /// <item>
  68. /// <term>
  69. /// <see cref="Dim.Height(View)"/>
  70. /// </term>
  71. /// <description>
  72. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  73. /// <see cref="View"/>.
  74. /// </description>
  75. /// </item>
  76. /// </list>
  77. /// </para>
  78. /// <para></para>
  79. /// </remarks>
  80. public abstract class Dim
  81. {
  82. #region static Dim creation methods
  83. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  84. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  85. /// <param name="size">The value to convert to the <see cref="Dim"/>.</param>
  86. public static Dim? Absolute (int size) { return new DimAbsolute (size); }
  87. /// <summary>
  88. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all the view's Content, Subviews, and/or Text.
  89. /// </summary>
  90. /// <remarks>
  91. /// <para>
  92. /// See <see cref="DimAutoStyle"/>.
  93. /// </para>
  94. /// </remarks>
  95. /// <example>
  96. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  97. /// SubViews.
  98. /// <code>
  99. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  100. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  101. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  102. /// view.Add (button, textField);
  103. /// </code>
  104. /// </example>
  105. /// <returns>The <see cref="Dim"/> object.</returns>
  106. /// <param name="style">
  107. /// Specifies how <see cref="Dim.Auto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  108. /// </param>
  109. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  110. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  111. public static Dim? Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim? minimumContentDim = null, Dim? maximumContentDim = null)
  112. {
  113. if (maximumContentDim is { })
  114. {
  115. Debug.WriteLine (@"WARNING: maximumContentDim is not fully implemented.");
  116. }
  117. return new DimAuto ()
  118. {
  119. MinimumContentDim = minimumContentDim,
  120. MaximumContentDim = maximumContentDim,
  121. Style = style
  122. };
  123. }
  124. /// <summary>
  125. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified margin.
  126. /// </summary>
  127. /// <returns>The Fill dimension.</returns>
  128. /// <param name="margin">Margin to use.</param>
  129. public static Dim? Fill (int margin = 0) { return new DimFill (margin); }
  130. /// <summary>
  131. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  132. /// The function will be called every time the dimension is needed.
  133. /// </summary>
  134. /// <param name="function">The function to be executed.</param>
  135. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  136. public static Dim Func (Func<int> function) { return new DimFunc (function); }
  137. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  138. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  139. /// <param name="view">The view that will be tracked.</param>
  140. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  141. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  142. /// <returns>The percent <see cref="Dim"/> object.</returns>
  143. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  144. /// <param name="usePosition">
  145. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  146. /// <see cref="View.Y"/>).
  147. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  148. /// </param>
  149. /// <example>
  150. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  151. /// height,
  152. /// and is 80% the width of the SuperView.
  153. /// <code>
  154. /// var textView = new TextField {
  155. /// X = Pos.Center (),
  156. /// Y = Pos.Percent (50),
  157. /// Width = Dim.Percent (80),
  158. /// Height = Dim.Percent (30),
  159. /// };
  160. /// </code>
  161. /// </example>
  162. public static Dim? Percent (int percent, DimPercentMode mode = DimPercentMode.ContentSize)
  163. {
  164. if (percent is < 0 /*or > 100*/)
  165. {
  166. throw new ArgumentException ("Percent value must be positive.");
  167. }
  168. return new DimPercent (percent, mode);
  169. }
  170. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  171. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  172. /// <param name="view">The view that will be tracked.</param>
  173. public static Dim Width (View view) { return new DimView (view, Dimension.Width); }
  174. #endregion static Dim creation methods
  175. #region virtual methods
  176. /// <summary>
  177. /// Gets a dimension that is anchored to a certain point in the layout.
  178. /// This method is typically used internally by the layout system to determine the size of a View.
  179. /// </summary>
  180. /// <param name="size">The width of the area where the View is being sized (Superview.ContentSize).</param>
  181. /// <returns>
  182. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  183. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  184. /// dimension that is a certain percentage of the super view's size, and so on.
  185. /// </returns>
  186. internal virtual int GetAnchor (int size) { return 0; }
  187. /// <summary>
  188. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  189. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its
  190. /// content.
  191. /// </summary>
  192. /// <param name="location">
  193. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  194. /// top edge for height calculation.
  195. /// </param>
  196. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  197. /// <param name="us">The View that holds this Pos object.</param>
  198. /// <param name="dimension">Width or Height</param>
  199. /// <returns>
  200. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  201. /// is used.
  202. /// </returns>
  203. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  204. {
  205. return Math.Max (GetAnchor (superviewContentSize - location), 0);
  206. }
  207. /// <summary>
  208. /// Diagnostics API to determine if this Dim object references other views.
  209. /// </summary>
  210. /// <returns></returns>
  211. internal virtual bool ReferencesOtherViews () { return false; }
  212. #endregion virtual methods
  213. #region operators
  214. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  215. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  216. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  217. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  218. public static Dim operator + (Dim? left, Dim? right)
  219. {
  220. if (left is DimAbsolute && right is DimAbsolute)
  221. {
  222. return new DimAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
  223. }
  224. var newDim = new DimCombine (AddOrSubtract.Add, left, right);
  225. (left as DimView)?.Target.SetNeedsLayout ();
  226. return newDim;
  227. }
  228. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  229. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  230. /// <param name="n">The value to convert to the pos.</param>
  231. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  232. /// <summary>
  233. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  234. /// <see cref="Dim"/>.
  235. /// </summary>
  236. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  237. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  238. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  239. public static Dim operator - (Dim? left, Dim? right)
  240. {
  241. if (left is DimAbsolute && right is DimAbsolute)
  242. {
  243. return new DimAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
  244. }
  245. var newDim = new DimCombine (AddOrSubtract.Subtract, left, right);
  246. (left as DimView)?.Target.SetNeedsLayout ();
  247. return newDim;
  248. }
  249. #endregion operators
  250. }