Pos.cs 17 KB

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