Pos.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 class 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.</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. if (offset < 0)
  196. {
  197. throw new ArgumentException (@"Must be positive", nameof (offset));
  198. }
  199. return new PosAnchorEnd (offset);
  200. }
  201. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  202. /// <returns>The center Pos.</returns>
  203. /// <example>
  204. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  205. /// is 80% the width of the <see cref="View"/> it added to.
  206. /// <code>
  207. /// var textView = new TextView () {
  208. /// X = Pos.Center (),
  209. /// Y = Pos.Percent (50),
  210. /// Width = Dim.Percent (80),
  211. /// Height = Dim.Percent (30),
  212. /// };
  213. /// </code>
  214. /// </example>
  215. public static Pos Center () { return new PosCenter (); }
  216. /// <summary>
  217. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  218. /// will be called every time the position is needed.
  219. /// </summary>
  220. /// <param name="function">The function to be executed.</param>
  221. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  222. public static Pos Func (Func<int> function) { return new PosFunc (function); }
  223. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  224. /// <returns>The percent <see cref="Pos"/> object.</returns>
  225. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  226. /// <example>
  227. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  228. /// is 80% the width of the <see cref="View"/> it added to.
  229. /// <code>
  230. /// var textView = new TextField {
  231. /// X = Pos.Center (),
  232. /// Y = Pos.Percent (50),
  233. /// Width = Dim.Percent (80),
  234. /// Height = Dim.Percent (30),
  235. /// };
  236. /// </code>
  237. /// </example>
  238. public static Pos Percent (int percent)
  239. {
  240. if (percent is < 0)
  241. {
  242. throw new ArgumentException ("Percent value must be positive.");
  243. }
  244. return new PosPercent (percent);
  245. }
  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 Top (View view) { return new PosView (view, Side.Top); }
  250. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) 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 Y (View view) { return new PosView (view, Side.Top); }
  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 Left (View view) { return new PosView (view, Side.Left); }
  258. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  259. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  260. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  261. public static Pos X (View view) { return new PosView (view, Side.Left); }
  262. /// <summary>
  263. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  264. /// <see cref="View"/>
  265. /// </summary>
  266. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  267. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  268. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  269. /// <summary>
  270. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  271. /// <see cref="View"/>.
  272. /// </summary>
  273. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  274. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  275. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  276. #endregion static Pos creation methods
  277. #region virtual methods
  278. /// <summary>
  279. /// Gets the starting point of an element based on the size of the parent element (typically
  280. /// <c>Superview.ContentSize</c>).
  281. /// This method is meant to be overridden by subclasses to provide different ways of calculating the starting point.
  282. /// This method is used
  283. /// internally by the layout system to determine where a View should be positioned.
  284. /// </summary>
  285. /// <param name="size">The size of the parent element (typically <c>Superview.ContentSize</c>).</param>
  286. /// <returns>
  287. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  288. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  289. /// position that is anchored to the end of the layout, and so on.
  290. /// </returns>
  291. internal virtual int GetAnchor (int size) { return 0; }
  292. /// <summary>
  293. /// Calculates and returns the final position of a <see cref="View"/> object. It takes into account the dimension of
  294. /// the
  295. /// superview and the dimension of the view itself.
  296. /// </summary>
  297. /// <remarks>
  298. /// </remarks>
  299. /// <param name="superviewDimension">
  300. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  301. /// height for y-coordinate calculation.
  302. /// </param>
  303. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  304. /// <param name="us">The View that holds this Pos object.</param>
  305. /// <param name="dimension">Width or Height</param>
  306. /// <returns>
  307. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  308. /// that
  309. /// is used.
  310. /// </returns>
  311. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return GetAnchor (superviewDimension); }
  312. /// <summary>
  313. /// Diagnostics API to determine if this Pos object references other views.
  314. /// </summary>
  315. /// <returns></returns>
  316. internal virtual bool ReferencesOtherViews () { return false; }
  317. #endregion virtual methods
  318. #region operators
  319. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  320. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  321. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  322. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  323. public static Pos operator + (Pos left, Pos right)
  324. {
  325. if (left is PosAbsolute && right is PosAbsolute)
  326. {
  327. return new PosAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
  328. }
  329. var newPos = new PosCombine (AddOrSubtract.Add, left, right);
  330. if (left is PosView view)
  331. {
  332. view.Target.SetNeedsLayout ();
  333. }
  334. return newPos;
  335. }
  336. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  337. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  338. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  339. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  340. /// <summary>
  341. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  342. /// <see cref="Pos"/>.
  343. /// </summary>
  344. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  345. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  346. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  347. public static Pos operator - (Pos left, Pos right)
  348. {
  349. if (left is PosAbsolute && right is PosAbsolute)
  350. {
  351. return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
  352. }
  353. var newPos = new PosCombine (AddOrSubtract.Subtract, left, right);
  354. if (left is PosView view)
  355. {
  356. view.Target.SetNeedsLayout ();
  357. }
  358. return newPos;
  359. }
  360. #endregion operators
  361. }