Pos.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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="Pos.Align"/>
  29. /// </term>
  30. /// <description>
  31. /// Creates a <see cref="Pos"/> object that aligns a set of views.
  32. /// </description>
  33. /// </item>
  34. /// <item>
  35. /// <term>
  36. /// <see cref="Func"/>
  37. /// </term>
  38. /// <description>
  39. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  40. /// function. The function will be called every time the position is needed.
  41. /// </description>
  42. /// </item>
  43. /// <item>
  44. /// <term>
  45. /// <see cref="Pos.Percent(int)"/>
  46. /// </term>
  47. /// <description>
  48. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  49. /// SuperView.
  50. /// </description>
  51. /// </item>
  52. /// <item>
  53. /// <term>
  54. /// <see cref="Pos.AnchorEnd()"/>
  55. /// </term>
  56. /// <description>
  57. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of
  58. /// the dimension, useful to flush the layout from the right or bottom.
  59. /// </description>
  60. /// </item>
  61. /// <item>
  62. /// <term>
  63. /// <see cref="Pos.Center"/>
  64. /// </term>
  65. /// <description>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</description>
  66. /// </item>
  67. /// <item>
  68. /// <term>
  69. /// <see cref="Absolute"/>
  70. /// </term>
  71. /// <description>
  72. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  73. /// integer value.
  74. /// </description>
  75. /// </item>
  76. /// <item>
  77. /// <term>
  78. /// <see cref="Pos.Left"/>
  79. /// </term>
  80. /// <description>
  81. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  82. /// <see cref="View"/>.
  83. /// </description>
  84. /// </item>
  85. /// <item>
  86. /// <term>
  87. /// <see cref="Pos.X(View)"/>
  88. /// </term>
  89. /// <description>
  90. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  91. /// <see cref="View"/>.
  92. /// </description>
  93. /// </item>
  94. /// <item>
  95. /// <term>
  96. /// <see cref="Pos.Top(View)"/>
  97. /// </term>
  98. /// <description>
  99. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  100. /// <see cref="View"/>.
  101. /// </description>
  102. /// </item>
  103. /// <item>
  104. /// <term>
  105. /// <see cref="Pos.Y(View)"/>
  106. /// </term>
  107. /// <description>
  108. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  109. /// <see cref="View"/>.
  110. /// </description>
  111. /// </item>
  112. /// <item>
  113. /// <term>
  114. /// <see cref="Pos.Right(View)"/>
  115. /// </term>
  116. /// <description>
  117. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  118. /// specified <see cref="View"/>.
  119. /// </description>
  120. /// </item>
  121. /// <item>
  122. /// <term>
  123. /// <see cref="Pos.Bottom(View)"/>
  124. /// </term>
  125. /// <description>
  126. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  127. /// specified <see cref="View"/>
  128. /// </description>
  129. /// </item>
  130. /// </list>
  131. /// </para>
  132. /// </remarks>
  133. public abstract record Pos
  134. {
  135. #region static Pos creation methods
  136. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  137. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  138. /// <param name="position">The value to convert to the <see cref="Pos"/>.</param>
  139. public static Pos Absolute (int position) { return new PosAbsolute (position); }
  140. /// <summary>
  141. /// Creates a <see cref="Pos"/> object that aligns a set of views according to the specified <see cref="Alignment"/>
  142. /// and <see cref="AlignmentModes"/>.
  143. /// </summary>
  144. /// <param name="alignment">The alignment. The default includes <see cref="AlignmentModes.AddSpaceBetweenItems"/>.</param>
  145. /// <param name="modes">The optional alignment modes.</param>
  146. /// <param name="groupId">
  147. /// The optional identifier of a set of views that should be aligned together. When only a single
  148. /// set of views in a SuperView is aligned, this parameter is optional.
  149. /// </param>
  150. /// <returns></returns>
  151. public static Pos Align (Alignment alignment, AlignmentModes modes = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems, int groupId = 0)
  152. {
  153. return new PosAlign
  154. {
  155. Aligner = new ()
  156. {
  157. Alignment = alignment,
  158. AlignmentModes = modes
  159. },
  160. GroupId = groupId
  161. };
  162. }
  163. /// <summary>
  164. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  165. /// bottom) of the SuperView's Content Area, minus the respective size of the View. This is equivalent to using
  166. /// <see cref="Pos.AnchorEnd(int)"/>,
  167. /// with an offset equivalent to the View's respective dimension.
  168. /// </summary>
  169. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  170. /// <example>
  171. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  172. /// <code>
  173. /// anchorButton.X = Pos.AnchorEnd ();
  174. /// anchorButton.Y = Pos.AnchorEnd ();
  175. /// </code>
  176. /// </example>
  177. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  178. /// <summary>
  179. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView's Content
  180. /// Area,
  181. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  182. /// dimension to ensure the view is fully visible.
  183. /// </summary>
  184. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  185. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  186. /// <example>
  187. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  188. /// <code>
  189. /// anchorButton.X = Pos.AnchorEnd (10);
  190. /// anchorButton.Y = 1
  191. /// </code>
  192. /// </example>
  193. public static Pos AnchorEnd (int offset)
  194. {
  195. ArgumentOutOfRangeException.ThrowIfNegative (offset, nameof (offset));
  196. return new PosAnchorEnd (offset);
  197. }
  198. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  199. /// <returns>The center Pos.</returns>
  200. /// <example>
  201. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  202. /// is 80% the width of the <see cref="View"/> it added to.
  203. /// <code>
  204. /// var textView = new TextView () {
  205. /// X = Pos.Center (),
  206. /// Y = Pos.Percent (50),
  207. /// Width = Dim.Percent (80),
  208. /// Height = Dim.Percent (30),
  209. /// };
  210. /// </code>
  211. /// </example>
  212. public static Pos Center () { return new PosCenter (); }
  213. /// <summary>
  214. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  215. /// will be called every time the position is needed.
  216. /// </summary>
  217. /// <param name="function">The function to be executed.</param>
  218. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  219. public static Pos Func (Func<int> function) { return new PosFunc (function); }
  220. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  221. /// <returns>The percent <see cref="Pos"/> object.</returns>
  222. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  223. /// <example>
  224. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  225. /// is 80% the width of the <see cref="View"/> it added to.
  226. /// <code>
  227. /// var textView = new TextField {
  228. /// X = Pos.Center (),
  229. /// Y = Pos.Percent (50),
  230. /// Width = Dim.Percent (80),
  231. /// Height = Dim.Percent (30),
  232. /// };
  233. /// </code>
  234. /// </example>
  235. public static Pos Percent (int percent)
  236. {
  237. ArgumentOutOfRangeException.ThrowIfNegative (percent, nameof (percent));
  238. return new PosPercent (percent);
  239. }
  240. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  241. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  242. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  243. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  244. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  245. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  246. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  247. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  248. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  249. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  250. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  251. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  252. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  253. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  254. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  255. public static Pos X (View view) { return new PosView (view, Side.Left); }
  256. /// <summary>
  257. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  258. /// <see cref="View"/>
  259. /// </summary>
  260. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  261. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  262. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  263. /// <summary>
  264. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  265. /// <see cref="View"/>.
  266. /// </summary>
  267. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  268. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  269. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  270. #endregion static Pos creation methods
  271. #region virtual methods
  272. /// <summary>
  273. /// Gets the starting point of an element based on the size of the parent element (typically
  274. /// <c>Superview.GetContentSize ()</c>).
  275. /// This method is meant to be overridden by subclasses to provide different ways of calculating the starting point.
  276. /// This method is used
  277. /// internally by the layout system to determine where a View should be positioned.
  278. /// </summary>
  279. /// <param name="size">The size of the parent element (typically <c>Superview.GetContentSize ()</c>).</param>
  280. /// <returns>
  281. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  282. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  283. /// position that is anchored to the end of the layout, and so on.
  284. /// </returns>
  285. internal virtual int GetAnchor (int size) { return 0; }
  286. /// <summary>
  287. /// Calculates and returns the final position of a <see cref="View"/> object. It takes into account the dimension of
  288. /// the
  289. /// superview and the dimension of the view itself.
  290. /// </summary>
  291. /// <remarks>
  292. /// </remarks>
  293. /// <param name="superviewDimension">
  294. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  295. /// height for y-coordinate calculation.
  296. /// </param>
  297. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  298. /// <param name="us">The View that holds this Pos object.</param>
  299. /// <param name="dimension">Width or Height</param>
  300. /// <returns>
  301. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  302. /// that
  303. /// is used.
  304. /// </returns>
  305. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return GetAnchor (superviewDimension); }
  306. /// <summary>
  307. /// Diagnostics API to determine if this Pos object references other views.
  308. /// </summary>
  309. /// <returns></returns>
  310. internal virtual bool ReferencesOtherViews () { return false; }
  311. /// <summary>
  312. /// Indicates whether the specified type <typeparamref name="T"/> is in the hierarchy of this Pos object.
  313. /// </summary>
  314. /// <param name="pos">A reference to this <see cref="Pos"/> instance.</param>
  315. /// <returns></returns>
  316. public bool Has<T> (out T pos) where T : Pos
  317. {
  318. pos = (this as T)!;
  319. return this switch
  320. {
  321. PosCombine combine => combine.Left.Has<T> (out pos) || combine.Right.Has<T> (out pos),
  322. T => true,
  323. _ => false
  324. };
  325. }
  326. #endregion virtual methods
  327. #region operators
  328. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  329. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  330. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  331. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  332. public static Pos operator + (Pos left, Pos right)
  333. {
  334. if (left is PosAbsolute && right is PosAbsolute)
  335. {
  336. return new PosAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
  337. }
  338. var newPos = new PosCombine (AddOrSubtract.Add, left, right);
  339. if (left is PosView view)
  340. {
  341. view.Target?.SetNeedsLayout ();
  342. }
  343. return newPos;
  344. }
  345. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  346. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  347. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  348. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  349. /// <summary>
  350. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  351. /// <see cref="Pos"/>.
  352. /// </summary>
  353. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  354. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  355. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  356. public static Pos operator - (Pos left, Pos right)
  357. {
  358. if (left is PosAbsolute && right is PosAbsolute)
  359. {
  360. return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
  361. }
  362. var newPos = new PosCombine (AddOrSubtract.Subtract, left, right);
  363. if (left is PosView view)
  364. {
  365. view.Target?.SetNeedsLayout ();
  366. }
  367. return newPos;
  368. }
  369. #endregion operators
  370. }