PosDim.cs 36 KB

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