PosDim.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. if (us.IdealContentSize.HasValue)
  837. {
  838. subviewsSize = dimension == Dimension.Width ? us.IdealContentSize.Value.Width : us.IdealContentSize.Value.Height;
  839. }
  840. else
  841. {
  842. subviewsSize = us.Subviews.Count == 0
  843. ? 0
  844. : us.Subviews
  845. .Where (v => dimension == Dimension.Width ? v.X is not Pos.PosAnchorEnd : v.Y is not Pos.PosAnchorEnd)
  846. .Max (v => dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height);
  847. }
  848. }
  849. int max = int.Max (textSize, subviewsSize);
  850. Thickness thickness = us.GetAdornmentsThickness ();
  851. if (dimension == Dimension.Width)
  852. {
  853. max += thickness.Horizontal;
  854. }
  855. else
  856. {
  857. max += thickness.Vertical;
  858. }
  859. max = int.Max (max, autoMin);
  860. return int.Min (max, _max?.Anchor (superviewContentSize) ?? superviewContentSize);
  861. }
  862. /// <summary>
  863. /// Diagnostics API to determine if this Dim object references other views.
  864. /// </summary>
  865. /// <returns></returns>
  866. internal override bool ReferencesOtherViews ()
  867. {
  868. return _style is Dim.DimAutoStyle.Subviews or Dim.DimAutoStyle.Auto;
  869. }
  870. }
  871. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  872. {
  873. internal bool _add = add;
  874. internal Dim _left = left, _right = right;
  875. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  876. internal override int Anchor (int width)
  877. {
  878. int la = _left.Anchor (width);
  879. int ra = _right.Anchor (width);
  880. if (_add)
  881. {
  882. return la + ra;
  883. }
  884. return la - ra;
  885. }
  886. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  887. {
  888. int leftNewDim = _left.Calculate (location, superviewContentSize, us, dimension);
  889. int rightNewDim = _right.Calculate (location, superviewContentSize, us, dimension);
  890. int newDimension;
  891. if (_add)
  892. {
  893. newDimension = leftNewDim + rightNewDim;
  894. }
  895. else
  896. {
  897. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  898. }
  899. return newDimension;
  900. }
  901. /// <summary>
  902. /// Diagnostics API to determine if this Dim object references other views.
  903. /// </summary>
  904. /// <returns></returns>
  905. internal override bool ReferencesOtherViews ()
  906. {
  907. if (_left.ReferencesOtherViews ())
  908. {
  909. return true;
  910. }
  911. if (_right.ReferencesOtherViews ())
  912. {
  913. return true;
  914. }
  915. return false;
  916. }
  917. }
  918. internal class DimFactor (float factor, bool remaining = false) : Dim
  919. {
  920. private readonly float _factor = factor;
  921. private readonly bool _remaining = remaining;
  922. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  923. public override int GetHashCode () { return _factor.GetHashCode (); }
  924. public bool IsFromRemaining () { return _remaining; }
  925. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  926. internal override int Anchor (int width) { return (int)(width * _factor); }
  927. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  928. {
  929. return _remaining ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  930. }
  931. }
  932. internal class DimFill (int margin) : Dim
  933. {
  934. private readonly int _margin = margin;
  935. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  936. public override int GetHashCode () { return _margin.GetHashCode (); }
  937. public override string ToString () { return $"Fill({_margin})"; }
  938. internal override int Anchor (int width) { return width - _margin; }
  939. }
  940. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  941. internal class DimFunc (Func<int> n) : Dim
  942. {
  943. private readonly Func<int> _function = n;
  944. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  945. public override int GetHashCode () { return _function.GetHashCode (); }
  946. public override string ToString () { return $"DimFunc({_function ()})"; }
  947. internal override int Anchor (int width) { return _function (); }
  948. }
  949. internal class DimView : Dim
  950. {
  951. private readonly Dimension _side;
  952. internal DimView (View view, Dimension side)
  953. {
  954. Target = view;
  955. _side = side;
  956. }
  957. public View Target { get; init; }
  958. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  959. public override int GetHashCode () { return Target.GetHashCode (); }
  960. public override string ToString ()
  961. {
  962. if (Target == null)
  963. {
  964. throw new NullReferenceException ();
  965. }
  966. string sideString = _side switch
  967. {
  968. Dimension.Height => "Height",
  969. Dimension.Width => "Width",
  970. _ => "unknown"
  971. };
  972. return $"View({sideString},{Target})";
  973. }
  974. internal override int Anchor (int width)
  975. {
  976. return _side switch
  977. {
  978. Dimension.Height => Target.Frame.Height,
  979. Dimension.Width => Target.Frame.Width,
  980. _ => 0
  981. };
  982. }
  983. internal override bool ReferencesOtherViews ()
  984. {
  985. return true;
  986. }
  987. }
  988. }