PosDim.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. using System;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
  5. /// relative to the ending dimension. Integer values are implicitly convertible to
  6. /// an absolute <see cref="Pos"/>. These objects are created using the static methods Percent,
  7. /// AnchorEnd, and Center. The <see cref="Pos"/> objects can be combined with the addition and
  8. /// subtraction operators.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.
  13. /// </para>
  14. /// <para>
  15. /// These can be used to set the absolute position, when merely assigning an
  16. /// integer value (via the implicit integer to <see cref="Pos"/> conversion), and they can be combined
  17. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the position
  18. /// of the <see cref="View"/> 3 characters to the left after centering for example.
  19. /// </para>
  20. /// <para>
  21. /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View).
  22. /// The X(View) and Y(View) are
  23. /// aliases to Left(View) and Top(View) respectively.
  24. /// </para>
  25. /// <para>
  26. /// <list type="table">
  27. /// <listheader>
  28. /// <term>Pos Object</term>
  29. /// <description>Description</description>
  30. /// </listheader>
  31. /// <item>
  32. /// <term>
  33. /// <see cref="Pos.Function(Func{int})"/>
  34. /// </term>
  35. /// <description>
  36. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  37. /// function. The function will be called every time the position is needed.
  38. /// </description>
  39. /// </item>
  40. /// <item>
  41. /// <term>
  42. /// <see cref="Pos.Percent(float)"/>
  43. /// </term>
  44. /// <description>
  45. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  46. /// SuperView.
  47. /// </description>
  48. /// </item>
  49. /// <item>
  50. /// <term>
  51. /// <see cref="Pos.Anchor(int)"/>
  52. /// </term>
  53. /// <description>
  54. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom)
  55. /// of the dimension,
  56. /// useful to flush the layout from the right or bottom.
  57. /// </description>
  58. /// </item>
  59. /// <item>
  60. /// <term>
  61. /// <see cref="Pos.Center"/>
  62. /// </term>
  63. /// <description>
  64. /// Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.
  65. /// </description>
  66. /// </item>
  67. /// <item>
  68. /// <term>
  69. /// <see cref="Pos.At(int)"/>
  70. /// </term>
  71. /// <description>
  72. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  73. /// integer value.
  74. /// </description>
  75. /// </item>
  76. /// <item>
  77. /// <term>
  78. /// <see cref="Pos.Left"/>
  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.X(View)"/>
  88. /// </term>
  89. /// <description>
  90. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  91. /// <see cref="View"/>.
  92. /// </description>
  93. /// </item>
  94. /// <item>
  95. /// <term>
  96. /// <see cref="Pos.Top(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.Y(View)"/>
  106. /// </term>
  107. /// <description>
  108. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  109. /// <see cref="View"/>.
  110. /// </description>
  111. /// </item>
  112. /// <item>
  113. /// <term>
  114. /// <see cref="Pos.Right(View)"/>
  115. /// </term>
  116. /// <description>
  117. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  118. /// specified <see cref="View"/>.
  119. /// </description>
  120. /// </item>
  121. /// <item>
  122. /// <term>
  123. /// <see cref="Pos.Bottom(View)"/>
  124. /// </term>
  125. /// <description>
  126. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  127. /// specified <see cref="View"/>
  128. /// </description>
  129. /// </item>
  130. ///
  131. /// </list>
  132. /// </para>
  133. /// </remarks>
  134. public class Pos {
  135. internal virtual int Anchor (int width) => 0;
  136. /// <summary>
  137. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function will be
  138. /// called every time the position is needed.
  139. /// </summary>
  140. /// <param name="function">The function to be executed.</param>
  141. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  142. public static Pos Function (Func<int> function) => new PosFunc (function);
  143. /// <summary>
  144. /// Creates a percentage <see cref="Pos"/> object
  145. /// </summary>
  146. /// <returns>The percent <see cref="Pos"/> object.</returns>
  147. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  148. /// <example>
  149. /// This creates a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
  150. /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
  151. /// <code>
  152. /// var textView = new TextView () {
  153. /// X = Pos.Center (),
  154. /// Y = Pos.Percent (50),
  155. /// Width = Dim.Percent (80),
  156. /// Height = Dim.Percent (30),
  157. /// };
  158. /// </code>
  159. /// </example>
  160. public static Pos Percent (float n)
  161. {
  162. if (n is < 0 or > 100) {
  163. throw new ArgumentException ("Percent value must be between 0 and 100");
  164. }
  165. return new PosFactor (n / 100);
  166. }
  167. /// <summary>
  168. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the dimension,
  169. /// useful to flush the layout from the right or bottom.
  170. /// </summary>
  171. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  172. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  173. /// <example>
  174. /// This sample shows how align a <see cref="Button"/> to the bottom-right of a <see cref="View"/>.
  175. /// <code>
  176. /// // See Issue #502
  177. /// anchorButton.X = Pos.AnchorEnd () - (Pos.Right (anchorButton) - Pos.Left (anchorButton));
  178. /// anchorButton.Y = Pos.AnchorEnd (1);
  179. /// </code>
  180. /// </example>
  181. public static Pos AnchorEnd (int offset = 0)
  182. {
  183. if (offset < 0) {
  184. throw new ArgumentException (@"Must be positive", nameof (offset));
  185. }
  186. return new PosAnchorEnd (offset);
  187. }
  188. /// <summary>
  189. /// Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.
  190. /// </summary>
  191. /// <returns>The center Pos.</returns>
  192. /// <example>
  193. /// This creates a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
  194. /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
  195. /// <code>
  196. /// var textView = new TextView () {
  197. /// X = Pos.Center (),
  198. /// Y = Pos.Percent (50),
  199. /// Width = Dim.Percent (80),
  200. /// Height = Dim.Percent (30),
  201. /// };
  202. /// </code>
  203. /// </example>
  204. public static Pos Center () => new PosCenter ();
  205. /// <summary>
  206. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.
  207. /// </summary>
  208. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  209. /// <param name="n">The value to convert to the <see cref="Pos"/>.</param>
  210. public static Pos At (int n) => new PosAbsolute (n);
  211. /// <summary>
  212. /// Creates an Absolute <see cref="Pos"/> from the specified integer value.
  213. /// </summary>
  214. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  215. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  216. public static implicit operator Pos (int n) => new PosAbsolute (n);
  217. /// <summary>
  218. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.
  219. /// </summary>
  220. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  221. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  222. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  223. public static Pos operator + (Pos left, Pos right)
  224. {
  225. if (left is PosAbsolute && right is PosAbsolute) {
  226. return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
  227. }
  228. var newPos = new PosCombine (true, left, right);
  229. SetPosCombine (left, newPos);
  230. return newPos;
  231. }
  232. /// <summary>
  233. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <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. 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. static void SetPosCombine (Pos left, PosCombine newPos)
  248. {
  249. var view = left as PosView;
  250. if (view != null) {
  251. view.Target.SetNeedsLayout ();
  252. }
  253. }
  254. /// <summary>
  255. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.
  256. /// </summary>
  257. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  258. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  259. public static Pos Left (View view) => new PosView (view, 0);
  260. /// <summary>
  261. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.
  262. /// </summary>
  263. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  264. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  265. public static Pos X (View view) => new PosView (view, 0);
  266. /// <summary>
  267. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.
  268. /// </summary>
  269. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  270. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  271. public static Pos Top (View view) => new PosView (view, 1);
  272. /// <summary>
  273. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <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 Y (View view) => new PosView (view, 1);
  278. /// <summary>
  279. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified <see cref="View"/>.
  280. /// </summary>
  281. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  282. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  283. public static Pos Right (View view) => new PosView (view, 2);
  284. /// <summary>
  285. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified <see cref="View"/>
  286. /// </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 Bottom (View view) => new PosView (view, 3);
  290. /// <summary>Serves as the default hash function. </summary>
  291. /// <returns>A hash code for the current object.</returns>
  292. public override int GetHashCode () => Anchor (0).GetHashCode ();
  293. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  294. /// <param name="other">The object to compare with the current object. </param>
  295. /// <returns>
  296. /// <see langword="true"/> if the specified object is equal to the current object; otherwise, <see langword="false"/>.
  297. /// </returns>
  298. public override bool Equals (object other) => other is Pos abs && abs == this;
  299. internal class PosFactor : Pos {
  300. readonly float _factor;
  301. public PosFactor (float n) => _factor = n;
  302. internal override int Anchor (int width) => (int)(width * _factor);
  303. public override string ToString () => $"Factor({_factor})";
  304. public override int GetHashCode () => _factor.GetHashCode ();
  305. public override bool Equals (object other) => other is PosFactor f && f._factor == _factor;
  306. }
  307. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  308. internal class PosFunc : Pos {
  309. readonly Func<int> _function;
  310. public PosFunc (Func<int> n) => _function = n;
  311. internal override int Anchor (int width) => _function ();
  312. public override string ToString () => $"PosFunc({_function ()})";
  313. public override int GetHashCode () => _function.GetHashCode ();
  314. public override bool Equals (object other) => other is PosFunc f && f._function () == _function ();
  315. }
  316. internal class PosAnchorEnd : Pos {
  317. readonly int _offset;
  318. public PosAnchorEnd (int offset) => _offset = offset;
  319. internal override int Anchor (int width) => width - _offset;
  320. public override string ToString () => $"AnchorEnd({_offset})";
  321. public override int GetHashCode () => _offset.GetHashCode ();
  322. public override bool Equals (object other) => other is PosAnchorEnd anchorEnd && anchorEnd._offset == _offset;
  323. }
  324. internal class PosAbsolute : Pos {
  325. readonly int _n;
  326. public PosAbsolute (int n) => _n = n;
  327. public override string ToString () => $"Absolute({_n})";
  328. internal override int Anchor (int width) => _n;
  329. public override int GetHashCode () => _n.GetHashCode ();
  330. public override bool Equals (object other) => other is PosAbsolute abs && abs._n == _n;
  331. }
  332. internal class PosCenter : Pos {
  333. internal override int Anchor (int width) => width / 2;
  334. public override string ToString () => "Center";
  335. }
  336. internal class PosCombine : Pos {
  337. internal bool _add;
  338. internal Pos _left, _right;
  339. public PosCombine (bool add, Pos left, Pos right)
  340. {
  341. _left = left;
  342. _right = right;
  343. _add = add;
  344. }
  345. internal override int Anchor (int width)
  346. {
  347. var la = _left.Anchor (width);
  348. var ra = _right.Anchor (width);
  349. if (_add) {
  350. return la + ra;
  351. }
  352. return la - ra;
  353. }
  354. public override string ToString () => $"Combine({_left}{(_add ? '+' : '-')}{_right})";
  355. }
  356. internal class PosView : Pos {
  357. readonly int side;
  358. public readonly View Target;
  359. public PosView (View view, int side)
  360. {
  361. Target = view;
  362. this.side = side;
  363. }
  364. internal override int Anchor (int width)
  365. {
  366. switch (side) {
  367. case 0: return Target.Frame.X;
  368. case 1: return Target.Frame.Y;
  369. case 2: return Target.Frame.Right;
  370. case 3: return Target.Frame.Bottom;
  371. default:
  372. return 0;
  373. }
  374. }
  375. public override string ToString ()
  376. {
  377. string tside;
  378. switch (side) {
  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 == null) {
  396. throw new NullReferenceException (nameof (Target));
  397. }
  398. return $"View(side={tside},target={Target})";
  399. }
  400. public override int GetHashCode () => Target.GetHashCode ();
  401. public override bool Equals (object other) => other is PosView abs && abs.Target == Target;
  402. }
  403. }
  404. /// <summary>
  405. /// <para>
  406. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the <see cref="View.Width"/>
  407. /// and
  408. /// <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable Computed Layout (see
  409. /// <see cref="LayoutStyle.Computed"/>)
  410. /// to automatically manage the dimensions of a view.
  411. /// </para>
  412. /// <para>
  413. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using the
  414. /// static methods described below.
  415. /// The <see cref="Dim"/> objects can be combined with the addition and subtraction operators.
  416. /// </para>
  417. /// </summary>
  418. /// <remarks>
  419. /// <para>
  420. /// <list type="table">
  421. /// <listheader>
  422. /// <term>Dim Object</term>
  423. /// <description>Description</description>
  424. /// </listheader>
  425. /// <item>
  426. /// <term>
  427. /// <see cref="Dim.Function(Func{int})"/>
  428. /// </term>
  429. /// <description>
  430. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the
  431. /// provided function. The function will be called every time the dimension is needed.
  432. /// </description>
  433. /// </item>
  434. /// <item>
  435. /// <term>
  436. /// <see cref="Dim.Percent(float, bool)"/>
  437. /// </term>
  438. /// <description>
  439. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  440. /// SuperView.
  441. /// </description>
  442. /// </item>
  443. /// <item>
  444. /// <term>
  445. /// <see cref="Dim.Fill(int)"/>
  446. /// </term>
  447. /// <description>
  448. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified
  449. /// number of columns for a margin.
  450. /// </description>
  451. /// </item>
  452. /// <item>
  453. /// <term>
  454. /// <see cref="Dim.Width(View)"/>
  455. /// </term>
  456. /// <description>
  457. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  458. /// <see cref="View"/>.
  459. /// </description>
  460. /// </item>
  461. /// <item>
  462. /// <term>
  463. /// <see cref="Dim.Height(View)"/>
  464. /// </term>
  465. /// <description>
  466. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  467. /// <see cref="View"/>.
  468. /// </description>
  469. /// </item>
  470. /// </list>
  471. /// </para>
  472. /// <para>
  473. /// </para>
  474. /// </remarks>
  475. public class Dim {
  476. internal virtual int Anchor (int width) => 0;
  477. /// <summary>
  478. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  479. /// The function will be called every time the dimension is needed.
  480. /// </summary>
  481. /// <param name="function">The function to be executed.</param>
  482. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  483. public static Dim Function (Func<int> function) => new DimFunc (function);
  484. /// <summary>
  485. /// Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.
  486. /// </summary>
  487. /// <returns>The percent <see cref="Dim"/> object.</returns>
  488. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  489. /// <param name="r">
  490. /// If <c>true</c> the Percent is computed based on the remaining space after the X/Y anchor positions.
  491. /// If <c>false</c> is computed based on the whole original space.
  492. /// </param>
  493. /// <example>
  494. /// This initializes a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
  495. /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
  496. /// <code>
  497. /// var textView = new TextView () {
  498. /// X = Pos.Center (),
  499. /// Y = Pos.Percent (50),
  500. /// Width = Dim.Percent (80),
  501. /// Height = Dim.Percent (30),
  502. /// };
  503. /// </code>
  504. /// </example>
  505. public static Dim Percent (float n, bool r = false)
  506. {
  507. if (n is < 0 or > 100) {
  508. throw new ArgumentException ("Percent value must be between 0 and 100");
  509. }
  510. return new DimFactor (n / 100, r);
  511. }
  512. /// <summary>
  513. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a margin.
  514. /// </summary>
  515. /// <returns>The Fill dimension.</returns>
  516. /// <param name="margin">Margin to use.</param>
  517. public static Dim Fill (int margin = 0) => new DimFill (margin);
  518. /// <summary>
  519. /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
  520. /// </summary>
  521. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  522. /// <param name="n">The value to convert to the pos.</param>
  523. public static implicit operator Dim (int n) => new DimAbsolute (n);
  524. /// <summary>
  525. /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
  526. /// </summary>
  527. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  528. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  529. public static Dim Sized (int n) => new DimAbsolute (n);
  530. /// <summary>
  531. /// Adds a <see cref="Terminal.Gui.Dim"/> to a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
  532. /// </summary>
  533. /// <param name="left">The first <see cref="Terminal.Gui.Dim"/> to add.</param>
  534. /// <param name="right">The second <see cref="Terminal.Gui.Dim"/> to add.</param>
  535. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  536. public static Dim operator + (Dim left, Dim right)
  537. {
  538. if (left is DimAbsolute && right is DimAbsolute) {
  539. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  540. }
  541. var newDim = new DimCombine (true, left, right);
  542. SetDimCombine (left, newDim);
  543. return newDim;
  544. }
  545. /// <summary>
  546. /// Subtracts a <see cref="Terminal.Gui.Dim"/> from a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
  547. /// </summary>
  548. /// <param name="left">The <see cref="Terminal.Gui.Dim"/> to subtract from (the minuend).</param>
  549. /// <param name="right">The <see cref="Terminal.Gui.Dim"/> to subtract (the subtrahend).</param>
  550. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  551. public static Dim operator - (Dim left, Dim right)
  552. {
  553. if (left is DimAbsolute && right is DimAbsolute) {
  554. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  555. }
  556. var newDim = new DimCombine (false, left, right);
  557. SetDimCombine (left, newDim);
  558. return newDim;
  559. }
  560. // BUGBUG: newPos is never used.
  561. static void SetDimCombine (Dim left, DimCombine newPos) => (left as DimView)?.Target.SetNeedsLayout ();
  562. /// <summary>
  563. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.
  564. /// </summary>
  565. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  566. /// <param name="view">The view that will be tracked.</param>
  567. public static Dim Width (View view) => new DimView (view, 1);
  568. /// <summary>
  569. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.
  570. /// </summary>
  571. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  572. /// <param name="view">The view that will be tracked.</param>
  573. public static Dim Height (View view) => new DimView (view, 0);
  574. /// <summary>Serves as the default hash function. </summary>
  575. /// <returns>A hash code for the current object.</returns>
  576. public override int GetHashCode () => Anchor (0).GetHashCode ();
  577. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  578. /// <param name="other">The object to compare with the current object. </param>
  579. /// <returns>
  580. /// <see langword="true"/> if the specified object is equal to the current object; otherwise, <see langword="false"/>.
  581. /// </returns>
  582. public override bool Equals (object other) => other is Dim abs && abs == this;
  583. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  584. internal class DimFunc : Dim {
  585. readonly Func<int> _function;
  586. public DimFunc (Func<int> n) => _function = n;
  587. internal override int Anchor (int width) => _function ();
  588. public override string ToString () => $"DimFunc({_function ()})";
  589. public override int GetHashCode () => _function.GetHashCode ();
  590. public override bool Equals (object other) => other is DimFunc f && f._function () == _function ();
  591. }
  592. internal class DimFactor : Dim {
  593. readonly float _factor;
  594. readonly bool _remaining;
  595. public DimFactor (float n, bool r = false)
  596. {
  597. _factor = n;
  598. _remaining = r;
  599. }
  600. internal override int Anchor (int width) => (int)(width * _factor);
  601. public bool IsFromRemaining () => _remaining;
  602. public override string ToString () => $"Factor({_factor},{_remaining})";
  603. public override int GetHashCode () => _factor.GetHashCode ();
  604. public override bool Equals (object other) => other is DimFactor f && f._factor == _factor && f._remaining == _remaining;
  605. }
  606. internal class DimAbsolute : Dim {
  607. readonly int _n;
  608. public DimAbsolute (int n) => _n = n;
  609. public override string ToString () => $"Absolute({_n})";
  610. internal override int Anchor (int width) => _n;
  611. public override int GetHashCode () => _n.GetHashCode ();
  612. public override bool Equals (object other) => other is DimAbsolute abs && abs._n == _n;
  613. }
  614. internal class DimFill : Dim {
  615. readonly int _margin;
  616. public DimFill (int margin) => _margin = margin;
  617. public override string ToString () => $"Fill({_margin})";
  618. internal override int Anchor (int width) => width - _margin;
  619. public override int GetHashCode () => _margin.GetHashCode ();
  620. public override bool Equals (object other) => other is DimFill fill && fill._margin == _margin;
  621. }
  622. internal class DimCombine : Dim {
  623. internal bool _add;
  624. internal Dim _left, _right;
  625. public DimCombine (bool add, Dim left, Dim right)
  626. {
  627. _left = left;
  628. _right = right;
  629. _add = add;
  630. }
  631. internal override int Anchor (int width)
  632. {
  633. var la = _left.Anchor (width);
  634. var ra = _right.Anchor (width);
  635. if (_add) {
  636. return la + ra;
  637. }
  638. return la - ra;
  639. }
  640. public override string ToString () => $"Combine({_left}{(_add ? '+' : '-')}{_right})";
  641. }
  642. internal class DimView : Dim {
  643. readonly int _side;
  644. public DimView (View view, int side)
  645. {
  646. Target = view;
  647. _side = side;
  648. }
  649. public View Target { get; init; }
  650. internal override int Anchor (int width) => _side switch {
  651. 0 => Target.Frame.Height,
  652. 1 => Target.Frame.Width,
  653. _ => 0
  654. };
  655. public override string ToString ()
  656. {
  657. if (Target == null) {
  658. throw new NullReferenceException ();
  659. }
  660. var tside = _side switch {
  661. 0 => "Height",
  662. 1 => "Width",
  663. _ => "unknown"
  664. };
  665. return $"View({tside},{Target})";
  666. }
  667. public override int GetHashCode () => Target.GetHashCode ();
  668. public override bool Equals (object other) => other is DimView abs && abs.Target == Target;
  669. }
  670. }