PosDim.cs 38 KB

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