Pos.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #nullable enable
  2. namespace Terminal.Gui.ViewBase;
  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 based on the passed view and by executing the
  215. /// provided function.
  216. /// The function will be called every time the position is needed.
  217. /// </summary>
  218. /// <param name="function">The function to be executed.</param>
  219. /// <param name="view">The view where the data will be retrieved.</param>
  220. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  221. public static Pos Func (Func<View?, int> function, View? view = null) { return new PosFunc (function, view); }
  222. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  223. /// <returns>The percent <see cref="Pos"/> object.</returns>
  224. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  225. /// <example>
  226. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  227. /// is 80% the width of the <see cref="View"/> it added to.
  228. /// <code>
  229. /// var textView = new TextField {
  230. /// X = Pos.Center (),
  231. /// Y = Pos.Percent (50),
  232. /// Width = Dim.Percent (80),
  233. /// Height = Dim.Percent (30),
  234. /// };
  235. /// </code>
  236. /// </example>
  237. public static Pos Percent (int percent)
  238. {
  239. ArgumentOutOfRangeException.ThrowIfNegative (percent, nameof (percent));
  240. return new PosPercent (percent);
  241. }
  242. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  243. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  244. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  245. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  246. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  247. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  248. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  249. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  250. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  251. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  252. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  253. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  254. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  255. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  256. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  257. public static Pos X (View view) { return new PosView (view, Side.Left); }
  258. /// <summary>
  259. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  260. /// <see cref="View"/>
  261. /// </summary>
  262. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  263. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  264. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  265. /// <summary>
  266. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  267. /// <see cref="View"/>.
  268. /// </summary>
  269. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  270. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  271. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  272. #endregion static Pos creation methods
  273. #region virtual methods
  274. /// <summary>
  275. /// Gets the starting point of an element based on the size of the parent element (typically
  276. /// <c>Superview.GetContentSize ()</c>).
  277. /// This method is meant to be overridden by subclasses to provide different ways of calculating the starting point.
  278. /// This method is used
  279. /// internally by the layout system to determine where a View should be positioned.
  280. /// </summary>
  281. /// <param name="size">The size of the parent element (typically <c>Superview.GetContentSize ()</c>).</param>
  282. /// <returns>
  283. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  284. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  285. /// position that is anchored to the end of the layout, and so on.
  286. /// </returns>
  287. internal virtual int GetAnchor (int size) { return 0; }
  288. /// <summary>
  289. /// Calculates and returns the final position of a <see cref="View"/> object. It takes into account the dimension of
  290. /// the
  291. /// superview and the dimension of the view itself.
  292. /// </summary>
  293. /// <remarks>
  294. /// </remarks>
  295. /// <param name="superviewDimension">
  296. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  297. /// height for y-coordinate calculation.
  298. /// </param>
  299. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  300. /// <param name="us">The View that holds this Pos object.</param>
  301. /// <param name="dimension">Width or Height</param>
  302. /// <returns>
  303. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  304. /// that
  305. /// is used.
  306. /// </returns>
  307. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return GetAnchor (superviewDimension); }
  308. /// <summary>
  309. /// Diagnostics API to determine if this Pos object references other views.
  310. /// </summary>
  311. /// <returns></returns>
  312. internal virtual bool ReferencesOtherViews () { return false; }
  313. /// <summary>
  314. /// Indicates whether the specified type <typeparamref name="T"/> is in the hierarchy of this Pos object.
  315. /// </summary>
  316. /// <param name="pos">A reference to this <see cref="Pos"/> instance.</param>
  317. /// <returns></returns>
  318. public bool Has<T> (out T pos) where T : Pos
  319. {
  320. pos = (this as T)!;
  321. return this switch
  322. {
  323. PosCombine combine => combine.Left.Has<T> (out pos) || combine.Right.Has<T> (out pos),
  324. T => true,
  325. _ => false
  326. };
  327. }
  328. #endregion virtual methods
  329. #region operators
  330. /// <summary>Adds a <see cref="Pos"/> to a <see cref="Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  331. /// <param name="left">The first <see cref="Pos"/> to add.</param>
  332. /// <param name="right">The second <see cref="Pos"/> to add.</param>
  333. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  334. public static Pos operator + (Pos left, Pos right)
  335. {
  336. if (left is PosAbsolute && right is PosAbsolute)
  337. {
  338. return new PosAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
  339. }
  340. var newPos = new PosCombine (AddOrSubtract.Add, left, right);
  341. if (left is PosView view)
  342. {
  343. view.Target?.SetNeedsLayout ();
  344. }
  345. return newPos;
  346. }
  347. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  348. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  349. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  350. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  351. /// <summary>
  352. /// Subtracts a <see cref="Pos"/> from a <see cref="Pos"/>, yielding a new
  353. /// <see cref="Pos"/>.
  354. /// </summary>
  355. /// <param name="left">The <see cref="Pos"/> to subtract from (the minuend).</param>
  356. /// <param name="right">The <see cref="Pos"/> to subtract (the subtrahend).</param>
  357. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  358. public static Pos operator - (Pos left, Pos right)
  359. {
  360. if (left is PosAbsolute && right is PosAbsolute)
  361. {
  362. return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
  363. }
  364. var newPos = new PosCombine (AddOrSubtract.Subtract, left, right);
  365. if (left is PosView view)
  366. {
  367. view.Target?.SetNeedsLayout ();
  368. }
  369. return newPos;
  370. }
  371. #endregion operators
  372. }