PosDim.cs 32 KB

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