PosDim.cs 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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 = 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. #region Static Methods - Create Dim objects
  736. /// <summary>
  737. /// Specifies how <see cref="DimAuto"/> will compute the dimension.
  738. /// </summary>
  739. [Flags]
  740. public enum DimAutoStyle
  741. {
  742. // BUGBUG: This should not be a nested enum. Extract it.
  743. /// <summary>
  744. /// The dimension will be computed using both the view's <see cref="View.Text"/> and
  745. /// <see cref="View.Subviews"/> (whichever is larger).
  746. /// </summary>
  747. Auto = Content | Text,
  748. /// <summary>
  749. /// The dimensions will be computed based on the View's non-Text content.
  750. /// <para>
  751. /// If <see cref="View.ContentSize"/> is explicitly set (is not <see langword="null"/>) then
  752. /// <see cref="View.ContentSize"/>
  753. /// will be used to determine the dimension.
  754. /// </para>
  755. /// <para>
  756. /// Otherwise, the Subview in <see cref="View.Subviews"/> with the largest corresponding position plus dimension
  757. /// will determine the dimension.
  758. /// </para>
  759. /// <para>
  760. /// The corresponding dimension of the view's <see cref="View.Text"/> will be ignored.
  761. /// </para>
  762. /// </summary>
  763. Content = 1,
  764. /// <summary>
  765. /// <para>
  766. /// The corresponding dimension of the view's <see cref="View.Text"/>, formatted using the
  767. /// <see cref="View.TextFormatter"/> settings,
  768. /// will be used to determine the dimension.
  769. /// </para>
  770. /// <para>
  771. /// The corresponding dimensions of the <see cref="View.Subviews"/> will be ignored.
  772. /// </para>
  773. /// </summary>
  774. Text = 2
  775. }
  776. /// <summary>
  777. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all the view's SubViews and/or Text.
  778. /// </summary>
  779. /// <remarks>
  780. /// <para>
  781. /// See <see cref="DimAutoStyle"/>.
  782. /// </para>
  783. /// </remarks>
  784. /// <example>
  785. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  786. /// SubViews.
  787. /// <code>
  788. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  789. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  790. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  791. /// view.Add (button, textField);
  792. /// </code>
  793. /// </example>
  794. /// <returns>The <see cref="Dim"/> object.</returns>
  795. /// <param name="style">
  796. /// Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  797. /// </param>
  798. /// <param name="min">Specifies the minimum dimension that view will be automatically sized to.</param>
  799. /// <param name="max">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  800. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim min = null, Dim max = null)
  801. {
  802. if (max != null)
  803. {
  804. throw new NotImplementedException (@"max is not implemented");
  805. }
  806. return new DimAuto (style, min, max);
  807. }
  808. /// <summary>
  809. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  810. /// margin.
  811. /// </summary>
  812. /// <returns>The Fill dimension.</returns>
  813. /// <param name="margin">Margin to use.</param>
  814. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  815. /// <summary>
  816. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  817. /// The function will be called every time the dimension is needed.
  818. /// </summary>
  819. /// <param name="function">The function to be executed.</param>
  820. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  821. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  822. /// <summary>Serves as the default hash function. </summary>
  823. /// <returns>A hash code for the current object.</returns>
  824. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  825. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  826. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  827. /// <param name="view">The view that will be tracked.</param>
  828. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  829. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  830. /// <returns>The percent <see cref="Dim"/> object.</returns>
  831. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  832. /// <param name="usePosition">
  833. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  834. /// <see cref="View.Y"/>).
  835. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  836. /// </param>
  837. /// <example>
  838. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  839. /// height,
  840. /// and is 80% the width of the SuperView.
  841. /// <code>
  842. /// var textView = new TextField {
  843. /// X = Pos.Center (),
  844. /// Y = Pos.Percent (50),
  845. /// Width = Dim.Percent (80),
  846. /// Height = Dim.Percent (30),
  847. /// };
  848. /// </code>
  849. /// </example>
  850. public static Dim Percent (float percent, bool usePosition = false)
  851. {
  852. if (percent is < 0 or > 100)
  853. {
  854. throw new ArgumentException ("Percent value must be between 0 and 100");
  855. }
  856. return new DimFactor (percent / 100, usePosition);
  857. }
  858. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  859. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  860. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  861. public static Dim Sized (int n) { return new DimAbsolute (n); }
  862. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  863. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  864. /// <param name="view">The view that will be tracked.</param>
  865. public static Dim Width (View view) { return new DimView (view, Dimension.Width); }
  866. #endregion Static Methods - Create Dim objects
  867. #region Methods
  868. /// <summary>
  869. /// </summary>
  870. internal enum Dimension
  871. {
  872. // BUGBUG: This should not be a nested enum. Extract it.
  873. /// <summary>
  874. /// No dimension specified.
  875. /// </summary>
  876. None = 0,
  877. /// <summary>
  878. /// The height dimension.
  879. /// </summary>
  880. Height = 1,
  881. /// <summary>
  882. /// The width dimension.
  883. /// </summary>
  884. Width = 2
  885. }
  886. /// <summary>
  887. /// Gets a dimension that is anchored to a certain point in the layout.
  888. /// This method is typically used internally by the layout system to determine the size of a View.
  889. /// </summary>
  890. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  891. /// <returns>
  892. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  893. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  894. /// dimension that is a certain percentage of the super view's size, and so on.
  895. /// </returns>
  896. internal virtual int Anchor (int width) { return 0; }
  897. /// <summary>
  898. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  899. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its
  900. /// content.
  901. /// </summary>
  902. /// <param name="location">
  903. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  904. /// top edge for height calculation.
  905. /// </param>
  906. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  907. /// <param name="us">The View that holds this Pos object.</param>
  908. /// <param name="dimension">Width or Height</param>
  909. /// <returns>
  910. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  911. /// is used.
  912. /// </returns>
  913. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  914. {
  915. return Math.Max (Anchor (superviewContentSize - location), 0);
  916. }
  917. /// <summary>
  918. /// Diagnostics API to determine if this Dim object references other views.
  919. /// </summary>
  920. /// <returns></returns>
  921. internal virtual bool ReferencesOtherViews () { return false; }
  922. #endregion
  923. #region Operators
  924. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  925. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  926. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  927. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  928. public static Dim operator + (Dim left, Dim right)
  929. {
  930. if (left is DimAbsolute && right is DimAbsolute)
  931. {
  932. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  933. }
  934. var newDim = new DimCombine (true, left, right);
  935. (left as DimView)?.Target.SetNeedsLayout ();
  936. return newDim;
  937. }
  938. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  939. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  940. /// <param name="n">The value to convert to the pos.</param>
  941. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  942. /// <summary>
  943. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  944. /// <see cref="Dim"/>.
  945. /// </summary>
  946. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  947. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  948. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  949. public static Dim operator - (Dim left, Dim right)
  950. {
  951. if (left is DimAbsolute && right is DimAbsolute)
  952. {
  953. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  954. }
  955. var newDim = new DimCombine (false, left, right);
  956. (left as DimView)?.Target.SetNeedsLayout ();
  957. return newDim;
  958. }
  959. #endregion
  960. #region Overrides
  961. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  962. /// <param name="other">The object to compare with the current object. </param>
  963. /// <returns>
  964. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  965. /// <see langword="false"/>.
  966. /// </returns>
  967. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  968. #endregion Overrides
  969. #region Dim Types
  970. internal class DimAbsolute (int n) : Dim
  971. {
  972. private readonly int _n = n;
  973. public override bool Equals (object other) { return other is DimAbsolute abs && abs._n == _n; }
  974. public override int GetHashCode () { return _n.GetHashCode (); }
  975. public override string ToString () { return $"Absolute({_n})"; }
  976. internal override int Anchor (int width) { return _n; }
  977. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  978. {
  979. // DimAbsolute.Anchor (int width) ignores width and returns n
  980. return Math.Max (Anchor (0), 0);
  981. }
  982. }
  983. /// <summary>
  984. /// A <see cref="Dim"/> object that automatically sizes the view to fit all the view's SubViews and/or Text.
  985. /// </summary>
  986. /// <remarks>
  987. /// <para>
  988. /// See <see cref="Dim.DimAutoStyle"/>.
  989. /// </para>
  990. /// </remarks>
  991. /// <param name="style">
  992. /// Specifies how <see cref="Dim.DimAuto"/> will compute the dimension. The default is
  993. /// <see cref="Dim.DimAutoStyle.Auto"/>.
  994. /// </param>
  995. /// <param name="min">Specifies the minimum dimension that view will be automatically sized to.</param>
  996. /// <param name="max">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  997. internal class DimAuto (DimAutoStyle style, Dim min, Dim max) : Dim
  998. {
  999. internal readonly Dim _max = max;
  1000. internal readonly Dim _min = min;
  1001. internal readonly DimAutoStyle _style = style;
  1002. internal int _size;
  1003. /// <inheritdoc/>
  1004. public override bool Equals (object other) { return other is DimAuto auto && auto._min == _min && auto._max == _max && auto._style == _style; }
  1005. /// <inheritdoc/>
  1006. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), _min, _max, _style); }
  1007. /// <inheritdoc/>
  1008. public override string ToString () { return $"Auto({_style},{_min},{_max})"; }
  1009. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  1010. {
  1011. if (us == null)
  1012. {
  1013. return _max?.Anchor (0) ?? 0;
  1014. }
  1015. var textSize = 0;
  1016. var subviewsSize = 0;
  1017. int autoMin = _min?.Anchor (superviewContentSize) ?? 0;
  1018. if (superviewContentSize < autoMin)
  1019. {
  1020. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  1021. return superviewContentSize;
  1022. }
  1023. if (_style.HasFlag (DimAutoStyle.Text))
  1024. {
  1025. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  1026. }
  1027. if (_style.HasFlag (DimAutoStyle.Content))
  1028. {
  1029. if (us._contentSize is { })
  1030. {
  1031. subviewsSize = dimension == Dimension.Width ? us.ContentSize!.Value.Width : us.ContentSize!.Value.Height;
  1032. }
  1033. else
  1034. {
  1035. // TODO: AnchorEnd needs work
  1036. // TODO: If _min > 0 we can SetRelativeLayout for the subviews?
  1037. subviewsSize = 0;
  1038. if (us.Subviews.Count > 0)
  1039. {
  1040. for (var i = 0; i < us.Subviews.Count; i++)
  1041. {
  1042. View v = us.Subviews [i];
  1043. bool isNotPosAnchorEnd = dimension == Dimension.Width ? v.X is not Pos.PosAnchorEnd : v.Y is not Pos.PosAnchorEnd;
  1044. //if (!isNotPosAnchorEnd)
  1045. //{
  1046. // v.SetRelativeLayout(dimension == Dim.Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin));
  1047. //}
  1048. if (isNotPosAnchorEnd)
  1049. {
  1050. int size = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  1051. if (size > subviewsSize)
  1052. {
  1053. subviewsSize = size;
  1054. }
  1055. }
  1056. }
  1057. }
  1058. }
  1059. }
  1060. int max = int.Max (textSize, subviewsSize);
  1061. Thickness thickness = us.GetAdornmentsThickness ();
  1062. if (dimension == Dimension.Width)
  1063. {
  1064. max += thickness.Horizontal;
  1065. }
  1066. else
  1067. {
  1068. max += thickness.Vertical;
  1069. }
  1070. max = int.Max (max, autoMin);
  1071. return int.Min (max, _max?.Anchor (superviewContentSize) ?? superviewContentSize);
  1072. }
  1073. /// <summary>
  1074. /// Diagnostics API to determine if this Dim object references other views.
  1075. /// </summary>
  1076. /// <returns></returns>
  1077. internal override bool ReferencesOtherViews ()
  1078. {
  1079. // BUGBUG: This is not correct. _contentSize may be null.
  1080. return _style.HasFlag (DimAutoStyle.Content);
  1081. }
  1082. }
  1083. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  1084. {
  1085. internal bool _add = add;
  1086. internal Dim _left = left, _right = right;
  1087. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  1088. internal override int Anchor (int width)
  1089. {
  1090. int la = _left.Anchor (width);
  1091. int ra = _right.Anchor (width);
  1092. if (_add)
  1093. {
  1094. return la + ra;
  1095. }
  1096. return la - ra;
  1097. }
  1098. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  1099. {
  1100. int leftNewDim = _left.Calculate (location, superviewContentSize, us, dimension);
  1101. int rightNewDim = _right.Calculate (location, superviewContentSize, us, dimension);
  1102. int newDimension;
  1103. if (_add)
  1104. {
  1105. newDimension = leftNewDim + rightNewDim;
  1106. }
  1107. else
  1108. {
  1109. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  1110. }
  1111. return newDimension;
  1112. }
  1113. /// <summary>
  1114. /// Diagnostics API to determine if this Dim object references other views.
  1115. /// </summary>
  1116. /// <returns></returns>
  1117. internal override bool ReferencesOtherViews ()
  1118. {
  1119. if (_left.ReferencesOtherViews ())
  1120. {
  1121. return true;
  1122. }
  1123. if (_right.ReferencesOtherViews ())
  1124. {
  1125. return true;
  1126. }
  1127. return false;
  1128. }
  1129. }
  1130. internal class DimFactor (float factor, bool remaining = false) : Dim
  1131. {
  1132. private readonly float _factor = factor;
  1133. private readonly bool _remaining = remaining;
  1134. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  1135. public override int GetHashCode () { return _factor.GetHashCode (); }
  1136. public bool IsFromRemaining () { return _remaining; }
  1137. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  1138. internal override int Anchor (int width) { return (int)(width * _factor); }
  1139. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  1140. {
  1141. return _remaining ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  1142. }
  1143. }
  1144. internal class DimFill (int margin) : Dim
  1145. {
  1146. private readonly int _margin = margin;
  1147. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  1148. public override int GetHashCode () { return _margin.GetHashCode (); }
  1149. public override string ToString () { return $"Fill({_margin})"; }
  1150. internal override int Anchor (int width) { return width - _margin; }
  1151. }
  1152. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  1153. internal class DimFunc (Func<int> n) : Dim
  1154. {
  1155. private readonly Func<int> _function = n;
  1156. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  1157. public override int GetHashCode () { return _function.GetHashCode (); }
  1158. public override string ToString () { return $"DimFunc({_function ()})"; }
  1159. internal override int Anchor (int width) { return _function (); }
  1160. }
  1161. internal class DimView : Dim
  1162. {
  1163. private readonly Dimension _side;
  1164. internal DimView (View view, Dimension side)
  1165. {
  1166. Target = view;
  1167. _side = side;
  1168. }
  1169. public View Target { get; init; }
  1170. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  1171. public override int GetHashCode () { return Target.GetHashCode (); }
  1172. public override string ToString ()
  1173. {
  1174. if (Target == null)
  1175. {
  1176. throw new NullReferenceException ();
  1177. }
  1178. string sideString = _side switch
  1179. {
  1180. Dimension.Height => "Height",
  1181. Dimension.Width => "Width",
  1182. _ => "unknown"
  1183. };
  1184. return $"View({sideString},{Target})";
  1185. }
  1186. internal override int Anchor (int width)
  1187. {
  1188. return _side switch
  1189. {
  1190. Dimension.Height => Target.Frame.Height,
  1191. Dimension.Width => Target.Frame.Width,
  1192. _ => 0
  1193. };
  1194. }
  1195. internal override bool ReferencesOtherViews () { return true; }
  1196. }
  1197. #endregion Dim Types
  1198. }