PosDim.cs 34 KB

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