PosDim.cs 31 KB

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