PosDim.cs 45 KB

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