PosDim.cs 25 KB

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