PosDim.cs 54 KB

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