PosDim.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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.PutSpaceBetweenItems = true;
  537. Justifier.Justification = justification;
  538. _groupId = groupId;
  539. Justifier.PropertyChanged += Justifier_PropertyChanged;
  540. }
  541. private void Justifier_PropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
  542. {
  543. _location = null;
  544. }
  545. /// <inheritdoc />
  546. public override bool Equals (object other)
  547. {
  548. return other is PosJustify justify && _groupId == justify._groupId && _location == justify._location && justify.Justifier.Equals (Justifier);
  549. }
  550. /// <inheritdoc />
  551. public override int GetHashCode ()
  552. {
  553. return Justifier.GetHashCode () ^ _groupId.GetHashCode ();
  554. }
  555. /// <inheritdoc />
  556. public override string ToString ()
  557. {
  558. return $"Justify(groupId={_groupId}, justification={Justifier.Justification})";
  559. }
  560. internal override int Anchor (int width)
  561. {
  562. return _location ?? 0 - width;
  563. }
  564. internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
  565. {
  566. if (_location.HasValue && Justifier.ContainerSize == superviewDimension)
  567. {
  568. return _location.Value;
  569. }
  570. if (us.SuperView is null)
  571. {
  572. return 0;
  573. }
  574. JustifyGroup (_groupId, us.SuperView.Subviews, dimension, superviewDimension);
  575. if (_location.HasValue)
  576. {
  577. return _location.Value;
  578. }
  579. return 0;
  580. }
  581. }
  582. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  583. internal class PosFunc (Func<int> n) : Pos
  584. {
  585. private readonly Func<int> _function = n;
  586. public override bool Equals (object other) { return other is PosFunc f && f._function () == _function (); }
  587. public override int GetHashCode () { return _function.GetHashCode (); }
  588. public override string ToString () { return $"PosFunc({_function ()})"; }
  589. internal override int Anchor (int width) { return _function (); }
  590. }
  591. /// <summary>
  592. /// Describes which side of the view to use for the position.
  593. /// </summary>
  594. public enum Side
  595. {
  596. /// <summary>
  597. /// The left (X) side of the view.
  598. /// </summary>
  599. Left = 0,
  600. /// <summary>
  601. /// The top (Y) side of the view.
  602. /// </summary>
  603. Top = 1,
  604. /// <summary>
  605. /// The right (X + Width) side of the view.
  606. /// </summary>
  607. Right = 2,
  608. /// <summary>
  609. /// The bottom (Y + Height) side of the view.
  610. /// </summary>
  611. Bottom = 3
  612. }
  613. internal class PosView (View view, Side side) : Pos
  614. {
  615. public readonly View Target = view;
  616. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  617. public override int GetHashCode () { return Target.GetHashCode (); }
  618. public override string ToString ()
  619. {
  620. string sideString = side switch
  621. {
  622. Side.Left => "left",
  623. Side.Top => "top",
  624. Side.Right => "right",
  625. Side.Bottom => "bottom",
  626. _ => "unknown"
  627. };
  628. if (Target == null)
  629. {
  630. throw new NullReferenceException (nameof (Target));
  631. }
  632. return $"View(side={sideString},target={Target})";
  633. }
  634. internal override int Anchor (int width)
  635. {
  636. return side switch
  637. {
  638. Side.Left => Target.Frame.X,
  639. Side.Top => Target.Frame.Y,
  640. Side.Right => Target.Frame.Right,
  641. Side.Bottom => Target.Frame.Bottom,
  642. _ => 0
  643. };
  644. }
  645. /// <summary>
  646. /// Diagnostics API to determine if this Pos object references other views.
  647. /// </summary>
  648. /// <returns></returns>
  649. internal override bool ReferencesOtherViews ()
  650. {
  651. return true;
  652. }
  653. }
  654. }
  655. /// <summary>
  656. /// <para>
  657. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  658. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  659. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  660. /// </para>
  661. /// <para>
  662. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  663. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  664. /// subtraction operators.
  665. /// </para>
  666. /// </summary>
  667. /// <remarks>
  668. /// <para>
  669. /// <list type="table">
  670. /// <listheader>
  671. /// <term>Dim Object</term> <description>Description</description>
  672. /// </listheader>
  673. /// <item>
  674. /// <term>
  675. /// <see cref="Dim.Auto"/>
  676. /// </term>
  677. /// <description>
  678. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit
  679. /// the view's Text, SubViews, or ContentArea.
  680. /// </description>
  681. /// </item>
  682. /// <item>
  683. /// <term>
  684. /// <see cref="Dim.Function(Func{int})"/>
  685. /// </term>
  686. /// <description>
  687. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  688. /// function. The function will be called every time the dimension is needed.
  689. /// </description>
  690. /// </item>
  691. /// <item>
  692. /// <term>
  693. /// <see cref="Dim.Percent(float, bool)"/>
  694. /// </term>
  695. /// <description>
  696. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  697. /// SuperView.
  698. /// </description>
  699. /// </item>
  700. /// <item>
  701. /// <term>
  702. /// <see cref="Dim.Fill(int)"/>
  703. /// </term>
  704. /// <description>
  705. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  706. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  707. /// </description>
  708. /// </item>
  709. /// <item>
  710. /// <term>
  711. /// <see cref="Dim.Width(View)"/>
  712. /// </term>
  713. /// <description>
  714. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  715. /// <see cref="View"/>.
  716. /// </description>
  717. /// </item>
  718. /// <item>
  719. /// <term>
  720. /// <see cref="Dim.Height(View)"/>
  721. /// </term>
  722. /// <description>
  723. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  724. /// <see cref="View"/>.
  725. /// </description>
  726. /// </item>
  727. /// </list>
  728. /// </para>
  729. /// <para></para>
  730. /// </remarks>
  731. public class Dim
  732. {
  733. /// <summary>
  734. /// Specifies how <see cref="DimAuto"/> will compute the dimension.
  735. /// </summary>
  736. [Flags]
  737. public enum DimAutoStyle
  738. {
  739. /// <summary>
  740. /// The dimension will be computed using both the view's <see cref="View.Text"/> and
  741. /// <see cref="View.Subviews"/> (whichever is larger).
  742. /// </summary>
  743. Auto = Content | Text,
  744. /// <summary>
  745. /// The dimensions will be computed based on the View's non-Text content.
  746. /// <para>
  747. /// If <see cref="View.ContentSize"/> is explicitly set (is not <see langword="null"/>) then <see cref="View.ContentSize"/>
  748. /// will be used to determine the dimension.
  749. /// </para>
  750. /// <para>
  751. /// Otherwise, the Subview in <see cref="View.Subviews"/> with the largest corresponding position plus dimension
  752. /// will determine the dimension.
  753. /// </para>
  754. /// <para>
  755. /// The corresponding dimension of the view's <see cref="View.Text"/> will be ignored.
  756. /// </para>
  757. /// </summary>
  758. Content = 1,
  759. /// <summary>
  760. /// <para>
  761. /// The corresponding dimension of the view's <see cref="View.Text"/>, formatted using the
  762. /// <see cref="View.TextFormatter"/> settings,
  763. /// will be used to determine the dimension.
  764. /// </para>
  765. /// <para>
  766. /// The corresponding dimensions of the <see cref="View.Subviews"/> will be ignored.
  767. /// </para>
  768. /// </summary>
  769. Text = 2
  770. }
  771. /// <summary>
  772. ///
  773. /// </summary>
  774. public enum Dimension
  775. {
  776. /// <summary>
  777. /// No dimension specified.
  778. /// </summary>
  779. None = 0,
  780. /// <summary>
  781. /// The height dimension.
  782. /// </summary>
  783. Height = 1,
  784. /// <summary>
  785. /// The width dimension.
  786. /// </summary>
  787. Width = 2
  788. }
  789. /// <summary>
  790. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all the view's SubViews and/or Text.
  791. /// </summary>
  792. /// <remarks>
  793. /// <para>
  794. /// See <see cref="DimAutoStyle"/>.
  795. /// </para>
  796. /// </remarks>
  797. /// <example>
  798. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  799. /// SubViews.
  800. /// <code>
  801. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  802. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  803. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  804. /// view.Add (button, textField);
  805. /// </code>
  806. /// </example>
  807. /// <returns>The <see cref="Dim"/> object.</returns>
  808. /// <param name="style">
  809. /// Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  810. /// </param>
  811. /// <param name="min">Specifies the minimum dimension that view will be automatically sized to.</param>
  812. /// <param name="max">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  813. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim min = null, Dim max = null)
  814. {
  815. if (max != null)
  816. {
  817. throw new NotImplementedException (@"max is not implemented");
  818. }
  819. return new DimAuto (style, min, max);
  820. }
  821. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  822. /// <param name="other">The object to compare with the current object. </param>
  823. /// <returns>
  824. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  825. /// <see langword="false"/>.
  826. /// </returns>
  827. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  828. /// <summary>
  829. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  830. /// margin.
  831. /// </summary>
  832. /// <returns>The Fill dimension.</returns>
  833. /// <param name="margin">Margin to use.</param>
  834. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  835. /// <summary>
  836. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  837. /// The function will be called every time the dimension is needed.
  838. /// </summary>
  839. /// <param name="function">The function to be executed.</param>
  840. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  841. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  842. /// <summary>Serves as the default hash function. </summary>
  843. /// <returns>A hash code for the current object.</returns>
  844. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  845. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  846. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  847. /// <param name="view">The view that will be tracked.</param>
  848. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  849. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  850. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  851. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  852. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  853. public static Dim operator + (Dim left, Dim right)
  854. {
  855. if (left is DimAbsolute && right is DimAbsolute)
  856. {
  857. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  858. }
  859. var newDim = new DimCombine (true, left, right);
  860. (left as DimView)?.Target.SetNeedsLayout ();
  861. return newDim;
  862. }
  863. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  864. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  865. /// <param name="n">The value to convert to the pos.</param>
  866. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  867. /// <summary>
  868. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  869. /// <see cref="Dim"/>.
  870. /// </summary>
  871. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  872. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  873. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  874. public static Dim operator - (Dim left, Dim right)
  875. {
  876. if (left is DimAbsolute && right is DimAbsolute)
  877. {
  878. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  879. }
  880. var newDim = new DimCombine (false, left, right);
  881. (left as DimView)?.Target.SetNeedsLayout ();
  882. return newDim;
  883. }
  884. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  885. /// <returns>The percent <see cref="Dim"/> object.</returns>
  886. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  887. /// <param name="usePosition">
  888. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  889. /// <see cref="View.Y"/>).
  890. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  891. /// </param>
  892. /// <example>
  893. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  894. /// height,
  895. /// and is 80% the width of the SuperView.
  896. /// <code>
  897. /// var textView = new TextField {
  898. /// X = Pos.Center (),
  899. /// Y = Pos.Percent (50),
  900. /// Width = Dim.Percent (80),
  901. /// Height = Dim.Percent (30),
  902. /// };
  903. /// </code>
  904. /// </example>
  905. public static Dim Percent (float percent, bool usePosition = false)
  906. {
  907. if (percent is < 0 or > 100)
  908. {
  909. throw new ArgumentException ("Percent value must be between 0 and 100");
  910. }
  911. return new DimFactor (percent / 100, usePosition);
  912. }
  913. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  914. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  915. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  916. public static Dim Sized (int n) { return new DimAbsolute (n); }
  917. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  918. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  919. /// <param name="view">The view that will be tracked.</param>
  920. public static Dim Width (View view) { return new DimView (view, Dimension.Width); }
  921. /// <summary>
  922. /// Gets a dimension that is anchored to a certain point in the layout.
  923. /// This method is typically used internally by the layout system to determine the size of a View.
  924. /// </summary>
  925. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  926. /// <returns>
  927. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  928. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  929. /// dimension that is a certain percentage of the super view's size, and so on.
  930. /// </returns>
  931. internal virtual int Anchor (int width) { return 0; }
  932. /// <summary>
  933. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  934. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its content.
  935. /// </summary>
  936. /// <param name="location">
  937. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  938. /// top edge for height calculation.
  939. /// </param>
  940. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  941. /// <param name="us">The View that holds this Pos object.</param>
  942. /// <param name="dimension">Width or Height</param>
  943. /// <returns>
  944. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  945. /// is used.
  946. /// </returns>
  947. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  948. {
  949. return Math.Max (Anchor (superviewContentSize - location), 0);
  950. }
  951. /// <summary>
  952. /// Diagnostics API to determine if this Dim object references other views.
  953. /// </summary>
  954. /// <returns></returns>
  955. internal virtual bool ReferencesOtherViews ()
  956. {
  957. return false;
  958. }
  959. internal class DimAbsolute (int n) : Dim
  960. {
  961. private readonly int _n = n;
  962. public override bool Equals (object other) { return other is DimAbsolute abs && abs._n == _n; }
  963. public override int GetHashCode () { return _n.GetHashCode (); }
  964. public override string ToString () { return $"Absolute({_n})"; }
  965. internal override int Anchor (int width) { return _n; }
  966. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  967. {
  968. // DimAbsolute.Anchor (int width) ignores width and returns n
  969. return Math.Max (Anchor (0), 0);
  970. }
  971. }
  972. /// <summary>
  973. /// A <see cref="Dim"/> object that automatically sizes the view to fit all the view's SubViews and/or Text.
  974. /// </summary>
  975. /// <remarks>
  976. /// <para>
  977. /// See <see cref="Dim.DimAutoStyle"/>.
  978. /// </para>
  979. /// </remarks>
  980. /// <param name="style">
  981. /// Specifies how <see cref="Dim.DimAuto"/> will compute the dimension. The default is <see cref="Dim.DimAutoStyle.Auto"/>.
  982. /// </param>
  983. /// <param name="min">Specifies the minimum dimension that view will be automatically sized to.</param>
  984. /// <param name="max">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  985. public class DimAuto (DimAutoStyle style, Dim min, Dim max) : Dim
  986. {
  987. internal readonly Dim _max = max;
  988. internal readonly Dim _min = min;
  989. internal readonly DimAutoStyle _style = style;
  990. internal int _size;
  991. /// <inheritdoc />
  992. public override bool Equals (object other) { return other is DimAuto auto && auto._min == _min && auto._max == _max && auto._style == _style; }
  993. /// <inheritdoc />
  994. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), _min, _max, _style); }
  995. /// <inheritdoc />
  996. public override string ToString () { return $"Auto({_style},{_min},{_max})"; }
  997. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  998. {
  999. if (us == null)
  1000. {
  1001. return _max?.Anchor (0) ?? 0;
  1002. }
  1003. var textSize = 0;
  1004. var subviewsSize = 0;
  1005. int autoMin = _min?.Anchor (superviewContentSize) ?? 0;
  1006. if (superviewContentSize < autoMin)
  1007. {
  1008. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  1009. return superviewContentSize;
  1010. }
  1011. if (_style.HasFlag (Dim.DimAutoStyle.Text))
  1012. {
  1013. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  1014. }
  1015. if (_style.HasFlag (DimAutoStyle.Content))
  1016. {
  1017. if (us._contentSize is { })
  1018. {
  1019. subviewsSize = dimension == Dimension.Width ? us.ContentSize!.Value.Width : us.ContentSize!.Value.Height;
  1020. }
  1021. else
  1022. {
  1023. // TODO: AnchorEnd needs work
  1024. // TODO: If _min > 0 we can SetRelativeLayout for the subviews?
  1025. subviewsSize = 0;
  1026. if (us.Subviews.Count > 0)
  1027. {
  1028. for (int i = 0; i < us.Subviews.Count; i++)
  1029. {
  1030. var v = us.Subviews [i];
  1031. bool isNotPosAnchorEnd = dimension == Dim.Dimension.Width ? v.X is not Pos.PosAnchorEnd : v.Y is not Pos.PosAnchorEnd;
  1032. //if (!isNotPosAnchorEnd)
  1033. //{
  1034. // v.SetRelativeLayout(dimension == Dim.Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin));
  1035. //}
  1036. if (isNotPosAnchorEnd)
  1037. {
  1038. int size = dimension == Dim.Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  1039. if (size > subviewsSize)
  1040. {
  1041. subviewsSize = size;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. }
  1047. }
  1048. int max = int.Max (textSize, subviewsSize);
  1049. Thickness thickness = us.GetAdornmentsThickness ();
  1050. if (dimension == Dimension.Width)
  1051. {
  1052. max += thickness.Horizontal;
  1053. }
  1054. else
  1055. {
  1056. max += thickness.Vertical;
  1057. }
  1058. max = int.Max (max, autoMin);
  1059. return int.Min (max, _max?.Anchor (superviewContentSize) ?? superviewContentSize);
  1060. }
  1061. /// <summary>
  1062. /// Diagnostics API to determine if this Dim object references other views.
  1063. /// </summary>
  1064. /// <returns></returns>
  1065. internal override bool ReferencesOtherViews ()
  1066. {
  1067. // BUGBUG: This is not correct. _contentSize may be null.
  1068. return _style.HasFlag (Dim.DimAutoStyle.Content);
  1069. }
  1070. }
  1071. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  1072. {
  1073. internal bool _add = add;
  1074. internal Dim _left = left, _right = right;
  1075. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  1076. internal override int Anchor (int width)
  1077. {
  1078. int la = _left.Anchor (width);
  1079. int ra = _right.Anchor (width);
  1080. if (_add)
  1081. {
  1082. return la + ra;
  1083. }
  1084. return la - ra;
  1085. }
  1086. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  1087. {
  1088. int leftNewDim = _left.Calculate (location, superviewContentSize, us, dimension);
  1089. int rightNewDim = _right.Calculate (location, superviewContentSize, us, dimension);
  1090. int newDimension;
  1091. if (_add)
  1092. {
  1093. newDimension = leftNewDim + rightNewDim;
  1094. }
  1095. else
  1096. {
  1097. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  1098. }
  1099. return newDimension;
  1100. }
  1101. /// <summary>
  1102. /// Diagnostics API to determine if this Dim object references other views.
  1103. /// </summary>
  1104. /// <returns></returns>
  1105. internal override bool ReferencesOtherViews ()
  1106. {
  1107. if (_left.ReferencesOtherViews ())
  1108. {
  1109. return true;
  1110. }
  1111. if (_right.ReferencesOtherViews ())
  1112. {
  1113. return true;
  1114. }
  1115. return false;
  1116. }
  1117. }
  1118. internal class DimFactor (float factor, bool remaining = false) : Dim
  1119. {
  1120. private readonly float _factor = factor;
  1121. private readonly bool _remaining = remaining;
  1122. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  1123. public override int GetHashCode () { return _factor.GetHashCode (); }
  1124. public bool IsFromRemaining () { return _remaining; }
  1125. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  1126. internal override int Anchor (int width) { return (int)(width * _factor); }
  1127. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  1128. {
  1129. return _remaining ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  1130. }
  1131. }
  1132. internal class DimFill (int margin) : Dim
  1133. {
  1134. private readonly int _margin = margin;
  1135. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  1136. public override int GetHashCode () { return _margin.GetHashCode (); }
  1137. public override string ToString () { return $"Fill({_margin})"; }
  1138. internal override int Anchor (int width) { return width - _margin; }
  1139. }
  1140. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  1141. internal class DimFunc (Func<int> n) : Dim
  1142. {
  1143. private readonly Func<int> _function = n;
  1144. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  1145. public override int GetHashCode () { return _function.GetHashCode (); }
  1146. public override string ToString () { return $"DimFunc({_function ()})"; }
  1147. internal override int Anchor (int width) { return _function (); }
  1148. }
  1149. internal class DimView : Dim
  1150. {
  1151. private readonly Dimension _side;
  1152. internal DimView (View view, Dimension side)
  1153. {
  1154. Target = view;
  1155. _side = side;
  1156. }
  1157. public View Target { get; init; }
  1158. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  1159. public override int GetHashCode () { return Target.GetHashCode (); }
  1160. public override string ToString ()
  1161. {
  1162. if (Target == null)
  1163. {
  1164. throw new NullReferenceException ();
  1165. }
  1166. string sideString = _side switch
  1167. {
  1168. Dimension.Height => "Height",
  1169. Dimension.Width => "Width",
  1170. _ => "unknown"
  1171. };
  1172. return $"View({sideString},{Target})";
  1173. }
  1174. internal override int Anchor (int width)
  1175. {
  1176. return _side switch
  1177. {
  1178. Dimension.Height => Target.Frame.Height,
  1179. Dimension.Width => Target.Frame.Width,
  1180. _ => 0
  1181. };
  1182. }
  1183. internal override bool ReferencesOtherViews ()
  1184. {
  1185. return true;
  1186. }
  1187. }
  1188. }