Dim.cs 13 KB

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