PosDim.cs 31 KB

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