PosDim.cs 31 KB

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