PosDim.cs 55 KB

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