Dim.cs 13 KB

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