Pos.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. /// <summary>
  137. /// Creates a <see cref="Pos"/> object that aligns a set of views according to the specified alignment setting.
  138. /// </summary>
  139. /// <param name="alignment"></param>
  140. /// <param name="groupId">
  141. /// The optional, unique identifier for the set of views to align according to
  142. /// <paramref name="alignment"/>.
  143. /// </param>
  144. /// <returns></returns>
  145. public static Pos Align (Alignment alignment, int groupId = 0) { return new PosAlign (alignment, groupId); }
  146. /// <summary>
  147. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  148. /// bottom) of the SuperView's Content Area, minus the respective size of the View. This is equivalent to using
  149. /// <see cref="Pos.AnchorEnd(int)"/>,
  150. /// with an offset equivalent to the View's respective dimension.
  151. /// </summary>
  152. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  153. /// <example>
  154. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  155. /// <code>
  156. /// anchorButton.X = Pos.AnchorEnd ();
  157. /// anchorButton.Y = Pos.AnchorEnd ();
  158. /// </code>
  159. /// </example>
  160. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  161. /// <summary>
  162. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView's Content
  163. /// Area,
  164. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  165. /// dimension to ensure the view is fully visible.
  166. /// </summary>
  167. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  168. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  169. /// <example>
  170. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  171. /// <code>
  172. /// anchorButton.X = Pos.AnchorEnd (10);
  173. /// anchorButton.Y = 1
  174. /// </code>
  175. /// </example>
  176. public static Pos AnchorEnd (int offset)
  177. {
  178. if (offset < 0)
  179. {
  180. throw new ArgumentException (@"Must be positive", nameof (offset));
  181. }
  182. return new PosAnchorEnd (offset);
  183. }
  184. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  185. /// <returns>The center Pos.</returns>
  186. /// <example>
  187. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  188. /// is 80% the width of the <see cref="View"/> it added to.
  189. /// <code>
  190. /// var textView = new TextView () {
  191. /// X = Pos.Center (),
  192. /// Y = Pos.Percent (50),
  193. /// Width = Dim.Percent (80),
  194. /// Height = Dim.Percent (30),
  195. /// };
  196. /// </code>
  197. /// </example>
  198. public static Pos Center () { return new PosCenter (); }
  199. /// <summary>
  200. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  201. /// will be called every time the position is needed.
  202. /// </summary>
  203. /// <param name="function">The function to be executed.</param>
  204. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  205. public static Pos Func (Func<int> function) { return new PosFunc (function); }
  206. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  207. /// <returns>The percent <see cref="Pos"/> object.</returns>
  208. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  209. /// <example>
  210. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  211. /// is 80% the width of the <see cref="View"/> it added to.
  212. /// <code>
  213. /// var textView = new TextField {
  214. /// X = Pos.Center (),
  215. /// Y = Pos.Percent (50),
  216. /// Width = Dim.Percent (80),
  217. /// Height = Dim.Percent (30),
  218. /// };
  219. /// </code>
  220. /// </example>
  221. public static Pos Percent (int percent)
  222. {
  223. if (percent is < 0)
  224. {
  225. throw new ArgumentException ("Percent value must be positive.");
  226. }
  227. return new PosPercent (percent);
  228. }
  229. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  230. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  231. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  232. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  233. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  234. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  235. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  236. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  237. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  238. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  239. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  240. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  241. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</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 X (View view) { return new PosView (view, Side.Left); }
  245. /// <summary>
  246. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  247. /// <see cref="View"/>
  248. /// </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 Bottom (View view) { return new PosView (view, Side.Bottom); }
  252. /// <summary>
  253. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  254. /// <see cref="View"/>.
  255. /// </summary>
  256. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  257. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  258. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  259. #endregion static Pos creation methods
  260. #region virtual methods
  261. /// <summary>
  262. /// Gets the starting point of an element based on the size of the parent element (typically
  263. /// <c>Superview.ContentSize</c>).
  264. /// This method is meant to be overridden by subclasses to provide different ways of calculating the starting point.
  265. /// This method is used
  266. /// internally by the layout system to determine where a View should be positioned.
  267. /// </summary>
  268. /// <param name="size">The size of the parent element (typically <c>Superview.ContentSize</c>).</param>
  269. /// <returns>
  270. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  271. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  272. /// position that is anchored to the end of the layout, and so on.
  273. /// </returns>
  274. internal virtual int GetAnchor (int size) { return 0; }
  275. /// <summary>
  276. /// Calculates and returns the final position of a <see cref="View"/> object. It takes into account the dimension of
  277. /// the
  278. /// superview and the dimension of the view itself.
  279. /// </summary>
  280. /// <remarks>
  281. /// </remarks>
  282. /// <param name="superviewDimension">
  283. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  284. /// height for y-coordinate calculation.
  285. /// </param>
  286. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  287. /// <param name="us">The View that holds this Pos object.</param>
  288. /// <param name="dimension">Width or Height</param>
  289. /// <returns>
  290. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  291. /// that
  292. /// is used.
  293. /// </returns>
  294. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return GetAnchor (superviewDimension); }
  295. /// <summary>
  296. /// Diagnostics API to determine if this Pos object references other views.
  297. /// </summary>
  298. /// <returns></returns>
  299. internal virtual bool ReferencesOtherViews () { return false; }
  300. }
  301. /// <summary>
  302. /// Represents an absolute position in the layout. This is used to specify a fixed position in the layout.
  303. /// </summary>
  304. /// <remarks>
  305. /// <para>
  306. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  307. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  308. /// </para>
  309. /// </remarks>
  310. /// <param name="position"></param>
  311. public class PosAbsolute (int position) : Pos
  312. {
  313. /// <summary>
  314. /// The position of the <see cref="View"/> in the layout.
  315. /// </summary>
  316. public int Position { get; } = position;
  317. /// <inheritdoc />
  318. public override bool Equals (object other) { return other is PosAbsolute abs && abs.Position == Position; }
  319. /// <inheritdoc />
  320. public override int GetHashCode () { return Position.GetHashCode (); }
  321. /// <inheritdoc />
  322. public override string ToString () { return $"Absolute({Position})"; }
  323. internal override int Anchor (int width) { return Position; }
  324. }
  325. /// <summary>
  326. /// Represents a position anchored to the end (right side or bottom).
  327. /// </summary>
  328. /// <remarks>
  329. /// <para>
  330. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  331. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  332. /// </para>
  333. /// </remarks>
  334. public class PosAnchorEnd : Pos
  335. {
  336. /// <summary>
  337. /// Gets the offset of the position from the right/bottom.
  338. /// </summary>
  339. public int Offset { get; }
  340. /// <summary>
  341. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  342. /// minus the respective dimension of the View. This is equivalent to using <see cref="PosAnchorEnd(int)"/>,
  343. /// with an offset equivalent to the View's respective dimension.
  344. /// </summary>
  345. public PosAnchorEnd () { UseDimForOffset = true; }
  346. /// <summary>
  347. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  348. /// </summary>
  349. /// <param name="offset"></param>
  350. public PosAnchorEnd (int offset) { Offset = offset; }
  351. /// <inheritdoc />
  352. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd.Offset == Offset; }
  353. /// <inheritdoc />
  354. public override int GetHashCode () { return Offset.GetHashCode (); }
  355. /// <summary>
  356. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  357. /// </summary>
  358. public bool UseDimForOffset { get; }
  359. /// <inheritdoc />
  360. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; }
  361. internal override int Anchor (int width)
  362. {
  363. if (UseDimForOffset)
  364. {
  365. return width;
  366. }
  367. var newPos = new PosCombine (AddOrSubtract.Add, left, right);
  368. if (left is PosView view)
  369. {
  370. view.Target.SetNeedsLayout ();
  371. }
  372. return newPos;
  373. }
  374. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  375. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  376. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  377. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  378. /// <summary>
  379. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  380. /// <see cref="Pos"/>.
  381. /// </summary>
  382. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  383. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  384. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  385. public static Pos operator - (Pos left, Pos right)
  386. {
  387. if (left is PosAbsolute && right is PosAbsolute)
  388. {
  389. return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
  390. }
  391. var newPos = new PosCombine (AddOrSubtract.Subtract, left, right);
  392. if (left is PosView view)
  393. {
  394. view.Target.SetNeedsLayout ();
  395. }
  396. return newPos;
  397. }
  398. #endregion operators
  399. }