Pos.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. /// <summary>
  149. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  150. /// bottom) of the SuperView, minus the respective dimension of the View. This is equivalent to using
  151. /// <see cref="Pos.AnchorEnd(int)"/>,
  152. /// with an offset equivalent to the View's respective dimension.
  153. /// </summary>
  154. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  155. /// <example>
  156. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  157. /// <code>
  158. /// anchorButton.X = Pos.AnchorEnd ();
  159. /// anchorButton.Y = Pos.AnchorEnd ();
  160. /// </code>
  161. /// </example>
  162. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  163. /// <summary>
  164. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView,
  165. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  166. /// dimension to ensure the view is fully visible.
  167. /// </summary>
  168. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  169. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  170. /// <example>
  171. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  172. /// <code>
  173. /// anchorButton.X = Pos.AnchorEnd (10);
  174. /// anchorButton.Y = 1
  175. /// </code>
  176. /// </example>
  177. public static Pos AnchorEnd (int offset)
  178. {
  179. if (offset < 0)
  180. {
  181. throw new ArgumentException (@"Must be positive", nameof (offset));
  182. }
  183. return new PosAnchorEnd (offset);
  184. }
  185. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  186. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  187. /// <param name="position">The value to convert to the <see cref="Pos"/>.</param>
  188. public static Pos Absolute (int position) { return new PosAbsolute (position); }
  189. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  190. /// <returns>The center Pos.</returns>
  191. /// <example>
  192. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  193. /// is 80% the width of the <see cref="View"/> it added to.
  194. /// <code>
  195. /// var textView = new TextView () {
  196. /// X = Pos.Center (),
  197. /// Y = Pos.Percent (50),
  198. /// Width = Dim.Percent (80),
  199. /// Height = Dim.Percent (30),
  200. /// };
  201. /// </code>
  202. /// </example>
  203. public static Pos Center () { return new PosCenter (); }
  204. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  205. /// <param name="other">The object to compare with the current object. </param>
  206. /// <returns>
  207. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  208. /// <see langword="false"/>.
  209. /// </returns>
  210. public override bool Equals (object other) { return other is Pos abs && abs == this; }
  211. /// <summary>
  212. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  213. /// will be called every time the position is needed.
  214. /// </summary>
  215. /// <param name="function">The function to be executed.</param>
  216. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  217. public static Pos Function (Func<int> function) { return new PosFunc (function); }
  218. /// <summary>Serves as the default hash function. </summary>
  219. /// <returns>A hash code for the current object.</returns>
  220. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  221. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  222. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  223. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  224. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  225. public static Pos operator + (Pos left, Pos right)
  226. {
  227. if (left is PosAbsolute && right is PosAbsolute)
  228. {
  229. return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
  230. }
  231. var newPos = new PosCombine (true, left, right);
  232. if (left is PosView view)
  233. {
  234. view.Target.SetNeedsLayout ();
  235. }
  236. return newPos;
  237. }
  238. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  239. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  240. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  241. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  242. /// <summary>
  243. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  244. /// <see cref="Pos"/>.
  245. /// </summary>
  246. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  247. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  248. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  249. public static Pos operator - (Pos left, Pos right)
  250. {
  251. if (left is PosAbsolute && right is PosAbsolute)
  252. {
  253. return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
  254. }
  255. var newPos = new PosCombine (false, left, right);
  256. if (left is PosView view)
  257. {
  258. view.Target.SetNeedsLayout ();
  259. }
  260. return newPos;
  261. }
  262. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  263. /// <returns>The percent <see cref="Pos"/> object.</returns>
  264. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  265. /// <example>
  266. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  267. /// is 80% the width of the <see cref="View"/> it added to.
  268. /// <code>
  269. /// var textView = new TextField {
  270. /// X = Pos.Center (),
  271. /// Y = Pos.Percent (50),
  272. /// Width = Dim.Percent (80),
  273. /// Height = Dim.Percent (30),
  274. /// };
  275. /// </code>
  276. /// </example>
  277. public static Pos Percent (float percent)
  278. {
  279. if (percent is < 0 or > 100)
  280. {
  281. throw new ArgumentException ("Percent value must be between 0 and 100.");
  282. }
  283. return new PosPercent (percent / 100);
  284. }
  285. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  286. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  287. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  288. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  289. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  290. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  291. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  292. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  293. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  294. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  295. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  296. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  297. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  298. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  299. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  300. public static Pos X (View view) { return new PosView (view, Side.Left); }
  301. /// <summary>
  302. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  303. /// <see cref="View"/>
  304. /// </summary>
  305. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  306. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  307. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  308. /// <summary>
  309. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  310. /// <see cref="View"/>.
  311. /// </summary>
  312. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  313. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  314. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  315. /// <summary>
  316. /// Gets a position that is anchored to a certain point in the layout. This method is typically used
  317. /// internally by the layout system to determine where a View should be positioned.
  318. /// </summary>
  319. /// <param name="width">The width of the area where the View is being positioned (Superview.ContentSize).</param>
  320. /// <returns>
  321. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  322. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  323. /// position that is anchored to the end of the layout, and so on.
  324. /// </returns>
  325. internal virtual int Anchor (int width) { return 0; }
  326. /// <summary>
  327. /// Calculates and returns the position of a <see cref="View"/> object. It takes into account the dimension of the
  328. /// superview and the dimension of the view itself.
  329. /// </summary>
  330. /// <param name="superviewDimension">
  331. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  332. /// height for y-coordinate calculation.
  333. /// </param>
  334. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  335. /// <param name="us">The View that holds this Pos object.</param>
  336. /// <param name="dimension">Width or Height</param>
  337. /// <returns>
  338. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  339. /// that
  340. /// is used.
  341. /// </returns>
  342. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return Anchor (superviewDimension); }
  343. /// <summary>
  344. /// Diagnostics API to determine if this Pos object references other views.
  345. /// </summary>
  346. /// <returns></returns>
  347. internal virtual bool ReferencesOtherViews () { return false; }
  348. }
  349. /// <summary>
  350. /// Represents an absolute position in the layout. This is used to specify a fixed position in the layout.
  351. /// </summary>
  352. /// <remarks>
  353. /// <para>
  354. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  355. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  356. /// </para>
  357. /// </remarks>
  358. /// <param name="position"></param>
  359. public class PosAbsolute (int position) : Pos
  360. {
  361. /// <summary>
  362. /// The position of the <see cref="View"/> in the layout.
  363. /// </summary>
  364. public int Position { get; } = position;
  365. /// <inheritdoc />
  366. public override bool Equals (object other) { return other is PosAbsolute abs && abs.Position == Position; }
  367. /// <inheritdoc />
  368. public override int GetHashCode () { return Position.GetHashCode (); }
  369. /// <inheritdoc />
  370. public override string ToString () { return $"Absolute({Position})"; }
  371. internal override int Anchor (int width) { return Position; }
  372. }
  373. /// <summary>
  374. /// Represents a position anchored to the end (right side or bottom).
  375. /// </summary>
  376. /// <remarks>
  377. /// <para>
  378. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  379. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  380. /// </para>
  381. /// </remarks>
  382. public class PosAnchorEnd : Pos
  383. {
  384. /// <summary>
  385. /// Gets the offset of the position from the right/bottom.
  386. /// </summary>
  387. public int Offset { get; }
  388. /// <summary>
  389. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  390. /// minus the respective dimension of the View. This is equivalent to using <see cref="PosAnchorEnd(int)"/>,
  391. /// with an offset equivalent to the View's respective dimension.
  392. /// </summary>
  393. public PosAnchorEnd () { UseDimForOffset = true; }
  394. /// <summary>
  395. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  396. /// </summary>
  397. /// <param name="offset"></param>
  398. public PosAnchorEnd (int offset) { Offset = offset; }
  399. /// <inheritdoc />
  400. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd.Offset == Offset; }
  401. /// <inheritdoc />
  402. public override int GetHashCode () { return Offset.GetHashCode (); }
  403. /// <summary>
  404. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  405. /// </summary>
  406. public bool UseDimForOffset { get; }
  407. /// <inheritdoc />
  408. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; }
  409. internal override int Anchor (int width)
  410. {
  411. if (UseDimForOffset)
  412. {
  413. return width;
  414. }
  415. return width - Offset;
  416. }
  417. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  418. {
  419. int newLocation = Anchor (superviewDimension);
  420. if (UseDimForOffset)
  421. {
  422. newLocation -= dim.Anchor (superviewDimension);
  423. }
  424. return newLocation;
  425. }
  426. }
  427. /// <summary>
  428. /// Represents a position that is centered.
  429. /// </summary>
  430. public class PosCenter : Pos
  431. {
  432. /// <inheritdoc />
  433. public override string ToString () { return "Center"; }
  434. internal override int Anchor (int width) { return width / 2; }
  435. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  436. {
  437. int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
  438. return Anchor (superviewDimension - newDimension);
  439. }
  440. }
  441. /// <summary>
  442. /// Represents a position that is a combination of two other positions.
  443. /// </summary>
  444. /// <remarks>
  445. /// <para>
  446. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  447. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  448. /// </para>
  449. /// </remarks>
  450. /// <param name="add">Indicates whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added, otherwise they are subtracted.</param>
  451. /// <param name="left">The left position.</param>
  452. /// <param name="right">The right position.</param>
  453. public class PosCombine (bool add, Pos left, Pos right) : Pos
  454. {
  455. /// <summary>
  456. /// Gets whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added, otherwise they are subtracted.
  457. /// </summary>
  458. public bool Add { get; } = add;
  459. /// <summary>
  460. /// Gets the left position.
  461. /// </summary>
  462. public new Pos Left { get; } = left;
  463. /// <summary>
  464. /// Gets the right position.
  465. /// </summary>
  466. public new Pos Right { get; } = right;
  467. /// <inheritdoc />
  468. public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
  469. internal override int Anchor (int width)
  470. {
  471. int la = Left.Anchor (width);
  472. int ra = Right.Anchor (width);
  473. if (Add)
  474. {
  475. return la + ra;
  476. }
  477. return la - ra;
  478. }
  479. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  480. {
  481. int newDimension = dim.Calculate (0, superviewDimension, us, dimension);
  482. int left = Left.Calculate (superviewDimension, dim, us, dimension);
  483. int right = Right.Calculate (superviewDimension, dim, us, dimension);
  484. if (Add)
  485. {
  486. return left + right;
  487. }
  488. return left - right;
  489. }
  490. internal override bool ReferencesOtherViews ()
  491. {
  492. if (Left.ReferencesOtherViews ())
  493. {
  494. return true;
  495. }
  496. if (Right.ReferencesOtherViews ())
  497. {
  498. return true;
  499. }
  500. return false;
  501. }
  502. }
  503. /// <summary>
  504. /// Represents a position that is a percentage of the width or height of the SuperView.
  505. /// </summary>
  506. /// <remarks>
  507. /// <para>
  508. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  509. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  510. /// </para>
  511. /// </remarks>
  512. /// <param name="percent"></param>
  513. public class PosPercent (float percent) : Pos
  514. {
  515. /// <summary>
  516. /// Gets the factor that represents the percentage of the width or height of the SuperView.
  517. /// </summary>
  518. public new float Percent { get; } = percent;
  519. /// <inheritdoc />
  520. public override bool Equals (object other) { return other is PosPercent f && f.Percent == Percent; }
  521. /// <inheritdoc />
  522. public override int GetHashCode () { return Percent.GetHashCode (); }
  523. /// <inheritdoc />
  524. public override string ToString () { return $"Percent({Percent})"; }
  525. internal override int Anchor (int width) { return (int)(width * Percent); }
  526. }
  527. /// <summary>
  528. /// Represents a position that is computed by executing a function that returns an integer position.
  529. /// </summary>
  530. /// <remarks>
  531. /// <para>
  532. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  533. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  534. /// </para>
  535. /// </remarks>
  536. /// <param name="pos">The position.</param>
  537. public class PosFunc (Func<int> pos) : Pos
  538. {
  539. /// <summary>
  540. /// Gets the function that computes the position.
  541. /// </summary>
  542. public Func<int> Func { get; } = pos;
  543. /// <inheritdoc />
  544. public override bool Equals (object other) { return other is PosFunc f && f.Func () == Func (); }
  545. /// <inheritdoc />
  546. public override int GetHashCode () { return Func.GetHashCode (); }
  547. /// <inheritdoc />
  548. public override string ToString () { return $"PosFunc({Func ()})"; }
  549. internal override int Anchor (int width) { return Func (); }
  550. }
  551. /// <summary>
  552. /// Represents a position that is anchored to the side of another view.
  553. /// </summary>
  554. /// <remarks>
  555. /// <para>
  556. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  557. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  558. /// </para>
  559. /// </remarks>
  560. /// <param name="view">The View the position is anchored to.</param>
  561. /// <param name="side">The side of the View the position is anchored to.</param>
  562. public class PosView (View view, Side side) : Pos
  563. {
  564. /// <summary>
  565. /// Gets the View the position is anchored to.
  566. /// </summary>
  567. public View Target { get; } = view;
  568. /// <summary>
  569. /// Gets the side of the View the position is anchored to.
  570. /// </summary>
  571. public Side Side { get; } = side;
  572. /// <inheritdoc />
  573. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  574. /// <inheritdoc />
  575. public override int GetHashCode () { return Target.GetHashCode (); }
  576. /// <inheritdoc />
  577. public override string ToString ()
  578. {
  579. string sideString = Side switch
  580. {
  581. Side.Left => "left",
  582. Side.Top => "top",
  583. Side.Right => "right",
  584. Side.Bottom => "bottom",
  585. _ => "unknown"
  586. };
  587. if (Target == null)
  588. {
  589. throw new NullReferenceException (nameof (Target));
  590. }
  591. return $"View(side={sideString},target={Target})";
  592. }
  593. internal override int Anchor (int width)
  594. {
  595. return Side switch
  596. {
  597. Side.Left => Target.Frame.X,
  598. Side.Top => Target.Frame.Y,
  599. Side.Right => Target.Frame.Right,
  600. Side.Bottom => Target.Frame.Bottom,
  601. _ => 0
  602. };
  603. }
  604. /// <summary>
  605. /// Diagnostics API to determine if this Pos object references other views.
  606. /// </summary>
  607. /// <returns></returns>
  608. internal override bool ReferencesOtherViews () { return true; }
  609. }