PosDim.cs 37 KB

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