Pos.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. using System.ComponentModel;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Indicates the side for <see cref="Pos"/> operations.
  5. /// </summary>
  6. public enum Side
  7. {
  8. /// <summary>
  9. /// The left (X) side of the view.
  10. /// </summary>
  11. Left = 0,
  12. /// <summary>
  13. /// The top (Y) side of the view.
  14. /// </summary>
  15. Top = 1,
  16. /// <summary>
  17. /// The right (X + Width) side of the view.
  18. /// </summary>
  19. Right = 2,
  20. /// <summary>
  21. /// The bottom (Y + Height) side of the view.
  22. /// </summary>
  23. Bottom = 3
  24. }
  25. /// <summary>
  26. /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
  27. /// relative to the ending dimension. Integer values are implicitly convertible to an absolute <see cref="Pos"/>. These
  28. /// objects are created using the static methods Percent, AnchorEnd, and Center. The <see cref="Pos"/> objects can be
  29. /// combined with the addition and subtraction operators.
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.</para>
  33. /// <para>
  34. /// These can be used to set the absolute position, when merely assigning an integer value (via the implicit
  35. /// integer to <see cref="Pos"/> conversion), and they can be combined to produce more useful layouts, like:
  36. /// Pos.Center - 3, which would shift the position of the <see cref="View"/> 3 characters to the left after
  37. /// centering for example.
  38. /// </para>
  39. /// <para>
  40. /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View).
  41. /// The X(View) and Y(View) are aliases to Left(View) and Top(View) respectively.
  42. /// </para>
  43. /// <para>
  44. /// <list type="table">
  45. /// <listheader>
  46. /// <term>Pos Object</term> <description>Description</description>
  47. /// </listheader>
  48. /// <item>
  49. /// <term>
  50. /// <see cref="Pos.Align"/>
  51. /// </term>
  52. /// <description>
  53. /// Creates a <see cref="Pos"/> object that aligns a set of views.
  54. /// </description>
  55. /// </item>
  56. /// <item>
  57. /// <term>
  58. /// <see cref="Pos.Function(Func{int})"/>
  59. /// </term>
  60. /// <description>
  61. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  62. /// function. The function will be called every time the position is needed.
  63. /// </description>
  64. /// </item>
  65. /// <item>
  66. /// <term>
  67. /// <see cref="Pos.Percent(float)"/>
  68. /// </term>
  69. /// <description>
  70. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  71. /// SuperView.
  72. /// </description>
  73. /// </item>
  74. /// <item>
  75. /// <term>
  76. /// <see cref="Pos.AnchorEnd()"/>
  77. /// </term>
  78. /// <description>
  79. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of
  80. /// the dimension, useful to flush the layout from the right or bottom.
  81. /// </description>
  82. /// </item>
  83. /// <item>
  84. /// <term>
  85. /// <see cref="Pos.Center"/>
  86. /// </term>
  87. /// <description>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</description>
  88. /// </item>
  89. /// <item>
  90. /// <term>
  91. /// <see cref="Absolute"/>
  92. /// </term>
  93. /// <description>
  94. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  95. /// integer value.
  96. /// </description>
  97. /// </item>
  98. /// <item>
  99. /// <term>
  100. /// <see cref="Pos.Left"/>
  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.X(View)"/>
  110. /// </term>
  111. /// <description>
  112. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  113. /// <see cref="View"/>.
  114. /// </description>
  115. /// </item>
  116. /// <item>
  117. /// <term>
  118. /// <see cref="Pos.Top(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.Y(View)"/>
  128. /// </term>
  129. /// <description>
  130. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  131. /// <see cref="View"/>.
  132. /// </description>
  133. /// </item>
  134. /// <item>
  135. /// <term>
  136. /// <see cref="Pos.Right(View)"/>
  137. /// </term>
  138. /// <description>
  139. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  140. /// specified <see cref="View"/>.
  141. /// </description>
  142. /// </item>
  143. /// <item>
  144. /// <term>
  145. /// <see cref="Pos.Bottom(View)"/>
  146. /// </term>
  147. /// <description>
  148. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  149. /// specified <see cref="View"/>
  150. /// </description>
  151. /// </item>
  152. /// </list>
  153. /// </para>
  154. /// </remarks>
  155. public class Pos
  156. {
  157. /// <summary>
  158. /// Creates a <see cref="Pos"/> object that aligns a set of views according to the specified alignment setting.
  159. /// </summary>
  160. /// <param name="alignment"></param>
  161. /// <param name="groupId">
  162. /// The optional, unique identifier for the set of views to align according to
  163. /// <paramref name="alignment"/>.
  164. /// </param>
  165. /// <returns></returns>
  166. public static Pos Align (Alignment alignment, int groupId = 0) { return new PosAlign (alignment, groupId); }
  167. /// <summary>
  168. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  169. /// bottom) of the SuperView, minus the respective dimension of the View. This is equivalent to using
  170. /// <see cref="Pos.AnchorEnd(int)"/>,
  171. /// with an offset equivalent to the View's respective dimension.
  172. /// </summary>
  173. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  174. /// <example>
  175. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  176. /// <code>
  177. /// anchorButton.X = Pos.AnchorEnd ();
  178. /// anchorButton.Y = Pos.AnchorEnd ();
  179. /// </code>
  180. /// </example>
  181. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  182. /// <summary>
  183. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView,
  184. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  185. /// dimension to ensure the view is fully visible.
  186. /// </summary>
  187. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  188. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  189. /// <example>
  190. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  191. /// <code>
  192. /// anchorButton.X = Pos.AnchorEnd (10);
  193. /// anchorButton.Y = 1
  194. /// </code>
  195. /// </example>
  196. public static Pos AnchorEnd (int offset)
  197. {
  198. if (offset < 0)
  199. {
  200. throw new ArgumentException (@"Must be positive", nameof (offset));
  201. }
  202. return new PosAnchorEnd (offset);
  203. }
  204. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  205. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  206. /// <param name="position">The value to convert to the <see cref="Pos"/>.</param>
  207. public static Pos Absolute (int position) { return new PosAbsolute (position); }
  208. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  209. /// <returns>The center Pos.</returns>
  210. /// <example>
  211. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  212. /// is 80% the width of the <see cref="View"/> it added to.
  213. /// <code>
  214. /// var textView = new TextView () {
  215. /// X = Pos.Center (),
  216. /// Y = Pos.Percent (50),
  217. /// Width = Dim.Percent (80),
  218. /// Height = Dim.Percent (30),
  219. /// };
  220. /// </code>
  221. /// </example>
  222. public static Pos Center () { return new PosCenter (); }
  223. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  224. /// <param name="other">The object to compare with the current object. </param>
  225. /// <returns>
  226. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  227. /// <see langword="false"/>.
  228. /// </returns>
  229. public override bool Equals (object other) { return other is Pos abs && abs == this; }
  230. /// <summary>
  231. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  232. /// will be called every time the position is needed.
  233. /// </summary>
  234. /// <param name="function">The function to be executed.</param>
  235. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  236. public static Pos Function (Func<int> function) { return new PosFunc (function); }
  237. /// <summary>Serves as the default hash function. </summary>
  238. /// <returns>A hash code for the current object.</returns>
  239. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  240. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  241. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  242. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  243. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  244. public static Pos operator + (Pos left, Pos right)
  245. {
  246. if (left is PosAbsolute && right is PosAbsolute)
  247. {
  248. return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
  249. }
  250. var newPos = new PosCombine (true, left, right);
  251. if (left is PosView view)
  252. {
  253. view.Target.SetNeedsLayout ();
  254. }
  255. return newPos;
  256. }
  257. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  258. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  259. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  260. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  261. /// <summary>
  262. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  263. /// <see cref="Pos"/>.
  264. /// </summary>
  265. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  266. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  267. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  268. public static Pos operator - (Pos left, Pos right)
  269. {
  270. if (left is PosAbsolute && right is PosAbsolute)
  271. {
  272. return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
  273. }
  274. var newPos = new PosCombine (false, left, right);
  275. if (left is PosView view)
  276. {
  277. view.Target.SetNeedsLayout ();
  278. }
  279. return newPos;
  280. }
  281. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  282. /// <returns>The percent <see cref="Pos"/> object.</returns>
  283. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  284. /// <example>
  285. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  286. /// is 80% the width of the <see cref="View"/> it added to.
  287. /// <code>
  288. /// var textView = new TextField {
  289. /// X = Pos.Center (),
  290. /// Y = Pos.Percent (50),
  291. /// Width = Dim.Percent (80),
  292. /// Height = Dim.Percent (30),
  293. /// };
  294. /// </code>
  295. /// </example>
  296. public static Pos Percent (float percent)
  297. {
  298. if (percent is < 0 or > 100)
  299. {
  300. throw new ArgumentException ("Percent value must be between 0 and 100.");
  301. }
  302. return new PosPercent (percent / 100);
  303. }
  304. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</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 Top (View view) { return new PosView (view, Side.Top); }
  308. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  309. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  310. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  311. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  312. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  313. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  314. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  315. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  316. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  317. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  318. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  319. public static Pos X (View view) { return new PosView (view, Side.Left); }
  320. /// <summary>
  321. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  322. /// <see cref="View"/>
  323. /// </summary>
  324. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  325. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  326. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  327. /// <summary>
  328. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  329. /// <see cref="View"/>.
  330. /// </summary>
  331. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  332. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  333. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  334. /// <summary>
  335. /// Gets a position that is anchored to a certain point in the layout. This method is typically used
  336. /// internally by the layout system to determine where a View should be positioned.
  337. /// </summary>
  338. /// <param name="width">The width of the area where the View is being positioned (Superview.ContentSize).</param>
  339. /// <returns>
  340. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  341. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  342. /// position that is anchored to the end of the layout, and so on.
  343. /// </returns>
  344. internal virtual int Anchor (int width) { return 0; }
  345. /// <summary>
  346. /// Calculates and returns the position of a <see cref="View"/> object. It takes into account the dimension of the
  347. /// superview and the dimension of the view itself.
  348. /// </summary>
  349. /// <param name="superviewDimension">
  350. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  351. /// height for y-coordinate calculation.
  352. /// </param>
  353. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  354. /// <param name="us">The View that holds this Pos object.</param>
  355. /// <param name="dimension">Width or Height</param>
  356. /// <returns>
  357. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  358. /// that
  359. /// is used.
  360. /// </returns>
  361. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return Anchor (superviewDimension); }
  362. /// <summary>
  363. /// Diagnostics API to determine if this Pos object references other views.
  364. /// </summary>
  365. /// <returns></returns>
  366. internal virtual bool ReferencesOtherViews () { return false; }
  367. }
  368. /// <summary>
  369. /// Enables alignment of a set of views.
  370. /// </summary>
  371. /// <remarks>
  372. /// <para>
  373. /// The Group ID is used to identify a set of views that should be alignment together. When only a single
  374. /// set of views is aligned, setting the Group ID is not needed because it defaults to 0.
  375. /// </para>
  376. /// <para>
  377. /// The first view added to the Superview with a given Group ID is used to determine the alignment of the group.
  378. /// The alignment is applied to all views with the same Group ID.
  379. /// </para>
  380. /// </remarks>
  381. public class PosAlign : Pos
  382. {
  383. // BUGBUG: PosAlign should be internal like all other Pos classes. It is public because the PosAlign Scenario uses it. Refactor that Scenario.
  384. /// <summary>
  385. /// The cached location. Used to store the calculated location to avoid recalculating it.
  386. /// </summary>
  387. private int? _location;
  388. /// <summary>
  389. /// Gets the identifier of a set of views that should be aligned together. When only a single
  390. /// set of views is aligned, setting the <see cref="_groupId"/> is not needed because it defaults to 0.
  391. /// </summary>
  392. private readonly int _groupId;
  393. /// <summary>
  394. /// Gets the alignment settings.
  395. /// </summary>
  396. public Aligner Aligner { get; } = new ();
  397. /// <summary>
  398. /// Aligns the views in <paramref name="views"/> that have the same group ID as <paramref name="groupId"/>.
  399. /// </summary>
  400. /// <param name="groupId"></param>
  401. /// <param name="views"></param>
  402. /// <param name="dimension"></param>
  403. /// <param name="size"></param>
  404. private static void AlignGroup (int groupId, IList<View> views, Dimension dimension, int size)
  405. {
  406. if (views is null)
  407. {
  408. return;
  409. }
  410. Aligner firstInGroup = null;
  411. List<int> dimensionsList = new ();
  412. List<View> viewsInGroup = views.Where (
  413. v =>
  414. {
  415. if (dimension == Dimension.Width && v.X is PosAlign alignX)
  416. {
  417. return alignX._groupId == groupId;
  418. }
  419. if (dimension == Dimension.Height && v.Y is PosAlign alignY)
  420. {
  421. return alignY._groupId == groupId;
  422. }
  423. return false;
  424. })
  425. .ToList ();
  426. if (viewsInGroup.Count == 0)
  427. {
  428. return;
  429. }
  430. foreach (View view in viewsInGroup)
  431. {
  432. PosAlign posAlign = dimension == Dimension.Width ? view.X as PosAlign : view.Y as PosAlign;
  433. if (posAlign is { })
  434. {
  435. if (firstInGroup is null)
  436. {
  437. firstInGroup = posAlign.Aligner;
  438. }
  439. dimensionsList.Add (dimension == Dimension.Width ? view.Frame.Width : view.Frame.Height);
  440. }
  441. }
  442. if (firstInGroup is null)
  443. {
  444. return;
  445. }
  446. firstInGroup.ContainerSize = size;
  447. int [] locations = firstInGroup.Align (dimensionsList.ToArray ());
  448. for (var index = 0; index < viewsInGroup.Count; index++)
  449. {
  450. View view = viewsInGroup [index];
  451. PosAlign align = dimension == Dimension.Width ? view.X as PosAlign : view.Y as PosAlign;
  452. if (align is { })
  453. {
  454. align._location = locations [index];
  455. }
  456. }
  457. }
  458. /// <summary>
  459. /// Enables alignment of a set of views.
  460. /// </summary>
  461. /// <param name="alignment"></param>
  462. /// <param name="groupId">The unique identifier for the set of views to align according to <paramref name="alignment"/>.</param>
  463. public PosAlign (Alignment alignment, int groupId = 0)
  464. {
  465. Aligner.SpaceBetweenItems = true;
  466. Aligner.Alignment = alignment;
  467. _groupId = groupId;
  468. Aligner.PropertyChanged += Aligner_PropertyChanged;
  469. }
  470. private void Aligner_PropertyChanged (object sender, PropertyChangedEventArgs e) { _location = null; }
  471. /// <inheritdoc/>
  472. public override bool Equals (object other)
  473. {
  474. return other is PosAlign align && _groupId == align._groupId && _location == align._location && align.Aligner.Alignment == Aligner.Alignment;
  475. }
  476. /// <inheritdoc/>
  477. public override int GetHashCode () { return Aligner.GetHashCode () ^ _groupId.GetHashCode (); }
  478. /// <inheritdoc/>
  479. public override string ToString () { return $"Align(groupId={_groupId}, alignment={Aligner.Alignment})"; }
  480. internal override int Anchor (int width) { return _location ?? 0 - width; }
  481. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  482. {
  483. if (_location.HasValue && Aligner.ContainerSize == superviewDimension)
  484. {
  485. return _location.Value;
  486. }
  487. if (us?.SuperView is null)
  488. {
  489. return 0;
  490. }
  491. AlignGroup (_groupId, us.SuperView.Subviews, dimension, superviewDimension);
  492. if (_location.HasValue)
  493. {
  494. return _location.Value;
  495. }
  496. return 0;
  497. }
  498. }
  499. /// <summary>
  500. /// Represents an absolute position in the layout. This is used to specify a fixed position in the layout.
  501. /// </summary>
  502. /// <remarks>
  503. /// <para>
  504. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  505. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  506. /// </para>
  507. /// </remarks>
  508. /// <param name="position"></param>
  509. public class PosAbsolute (int position) : Pos
  510. {
  511. /// <summary>
  512. /// The position of the <see cref="View"/> in the layout.
  513. /// </summary>
  514. public int Position { get; } = position;
  515. /// <inheritdoc />
  516. public override bool Equals (object other) { return other is PosAbsolute abs && abs.Position == Position; }
  517. /// <inheritdoc />
  518. public override int GetHashCode () { return Position.GetHashCode (); }
  519. /// <inheritdoc />
  520. public override string ToString () { return $"Absolute({Position})"; }
  521. internal override int Anchor (int width) { return Position; }
  522. }
  523. /// <summary>
  524. /// Represents a position anchored to the end (right side or bottom).
  525. /// </summary>
  526. /// <remarks>
  527. /// <para>
  528. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  529. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  530. /// </para>
  531. /// </remarks>
  532. public class PosAnchorEnd : Pos
  533. {
  534. /// <summary>
  535. /// Gets the offset of the position from the right/bottom.
  536. /// </summary>
  537. public int Offset { get; }
  538. /// <summary>
  539. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  540. /// minus the respective dimension of the View. This is equivalent to using <see cref="PosAnchorEnd(int)"/>,
  541. /// with an offset equivalent to the View's respective dimension.
  542. /// </summary>
  543. public PosAnchorEnd () { UseDimForOffset = true; }
  544. /// <summary>
  545. /// Constructs a new position anchored to the end (right side or bottom) of the SuperView,
  546. /// </summary>
  547. /// <param name="offset"></param>
  548. public PosAnchorEnd (int offset) { Offset = offset; }
  549. /// <inheritdoc />
  550. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd.Offset == Offset; }
  551. /// <inheritdoc />
  552. public override int GetHashCode () { return Offset.GetHashCode (); }
  553. /// <summary>
  554. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  555. /// </summary>
  556. public bool UseDimForOffset { get; }
  557. /// <inheritdoc />
  558. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; }
  559. internal override int Anchor (int width)
  560. {
  561. if (UseDimForOffset)
  562. {
  563. return width;
  564. }
  565. return width - Offset;
  566. }
  567. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  568. {
  569. int newLocation = Anchor (superviewDimension);
  570. if (UseDimForOffset)
  571. {
  572. newLocation -= dim.Anchor (superviewDimension);
  573. }
  574. return newLocation;
  575. }
  576. }
  577. /// <summary>
  578. /// Represents a position that is centered.
  579. /// </summary>
  580. public class PosCenter : Pos
  581. {
  582. /// <inheritdoc />
  583. public override string ToString () { return "Center"; }
  584. internal override int Anchor (int width) { return width / 2; }
  585. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  586. {
  587. int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
  588. return Anchor (superviewDimension - newDimension);
  589. }
  590. }
  591. /// <summary>
  592. /// Represents a position that is a combination of two other positions.
  593. /// </summary>
  594. /// <remarks>
  595. /// <para>
  596. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  597. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  598. /// </para>
  599. /// </remarks>
  600. /// <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>
  601. /// <param name="left">The left position.</param>
  602. /// <param name="right">The right position.</param>
  603. public class PosCombine (bool add, Pos left, Pos right) : Pos
  604. {
  605. /// <summary>
  606. /// Gets whether the two positions are added or subtracted. If <see langword="true"/>, the positions are added, otherwise they are subtracted.
  607. /// </summary>
  608. public bool Add { get; } = add;
  609. /// <summary>
  610. /// Gets the left position.
  611. /// </summary>
  612. public new Pos Left { get; } = left;
  613. /// <summary>
  614. /// Gets the right position.
  615. /// </summary>
  616. public new Pos Right { get; } = right;
  617. /// <inheritdoc />
  618. public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
  619. internal override int Anchor (int width)
  620. {
  621. int la = Left.Anchor (width);
  622. int ra = Right.Anchor (width);
  623. if (Add)
  624. {
  625. return la + ra;
  626. }
  627. return la - ra;
  628. }
  629. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  630. {
  631. int newDimension = dim.Calculate (0, superviewDimension, us, dimension);
  632. int left = Left.Calculate (superviewDimension, dim, us, dimension);
  633. int right = Right.Calculate (superviewDimension, dim, us, dimension);
  634. if (Add)
  635. {
  636. return left + right;
  637. }
  638. return left - right;
  639. }
  640. internal override bool ReferencesOtherViews ()
  641. {
  642. if (Left.ReferencesOtherViews ())
  643. {
  644. return true;
  645. }
  646. if (Right.ReferencesOtherViews ())
  647. {
  648. return true;
  649. }
  650. return false;
  651. }
  652. }
  653. /// <summary>
  654. /// Represents a position that is a percentage of the width or height of the SuperView.
  655. /// </summary>
  656. /// <remarks>
  657. /// <para>
  658. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  659. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  660. /// </para>
  661. /// </remarks>
  662. /// <param name="percent"></param>
  663. public class PosPercent (float percent) : Pos
  664. {
  665. /// <summary>
  666. /// Gets the factor that represents the percentage of the width or height of the SuperView.
  667. /// </summary>
  668. public new float Percent { get; } = percent;
  669. /// <inheritdoc />
  670. public override bool Equals (object other) { return other is PosPercent f && f.Percent == Percent; }
  671. /// <inheritdoc />
  672. public override int GetHashCode () { return Percent.GetHashCode (); }
  673. /// <inheritdoc />
  674. public override string ToString () { return $"Percent({Percent})"; }
  675. internal override int Anchor (int width) { return (int)(width * Percent); }
  676. }
  677. /// <summary>
  678. /// Represents a position that is computed by executing a function that returns an integer position.
  679. /// </summary>
  680. /// <remarks>
  681. /// <para>
  682. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  683. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  684. /// </para>
  685. /// </remarks>
  686. /// <param name="pos">The position.</param>
  687. public class PosFunc (Func<int> pos) : Pos
  688. {
  689. /// <summary>
  690. /// Gets the function that computes the position.
  691. /// </summary>
  692. public Func<int> Func { get; } = pos;
  693. /// <inheritdoc />
  694. public override bool Equals (object other) { return other is PosFunc f && f.Func () == Func (); }
  695. /// <inheritdoc />
  696. public override int GetHashCode () { return Func.GetHashCode (); }
  697. /// <inheritdoc />
  698. public override string ToString () { return $"PosFunc({Func ()})"; }
  699. internal override int Anchor (int width) { return Func (); }
  700. }
  701. /// <summary>
  702. /// Represents a position that is anchored to the side of another view.
  703. /// </summary>
  704. /// <remarks>
  705. /// <para>
  706. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  707. /// methods on the <see cref="Pos"/> class to create <see cref="Pos"/> objects instead.
  708. /// </para>
  709. /// </remarks>
  710. /// <param name="view">The View the position is anchored to.</param>
  711. /// <param name="side">The side of the View the position is anchored to.</param>
  712. public class PosView (View view, Side side) : Pos
  713. {
  714. /// <summary>
  715. /// Gets the View the position is anchored to.
  716. /// </summary>
  717. public View Target { get; } = view;
  718. /// <summary>
  719. /// Gets the side of the View the position is anchored to.
  720. /// </summary>
  721. public Side Side { get; } = side;
  722. /// <inheritdoc />
  723. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  724. /// <inheritdoc />
  725. public override int GetHashCode () { return Target.GetHashCode (); }
  726. /// <inheritdoc />
  727. public override string ToString ()
  728. {
  729. string sideString = Side switch
  730. {
  731. Side.Left => "left",
  732. Side.Top => "top",
  733. Side.Right => "right",
  734. Side.Bottom => "bottom",
  735. _ => "unknown"
  736. };
  737. if (Target == null)
  738. {
  739. throw new NullReferenceException (nameof (Target));
  740. }
  741. return $"View(side={sideString},target={Target})";
  742. }
  743. internal override int Anchor (int width)
  744. {
  745. return Side switch
  746. {
  747. Side.Left => Target.Frame.X,
  748. Side.Top => Target.Frame.Y,
  749. Side.Right => Target.Frame.Right,
  750. Side.Bottom => Target.Frame.Bottom,
  751. _ => 0
  752. };
  753. }
  754. /// <summary>
  755. /// Diagnostics API to determine if this Pos object references other views.
  756. /// </summary>
  757. /// <returns></returns>
  758. internal override bool ReferencesOtherViews () { return true; }
  759. }