PosDim.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. using System.Diagnostics;
  2. using static System.Net.Mime.MediaTypeNames;
  3. using static Terminal.Gui.Dialog;
  4. using static Terminal.Gui.Dim;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
  8. /// relative to the ending dimension. Integer values are implicitly convertible to an absolute <see cref="Pos"/>. These
  9. /// objects are created using the static methods Percent, AnchorEnd, and Center. The <see cref="Pos"/> objects can be
  10. /// combined with the addition and subtraction operators.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.</para>
  14. /// <para>
  15. /// These can be used to set the absolute position, when merely assigning an integer value (via the implicit
  16. /// integer to <see cref="Pos"/> conversion), and they can be combined to produce more useful layouts, like:
  17. /// Pos.Center - 3, which would shift the position of the <see cref="View"/> 3 characters to the left after
  18. /// centering for example.
  19. /// </para>
  20. /// <para>
  21. /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View).
  22. /// The X(View) and Y(View) are aliases to Left(View) and Top(View) respectively.
  23. /// </para>
  24. /// <para>
  25. /// <list type="table">
  26. /// <listheader>
  27. /// <term>Pos Object</term> <description>Description</description>
  28. /// </listheader>
  29. /// <item>
  30. /// <term>
  31. /// <see cref="Pos.Function(Func{int})"/>
  32. /// </term>
  33. /// <description>
  34. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  35. /// function. The function will be called every time the position is needed.
  36. /// </description>
  37. /// </item>
  38. /// <item>
  39. /// <term>
  40. /// <see cref="Pos.Percent(float)"/>
  41. /// </term>
  42. /// <description>
  43. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  44. /// SuperView.
  45. /// </description>
  46. /// </item>
  47. /// <item>
  48. /// <term>
  49. /// <see cref="Pos.AnchorEnd()"/>
  50. /// </term>
  51. /// <description>
  52. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of
  53. /// the dimension, useful to flush the layout from the right or bottom.
  54. /// </description>
  55. /// </item>
  56. /// <item>
  57. /// <term>
  58. /// <see cref="Pos.Center"/>
  59. /// </term>
  60. /// <description>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</description>
  61. /// </item>
  62. /// <item>
  63. /// <term>
  64. /// <see cref="Pos.At(int)"/>
  65. /// </term>
  66. /// <description>
  67. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  68. /// integer value.
  69. /// </description>
  70. /// </item>
  71. /// <item>
  72. /// <term>
  73. /// <see cref="Pos.Left"/>
  74. /// </term>
  75. /// <description>
  76. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  77. /// <see cref="View"/>.
  78. /// </description>
  79. /// </item>
  80. /// <item>
  81. /// <term>
  82. /// <see cref="Pos.X(View)"/>
  83. /// </term>
  84. /// <description>
  85. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  86. /// <see cref="View"/>.
  87. /// </description>
  88. /// </item>
  89. /// <item>
  90. /// <term>
  91. /// <see cref="Pos.Top(View)"/>
  92. /// </term>
  93. /// <description>
  94. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  95. /// <see cref="View"/>.
  96. /// </description>
  97. /// </item>
  98. /// <item>
  99. /// <term>
  100. /// <see cref="Pos.Y(View)"/>
  101. /// </term>
  102. /// <description>
  103. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  104. /// <see cref="View"/>.
  105. /// </description>
  106. /// </item>
  107. /// <item>
  108. /// <term>
  109. /// <see cref="Pos.Right(View)"/>
  110. /// </term>
  111. /// <description>
  112. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  113. /// specified <see cref="View"/>.
  114. /// </description>
  115. /// </item>
  116. /// <item>
  117. /// <term>
  118. /// <see cref="Pos.Bottom(View)"/>
  119. /// </term>
  120. /// <description>
  121. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  122. /// specified <see cref="View"/>
  123. /// </description>
  124. /// </item>
  125. /// </list>
  126. /// </para>
  127. /// </remarks>
  128. public class Pos
  129. {
  130. /// <summary>
  131. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  132. /// bottom) of the SuperView, minus the respective dimension of the View. This is equivalent to using
  133. /// <see cref="Pos.AnchorEnd(int)"/>,
  134. /// with an offset equivalent to the View's respective dimension.
  135. /// </summary>
  136. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  137. /// <example>
  138. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  139. /// <code>
  140. /// anchorButton.X = Pos.AnchorEnd ();
  141. /// anchorButton.Y = Pos.AnchorEnd ();
  142. /// </code>
  143. /// </example>
  144. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  145. /// <summary>
  146. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView,
  147. /// useful to flush the layout from the right or bottom. See also <see cref="Pos.AnchorEnd()"/>, which uses the view
  148. /// dimension to ensure the view is fully visible.
  149. /// </summary>
  150. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  151. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  152. /// <example>
  153. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  154. /// <code>
  155. /// anchorButton.X = Pos.AnchorEnd (10);
  156. /// anchorButton.Y = 1
  157. /// </code>
  158. /// </example>
  159. public static Pos AnchorEnd (int offset)
  160. {
  161. if (offset < 0)
  162. {
  163. throw new ArgumentException (@"Must be positive", nameof (offset));
  164. }
  165. return new PosAnchorEnd (offset);
  166. }
  167. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  168. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  169. /// <param name="n">The value to convert to the <see cref="Pos"/>.</param>
  170. public static Pos At (int n) { return new PosAbsolute (n); }
  171. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  172. /// <returns>The center Pos.</returns>
  173. /// <example>
  174. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  175. /// is 80% the width of the <see cref="View"/> it added to.
  176. /// <code>
  177. /// var textView = new TextView () {
  178. /// X = Pos.Center (),
  179. /// Y = Pos.Percent (50),
  180. /// Width = Dim.Percent (80),
  181. /// Height = Dim.Percent (30),
  182. /// };
  183. /// </code>
  184. /// </example>
  185. public static Pos Center () { return new PosCenter (); }
  186. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  187. /// <param name="other">The object to compare with the current object. </param>
  188. /// <returns>
  189. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  190. /// <see langword="false"/>.
  191. /// </returns>
  192. public override bool Equals (object other) { return other is Pos abs && abs == this; }
  193. /// <summary>
  194. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  195. /// will be called every time the position is needed.
  196. /// </summary>
  197. /// <param name="function">The function to be executed.</param>
  198. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  199. public static Pos Function (Func<int> function) { return new PosFunc (function); }
  200. /// <summary>Serves as the default hash function. </summary>
  201. /// <returns>A hash code for the current object.</returns>
  202. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  203. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  204. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  205. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  206. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  207. public static Pos operator + (Pos left, Pos right)
  208. {
  209. if (left is PosAbsolute && right is PosAbsolute)
  210. {
  211. return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
  212. }
  213. var newPos = new PosCombine (true, left, right);
  214. if (left is PosView view)
  215. {
  216. view.Target.SetNeedsLayout ();
  217. }
  218. return newPos;
  219. }
  220. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  221. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  222. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  223. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  224. /// <summary>
  225. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  226. /// <see cref="Pos"/>.
  227. /// </summary>
  228. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  229. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  230. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  231. public static Pos operator - (Pos left, Pos right)
  232. {
  233. if (left is PosAbsolute && right is PosAbsolute)
  234. {
  235. return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
  236. }
  237. var newPos = new PosCombine (false, left, right);
  238. if (left is PosView view)
  239. {
  240. view.Target.SetNeedsLayout ();
  241. }
  242. return newPos;
  243. }
  244. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  245. /// <returns>The percent <see cref="Pos"/> object.</returns>
  246. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  247. /// <example>
  248. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  249. /// is 80% the width of the <see cref="View"/> it added to.
  250. /// <code>
  251. /// var textView = new TextField {
  252. /// X = Pos.Center (),
  253. /// Y = Pos.Percent (50),
  254. /// Width = Dim.Percent (80),
  255. /// Height = Dim.Percent (30),
  256. /// };
  257. /// </code>
  258. /// </example>
  259. public static Pos Percent (float percent)
  260. {
  261. if (percent is < 0 or > 100)
  262. {
  263. throw new ArgumentException ("Percent value must be between 0 and 100.");
  264. }
  265. return new PosFactor (percent / 100);
  266. }
  267. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  268. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  269. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  270. public static Pos Top (View view) { return new PosView (view, Side.Top); }
  271. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  272. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  273. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  274. public static Pos Y (View view) { return new PosView (view, Side.Top); }
  275. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  276. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  277. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  278. public static Pos Left (View view) { return new PosView (view, Side.Left); }
  279. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  280. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  281. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  282. public static Pos X (View view) { return new PosView (view, Side.Left); }
  283. /// <summary>
  284. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  285. /// <see cref="View"/>
  286. /// </summary>
  287. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  288. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  289. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  290. /// <summary>
  291. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  292. /// <see cref="View"/>.
  293. /// </summary>
  294. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  295. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  296. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  297. /// <summary>
  298. /// Gets a position that is anchored to a certain point in the layout. This method is typically used
  299. /// internally by the layout system to determine where a View should be positioned.
  300. /// </summary>
  301. /// <param name="width">The width of the area where the View is being positioned (Superview.ContentSize).</param>
  302. /// <returns>
  303. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  304. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  305. /// position that is anchored to the end of the layout, and so on.
  306. /// </returns>
  307. internal virtual int Anchor (int width) { return 0; }
  308. /// <summary>
  309. /// Calculates and returns the position of a <see cref="View"/> object. It takes into account the dimension of the
  310. /// superview and the dimension of the view itself.
  311. /// </summary>
  312. /// <param name="superviewDimension">
  313. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  314. /// height for y-coordinate calculation.
  315. /// </param>
  316. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  317. /// <param name="us">The View that holds this Pos object.</param>
  318. /// <param name="dimension">Width or Height</param>
  319. /// <returns>
  320. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  321. /// that
  322. /// is used.
  323. /// </returns>
  324. internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  325. {
  326. return Anchor (superviewDimension);
  327. }
  328. internal class PosAbsolute (int n) : Pos
  329. {
  330. private readonly int _n = n;
  331. public override bool Equals (object other) { return other is PosAbsolute abs && abs._n == _n; }
  332. public override int GetHashCode () { return _n.GetHashCode (); }
  333. public override string ToString () { return $"Absolute({_n})"; }
  334. internal override int Anchor (int width) { return _n; }
  335. }
  336. internal class PosAnchorEnd : Pos
  337. {
  338. private readonly int _offset;
  339. public PosAnchorEnd () { UseDimForOffset = true; }
  340. public PosAnchorEnd (int offset) { _offset = offset; }
  341. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd._offset == _offset; }
  342. public override int GetHashCode () { return _offset.GetHashCode (); }
  343. /// <summary>
  344. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  345. /// </summary>
  346. internal bool UseDimForOffset { get; set; }
  347. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({_offset})"; }
  348. internal override int Anchor (int width)
  349. {
  350. if (UseDimForOffset)
  351. {
  352. return width;
  353. }
  354. return width - _offset;
  355. }
  356. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  357. {
  358. int newLocation = Anchor (superviewDimension);
  359. if (UseDimForOffset)
  360. {
  361. newLocation -= dim.Anchor (superviewDimension);
  362. }
  363. return newLocation;
  364. }
  365. }
  366. internal class PosCenter : Pos
  367. {
  368. public override string ToString () { return "Center"; }
  369. internal override int Anchor (int width) { return width / 2; }
  370. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  371. {
  372. int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
  373. return Anchor (superviewDimension - newDimension);
  374. }
  375. }
  376. internal class PosCombine (bool add, Pos left, Pos right) : Pos
  377. {
  378. internal bool _add = add;
  379. internal Pos _left = left, _right = right;
  380. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  381. internal override int Anchor (int width)
  382. {
  383. int la = _left.Anchor (width);
  384. int ra = _right.Anchor (width);
  385. if (_add)
  386. {
  387. return la + ra;
  388. }
  389. return la - ra;
  390. }
  391. internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
  392. {
  393. int newDimension = dim.Calculate (0, superviewDimension, us, dimension);
  394. int left = _left.Calculate (superviewDimension, dim, us, dimension);
  395. int right = _right.Calculate (superviewDimension, dim, us, dimension);
  396. if (_add)
  397. {
  398. return left + right;
  399. }
  400. return left - right;
  401. }
  402. }
  403. internal class PosFactor (float factor) : Pos
  404. {
  405. private readonly float _factor = factor;
  406. public override bool Equals (object other) { return other is PosFactor f && f._factor == _factor; }
  407. public override int GetHashCode () { return _factor.GetHashCode (); }
  408. public override string ToString () { return $"Factor({_factor})"; }
  409. internal override int Anchor (int width) { return (int)(width * _factor); }
  410. }
  411. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  412. internal class PosFunc (Func<int> n) : Pos
  413. {
  414. private readonly Func<int> _function = n;
  415. public override bool Equals (object other) { return other is PosFunc f && f._function () == _function (); }
  416. public override int GetHashCode () { return _function.GetHashCode (); }
  417. public override string ToString () { return $"PosFunc({_function ()})"; }
  418. internal override int Anchor (int width) { return _function (); }
  419. }
  420. /// <summary>
  421. /// Describes which side of the view to use for the position.
  422. /// </summary>
  423. public enum Side
  424. {
  425. /// <summary>
  426. /// The left (X) side of the view.
  427. /// </summary>
  428. Left = 0,
  429. /// <summary>
  430. /// The top (Y) side of the view.
  431. /// </summary>
  432. Top = 1,
  433. /// <summary>
  434. /// The right (X + Width) side of the view.
  435. /// </summary>
  436. Right = 2,
  437. /// <summary>
  438. /// The bottom (Y + Height) side of the view.
  439. /// </summary>
  440. Bottom = 3
  441. }
  442. internal class PosView (View view, Side side) : Pos
  443. {
  444. public readonly View Target = view;
  445. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  446. public override int GetHashCode () { return Target.GetHashCode (); }
  447. public override string ToString ()
  448. {
  449. string sideString = side switch
  450. {
  451. Side.Left => "left",
  452. Side.Top => "top",
  453. Side.Right => "right",
  454. Side.Bottom => "bottom",
  455. _ => "unknown"
  456. };
  457. if (Target == null)
  458. {
  459. throw new NullReferenceException (nameof (Target));
  460. }
  461. return $"View(side={sideString},target={Target})";
  462. }
  463. internal override int Anchor (int width)
  464. {
  465. return side switch
  466. {
  467. Side.Left => Target.Frame.X,
  468. Side.Top => Target.Frame.Y,
  469. Side.Right => Target.Frame.Right,
  470. Side.Bottom => Target.Frame.Bottom,
  471. _ => 0
  472. };
  473. }
  474. }
  475. }
  476. /// <summary>
  477. /// <para>
  478. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  479. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  480. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  481. /// </para>
  482. /// <para>
  483. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  484. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  485. /// subtraction operators.
  486. /// </para>
  487. /// </summary>
  488. /// <remarks>
  489. /// <para>
  490. /// <list type="table">
  491. /// <listheader>
  492. /// <term>Dim Object</term> <description>Description</description>
  493. /// </listheader>
  494. /// <item>
  495. /// <term>
  496. /// <see cref="Dim.Auto"/>
  497. /// </term>
  498. /// <description>
  499. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit
  500. /// the view's SubViews.
  501. /// </description>
  502. /// </item>
  503. /// <item>
  504. /// <term>
  505. /// <see cref="Dim.Function(Func{int})"/>
  506. /// </term>
  507. /// <description>
  508. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  509. /// function. The function will be called every time the dimension is needed.
  510. /// </description>
  511. /// </item>
  512. /// <item>
  513. /// <term>
  514. /// <see cref="Dim.Percent(float, bool)"/>
  515. /// </term>
  516. /// <description>
  517. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  518. /// SuperView.
  519. /// </description>
  520. /// </item>
  521. /// <item>
  522. /// <term>
  523. /// <see cref="Dim.Fill(int)"/>
  524. /// </term>
  525. /// <description>
  526. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  527. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  528. /// </description>
  529. /// </item>
  530. /// <item>
  531. /// <term>
  532. /// <see cref="Dim.Width(View)"/>
  533. /// </term>
  534. /// <description>
  535. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  536. /// <see cref="View"/>.
  537. /// </description>
  538. /// </item>
  539. /// <item>
  540. /// <term>
  541. /// <see cref="Dim.Height(View)"/>
  542. /// </term>
  543. /// <description>
  544. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  545. /// <see cref="View"/>.
  546. /// </description>
  547. /// </item>
  548. /// </list>
  549. /// </para>
  550. /// <para></para>
  551. /// </remarks>
  552. public class Dim
  553. {
  554. /// <summary>
  555. /// Specifies how <see cref="DimAuto"/> will compute the dimension.
  556. /// </summary>
  557. public enum DimAutoStyle
  558. {
  559. /// <summary>
  560. /// The dimension will be computed using both the view's <see cref="View.Text"/> and
  561. /// <see cref="View.Subviews"/> (whichever is larger).
  562. /// </summary>
  563. Auto,
  564. /// <summary>
  565. /// The Subview in <see cref="View.Subviews"/> with the largest corresponding position plus dimension
  566. /// will determine the dimension.
  567. /// The corresponding dimension of the view's <see cref="View.Text"/> will be ignored.
  568. /// </summary>
  569. Subviews,
  570. /// <summary>
  571. /// The corresponding dimension of the view's <see cref="View.Text"/>, formatted using the
  572. /// <see cref="View.TextFormatter"/> settings,
  573. /// will be used to determine the dimension.
  574. /// The corresponding dimensions of the <see cref="View.Subviews"/> will be ignored.
  575. /// </summary>
  576. Text
  577. }
  578. /// <summary>
  579. ///
  580. /// </summary>
  581. public enum Dimension
  582. {
  583. /// <summary>
  584. /// No dimension specified.
  585. /// </summary>
  586. None = 0,
  587. /// <summary>
  588. /// The height dimension.
  589. /// </summary>
  590. Height = 1,
  591. /// <summary>
  592. /// The width dimension.
  593. /// </summary>
  594. Width = 2
  595. }
  596. /// <summary>
  597. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all of the view's SubViews and/or Text.
  598. /// </summary>
  599. /// <example>
  600. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  601. /// SubViews.
  602. /// <code>
  603. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  604. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  605. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  606. /// view.Add (button, textField);
  607. /// </code>
  608. /// </example>
  609. /// <returns>The <see cref="Dim"/> object.</returns>
  610. /// <param name="style">
  611. /// Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  612. /// </param>
  613. /// <param name="min">Specifies the minimum dimension that view will be automatically sized to.</param>
  614. /// <param name="max">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  615. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim min = null, Dim max = null)
  616. {
  617. if (max != null)
  618. {
  619. throw new NotImplementedException (@"max is not implemented");
  620. }
  621. return new DimAuto (style, min, max);
  622. }
  623. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  624. /// <param name="other">The object to compare with the current object. </param>
  625. /// <returns>
  626. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  627. /// <see langword="false"/>.
  628. /// </returns>
  629. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  630. /// <summary>
  631. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  632. /// margin.
  633. /// </summary>
  634. /// <returns>The Fill dimension.</returns>
  635. /// <param name="margin">Margin to use.</param>
  636. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  637. /// <summary>
  638. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  639. /// The function will be called every time the dimension is needed.
  640. /// </summary>
  641. /// <param name="function">The function to be executed.</param>
  642. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  643. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  644. /// <summary>Serves as the default hash function. </summary>
  645. /// <returns>A hash code for the current object.</returns>
  646. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  647. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  648. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  649. /// <param name="view">The view that will be tracked.</param>
  650. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  651. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  652. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  653. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  654. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  655. public static Dim operator + (Dim left, Dim right)
  656. {
  657. if (left is DimAbsolute && right is DimAbsolute)
  658. {
  659. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  660. }
  661. var newDim = new DimCombine (true, left, right);
  662. (left as DimView)?.Target.SetNeedsLayout ();
  663. return newDim;
  664. }
  665. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  666. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  667. /// <param name="n">The value to convert to the pos.</param>
  668. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  669. /// <summary>
  670. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  671. /// <see cref="Dim"/>.
  672. /// </summary>
  673. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  674. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  675. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  676. public static Dim operator - (Dim left, Dim right)
  677. {
  678. if (left is DimAbsolute && right is DimAbsolute)
  679. {
  680. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  681. }
  682. var newDim = new DimCombine (false, left, right);
  683. (left as DimView)?.Target.SetNeedsLayout ();
  684. return newDim;
  685. }
  686. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  687. /// <returns>The percent <see cref="Dim"/> object.</returns>
  688. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  689. /// <param name="usePosition">
  690. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  691. /// <see cref="View.Y"/>).
  692. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  693. /// </param>
  694. /// <example>
  695. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  696. /// height,
  697. /// and is 80% the width of the SuperView.
  698. /// <code>
  699. /// var textView = new TextField {
  700. /// X = Pos.Center (),
  701. /// Y = Pos.Percent (50),
  702. /// Width = Dim.Percent (80),
  703. /// Height = Dim.Percent (30),
  704. /// };
  705. /// </code>
  706. /// </example>
  707. public static Dim Percent (float percent, bool usePosition = false)
  708. {
  709. if (percent is < 0 or > 100)
  710. {
  711. throw new ArgumentException ("Percent value must be between 0 and 100");
  712. }
  713. return new DimFactor (percent / 100, usePosition);
  714. }
  715. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  716. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  717. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  718. public static Dim Sized (int n) { return new DimAbsolute (n); }
  719. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  720. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  721. /// <param name="view">The view that will be tracked.</param>
  722. public static Dim Width (View view) { return new DimView (view, Dimension.Width); }
  723. /// <summary>
  724. /// Gets a dimension that is anchored to a certain point in the layout.
  725. /// This method is typically used internally by the layout system to determine the size of a View.
  726. /// </summary>
  727. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  728. /// <returns>
  729. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  730. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  731. /// dimension that is a certain percentage of the super view's size, and so on.
  732. /// </returns>
  733. internal virtual int Anchor (int width) { return 0; }
  734. /// <summary>
  735. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  736. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its content.
  737. /// </summary>
  738. /// <param name="location">
  739. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  740. /// top edge for height calculation.
  741. /// </param>
  742. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  743. /// <param name="us">The View that holds this Pos object.</param>
  744. /// <param name="dimension">Width or Height</param>
  745. /// <returns>
  746. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  747. /// is used.
  748. /// </returns>
  749. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  750. {
  751. return Math.Max (Anchor (superviewContentSize - location), 0);
  752. }
  753. internal class DimAbsolute (int n) : Dim
  754. {
  755. private readonly int _n = n;
  756. public override bool Equals (object other) { return other is DimAbsolute abs && abs._n == _n; }
  757. public override int GetHashCode () { return _n.GetHashCode (); }
  758. public override string ToString () { return $"Absolute({_n})"; }
  759. internal override int Anchor (int width) { return _n; }
  760. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  761. {
  762. // DimAbsolute.Anchor (int width) ignores width and returns n
  763. return Math.Max (Anchor (0), 0);
  764. }
  765. }
  766. internal class DimAuto (DimAutoStyle style, Dim min, Dim max) : Dim
  767. {
  768. internal readonly Dim _max = max;
  769. internal readonly Dim _min = min;
  770. internal readonly DimAutoStyle _style = style;
  771. internal int Size;
  772. public override bool Equals (object other) { return other is DimAuto auto && auto._min == _min && auto._max == _max && auto._style == _style; }
  773. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), _min, _max, _style); }
  774. public override string ToString () { return $"Auto({_style},{_min},{_max})"; }
  775. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  776. {
  777. if (us == null)
  778. {
  779. return _max?.Anchor (0) ?? 0;
  780. }
  781. var textSize = 0;
  782. var subviewsSize = 0;
  783. int autoMin = _min?.Anchor (superviewContentSize) ?? 0;
  784. if (superviewContentSize < autoMin)
  785. {
  786. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  787. return superviewContentSize;
  788. }
  789. if (_style is Dim.DimAutoStyle.Text or Dim.DimAutoStyle.Auto)
  790. {
  791. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  792. }
  793. if (_style is Dim.DimAutoStyle.Subviews or Dim.DimAutoStyle.Auto)
  794. {
  795. subviewsSize = us.Subviews.Count == 0
  796. ? 0
  797. : us.Subviews
  798. .Where (v => dimension == Dimension.Width ? v.X is not Pos.PosAnchorEnd : v.Y is not Pos.PosAnchorEnd)
  799. .Max (v => dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height);
  800. }
  801. int max = int.Max (textSize, subviewsSize);
  802. Thickness thickness = us.GetAdornmentsThickness ();
  803. if (dimension == Dimension.Width)
  804. {
  805. max += thickness.Horizontal;
  806. }
  807. else
  808. {
  809. max += thickness.Vertical;
  810. }
  811. max = int.Max (max, autoMin);
  812. return int.Min (max, _max?.Anchor (superviewContentSize) ?? superviewContentSize);
  813. }
  814. }
  815. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  816. {
  817. internal bool _add = add;
  818. internal Dim _left = left, _right = right;
  819. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  820. internal override int Anchor (int width)
  821. {
  822. int la = _left.Anchor (width);
  823. int ra = _right.Anchor (width);
  824. if (_add)
  825. {
  826. return la + ra;
  827. }
  828. return la - ra;
  829. }
  830. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  831. {
  832. int leftNewDim = _left.Calculate (location, superviewContentSize, us, dimension);
  833. int rightNewDim = _right.Calculate (location, superviewContentSize, us, dimension);
  834. int newDimension;
  835. if (_add)
  836. {
  837. newDimension = leftNewDim + rightNewDim;
  838. }
  839. else
  840. {
  841. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  842. }
  843. return newDimension;
  844. }
  845. }
  846. internal class DimFactor (float factor, bool remaining = false) : Dim
  847. {
  848. private readonly float _factor = factor;
  849. private readonly bool _remaining = remaining;
  850. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  851. public override int GetHashCode () { return _factor.GetHashCode (); }
  852. public bool IsFromRemaining () { return _remaining; }
  853. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  854. internal override int Anchor (int width) { return (int)(width * _factor); }
  855. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  856. {
  857. return _remaining ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  858. }
  859. }
  860. internal class DimFill (int margin) : Dim
  861. {
  862. private readonly int _margin = margin;
  863. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  864. public override int GetHashCode () { return _margin.GetHashCode (); }
  865. public override string ToString () { return $"Fill({_margin})"; }
  866. internal override int Anchor (int width) { return width - _margin; }
  867. }
  868. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  869. internal class DimFunc (Func<int> n) : Dim
  870. {
  871. private readonly Func<int> _function = n;
  872. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  873. public override int GetHashCode () { return _function.GetHashCode (); }
  874. public override string ToString () { return $"DimFunc({_function ()})"; }
  875. internal override int Anchor (int width) { return _function (); }
  876. }
  877. internal class DimView : Dim
  878. {
  879. private readonly Dimension _side;
  880. internal DimView (View view, Dimension side)
  881. {
  882. Target = view;
  883. _side = side;
  884. }
  885. public View Target { get; init; }
  886. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  887. public override int GetHashCode () { return Target.GetHashCode (); }
  888. public override string ToString ()
  889. {
  890. if (Target == null)
  891. {
  892. throw new NullReferenceException ();
  893. }
  894. string sideString = _side switch
  895. {
  896. Dimension.Height => "Height",
  897. Dimension.Width => "Width",
  898. _ => "unknown"
  899. };
  900. return $"View({sideString},{Target})";
  901. }
  902. internal override int Anchor (int width)
  903. {
  904. return _side switch
  905. {
  906. Dimension.Height => Target.Frame.Height,
  907. Dimension.Width => Target.Frame.Width,
  908. _ => 0
  909. };
  910. }
  911. }
  912. }