Dim.cs 12 KB

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