PosDim.cs 55 KB

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