PosDim.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. /// Gets a position that is anchored to a certain point in the layout. This method is typically used
  295. /// internally by the layout system to determine where a View should be positioned.
  296. /// </summary>
  297. /// <param name="width">The width of the area where the View is being positioned (Superview.ContentSize).</param>
  298. /// <returns>
  299. /// An integer representing the calculated position. The way this position is calculated depends on the specific
  300. /// subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
  301. /// position that is anchored to the end of the layout, and so on.
  302. /// </returns>
  303. internal virtual int Anchor (int width) { return 0; }
  304. /// <summary>
  305. /// Calculates and returns the position of a <see cref="View"/> object. It takes into account the dimension of the
  306. /// superview and the dimension of the view itself.
  307. /// </summary>
  308. /// <param name="superviewDimension">
  309. /// The dimension of the superview. This could be the width for x-coordinate calculation or the
  310. /// height for y-coordinate calculation.
  311. /// </param>
  312. /// <param name="dim">The dimension of the View. It could be the current width or height.</param>
  313. /// <param name="autosize">Obsolete; to be deprecated.</param>
  314. /// <param name="autoSize">Obsolete; to be deprecated.</param>
  315. /// <returns>
  316. /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos
  317. /// that
  318. /// is used.
  319. /// </returns>
  320. internal virtual int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize) { return Anchor (superviewDimension); }
  321. internal class PosAbsolute (int n) : Pos
  322. {
  323. private readonly int _n = n;
  324. public override bool Equals (object other) { return other is PosAbsolute abs && abs._n == _n; }
  325. public override int GetHashCode () { return _n.GetHashCode (); }
  326. public override string ToString () { return $"Absolute({_n})"; }
  327. internal override int Anchor (int width) { return _n; }
  328. }
  329. internal class PosAnchorEnd : Pos
  330. {
  331. private readonly int _offset;
  332. public PosAnchorEnd () { UseDimForOffset = true; }
  333. public PosAnchorEnd (int offset) { _offset = offset; }
  334. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd._offset == _offset; }
  335. public override int GetHashCode () { return _offset.GetHashCode (); }
  336. /// <summary>
  337. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  338. /// </summary>
  339. internal bool UseDimForOffset { get; set; }
  340. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({_offset})"; }
  341. internal override int Anchor (int width)
  342. {
  343. if (UseDimForOffset)
  344. {
  345. return width;
  346. }
  347. return width - _offset;
  348. }
  349. internal override int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize)
  350. {
  351. int newLocation = Anchor (superviewDimension);
  352. if (UseDimForOffset)
  353. {
  354. newLocation -= dim.Anchor (superviewDimension);
  355. }
  356. return newLocation;
  357. }
  358. }
  359. internal class PosCenter : Pos
  360. {
  361. public override string ToString () { return "Center"; }
  362. internal override int Anchor (int width) { return width / 2; }
  363. internal override int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize)
  364. {
  365. int newDimension = Math.Max (dim.Calculate (0, superviewDimension, autosize, autoSize), 0);
  366. return Anchor (superviewDimension - newDimension);
  367. }
  368. }
  369. internal class PosCombine (bool add, Pos left, Pos right) : Pos
  370. {
  371. internal bool _add = add;
  372. internal Pos _left = left, _right = right;
  373. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  374. internal override int Anchor (int width)
  375. {
  376. int la = _left.Anchor (width);
  377. int ra = _right.Anchor (width);
  378. if (_add)
  379. {
  380. return la + ra;
  381. }
  382. return la - ra;
  383. }
  384. internal override int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize)
  385. {
  386. int newDimension = dim.Calculate (0, superviewDimension, autosize, autoSize);
  387. int left = _left.Calculate (superviewDimension, dim, autosize, autoSize);
  388. int right = _right.Calculate (superviewDimension, dim, autosize, autoSize);
  389. if (_add)
  390. {
  391. return left + right;
  392. }
  393. return left - right;
  394. }
  395. }
  396. internal class PosFactor (float factor) : Pos
  397. {
  398. private readonly float _factor = factor;
  399. public override bool Equals (object other) { return other is PosFactor f && f._factor == _factor; }
  400. public override int GetHashCode () { return _factor.GetHashCode (); }
  401. public override string ToString () { return $"Factor({_factor})"; }
  402. internal override int Anchor (int width) { return (int)(width * _factor); }
  403. }
  404. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  405. internal class PosFunc (Func<int> n) : Pos
  406. {
  407. private readonly Func<int> _function = n;
  408. public override bool Equals (object other) { return other is PosFunc f && f._function () == _function (); }
  409. public override int GetHashCode () { return _function.GetHashCode (); }
  410. public override string ToString () { return $"PosFunc({_function ()})"; }
  411. internal override int Anchor (int width) { return _function (); }
  412. }
  413. internal enum Side
  414. {
  415. X = 0,
  416. Y = 1,
  417. Right = 2,
  418. Bottom = 3
  419. }
  420. internal class PosView (View view, Side side) : Pos
  421. {
  422. public readonly View Target = view;
  423. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  424. public override int GetHashCode () { return Target.GetHashCode (); }
  425. public override string ToString ()
  426. {
  427. string sideString = side switch
  428. {
  429. Side.X => "x",
  430. Side.Y => "y",
  431. Side.Right => "right",
  432. Side.Bottom => "bottom",
  433. _ => "unknown"
  434. };
  435. if (Target == null)
  436. {
  437. throw new NullReferenceException (nameof (Target));
  438. }
  439. return $"View(side={sideString},target={Target})";
  440. }
  441. internal override int Anchor (int width)
  442. {
  443. return side switch
  444. {
  445. Side.X => Target.Frame.X,
  446. Side.Y => Target.Frame.Y,
  447. Side.Right => Target.Frame.Right,
  448. Side.Bottom => Target.Frame.Bottom,
  449. _ => 0
  450. };
  451. }
  452. }
  453. }
  454. /// <summary>
  455. /// <para>
  456. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  457. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  458. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  459. /// </para>
  460. /// <para>
  461. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  462. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  463. /// subtraction operators.
  464. /// </para>
  465. /// </summary>
  466. /// <remarks>
  467. /// <para>
  468. /// <list type="table">
  469. /// <listheader>
  470. /// <term>Dim Object</term> <description>Description</description>
  471. /// </listheader>
  472. /// <item>
  473. /// <term>
  474. /// <see cref="Dim.Function(Func{int})"/>
  475. /// </term>
  476. /// <description>
  477. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  478. /// function. The function will be called every time the dimension is needed.
  479. /// </description>
  480. /// </item>
  481. /// <item>
  482. /// <term>
  483. /// <see cref="Dim.Percent(float, bool)"/>
  484. /// </term>
  485. /// <description>
  486. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  487. /// SuperView.
  488. /// </description>
  489. /// </item>
  490. /// <item>
  491. /// <term>
  492. /// <see cref="Dim.Fill(int)"/>
  493. /// </term>
  494. /// <description>
  495. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  496. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  497. /// </description>
  498. /// </item>
  499. /// <item>
  500. /// <term>
  501. /// <see cref="Dim.Width(View)"/>
  502. /// </term>
  503. /// <description>
  504. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  505. /// <see cref="View"/>.
  506. /// </description>
  507. /// </item>
  508. /// <item>
  509. /// <term>
  510. /// <see cref="Dim.Height(View)"/>
  511. /// </term>
  512. /// <description>
  513. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  514. /// <see cref="View"/>.
  515. /// </description>
  516. /// </item>
  517. /// </list>
  518. /// </para>
  519. /// <para></para>
  520. /// </remarks>
  521. public class Dim
  522. {
  523. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  524. /// <param name="other">The object to compare with the current object. </param>
  525. /// <returns>
  526. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  527. /// <see langword="false"/>.
  528. /// </returns>
  529. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  530. /// <summary>
  531. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  532. /// margin.
  533. /// </summary>
  534. /// <returns>The Fill dimension.</returns>
  535. /// <param name="margin">Margin to use.</param>
  536. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  537. /// <summary>
  538. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  539. /// The function will be called every time the dimension is needed.
  540. /// </summary>
  541. /// <param name="function">The function to be executed.</param>
  542. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  543. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  544. /// <summary>Serves as the default hash function. </summary>
  545. /// <returns>A hash code for the current object.</returns>
  546. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  547. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  548. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  549. /// <param name="view">The view that will be tracked.</param>
  550. public static Dim Height (View view) { return new DimView (view, Side.Height); }
  551. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  552. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  553. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  554. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  555. public static Dim operator + (Dim left, Dim right)
  556. {
  557. if (left is DimAbsolute && right is DimAbsolute)
  558. {
  559. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  560. }
  561. var newDim = new DimCombine (true, left, right);
  562. (left as DimView)?.Target.SetNeedsLayout ();
  563. return newDim;
  564. }
  565. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  566. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  567. /// <param name="n">The value to convert to the pos.</param>
  568. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  569. /// <summary>
  570. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  571. /// <see cref="Dim"/>.
  572. /// </summary>
  573. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  574. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  575. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  576. public static Dim operator - (Dim left, Dim right)
  577. {
  578. if (left is DimAbsolute && right is DimAbsolute)
  579. {
  580. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  581. }
  582. var newDim = new DimCombine (false, left, right);
  583. (left as DimView)?.Target.SetNeedsLayout ();
  584. return newDim;
  585. }
  586. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  587. /// <returns>The percent <see cref="Dim"/> object.</returns>
  588. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  589. /// <param name="usePosition">
  590. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  591. /// <see cref="View.Y"/>).
  592. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  593. /// </param>
  594. /// <example>
  595. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  596. /// height,
  597. /// and is 80% the width of the SuperView.
  598. /// <code>
  599. /// var textView = new TextField {
  600. /// X = Pos.Center (),
  601. /// Y = Pos.Percent (50),
  602. /// Width = Dim.Percent (80),
  603. /// Height = Dim.Percent (30),
  604. /// };
  605. /// </code>
  606. /// </example>
  607. public static Dim Percent (float percent, bool usePosition = false)
  608. {
  609. if (percent is < 0 or > 100)
  610. {
  611. throw new ArgumentException ("Percent value must be between 0 and 100");
  612. }
  613. return new DimFactor (percent / 100, usePosition);
  614. }
  615. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  616. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  617. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  618. public static Dim Sized (int n) { return new DimAbsolute (n); }
  619. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  620. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  621. /// <param name="view">The view that will be tracked.</param>
  622. public static Dim Width (View view) { return new DimView (view, Side.Width); }
  623. /// <summary>
  624. /// Gets a dimension that is anchored to a certain point in the layout.
  625. /// This method is typically used internally by the layout system to determine the size of a View.
  626. /// </summary>
  627. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  628. /// <returns>
  629. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  630. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  631. /// dimension that is a certain percentage of the super view's size, and so on.
  632. /// </returns>
  633. internal virtual int Anchor (int width) { return 0; }
  634. /// <summary>
  635. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  636. /// <see cref="View"/>, its current size, and whether it should automatically adjust its size based on its content.
  637. /// </summary>
  638. /// <param name="location">
  639. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  640. /// top edge for height calculation.
  641. /// </param>
  642. /// <param name="dimension">The current size of the View. It could be the current width or height.</param>
  643. /// <param name="autosize">Obsolete; To be deprecated.</param>
  644. /// <param name="autoSize">Obsolete; To be deprecated.</param>
  645. /// <returns>
  646. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  647. /// is used.
  648. /// </returns>
  649. internal virtual int Calculate (int location, int dimension, int autosize, bool autoSize)
  650. {
  651. int newDimension = Math.Max (Anchor (dimension - location), 0);
  652. return autoSize && autosize > newDimension ? autosize : newDimension;
  653. }
  654. internal class DimAbsolute (int n) : Dim
  655. {
  656. private readonly int _n = n;
  657. public override bool Equals (object other) { return other is DimAbsolute abs && abs._n == _n; }
  658. public override int GetHashCode () { return _n.GetHashCode (); }
  659. public override string ToString () { return $"Absolute({_n})"; }
  660. internal override int Anchor (int width) { return _n; }
  661. internal override int Calculate (int location, int dimension, int autosize, bool autoSize)
  662. {
  663. // DimAbsolute.Anchor (int width) ignores width and returns n
  664. int newDimension = Math.Max (Anchor (0), 0);
  665. return autoSize && autosize > newDimension ? autosize : newDimension;
  666. }
  667. }
  668. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  669. {
  670. internal bool _add = add;
  671. internal Dim _left = left, _right = right;
  672. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  673. internal override int Anchor (int width)
  674. {
  675. int la = _left.Anchor (width);
  676. int ra = _right.Anchor (width);
  677. if (_add)
  678. {
  679. return la + ra;
  680. }
  681. return la - ra;
  682. }
  683. internal override int Calculate (int location, int dimension, int autosize, bool autoSize)
  684. {
  685. int leftNewDim = _left.Calculate (location, dimension, autosize, autoSize);
  686. int rightNewDim = _right.Calculate (location, dimension, autosize, autoSize);
  687. int newDimension;
  688. if (_add)
  689. {
  690. newDimension = leftNewDim + rightNewDim;
  691. }
  692. else
  693. {
  694. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  695. }
  696. return autoSize && autosize > newDimension ? autosize : newDimension;
  697. }
  698. }
  699. internal class DimFactor (float factor, bool remaining = false) : Dim
  700. {
  701. private readonly float _factor = factor;
  702. private readonly bool _remaining = remaining;
  703. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  704. public override int GetHashCode () { return _factor.GetHashCode (); }
  705. public bool IsFromRemaining () { return _remaining; }
  706. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  707. internal override int Anchor (int width) { return (int)(width * _factor); }
  708. internal override int Calculate (int location, int dimension, int autosize, bool autoSize)
  709. {
  710. int newDimension = _remaining ? Math.Max (Anchor (dimension - location), 0) : Anchor (dimension);
  711. return autoSize && autosize > newDimension ? autosize : newDimension;
  712. }
  713. }
  714. internal class DimFill (int margin) : Dim
  715. {
  716. private readonly int _margin = margin;
  717. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  718. public override int GetHashCode () { return _margin.GetHashCode (); }
  719. public override string ToString () { return $"Fill({_margin})"; }
  720. internal override int Anchor (int width) { return width - _margin; }
  721. }
  722. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  723. internal class DimFunc (Func<int> n) : Dim
  724. {
  725. private readonly Func<int> _function = n;
  726. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  727. public override int GetHashCode () { return _function.GetHashCode (); }
  728. public override string ToString () { return $"DimFunc({_function ()})"; }
  729. internal override int Anchor (int width) { return _function (); }
  730. }
  731. internal enum Side
  732. {
  733. Height = 0,
  734. Width = 1
  735. }
  736. internal class DimView : Dim
  737. {
  738. private readonly Side _side;
  739. internal DimView (View view, Side side)
  740. {
  741. Target = view;
  742. _side = side;
  743. }
  744. public View Target { get; init; }
  745. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  746. public override int GetHashCode () { return Target.GetHashCode (); }
  747. public override string ToString ()
  748. {
  749. if (Target == null)
  750. {
  751. throw new NullReferenceException ();
  752. }
  753. string sideString = _side switch
  754. {
  755. Side.Height => "Height",
  756. Side.Width => "Width",
  757. _ => "unknown"
  758. };
  759. return $"View({sideString},{Target})";
  760. }
  761. internal override int Anchor (int width)
  762. {
  763. return _side switch
  764. {
  765. Side.Height => Target.Frame.Height,
  766. Side.Width => Target.Frame.Width,
  767. _ => 0
  768. };
  769. }
  770. }
  771. }