PosDim.cs 54 KB

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