PosDim.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
  5. /// relative to the ending dimension. Integer values are implicitly convertible to an absolute <see cref="Pos"/>. These
  6. /// objects are created using the static methods Percent, AnchorEnd, and Center. The <see cref="Pos"/> objects can be
  7. /// combined with the addition and subtraction operators.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.</para>
  11. /// <para>
  12. /// These can be used to set the absolute position, when merely assigning an integer value (via the implicit
  13. /// integer to <see cref="Pos"/> conversion), and they can be combined to produce more useful layouts, like:
  14. /// Pos.Center - 3, which would shift the position of the <see cref="View"/> 3 characters to the left after
  15. /// centering for example.
  16. /// </para>
  17. /// <para>
  18. /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View).
  19. /// The X(View) and Y(View) are aliases to Left(View) and Top(View) respectively.
  20. /// </para>
  21. /// <para>
  22. /// <list type="table">
  23. /// <listheader>
  24. /// <term>Pos Object</term> <description>Description</description>
  25. /// </listheader>
  26. /// <item>
  27. /// <term>
  28. /// <see cref="Pos.Function(Func{int})"/>
  29. /// </term>
  30. /// <description>
  31. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  32. /// function. The function will be called every time the position is needed.
  33. /// </description>
  34. /// </item>
  35. /// <item>
  36. /// <term>
  37. /// <see cref="Pos.Percent(float)"/>
  38. /// </term>
  39. /// <description>
  40. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  41. /// SuperView.
  42. /// </description>
  43. /// </item>
  44. /// <item>
  45. /// <term>
  46. /// <see cref="Pos.AnchorEnd()"/>
  47. /// </term>
  48. /// <description>
  49. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of
  50. /// the dimension, useful to flush the layout from the right or bottom.
  51. /// </description>
  52. /// </item>
  53. /// <item>
  54. /// <term>
  55. /// <see cref="Pos.Center"/>
  56. /// </term>
  57. /// <description>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</description>
  58. /// </item>
  59. /// <item>
  60. /// <term>
  61. /// <see cref="Pos.At(int)"/>
  62. /// </term>
  63. /// <description>
  64. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  65. /// integer value.
  66. /// </description>
  67. /// </item>
  68. /// <item>
  69. /// <term>
  70. /// <see cref="Pos.Left"/>
  71. /// </term>
  72. /// <description>
  73. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  74. /// <see cref="View"/>.
  75. /// </description>
  76. /// </item>
  77. /// <item>
  78. /// <term>
  79. /// <see cref="Pos.X(View)"/>
  80. /// </term>
  81. /// <description>
  82. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  83. /// <see cref="View"/>.
  84. /// </description>
  85. /// </item>
  86. /// <item>
  87. /// <term>
  88. /// <see cref="Pos.Top(View)"/>
  89. /// </term>
  90. /// <description>
  91. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  92. /// <see cref="View"/>.
  93. /// </description>
  94. /// </item>
  95. /// <item>
  96. /// <term>
  97. /// <see cref="Pos.Y(View)"/>
  98. /// </term>
  99. /// <description>
  100. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  101. /// <see cref="View"/>.
  102. /// </description>
  103. /// </item>
  104. /// <item>
  105. /// <term>
  106. /// <see cref="Pos.Right(View)"/>
  107. /// </term>
  108. /// <description>
  109. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  110. /// specified <see cref="View"/>.
  111. /// </description>
  112. /// </item>
  113. /// <item>
  114. /// <term>
  115. /// <see cref="Pos.Bottom(View)"/>
  116. /// </term>
  117. /// <description>
  118. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  119. /// specified <see cref="View"/>
  120. /// </description>
  121. /// </item>
  122. /// </list>
  123. /// </para>
  124. /// </remarks>
  125. public class Pos
  126. {
  127. /// <summary>
  128. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  129. /// bottom) of the SuperView, minus the respective dimension of the View. This is equivalent to using
  130. /// <see cref="Pos.AnchorEnd(int)"/>,
  131. /// with an offset equivalent to the View's respective dimension.
  132. /// </summary>
  133. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  134. /// <example>
  135. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  136. /// <code>
  137. /// anchorButton.X = Pos.AnchorEnd ();
  138. /// anchorButton.Y = Pos.AnchorEnd ();
  139. /// </code>
  140. /// </example>
  141. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  142. /// <summary>
  143. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView,
  144. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  145. /// dimension to ensure the view is fully visible.
  146. /// </summary>
  147. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  148. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  149. /// <example>
  150. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  151. /// <code>
  152. /// anchorButton.X = Pos.AnchorEnd (10);
  153. /// anchorButton.Y = 1
  154. /// </code>
  155. /// </example>
  156. public static Pos AnchorEnd (int offset)
  157. {
  158. if (offset < 0)
  159. {
  160. throw new ArgumentException (@"Must be positive", nameof (offset));
  161. }
  162. return new PosAnchorEnd (offset);
  163. }
  164. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  165. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  166. /// <param name="n">The value to convert to the <see cref="Pos"/>.</param>
  167. public static Pos At (int n) { return new PosAbsolute (n); }
  168. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  169. /// <returns>The center Pos.</returns>
  170. /// <example>
  171. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  172. /// is 80% the width of the <see cref="View"/> it added to.
  173. /// <code>
  174. /// var textView = new TextView () {
  175. /// X = Pos.Center (),
  176. /// Y = Pos.Percent (50),
  177. /// Width = Dim.Percent (80),
  178. /// Height = Dim.Percent (30),
  179. /// };
  180. /// </code>
  181. /// </example>
  182. public static Pos Center () { return new PosCenter (); }
  183. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  184. /// <param name="other">The object to compare with the current object. </param>
  185. /// <returns>
  186. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  187. /// <see langword="false"/>.
  188. /// </returns>
  189. public override bool Equals (object other) { return other is Pos abs && abs == this; }
  190. /// <summary>
  191. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  192. /// will be called every time the position is needed.
  193. /// </summary>
  194. /// <param name="function">The function to be executed.</param>
  195. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  196. public static Pos Function (Func<int> function) { return new PosFunc (function); }
  197. /// <summary>
  198. /// Creates a <see cref="Pos"/> object that justifies a set of views according to the specified justification.
  199. /// </summary>
  200. /// <param name="views"></param>
  201. /// <param name="justification"></param>
  202. /// <returns></returns>
  203. public static Pos Justify ( Justification justification) { return new PosJustify (justification); }
  204. /// <summary>Serves as the default hash function. </summary>
  205. /// <returns>A hash code for the current object.</returns>
  206. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  207. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  208. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  209. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  210. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  211. public static Pos operator + (Pos left, Pos right)
  212. {
  213. if (left is PosAbsolute && right is PosAbsolute)
  214. {
  215. return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
  216. }
  217. var newPos = new PosCombine (true, left, right);
  218. if (left is PosView view)
  219. {
  220. view.Target.SetNeedsLayout ();
  221. }
  222. return newPos;
  223. }
  224. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  225. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  226. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  227. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  228. /// <summary>
  229. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  230. /// <see cref="Pos"/>.
  231. /// </summary>
  232. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  233. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  234. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  235. public static Pos operator - (Pos left, Pos right)
  236. {
  237. if (left is PosAbsolute && right is PosAbsolute)
  238. {
  239. return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
  240. }
  241. var newPos = new PosCombine (false, left, right);
  242. if (left is PosView view)
  243. {
  244. view.Target.SetNeedsLayout ();
  245. }
  246. return newPos;
  247. }
  248. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  249. /// <returns>The percent <see cref="Pos"/> object.</returns>
  250. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  251. /// <example>
  252. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  253. /// is 80% the width of the <see cref="View"/> it added to.
  254. /// <code>
  255. /// var textView = new TextField {
  256. /// X = Pos.Center (),
  257. /// Y = Pos.Percent (50),
  258. /// Width = Dim.Percent (80),
  259. /// Height = Dim.Percent (30),
  260. /// };
  261. /// </code>
  262. /// </example>
  263. public static Pos Percent (float percent)
  264. {
  265. if (percent is < 0 or > 100)
  266. {
  267. throw new ArgumentException ("Percent value must be between 0 and 100.");
  268. }
  269. return new PosFactor (percent / 100);
  270. }
  271. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  272. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  273. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  274. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  275. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  276. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  277. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  278. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  279. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  280. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  281. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  282. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  283. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  284. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  285. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  286. public static Pos X (View view) { return new PosView (view, Side.Left); }
  287. /// <summary>
  288. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  289. /// <see cref="View"/>
  290. /// </summary>
  291. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  292. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  293. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  294. /// <summary>
  295. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  296. /// <see cref="View"/>.
  297. /// </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 Right (View view) { return new PosView (view, Side.Right); }
  301. /// <summary>
  302. /// Gets a position that is anchored to a certain point in the layout. This method is typically used
  303. /// internally by the layout system to determine where a View should be positioned.
  304. /// </summary>
  305. /// <param name="width">The width of the area where the View is being positioned (Superview.ContentSize).</param>
  306. /// <returns>
  307. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  308. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  309. /// position that is anchored to the end of the layout, and so on.
  310. /// </returns>
  311. internal virtual int Anchor (int width) { return 0; }
  312. /// <summary>
  313. /// Calculates and returns the position of a <see cref="View"/> object. It takes into account the dimension of the
  314. /// superview and the dimension of the view itself.
  315. /// </summary>
  316. /// <param name="superviewDimension">
  317. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  318. /// height for y-coordinate calculation.
  319. /// </param>
  320. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  321. /// <param name="us">The View that holds this Pos object.</param>
  322. /// <param name="dimension">Width or Height</param>
  323. /// <returns>
  324. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  325. /// that
  326. /// is used.
  327. /// </returns>
  328. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
  329. {
  330. return Anchor (superviewDimension);
  331. }
  332. internal class PosAbsolute (int n) : Pos
  333. {
  334. private readonly int _n = n;
  335. public override bool Equals (object other) { return other is PosAbsolute abs && abs._n == _n; }
  336. public override int GetHashCode () { return _n.GetHashCode (); }
  337. public override string ToString () { return $"Absolute({_n})"; }
  338. internal override int Anchor (int width) { return _n; }
  339. }
  340. internal class PosAnchorEnd : Pos
  341. {
  342. private readonly int _offset;
  343. public PosAnchorEnd () { UseDimForOffset = true; }
  344. public PosAnchorEnd (int offset) { _offset = offset; }
  345. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd._offset == _offset; }
  346. public override int GetHashCode () { return _offset.GetHashCode (); }
  347. /// <summary>
  348. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  349. /// </summary>
  350. internal bool UseDimForOffset { get; set; }
  351. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({_offset})"; }
  352. internal override int Anchor (int width)
  353. {
  354. if (UseDimForOffset)
  355. {
  356. return width;
  357. }
  358. return width - _offset;
  359. }
  360. internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
  361. {
  362. int newLocation = Anchor (superviewDimension);
  363. if (UseDimForOffset)
  364. {
  365. newLocation -= dim.Anchor (superviewDimension);
  366. }
  367. return newLocation;
  368. }
  369. }
  370. internal class PosCenter : Pos
  371. {
  372. public override string ToString () { return "Center"; }
  373. internal override int Anchor (int width) { return width / 2; }
  374. internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
  375. {
  376. int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
  377. return Anchor (superviewDimension - newDimension);
  378. }
  379. }
  380. internal class PosCombine (bool add, Pos left, Pos right) : Pos
  381. {
  382. internal bool _add = add;
  383. internal Pos _left = left, _right = right;
  384. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  385. internal override int Anchor (int width)
  386. {
  387. int la = _left.Anchor (width);
  388. int ra = _right.Anchor (width);
  389. if (_add)
  390. {
  391. return la + ra;
  392. }
  393. return la - ra;
  394. }
  395. internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
  396. {
  397. int newDimension = dim.Calculate (0, superviewDimension, us, dimension);
  398. int left = _left.Calculate (superviewDimension, dim, us, dimension);
  399. int right = _right.Calculate (superviewDimension, dim, us, dimension);
  400. if (_add)
  401. {
  402. return left + right;
  403. }
  404. return left - right;
  405. }
  406. }
  407. internal class PosFactor (float factor) : Pos
  408. {
  409. private readonly float _factor = factor;
  410. public override bool Equals (object other) { return other is PosFactor f && f._factor == _factor; }
  411. public override int GetHashCode () { return _factor.GetHashCode (); }
  412. public override string ToString () { return $"Factor({_factor})"; }
  413. internal override int Anchor (int width) { return (int)(width * _factor); }
  414. }
  415. /// <summary>
  416. /// Enables justification of a set of views.
  417. /// </summary>
  418. public class PosJustify : Pos
  419. {
  420. private readonly Justification _justification;
  421. /// <summary>
  422. /// Enables justification of a set of views.
  423. /// </summary>
  424. /// <param name="views">The set of views to justify according to <paramref name="justification"/>.</param>
  425. /// <param name="justification"></param>
  426. public PosJustify (Justification justification)
  427. {
  428. _justification = justification;
  429. }
  430. public override bool Equals (object other)
  431. {
  432. return other is PosJustify justify && justify._justification == _justification;
  433. }
  434. public override int GetHashCode () { return _justification.GetHashCode (); }
  435. public override string ToString ()
  436. {
  437. return $"Justify(alignment={_justification})";
  438. }
  439. internal override int Anchor (int width)
  440. {
  441. return width;
  442. }
  443. internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
  444. {
  445. if (us.SuperView is null)
  446. {
  447. return 0;
  448. }
  449. // Find all the views that are being justified - they have the same justification and opposite position as us
  450. // Then, pass the array of views to the Justify method
  451. int [] dimensions;
  452. int [] positions;
  453. int ourIndex = 0;
  454. if (dimension == Dim.Dimension.Width)
  455. {
  456. List<int> dimensionsList = new List<int> ();
  457. for (int i = 0; i < us.SuperView.Subviews.Count; i++)
  458. {
  459. var v = us.SuperView.Subviews [i];
  460. var j = v.X as PosJustify;
  461. if (j?._justification == _justification && v.Frame.Y == us.Frame.Y)
  462. {
  463. dimensionsList.Add (v.Frame.Width);
  464. if (v == us)
  465. {
  466. ourIndex = dimensionsList.Count - 1;
  467. }
  468. }
  469. }
  470. dimensions = dimensionsList.ToArray ();
  471. positions = new Justifier () { PutSpaceBetweenItems = true }.Justify (dimensions, _justification, superviewDimension);
  472. }
  473. else
  474. {
  475. List<int> dimensionsList = new List<int> ();
  476. for (int i = 0; i < us.SuperView.Subviews.Count; i++)
  477. {
  478. var v = us.SuperView.Subviews [i];
  479. var j = v.Y as PosJustify;
  480. if (j?._justification == _justification && v.Frame.X == us.Frame.X)
  481. {
  482. dimensionsList.Add (v.Frame.Height);
  483. if (v == us)
  484. {
  485. ourIndex = dimensionsList.Count - 1;
  486. }
  487. }
  488. }
  489. dimensions = dimensionsList.ToArray ();
  490. positions = new Justifier () { PutSpaceBetweenItems = false }.Justify (dimensions, _justification, superviewDimension);
  491. }
  492. return positions [ourIndex];
  493. }
  494. }
  495. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  496. internal class PosFunc (Func<int> n) : Pos
  497. {
  498. private readonly Func<int> _function = n;
  499. public override bool Equals (object other) { return other is PosFunc f && f._function () == _function (); }
  500. public override int GetHashCode () { return _function.GetHashCode (); }
  501. public override string ToString () { return $"PosFunc({_function ()})"; }
  502. internal override int Anchor (int width) { return _function (); }
  503. }
  504. /// <summary>
  505. /// Describes which side of the view to use for the position.
  506. /// </summary>
  507. public enum Side
  508. {
  509. /// <summary>
  510. /// The left (X) side of the view.
  511. /// </summary>
  512. Left = 0,
  513. /// <summary>
  514. /// The top (Y) side of the view.
  515. /// </summary>
  516. Top = 1,
  517. /// <summary>
  518. /// The right (X + Width) side of the view.
  519. /// </summary>
  520. Right = 2,
  521. /// <summary>
  522. /// The bottom (Y + Height) side of the view.
  523. /// </summary>
  524. Bottom = 3
  525. }
  526. internal class PosView (View view, Side side) : Pos
  527. {
  528. public readonly View Target = view;
  529. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  530. public override int GetHashCode () { return Target.GetHashCode (); }
  531. public override string ToString ()
  532. {
  533. string sideString = side switch
  534. {
  535. Side.Left => "left",
  536. Side.Top => "top",
  537. Side.Right => "right",
  538. Side.Bottom => "bottom",
  539. _ => "unknown"
  540. };
  541. if (Target == null)
  542. {
  543. throw new NullReferenceException (nameof (Target));
  544. }
  545. return $"View(side={sideString},target={Target})";
  546. }
  547. internal override int Anchor (int width)
  548. {
  549. return side switch
  550. {
  551. Side.Left => Target.Frame.X,
  552. Side.Top => Target.Frame.Y,
  553. Side.Right => Target.Frame.Right,
  554. Side.Bottom => Target.Frame.Bottom,
  555. _ => 0
  556. };
  557. }
  558. }
  559. }
  560. /// <summary>
  561. /// <para>
  562. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  563. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  564. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  565. /// </para>
  566. /// <para>
  567. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  568. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  569. /// subtraction operators.
  570. /// </para>
  571. /// </summary>
  572. /// <remarks>
  573. /// <para>
  574. /// <list type="table">
  575. /// <listheader>
  576. /// <term>Dim Object</term> <description>Description</description>
  577. /// </listheader>
  578. /// <item>
  579. /// <term>
  580. /// <see cref="Dim.Auto"/>
  581. /// </term>
  582. /// <description>
  583. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit
  584. /// the view's SubViews.
  585. /// </description>
  586. /// </item>
  587. /// <item>
  588. /// <term>
  589. /// <see cref="Dim.Function(Func{int})"/>
  590. /// </term>
  591. /// <description>
  592. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  593. /// function. The function will be called every time the dimension is needed.
  594. /// </description>
  595. /// </item>
  596. /// <item>
  597. /// <term>
  598. /// <see cref="Dim.Percent(float, bool)"/>
  599. /// </term>
  600. /// <description>
  601. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  602. /// SuperView.
  603. /// </description>
  604. /// </item>
  605. /// <item>
  606. /// <term>
  607. /// <see cref="Dim.Fill(int)"/>
  608. /// </term>
  609. /// <description>
  610. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  611. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  612. /// </description>
  613. /// </item>
  614. /// <item>
  615. /// <term>
  616. /// <see cref="Dim.Width(View)"/>
  617. /// </term>
  618. /// <description>
  619. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  620. /// <see cref="View"/>.
  621. /// </description>
  622. /// </item>
  623. /// <item>
  624. /// <term>
  625. /// <see cref="Dim.Height(View)"/>
  626. /// </term>
  627. /// <description>
  628. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  629. /// <see cref="View"/>.
  630. /// </description>
  631. /// </item>
  632. /// </list>
  633. /// </para>
  634. /// <para></para>
  635. /// </remarks>
  636. public class Dim
  637. {
  638. /// <summary>
  639. /// Specifies how <see cref="DimAuto"/> will compute the dimension.
  640. /// </summary>
  641. public enum DimAutoStyle
  642. {
  643. /// <summary>
  644. /// The dimension will be computed using both the view's <see cref="View.Text"/> and
  645. /// <see cref="View.Subviews"/> (whichever is larger).
  646. /// </summary>
  647. Auto,
  648. /// <summary>
  649. /// The Subview in <see cref="View.Subviews"/> with the largest corresponding position plus dimension
  650. /// will determine the dimension.
  651. /// The corresponding dimension of the view's <see cref="View.Text"/> will be ignored.
  652. /// </summary>
  653. Subviews,
  654. /// <summary>
  655. /// The corresponding dimension of the view's <see cref="View.Text"/>, formatted using the
  656. /// <see cref="View.TextFormatter"/> settings,
  657. /// will be used to determine the dimension.
  658. /// The corresponding dimensions of the <see cref="View.Subviews"/> will be ignored.
  659. /// </summary>
  660. Text
  661. }
  662. /// <summary>
  663. ///
  664. /// </summary>
  665. public enum Dimension
  666. {
  667. /// <summary>
  668. /// No dimension specified.
  669. /// </summary>
  670. None = 0,
  671. /// <summary>
  672. /// The height dimension.
  673. /// </summary>
  674. Height = 1,
  675. /// <summary>
  676. /// The width dimension.
  677. /// </summary>
  678. Width = 2
  679. }
  680. /// <summary>
  681. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all of the view's SubViews and/or Text.
  682. /// </summary>
  683. /// <example>
  684. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  685. /// SubViews.
  686. /// <code>
  687. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  688. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  689. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  690. /// view.Add (button, textField);
  691. /// </code>
  692. /// </example>
  693. /// <returns>The <see cref="Dim"/> object.</returns>
  694. /// <param name="style">
  695. /// Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  696. /// </param>
  697. /// <param name="min">Specifies the minimum dimension that view will be automatically sized to.</param>
  698. /// <param name="max">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  699. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim min = null, Dim max = null)
  700. {
  701. if (max != null)
  702. {
  703. throw new NotImplementedException (@"max is not implemented");
  704. }
  705. return new DimAuto (style, min, max);
  706. }
  707. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  708. /// <param name="other">The object to compare with the current object. </param>
  709. /// <returns>
  710. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  711. /// <see langword="false"/>.
  712. /// </returns>
  713. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  714. /// <summary>
  715. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  716. /// margin.
  717. /// </summary>
  718. /// <returns>The Fill dimension.</returns>
  719. /// <param name="margin">Margin to use.</param>
  720. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  721. /// <summary>
  722. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  723. /// The function will be called every time the dimension is needed.
  724. /// </summary>
  725. /// <param name="function">The function to be executed.</param>
  726. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  727. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  728. /// <summary>Serves as the default hash function. </summary>
  729. /// <returns>A hash code for the current object.</returns>
  730. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  731. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  732. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  733. /// <param name="view">The view that will be tracked.</param>
  734. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  735. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  736. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  737. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  738. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  739. public static Dim operator + (Dim left, Dim right)
  740. {
  741. if (left is DimAbsolute && right is DimAbsolute)
  742. {
  743. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  744. }
  745. var newDim = new DimCombine (true, left, right);
  746. (left as DimView)?.Target.SetNeedsLayout ();
  747. return newDim;
  748. }
  749. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  750. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  751. /// <param name="n">The value to convert to the pos.</param>
  752. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  753. /// <summary>
  754. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  755. /// <see cref="Dim"/>.
  756. /// </summary>
  757. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  758. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  759. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  760. public static Dim operator - (Dim left, Dim right)
  761. {
  762. if (left is DimAbsolute && right is DimAbsolute)
  763. {
  764. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  765. }
  766. var newDim = new DimCombine (false, left, right);
  767. (left as DimView)?.Target.SetNeedsLayout ();
  768. return newDim;
  769. }
  770. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  771. /// <returns>The percent <see cref="Dim"/> object.</returns>
  772. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  773. /// <param name="usePosition">
  774. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  775. /// <see cref="View.Y"/>).
  776. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  777. /// </param>
  778. /// <example>
  779. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  780. /// height,
  781. /// and is 80% the width of the SuperView.
  782. /// <code>
  783. /// var textView = new TextField {
  784. /// X = Pos.Center (),
  785. /// Y = Pos.Percent (50),
  786. /// Width = Dim.Percent (80),
  787. /// Height = Dim.Percent (30),
  788. /// };
  789. /// </code>
  790. /// </example>
  791. public static Dim Percent (float percent, bool usePosition = false)
  792. {
  793. if (percent is < 0 or > 100)
  794. {
  795. throw new ArgumentException ("Percent value must be between 0 and 100");
  796. }
  797. return new DimFactor (percent / 100, usePosition);
  798. }
  799. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  800. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  801. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  802. public static Dim Sized (int n) { return new DimAbsolute (n); }
  803. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  804. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  805. /// <param name="view">The view that will be tracked.</param>
  806. public static Dim Width (View view) { return new DimView (view, Dimension.Width); }
  807. /// <summary>
  808. /// Gets a dimension that is anchored to a certain point in the layout.
  809. /// This method is typically used internally by the layout system to determine the size of a View.
  810. /// </summary>
  811. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  812. /// <returns>
  813. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  814. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  815. /// dimension that is a certain percentage of the super view's size, and so on.
  816. /// </returns>
  817. internal virtual int Anchor (int width) { return 0; }
  818. /// <summary>
  819. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  820. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its content.
  821. /// </summary>
  822. /// <param name="location">
  823. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  824. /// top edge for height calculation.
  825. /// </param>
  826. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  827. /// <param name="us">The View that holds this Pos object.</param>
  828. /// <param name="dimension">Width or Height</param>
  829. /// <returns>
  830. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  831. /// is used.
  832. /// </returns>
  833. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  834. {
  835. return Math.Max (Anchor (superviewContentSize - location), 0);
  836. }
  837. internal class DimAbsolute (int n) : Dim
  838. {
  839. private readonly int _n = n;
  840. public override bool Equals (object other) { return other is DimAbsolute abs && abs._n == _n; }
  841. public override int GetHashCode () { return _n.GetHashCode (); }
  842. public override string ToString () { return $"Absolute({_n})"; }
  843. internal override int Anchor (int width) { return _n; }
  844. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  845. {
  846. // DimAbsolute.Anchor (int width) ignores width and returns n
  847. return Math.Max (Anchor (0), 0);
  848. }
  849. }
  850. internal class DimAuto (DimAutoStyle style, Dim min, Dim max) : Dim
  851. {
  852. internal readonly Dim _max = max;
  853. internal readonly Dim _min = min;
  854. internal readonly DimAutoStyle _style = style;
  855. internal int Size;
  856. public override bool Equals (object other) { return other is DimAuto auto && auto._min == _min && auto._max == _max && auto._style == _style; }
  857. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), _min, _max, _style); }
  858. public override string ToString () { return $"Auto({_style},{_min},{_max})"; }
  859. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  860. {
  861. if (us == null)
  862. {
  863. return _max?.Anchor (0) ?? 0;
  864. }
  865. var textSize = 0;
  866. var subviewsSize = 0;
  867. int autoMin = _min?.Anchor (superviewContentSize) ?? 0;
  868. if (superviewContentSize < autoMin)
  869. {
  870. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  871. return superviewContentSize;
  872. }
  873. if (_style is Dim.DimAutoStyle.Text or Dim.DimAutoStyle.Auto)
  874. {
  875. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  876. }
  877. if (_style is Dim.DimAutoStyle.Subviews or Dim.DimAutoStyle.Auto)
  878. {
  879. subviewsSize = us.Subviews.Count == 0
  880. ? 0
  881. : us.Subviews
  882. .Where (v => dimension == Dimension.Width ? v.X is not Pos.PosAnchorEnd : v.Y is not Pos.PosAnchorEnd)
  883. .Max (v => dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height);
  884. }
  885. int max = int.Max (textSize, subviewsSize);
  886. Thickness thickness = us.GetAdornmentsThickness ();
  887. if (dimension == Dimension.Width)
  888. {
  889. max += thickness.Horizontal;
  890. }
  891. else
  892. {
  893. max += thickness.Vertical;
  894. }
  895. max = int.Max (max, autoMin);
  896. return int.Min (max, _max?.Anchor (superviewContentSize) ?? superviewContentSize);
  897. }
  898. }
  899. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  900. {
  901. internal bool _add = add;
  902. internal Dim _left = left, _right = right;
  903. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  904. internal override int Anchor (int width)
  905. {
  906. int la = _left.Anchor (width);
  907. int ra = _right.Anchor (width);
  908. if (_add)
  909. {
  910. return la + ra;
  911. }
  912. return la - ra;
  913. }
  914. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  915. {
  916. int leftNewDim = _left.Calculate (location, superviewContentSize, us, dimension);
  917. int rightNewDim = _right.Calculate (location, superviewContentSize, us, dimension);
  918. int newDimension;
  919. if (_add)
  920. {
  921. newDimension = leftNewDim + rightNewDim;
  922. }
  923. else
  924. {
  925. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  926. }
  927. return newDimension;
  928. }
  929. }
  930. internal class DimFactor (float factor, bool remaining = false) : Dim
  931. {
  932. private readonly float _factor = factor;
  933. private readonly bool _remaining = remaining;
  934. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  935. public override int GetHashCode () { return _factor.GetHashCode (); }
  936. public bool IsFromRemaining () { return _remaining; }
  937. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  938. internal override int Anchor (int width) { return (int)(width * _factor); }
  939. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  940. {
  941. return _remaining ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  942. }
  943. }
  944. internal class DimFill (int margin) : Dim
  945. {
  946. private readonly int _margin = margin;
  947. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  948. public override int GetHashCode () { return _margin.GetHashCode (); }
  949. public override string ToString () { return $"Fill({_margin})"; }
  950. internal override int Anchor (int width) { return width - _margin; }
  951. }
  952. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  953. internal class DimFunc (Func<int> n) : Dim
  954. {
  955. private readonly Func<int> _function = n;
  956. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  957. public override int GetHashCode () { return _function.GetHashCode (); }
  958. public override string ToString () { return $"DimFunc({_function ()})"; }
  959. internal override int Anchor (int width) { return _function (); }
  960. }
  961. internal class DimView : Dim
  962. {
  963. private readonly Dimension _side;
  964. internal DimView (View view, Dimension side)
  965. {
  966. Target = view;
  967. _side = side;
  968. }
  969. public View Target { get; init; }
  970. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  971. public override int GetHashCode () { return Target.GetHashCode (); }
  972. public override string ToString ()
  973. {
  974. if (Target == null)
  975. {
  976. throw new NullReferenceException ();
  977. }
  978. string sideString = _side switch
  979. {
  980. Dimension.Height => "Height",
  981. Dimension.Width => "Width",
  982. _ => "unknown"
  983. };
  984. return $"View({sideString},{Target})";
  985. }
  986. internal override int Anchor (int width)
  987. {
  988. return _side switch
  989. {
  990. Dimension.Height => Target.Frame.Height,
  991. Dimension.Width => Target.Frame.Width,
  992. _ => 0
  993. };
  994. }
  995. }
  996. }