Pos.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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="Pos.Function(Func{int})"/>
  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 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 Function (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. /// Calculates and returns 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 Anchor (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 Anchor (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.Anchor (0) + right.Anchor (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.Anchor (0) - right.Anchor (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. #region overrides
  352. /// <inheritdoc/>
  353. public override bool Equals (object other) { return other is Pos abs && abs == this; }
  354. /// <summary>Serves as the default hash function. </summary>
  355. /// <returns>A hash code for the current object.</returns>
  356. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  357. #endregion overrides
  358. }
  359. /// <summary>
  360. /// Represents an absolute position in the layout. This is used to specify a fixed position in the layout.
  361. /// </summary>
  362. /// <remarks>
  363. /// <para>
  364. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  365. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  366. /// </para>
  367. /// </remarks>
  368. /// <param name="position"></param>
  369. public class PosAbsolute (int position) : Pos
  370. {
  371. /// <summary>
  372. /// The position of the <see cref="View"/> in the layout.
  373. /// </summary>
  374. public int Position { get; } = position;
  375. /// <inheritdoc/>
  376. public override bool Equals (object other) { return other is PosAbsolute abs && abs.Position == Position; }
  377. /// <inheritdoc/>
  378. public override int GetHashCode () { return Position.GetHashCode (); }
  379. /// <inheritdoc/>
  380. public override string ToString () { return $"Absolute({Position})"; }
  381. internal override int Anchor (int size) { return Position; }
  382. }
  383. /// <summary>
  384. /// Represents a position anchored to the end (right side or bottom).
  385. /// </summary>
  386. /// <remarks>
  387. /// <para>
  388. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  389. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  390. /// </para>
  391. /// </remarks>
  392. public class PosAnchorEnd : Pos
  393. {
  394. /// <summary>
  395. /// Gets the offset of the position from the right/bottom.
  396. /// </summary>
  397. public int Offset { get; }
  398. /// <summary>
  399. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  400. /// minus the respective dimension of the View. This is equivalent to using <see cref="PosAnchorEnd(int)"/>,
  401. /// with an offset equivalent to the View's respective dimension.
  402. /// </summary>
  403. public PosAnchorEnd () { UseDimForOffset = true; }
  404. /// <summary>
  405. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  406. /// </summary>
  407. /// <param name="offset"></param>
  408. public PosAnchorEnd (int offset) { Offset = offset; }
  409. /// <inheritdoc/>
  410. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd.Offset == Offset; }
  411. /// <inheritdoc/>
  412. public override int GetHashCode () { return Offset.GetHashCode (); }
  413. /// <summary>
  414. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  415. /// </summary>
  416. public bool UseDimForOffset { get; }
  417. /// <inheritdoc/>
  418. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; }
  419. internal override int Anchor (int size)
  420. {
  421. if (UseDimForOffset)
  422. {
  423. return size;
  424. }
  425. return size - Offset;
  426. }
  427. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  428. {
  429. int newLocation = Anchor (superviewDimension);
  430. if (UseDimForOffset)
  431. {
  432. newLocation -= dim.Anchor (superviewDimension);
  433. }
  434. return newLocation;
  435. }
  436. }
  437. /// <summary>
  438. /// Represents a position that is centered.
  439. /// </summary>
  440. public class PosCenter : Pos
  441. {
  442. /// <inheritdoc/>
  443. public override string ToString () { return "Center"; }
  444. internal override int Anchor (int size) { return size / 2; }
  445. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  446. {
  447. int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
  448. return Anchor (superviewDimension - newDimension);
  449. }
  450. }
  451. /// <summary>
  452. /// Represents a position that is a combination of two other positions.
  453. /// </summary>
  454. /// <remarks>
  455. /// <para>
  456. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  457. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  458. /// </para>
  459. /// </remarks>
  460. /// <param name="add">
  461. /// Indicates whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added,
  462. /// otherwise they are subtracted.
  463. /// </param>
  464. /// <param name="left">The left position.</param>
  465. /// <param name="right">The right position.</param>
  466. public class PosCombine (bool add, Pos left, Pos right) : Pos
  467. {
  468. /// <summary>
  469. /// Gets whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added,
  470. /// otherwise they are subtracted.
  471. /// </summary>
  472. public bool Add { get; } = add;
  473. /// <summary>
  474. /// Gets the left position.
  475. /// </summary>
  476. public new Pos Left { get; } = left;
  477. /// <summary>
  478. /// Gets the right position.
  479. /// </summary>
  480. public new Pos Right { get; } = right;
  481. /// <inheritdoc/>
  482. public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
  483. internal override int Anchor (int size)
  484. {
  485. int la = Left.Anchor (size);
  486. int ra = Right.Anchor (size);
  487. if (Add)
  488. {
  489. return la + ra;
  490. }
  491. return la - ra;
  492. }
  493. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  494. {
  495. int left = Left.Calculate (superviewDimension, dim, us, dimension);
  496. int right = Right.Calculate (superviewDimension, dim, us, dimension);
  497. if (Add)
  498. {
  499. return left + right;
  500. }
  501. return left - right;
  502. }
  503. internal override bool ReferencesOtherViews ()
  504. {
  505. if (Left.ReferencesOtherViews ())
  506. {
  507. return true;
  508. }
  509. if (Right.ReferencesOtherViews ())
  510. {
  511. return true;
  512. }
  513. return false;
  514. }
  515. }
  516. /// <summary>
  517. /// Represents a position that is a percentage of the width or height of the SuperView.
  518. /// </summary>
  519. /// <remarks>
  520. /// <para>
  521. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  522. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  523. /// </para>
  524. /// </remarks>
  525. /// <param name="percent"></param>
  526. public class PosPercent (float percent) : Pos
  527. {
  528. /// <summary>
  529. /// Gets the factor that represents the percentage of the width or height of the SuperView.
  530. /// </summary>
  531. public new float Percent { get; } = percent;
  532. /// <inheritdoc/>
  533. public override bool Equals (object other) { return other is PosPercent f && f.Percent == Percent; }
  534. /// <inheritdoc/>
  535. public override int GetHashCode () { return Percent.GetHashCode (); }
  536. /// <inheritdoc/>
  537. public override string ToString () { return $"Percent({Percent})"; }
  538. internal override int Anchor (int size) { return (int)(size * Percent); }
  539. }
  540. /// <summary>
  541. /// Represents a position that is computed by executing a function that returns an integer position.
  542. /// </summary>
  543. /// <remarks>
  544. /// <para>
  545. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  546. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  547. /// </para>
  548. /// </remarks>
  549. /// <param name="pos">The position.</param>
  550. public class PosFunc (Func<int> pos) : Pos
  551. {
  552. /// <summary>
  553. /// Gets the function that computes the position.
  554. /// </summary>
  555. public Func<int> Func { get; } = pos;
  556. /// <inheritdoc/>
  557. public override bool Equals (object other) { return other is PosFunc f && f.Func () == Func (); }
  558. /// <inheritdoc/>
  559. public override int GetHashCode () { return Func.GetHashCode (); }
  560. /// <inheritdoc/>
  561. public override string ToString () { return $"PosFunc({Func ()})"; }
  562. internal override int Anchor (int size) { return Func (); }
  563. }
  564. /// <summary>
  565. /// Represents a position that is anchored to the side of another view.
  566. /// </summary>
  567. /// <remarks>
  568. /// <para>
  569. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  570. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  571. /// </para>
  572. /// </remarks>
  573. /// <param name="view">The View the position is anchored to.</param>
  574. /// <param name="side">The side of the View the position is anchored to.</param>
  575. public class PosView (View view, Side side) : Pos
  576. {
  577. /// <summary>
  578. /// Gets the View the position is anchored to.
  579. /// </summary>
  580. public View Target { get; } = view;
  581. /// <summary>
  582. /// Gets the side of the View the position is anchored to.
  583. /// </summary>
  584. public Side Side { get; } = side;
  585. /// <inheritdoc/>
  586. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  587. /// <inheritdoc/>
  588. public override int GetHashCode () { return Target.GetHashCode (); }
  589. /// <inheritdoc/>
  590. public override string ToString ()
  591. {
  592. string sideString = Side switch
  593. {
  594. Side.Left => "left",
  595. Side.Top => "top",
  596. Side.Right => "right",
  597. Side.Bottom => "bottom",
  598. _ => "unknown"
  599. };
  600. if (Target == null)
  601. {
  602. throw new NullReferenceException (nameof (Target));
  603. }
  604. return $"View(side={sideString},target={Target})";
  605. }
  606. internal override int Anchor (int size)
  607. {
  608. return Side switch
  609. {
  610. Side.Left => Target.Frame.X,
  611. Side.Top => Target.Frame.Y,
  612. Side.Right => Target.Frame.Right,
  613. Side.Bottom => Target.Frame.Bottom,
  614. _ => 0
  615. };
  616. }
  617. internal override bool ReferencesOtherViews () { return true; }
  618. }