PosDim.cs 31 KB

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