Pos.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
  5. /// relative to the ending dimension. Integer values are implicitly convertible to an absolute <see cref="Pos"/>. These
  6. /// objects are created using the static methods Percent, AnchorEnd, and Center. The <see cref="Pos"/> objects can be
  7. /// combined with the addition and subtraction operators.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.</para>
  11. /// <para>
  12. /// These can be used to set the absolute position, when merely assigning an integer value (via the implicit
  13. /// integer to <see cref="Pos"/> conversion), and they can be combined to produce more useful layouts, like:
  14. /// Pos.Center - 3, which would shift the position of the <see cref="View"/> 3 characters to the left after
  15. /// centering for example.
  16. /// </para>
  17. /// <para>
  18. /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View).
  19. /// The X(View) and Y(View) are aliases to Left(View) and Top(View) respectively.
  20. /// </para>
  21. /// <para>
  22. /// <list type="table">
  23. /// <listheader>
  24. /// <term>Pos Object</term> <description>Description</description>
  25. /// </listheader>
  26. /// <item>
  27. /// <term>
  28. /// <see cref="Func"/>
  29. /// </term>
  30. /// <description>
  31. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  32. /// function. The function will be called every time the position is needed.
  33. /// </description>
  34. /// </item>
  35. /// <item>
  36. /// <term>
  37. /// <see cref="Pos.Percent(float)"/>
  38. /// </term>
  39. /// <description>
  40. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  41. /// SuperView.
  42. /// </description>
  43. /// </item>
  44. /// <item>
  45. /// <term>
  46. /// <see cref="Pos.AnchorEnd()"/>
  47. /// </term>
  48. /// <description>
  49. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of
  50. /// the dimension, useful to flush the layout from the right or bottom.
  51. /// </description>
  52. /// </item>
  53. /// <item>
  54. /// <term>
  55. /// <see cref="Pos.Center"/>
  56. /// </term>
  57. /// <description>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</description>
  58. /// </item>
  59. /// <item>
  60. /// <term>
  61. /// <see cref="Absolute"/>
  62. /// </term>
  63. /// <description>
  64. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  65. /// integer value.
  66. /// </description>
  67. /// </item>
  68. /// <item>
  69. /// <term>
  70. /// <see cref="Pos.Left"/>
  71. /// </term>
  72. /// <description>
  73. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  74. /// <see cref="View"/>.
  75. /// </description>
  76. /// </item>
  77. /// <item>
  78. /// <term>
  79. /// <see cref="Pos.X(View)"/>
  80. /// </term>
  81. /// <description>
  82. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  83. /// <see cref="View"/>.
  84. /// </description>
  85. /// </item>
  86. /// <item>
  87. /// <term>
  88. /// <see cref="Pos.Top(View)"/>
  89. /// </term>
  90. /// <description>
  91. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  92. /// <see cref="View"/>.
  93. /// </description>
  94. /// </item>
  95. /// <item>
  96. /// <term>
  97. /// <see cref="Pos.Y(View)"/>
  98. /// </term>
  99. /// <description>
  100. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  101. /// <see cref="View"/>.
  102. /// </description>
  103. /// </item>
  104. /// <item>
  105. /// <term>
  106. /// <see cref="Pos.Right(View)"/>
  107. /// </term>
  108. /// <description>
  109. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  110. /// specified <see cref="View"/>.
  111. /// </description>
  112. /// </item>
  113. /// <item>
  114. /// <term>
  115. /// <see cref="Pos.Bottom(View)"/>
  116. /// </term>
  117. /// <description>
  118. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  119. /// specified <see cref="View"/>
  120. /// </description>
  121. /// </item>
  122. /// </list>
  123. /// </para>
  124. /// </remarks>
  125. public abstract class Pos
  126. {
  127. #region static Pos creation methods
  128. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  129. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  130. /// <param name="position">The value to convert to the <see cref="Pos"/>.</param>
  131. public static Pos Absolute (int position) { return new PosAbsolute (position); }
  132. /// <summary>
  133. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  134. /// bottom) of the SuperView's Content Area, minus the respective size of the View. This is equivalent to using
  135. /// <see cref="Pos.AnchorEnd(int)"/>,
  136. /// with an offset equivalent to the View's respective dimension.
  137. /// </summary>
  138. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  139. /// <example>
  140. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  141. /// <code>
  142. /// anchorButton.X = Pos.AnchorEnd ();
  143. /// anchorButton.Y = Pos.AnchorEnd ();
  144. /// </code>
  145. /// </example>
  146. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  147. /// <summary>
  148. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView's Content
  149. /// Area,
  150. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  151. /// dimension to ensure the view is fully visible.
  152. /// </summary>
  153. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  154. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  155. /// <example>
  156. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  157. /// <code>
  158. /// anchorButton.X = Pos.AnchorEnd (10);
  159. /// anchorButton.Y = 1
  160. /// </code>
  161. /// </example>
  162. public static Pos AnchorEnd (int offset)
  163. {
  164. if (offset < 0)
  165. {
  166. throw new ArgumentException (@"Must be positive", nameof (offset));
  167. }
  168. return new PosAnchorEnd (offset);
  169. }
  170. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  171. /// <returns>The center Pos.</returns>
  172. /// <example>
  173. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  174. /// is 80% the width of the <see cref="View"/> it added to.
  175. /// <code>
  176. /// var textView = new TextView () {
  177. /// X = Pos.Center (),
  178. /// Y = Pos.Percent (50),
  179. /// Width = Dim.Percent (80),
  180. /// Height = Dim.Percent (30),
  181. /// };
  182. /// </code>
  183. /// </example>
  184. public static Pos Center () { return new PosCenter (); }
  185. /// <summary>
  186. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  187. /// will be called every time the position is needed.
  188. /// </summary>
  189. /// <param name="function">The function to be executed.</param>
  190. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  191. public static Pos Func (Func<int> function) { return new PosFunc (function); }
  192. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  193. /// <returns>The percent <see cref="Pos"/> object.</returns>
  194. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  195. /// <example>
  196. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  197. /// is 80% the width of the <see cref="View"/> it added to.
  198. /// <code>
  199. /// var textView = new TextField {
  200. /// X = Pos.Center (),
  201. /// Y = Pos.Percent (50),
  202. /// Width = Dim.Percent (80),
  203. /// Height = Dim.Percent (30),
  204. /// };
  205. /// </code>
  206. /// </example>
  207. public static Pos Percent (float percent)
  208. {
  209. if (percent is < 0 or > 100)
  210. {
  211. throw new ArgumentException ("Percent value must be between 0 and 100.");
  212. }
  213. return new PosPercent (percent / 100);
  214. }
  215. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  216. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  217. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  218. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  219. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  220. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  221. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  222. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  223. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  224. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  225. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  226. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  227. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  228. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  229. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  230. public static Pos X (View view) { return new PosView (view, Side.Left); }
  231. /// <summary>
  232. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  233. /// <see cref="View"/>
  234. /// </summary>
  235. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  236. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  237. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  238. /// <summary>
  239. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  240. /// <see cref="View"/>.
  241. /// </summary>
  242. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  243. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  244. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  245. #endregion static Pos creation methods
  246. #region virtual methods
  247. /// <summary>
  248. /// Gets the starting point of an element based on the size of the parent element (typically
  249. /// <c>Superview.ContentSize</c>).
  250. /// This method is meant to be overridden by subclasses to provide different ways of calculating the starting point.
  251. /// This method is used
  252. /// internally by the layout system to determine where a View should be positioned.
  253. /// </summary>
  254. /// <param name="size">The size of the parent element (typically <c>Superview.ContentSize</c>).</param>
  255. /// <returns>
  256. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  257. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  258. /// position that is anchored to the end of the layout, and so on.
  259. /// </returns>
  260. internal virtual int GetAnchor (int size) { return 0; }
  261. /// <summary>
  262. /// Calculates and returns the final position of a <see cref="View"/> object. It takes into account the dimension of
  263. /// the
  264. /// superview and the dimension of the view itself.
  265. /// </summary>
  266. /// <remarks>
  267. /// </remarks>
  268. /// <param name="superviewDimension">
  269. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  270. /// height for y-coordinate calculation.
  271. /// </param>
  272. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  273. /// <param name="us">The View that holds this Pos object.</param>
  274. /// <param name="dimension">Width or Height</param>
  275. /// <returns>
  276. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  277. /// that
  278. /// is used.
  279. /// </returns>
  280. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return GetAnchor (superviewDimension); }
  281. /// <summary>
  282. /// Diagnostics API to determine if this Pos object references other views.
  283. /// </summary>
  284. /// <returns></returns>
  285. internal virtual bool ReferencesOtherViews () { return false; }
  286. #endregion virtual methods
  287. #region operators
  288. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  289. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  290. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  291. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  292. public static Pos operator + (Pos left, Pos right)
  293. {
  294. if (left is PosAbsolute && right is PosAbsolute)
  295. {
  296. return new PosAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
  297. }
  298. var newPos = new PosCombine (AddOrSubtract.Add, left, right);
  299. if (left is PosView view)
  300. {
  301. view.Target.SetNeedsLayout ();
  302. }
  303. return newPos;
  304. }
  305. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  306. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  307. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  308. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  309. /// <summary>
  310. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  311. /// <see cref="Pos"/>.
  312. /// </summary>
  313. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  314. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  315. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  316. public static Pos operator - (Pos left, Pos right)
  317. {
  318. if (left is PosAbsolute && right is PosAbsolute)
  319. {
  320. return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
  321. }
  322. var newPos = new PosCombine (AddOrSubtract.Subtract, left, right);
  323. if (left is PosView view)
  324. {
  325. view.Target.SetNeedsLayout ();
  326. }
  327. return newPos;
  328. }
  329. #endregion operators
  330. }