Pos.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Indicates the side for <see cref="Pos"/> operations.
  4. /// </summary>
  5. public enum Side
  6. {
  7. /// <summary>
  8. /// The left (X) side of the view.
  9. /// </summary>
  10. Left = 0,
  11. /// <summary>
  12. /// The top (Y) side of the view.
  13. /// </summary>
  14. Top = 1,
  15. /// <summary>
  16. /// The right (X + Width) side of the view.
  17. /// </summary>
  18. Right = 2,
  19. /// <summary>
  20. /// The bottom (Y + Height) side of the view.
  21. /// </summary>
  22. Bottom = 3
  23. }
  24. /// <summary>
  25. /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
  26. /// relative to the ending dimension. Integer values are implicitly convertible to an absolute <see cref="Pos"/>. These
  27. /// objects are created using the static methods Percent, AnchorEnd, and Center. The <see cref="Pos"/> objects can be
  28. /// combined with the addition and subtraction operators.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.</para>
  32. /// <para>
  33. /// These can be used to set the absolute position, when merely assigning an integer value (via the implicit
  34. /// integer to <see cref="Pos"/> conversion), and they can be combined to produce more useful layouts, like:
  35. /// Pos.Center - 3, which would shift the position of the <see cref="View"/> 3 characters to the left after
  36. /// centering for example.
  37. /// </para>
  38. /// <para>
  39. /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View).
  40. /// The X(View) and Y(View) are aliases to Left(View) and Top(View) respectively.
  41. /// </para>
  42. /// <para>
  43. /// <list type="table">
  44. /// <listheader>
  45. /// <term>Pos Object</term> <description>Description</description>
  46. /// </listheader>
  47. /// <item>
  48. /// <term>
  49. /// <see cref="Func"/>
  50. /// </term>
  51. /// <description>
  52. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  53. /// function. The function will be called every time the position is needed.
  54. /// </description>
  55. /// </item>
  56. /// <item>
  57. /// <term>
  58. /// <see cref="Pos.Percent(float)"/>
  59. /// </term>
  60. /// <description>
  61. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  62. /// SuperView.
  63. /// </description>
  64. /// </item>
  65. /// <item>
  66. /// <term>
  67. /// <see cref="Pos.AnchorEnd()"/>
  68. /// </term>
  69. /// <description>
  70. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of
  71. /// the dimension, useful to flush the layout from the right or bottom.
  72. /// </description>
  73. /// </item>
  74. /// <item>
  75. /// <term>
  76. /// <see cref="Pos.Center"/>
  77. /// </term>
  78. /// <description>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</description>
  79. /// </item>
  80. /// <item>
  81. /// <term>
  82. /// <see cref="Absolute"/>
  83. /// </term>
  84. /// <description>
  85. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  86. /// integer value.
  87. /// </description>
  88. /// </item>
  89. /// <item>
  90. /// <term>
  91. /// <see cref="Pos.Left"/>
  92. /// </term>
  93. /// <description>
  94. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  95. /// <see cref="View"/>.
  96. /// </description>
  97. /// </item>
  98. /// <item>
  99. /// <term>
  100. /// <see cref="Pos.X(View)"/>
  101. /// </term>
  102. /// <description>
  103. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  104. /// <see cref="View"/>.
  105. /// </description>
  106. /// </item>
  107. /// <item>
  108. /// <term>
  109. /// <see cref="Pos.Top(View)"/>
  110. /// </term>
  111. /// <description>
  112. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  113. /// <see cref="View"/>.
  114. /// </description>
  115. /// </item>
  116. /// <item>
  117. /// <term>
  118. /// <see cref="Pos.Y(View)"/>
  119. /// </term>
  120. /// <description>
  121. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  122. /// <see cref="View"/>.
  123. /// </description>
  124. /// </item>
  125. /// <item>
  126. /// <term>
  127. /// <see cref="Pos.Right(View)"/>
  128. /// </term>
  129. /// <description>
  130. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  131. /// specified <see cref="View"/>.
  132. /// </description>
  133. /// </item>
  134. /// <item>
  135. /// <term>
  136. /// <see cref="Pos.Bottom(View)"/>
  137. /// </term>
  138. /// <description>
  139. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  140. /// specified <see cref="View"/>
  141. /// </description>
  142. /// </item>
  143. /// </list>
  144. /// </para>
  145. /// </remarks>
  146. public abstract class Pos
  147. {
  148. #region static Pos creation methods
  149. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  150. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  151. /// <param name="position">The value to convert to the <see cref="Pos"/>.</param>
  152. public static Pos Absolute (int position) { return new PosAbsolute (position); }
  153. /// <summary>
  154. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  155. /// bottom) of the SuperView's Content Area, minus the respective size of the View. This is equivalent to using
  156. /// <see cref="Pos.AnchorEnd(int)"/>,
  157. /// with an offset equivalent to the View's respective dimension.
  158. /// </summary>
  159. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  160. /// <example>
  161. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  162. /// <code>
  163. /// anchorButton.X = Pos.AnchorEnd ();
  164. /// anchorButton.Y = Pos.AnchorEnd ();
  165. /// </code>
  166. /// </example>
  167. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  168. /// <summary>
  169. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView's Content
  170. /// Area,
  171. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  172. /// dimension to ensure the view is fully visible.
  173. /// </summary>
  174. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  175. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  176. /// <example>
  177. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  178. /// <code>
  179. /// anchorButton.X = Pos.AnchorEnd (10);
  180. /// anchorButton.Y = 1
  181. /// </code>
  182. /// </example>
  183. public static Pos AnchorEnd (int offset)
  184. {
  185. if (offset < 0)
  186. {
  187. throw new ArgumentException (@"Must be positive", nameof (offset));
  188. }
  189. return new PosAnchorEnd (offset);
  190. }
  191. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  192. /// <returns>The center Pos.</returns>
  193. /// <example>
  194. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  195. /// is 80% the width of the <see cref="View"/> it added to.
  196. /// <code>
  197. /// var textView = new TextView () {
  198. /// X = Pos.Center (),
  199. /// Y = Pos.Percent (50),
  200. /// Width = Dim.Percent (80),
  201. /// Height = Dim.Percent (30),
  202. /// };
  203. /// </code>
  204. /// </example>
  205. public static Pos Center () { return new PosCenter (); }
  206. /// <summary>
  207. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  208. /// will be called every time the position is needed.
  209. /// </summary>
  210. /// <param name="function">The function to be executed.</param>
  211. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  212. public static Pos Func (Func<int> function) { return new PosFunc (function); }
  213. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  214. /// <returns>The percent <see cref="Pos"/> object.</returns>
  215. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  216. /// <example>
  217. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  218. /// is 80% the width of the <see cref="View"/> it added to.
  219. /// <code>
  220. /// var textView = new TextField {
  221. /// X = Pos.Center (),
  222. /// Y = Pos.Percent (50),
  223. /// Width = Dim.Percent (80),
  224. /// Height = Dim.Percent (30),
  225. /// };
  226. /// </code>
  227. /// </example>
  228. public static Pos Percent (float percent)
  229. {
  230. if (percent is < 0 or > 100)
  231. {
  232. throw new ArgumentException ("Percent value must be between 0 and 100.");
  233. }
  234. return new PosPercent (percent / 100);
  235. }
  236. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  237. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  238. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  239. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  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 Y (View view) { return new PosView (view, Side.Top); }
  244. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) 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 Left (View view) { return new PosView (view, Side.Left); }
  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 X (View view) { return new PosView (view, Side.Left); }
  252. /// <summary>
  253. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) 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 Bottom (View view) { return new PosView (view, Side.Bottom); }
  259. /// <summary>
  260. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  261. /// <see cref="View"/>.
  262. /// </summary>
  263. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  264. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  265. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  266. #endregion static Pos creation methods
  267. #region virtual methods
  268. /// <summary>
  269. /// Gets the starting point of an element based on the size of the parent element (typically
  270. /// <c>Superview.ContentSize</c>).
  271. /// This method is meant to be overridden by subclasses to provide different ways of calculating the starting point.
  272. /// This method is used
  273. /// internally by the layout system to determine where a View should be positioned.
  274. /// </summary>
  275. /// <param name="size">The size of the parent element (typically <c>Superview.ContentSize</c>).</param>
  276. /// <returns>
  277. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  278. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  279. /// position that is anchored to the end of the layout, and so on.
  280. /// </returns>
  281. internal virtual int GetAnchor (int size) { return 0; }
  282. /// <summary>
  283. /// Calculates and returns the final position of a <see cref="View"/> object. It takes into account the dimension of
  284. /// the
  285. /// superview and the dimension of the view itself.
  286. /// </summary>
  287. /// <remarks>
  288. /// </remarks>
  289. /// <param name="superviewDimension">
  290. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  291. /// height for y-coordinate calculation.
  292. /// </param>
  293. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  294. /// <param name="us">The View that holds this Pos object.</param>
  295. /// <param name="dimension">Width or Height</param>
  296. /// <returns>
  297. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  298. /// that
  299. /// is used.
  300. /// </returns>
  301. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return GetAnchor (superviewDimension); }
  302. /// <summary>
  303. /// Diagnostics API to determine if this Pos object references other views.
  304. /// </summary>
  305. /// <returns></returns>
  306. internal virtual bool ReferencesOtherViews () { return false; }
  307. #endregion virtual methods
  308. #region operators
  309. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  310. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  311. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  312. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  313. public static Pos operator + (Pos left, Pos right)
  314. {
  315. if (left is PosAbsolute && right is PosAbsolute)
  316. {
  317. return new PosAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
  318. }
  319. var newPos = new PosCombine (true, left, right);
  320. if (left is PosView view)
  321. {
  322. view.Target.SetNeedsLayout ();
  323. }
  324. return newPos;
  325. }
  326. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  327. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  328. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  329. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  330. /// <summary>
  331. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  332. /// <see cref="Pos"/>.
  333. /// </summary>
  334. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  335. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  336. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  337. public static Pos operator - (Pos left, Pos right)
  338. {
  339. if (left is PosAbsolute && right is PosAbsolute)
  340. {
  341. return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
  342. }
  343. var newPos = new PosCombine (false, left, right);
  344. if (left is PosView view)
  345. {
  346. view.Target.SetNeedsLayout ();
  347. }
  348. return newPos;
  349. }
  350. #endregion operators
  351. }
  352. /// <summary>
  353. /// Represents an absolute position in the layout. This is used to specify a fixed position in the layout.
  354. /// </summary>
  355. /// <remarks>
  356. /// <para>
  357. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  358. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  359. /// </para>
  360. /// </remarks>
  361. /// <param name="position"></param>
  362. public class PosAbsolute (int position) : Pos
  363. {
  364. /// <summary>
  365. /// The position of the <see cref="View"/> in the layout.
  366. /// </summary>
  367. public int Position { get; } = position;
  368. /// <inheritdoc/>
  369. public override bool Equals (object other) { return other is PosAbsolute abs && abs.Position == Position; }
  370. /// <inheritdoc/>
  371. public override int GetHashCode () { return Position.GetHashCode (); }
  372. /// <inheritdoc/>
  373. public override string ToString () { return $"Absolute({Position})"; }
  374. internal override int GetAnchor (int size) { return Position; }
  375. }
  376. /// <summary>
  377. /// Represents a position anchored to the end (right side or bottom).
  378. /// </summary>
  379. /// <remarks>
  380. /// <para>
  381. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  382. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  383. /// </para>
  384. /// </remarks>
  385. public class PosAnchorEnd : Pos
  386. {
  387. /// <summary>
  388. /// Gets the offset of the position from the right/bottom.
  389. /// </summary>
  390. public int Offset { get; }
  391. /// <summary>
  392. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  393. /// minus the respective dimension of the View. This is equivalent to using <see cref="PosAnchorEnd(int)"/>,
  394. /// with an offset equivalent to the View's respective dimension.
  395. /// </summary>
  396. public PosAnchorEnd () { UseDimForOffset = true; }
  397. /// <summary>
  398. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  399. /// </summary>
  400. /// <param name="offset"></param>
  401. public PosAnchorEnd (int offset) { Offset = offset; }
  402. /// <inheritdoc/>
  403. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd.Offset == Offset; }
  404. /// <inheritdoc/>
  405. public override int GetHashCode () { return Offset.GetHashCode (); }
  406. /// <summary>
  407. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  408. /// </summary>
  409. public bool UseDimForOffset { get; }
  410. /// <inheritdoc/>
  411. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; }
  412. internal override int GetAnchor (int size)
  413. {
  414. if (UseDimForOffset)
  415. {
  416. return size;
  417. }
  418. return size - Offset;
  419. }
  420. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  421. {
  422. int newLocation = GetAnchor (superviewDimension);
  423. if (UseDimForOffset)
  424. {
  425. newLocation -= dim.GetAnchor (superviewDimension);
  426. }
  427. return newLocation;
  428. }
  429. }
  430. /// <summary>
  431. /// Represents a position that is centered.
  432. /// </summary>
  433. public class PosCenter : Pos
  434. {
  435. /// <inheritdoc/>
  436. public override string ToString () { return "Center"; }
  437. internal override int GetAnchor (int size) { return size / 2; }
  438. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  439. {
  440. int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
  441. return GetAnchor (superviewDimension - newDimension);
  442. }
  443. }
  444. /// <summary>
  445. /// Represents a position that is a combination of two other positions.
  446. /// </summary>
  447. /// <remarks>
  448. /// <para>
  449. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  450. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  451. /// </para>
  452. /// </remarks>
  453. /// <param name="add">
  454. /// Indicates whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added,
  455. /// otherwise they are subtracted.
  456. /// </param>
  457. /// <param name="left">The left position.</param>
  458. /// <param name="right">The right position.</param>
  459. public class PosCombine (bool add, Pos left, Pos right) : Pos
  460. {
  461. /// <summary>
  462. /// Gets whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added,
  463. /// otherwise they are subtracted.
  464. /// </summary>
  465. public bool Add { get; } = add;
  466. /// <summary>
  467. /// Gets the left position.
  468. /// </summary>
  469. public new Pos Left { get; } = left;
  470. /// <summary>
  471. /// Gets the right position.
  472. /// </summary>
  473. public new Pos Right { get; } = right;
  474. /// <inheritdoc/>
  475. public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
  476. internal override int GetAnchor (int size)
  477. {
  478. int la = Left.GetAnchor (size);
  479. int ra = Right.GetAnchor (size);
  480. if (Add)
  481. {
  482. return la + ra;
  483. }
  484. return la - ra;
  485. }
  486. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  487. {
  488. int left = Left.Calculate (superviewDimension, dim, us, dimension);
  489. int right = Right.Calculate (superviewDimension, dim, us, dimension);
  490. if (Add)
  491. {
  492. return left + right;
  493. }
  494. return left - right;
  495. }
  496. internal override bool ReferencesOtherViews ()
  497. {
  498. if (Left.ReferencesOtherViews ())
  499. {
  500. return true;
  501. }
  502. if (Right.ReferencesOtherViews ())
  503. {
  504. return true;
  505. }
  506. return false;
  507. }
  508. }
  509. /// <summary>
  510. /// Represents a position that is a percentage of the width or height of the SuperView.
  511. /// </summary>
  512. /// <remarks>
  513. /// <para>
  514. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  515. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  516. /// </para>
  517. /// </remarks>
  518. /// <param name="percent"></param>
  519. public class PosPercent (float percent) : Pos
  520. {
  521. /// <summary>
  522. /// Gets the factor that represents the percentage of the width or height of the SuperView.
  523. /// </summary>
  524. public new float Percent { get; } = percent;
  525. /// <inheritdoc/>
  526. public override bool Equals (object other) { return other is PosPercent f && f.Percent == Percent; }
  527. /// <inheritdoc/>
  528. public override int GetHashCode () { return Percent.GetHashCode (); }
  529. /// <inheritdoc/>
  530. public override string ToString () { return $"Percent({Percent})"; }
  531. internal override int GetAnchor (int size) { return (int)(size * Percent); }
  532. }
  533. /// <summary>
  534. /// Represents a position that is computed by executing a function that returns an integer position.
  535. /// </summary>
  536. /// <remarks>
  537. /// <para>
  538. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  539. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  540. /// </para>
  541. /// </remarks>
  542. /// <param name="pos">The position.</param>
  543. public class PosFunc (Func<int> pos) : Pos
  544. {
  545. /// <summary>
  546. /// Gets the function that computes the position.
  547. /// </summary>
  548. public new Func<int> Func { get; } = pos;
  549. /// <inheritdoc/>
  550. public override bool Equals (object other) { return other is PosFunc f && f.Func () == Func (); }
  551. /// <inheritdoc/>
  552. public override int GetHashCode () { return Func.GetHashCode (); }
  553. /// <inheritdoc/>
  554. public override string ToString () { return $"PosFunc({Func ()})"; }
  555. internal override int GetAnchor (int size) { return Func (); }
  556. }
  557. /// <summary>
  558. /// Represents a position that is anchored to the side of another view.
  559. /// </summary>
  560. /// <remarks>
  561. /// <para>
  562. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  563. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  564. /// </para>
  565. /// </remarks>
  566. /// <param name="view">The View the position is anchored to.</param>
  567. /// <param name="side">The side of the View the position is anchored to.</param>
  568. public class PosView (View view, Side side) : Pos
  569. {
  570. /// <summary>
  571. /// Gets the View the position is anchored to.
  572. /// </summary>
  573. public View Target { get; } = view;
  574. /// <summary>
  575. /// Gets the side of the View the position is anchored to.
  576. /// </summary>
  577. public Side Side { get; } = side;
  578. /// <inheritdoc/>
  579. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target && abs.Side == Side; }
  580. /// <inheritdoc/>
  581. public override int GetHashCode () { return Target.GetHashCode (); }
  582. /// <inheritdoc/>
  583. public override string ToString ()
  584. {
  585. string sideString = Side switch
  586. {
  587. Side.Left => "left",
  588. Side.Top => "top",
  589. Side.Right => "right",
  590. Side.Bottom => "bottom",
  591. _ => "unknown"
  592. };
  593. if (Target == null)
  594. {
  595. throw new NullReferenceException (nameof (Target));
  596. }
  597. return $"View(side={sideString},target={Target})";
  598. }
  599. internal override int GetAnchor (int size)
  600. {
  601. return Side switch
  602. {
  603. Side.Left => Target.Frame.X,
  604. Side.Top => Target.Frame.Y,
  605. Side.Right => Target.Frame.Right,
  606. Side.Bottom => Target.Frame.Bottom,
  607. _ => 0
  608. };
  609. }
  610. internal override bool ReferencesOtherViews () { return true; }
  611. }