PosDim.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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(int)"/></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="margin">Optional margin to place to the right or below.</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 margin = 0)
  166. {
  167. if (margin < 0) {
  168. throw new ArgumentException ("Margin must be positive");
  169. }
  170. return new PosAnchorEnd (margin);
  171. }
  172. internal class PosAnchorEnd : Pos {
  173. readonly int _p;
  174. public PosAnchorEnd (int n) => _p = n;
  175. internal override int Anchor (int width) => width - _p;
  176. public override string ToString () => $"AnchorEnd({_p})";
  177. public override int GetHashCode () => _p.GetHashCode ();
  178. public override bool Equals (object other) => other is PosAnchorEnd anchorEnd && anchorEnd._p == _p;
  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="Auto()"/></term>
  397. /// <description>
  398. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all of the view's SubViews.
  399. /// </description>
  400. /// </item>
  401. /// <item>
  402. /// <term><see cref="Dim.Width(View)"/></term>
  403. /// <description>
  404. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.
  405. /// </description>
  406. /// </item>
  407. /// <item>
  408. /// <term><see cref="Dim.Height(View)"/></term>
  409. /// <description>
  410. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.
  411. /// </description>
  412. /// </item>
  413. /// </list>
  414. /// </para>
  415. /// <para>
  416. /// </para>
  417. /// </remarks>
  418. public class Dim {
  419. internal virtual int Anchor (int width) => 0;
  420. /// <summary>
  421. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  422. /// The function will be called every time the dimension is needed.
  423. /// </summary>
  424. /// <param name="function">The function to be executed.</param>
  425. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  426. public static Dim Function (Func<int> function) => new DimFunc (function);
  427. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  428. internal class DimFunc : Dim {
  429. readonly Func<int> _function;
  430. public DimFunc (Func<int> n) => _function = n;
  431. internal override int Anchor (int width) => _function ();
  432. public override string ToString () => $"DimFunc({_function ()})";
  433. public override int GetHashCode () => _function.GetHashCode ();
  434. public override bool Equals (object other) => other is DimFunc f && f._function () == _function ();
  435. }
  436. /// <summary>
  437. /// Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.
  438. /// </summary>
  439. /// <returns>The percent <see cref="Dim"/> object.</returns>
  440. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  441. /// <param name="r">If <c>true</c> the Percent is computed based on the remaining space after the X/Y anchor positions.
  442. /// If <c>false</c> is computed based on the whole original space.</param>
  443. /// <example>
  444. /// This initializes a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
  445. /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
  446. /// <code>
  447. /// var textView = new TextView () {
  448. /// X = Pos.Center (),
  449. /// Y = Pos.Percent (50),
  450. /// Width = Dim.Percent (80),
  451. /// Height = Dim.Percent (30),
  452. /// };
  453. /// </code>
  454. /// </example>
  455. public static Dim Percent (float n, bool r = false)
  456. {
  457. if (n is < 0 or > 100) {
  458. throw new ArgumentException ("Percent value must be between 0 and 100");
  459. }
  460. return new DimFactor (n / 100, r);
  461. }
  462. internal class DimFactor : Dim {
  463. readonly float _factor;
  464. readonly bool _remaining;
  465. public DimFactor (float n, bool r = false)
  466. {
  467. _factor = n;
  468. _remaining = r;
  469. }
  470. internal override int Anchor (int width) => (int)(width * _factor);
  471. public bool IsFromRemaining () => _remaining;
  472. public override string ToString () => $"Factor({_factor},{_remaining})";
  473. public override int GetHashCode () => _factor.GetHashCode ();
  474. public override bool Equals (object other) => other is DimFactor f && f._factor == _factor && f._remaining == _remaining;
  475. }
  476. internal class DimAbsolute : Dim {
  477. readonly int _n;
  478. public DimAbsolute (int n) => _n = n;
  479. public override string ToString () => $"Absolute({_n})";
  480. internal override int Anchor (int width) => _n;
  481. public override int GetHashCode () => _n.GetHashCode ();
  482. public override bool Equals (object other) => other is DimAbsolute abs && abs._n == _n;
  483. }
  484. internal class DimFill : Dim {
  485. readonly int _margin;
  486. public DimFill (int margin) => _margin = margin;
  487. public override string ToString () => $"Fill({_margin})";
  488. internal override int Anchor (int width) => width - _margin;
  489. public override int GetHashCode () => _margin.GetHashCode ();
  490. public override bool Equals (object other) => other is DimFill fill && fill._margin == _margin;
  491. }
  492. /// <summary>
  493. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a margin.
  494. /// </summary>
  495. /// <returns>The Fill dimension.</returns>
  496. /// <param name="margin">Margin to use.</param>
  497. public static Dim Fill (int margin = 0) => new DimFill (margin);
  498. /// <summary>
  499. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all of the view's SubViews.
  500. /// </summary>
  501. /// <example>
  502. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two SubViews.
  503. /// <code>
  504. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  505. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  506. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.AutoSize (), Height = Dim.AutoSize () };
  507. /// view.Add (button, textField);
  508. /// </code>
  509. /// </example>
  510. /// <returns>The AutoSize <see cref="Dim"/> object.</returns>
  511. /// <param name="style">Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Text"/>. NOT CURRENTLY SUPPORTED.</param>
  512. /// <param name="min">Specifies the minimum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  513. /// <param name="min">Specifies the maximum dimension that view will be automatically sized to. NOT CURRENTLY SUPPORTED.</param>
  514. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Subviews, Dim min = null, Dim max = null)
  515. {
  516. if (style == DimAutoStyle.Text) {
  517. throw new NotImplementedException (@"DimAutoStyle.Text is not implemented.");
  518. }
  519. //if (min != null) {
  520. // throw new NotImplementedException (@"min is not implemented");
  521. //}
  522. if (max != null) {
  523. throw new NotImplementedException (@"max is not implemented");
  524. }
  525. return new DimAuto (style, min, max);
  526. }
  527. /// <summary>
  528. /// Specifies how <see cref="DimAuto"/> will compute the dimension.
  529. /// </summary>
  530. public enum DimAutoStyle {
  531. /// <summary>
  532. /// The dimension will be computed from the view's <see cref="View.Text"/>. NOT CURRENTLY SUPPORTED.
  533. /// </summary>
  534. Text,
  535. /// <summary>
  536. /// The dimension will be computed from the view's <see cref="View.Subviews"/>.
  537. /// </summary>
  538. Subviews,
  539. }
  540. internal class DimAuto : Dim {
  541. internal readonly Dim _min;
  542. internal readonly Dim _max;
  543. internal readonly DimAutoStyle _style;
  544. public DimAuto (DimAutoStyle style, Dim min, Dim max)
  545. {
  546. _min = min;
  547. _max = max;
  548. _style = style;
  549. }
  550. public override string ToString () => $"Auto({_style},{_min},{_max})";
  551. public override int GetHashCode () => HashCode.Combine (base.GetHashCode (), _min, _max, _style);
  552. public override bool Equals (object other) => other is DimAuto auto && (auto._min == _min && auto._max == _max && auto._style == _style);
  553. }
  554. /// <summary>
  555. /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
  556. /// </summary>
  557. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  558. /// <param name="n">The value to convert to the pos.</param>
  559. public static implicit operator Dim (int n) => new DimAbsolute (n);
  560. /// <summary>
  561. /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
  562. /// </summary>
  563. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  564. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  565. public static Dim Sized (int n) => new DimAbsolute (n);
  566. internal class DimCombine : Dim {
  567. internal Dim _left, _right;
  568. internal bool _add;
  569. public DimCombine (bool add, Dim left, Dim right)
  570. {
  571. _left = left;
  572. _right = right;
  573. _add = add;
  574. }
  575. internal override int Anchor (int width)
  576. {
  577. int la = _left.Anchor (width);
  578. int ra = _right.Anchor (width);
  579. if (_add) {
  580. return la + ra;
  581. } else {
  582. return la - ra;
  583. }
  584. }
  585. public override string ToString () => $"Combine({_left}{(_add ? '+' : '-')}{_right})";
  586. }
  587. /// <summary>
  588. /// Adds a <see cref="Terminal.Gui.Dim"/> to a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
  589. /// </summary>
  590. /// <param name="left">The first <see cref="Terminal.Gui.Dim"/> to add.</param>
  591. /// <param name="right">The second <see cref="Terminal.Gui.Dim"/> to add.</param>
  592. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  593. public static Dim operator + (Dim left, Dim right)
  594. {
  595. if (left is DimAbsolute && right is DimAbsolute) {
  596. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  597. }
  598. var newDim = new DimCombine (true, left, right);
  599. SetDimCombine (left, newDim);
  600. return newDim;
  601. }
  602. /// <summary>
  603. /// Subtracts a <see cref="Terminal.Gui.Dim"/> from a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
  604. /// </summary>
  605. /// <param name="left">The <see cref="Terminal.Gui.Dim"/> to subtract from (the minuend).</param>
  606. /// <param name="right">The <see cref="Terminal.Gui.Dim"/> to subtract (the subtrahend).</param>
  607. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  608. public static Dim operator - (Dim left, Dim right)
  609. {
  610. if (left is DimAbsolute && right is DimAbsolute) {
  611. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  612. }
  613. var newDim = new DimCombine (false, left, right);
  614. SetDimCombine (left, newDim);
  615. return newDim;
  616. }
  617. // BUGBUG: newPos is never used.
  618. static void SetDimCombine (Dim left, DimCombine newPos) => (left as DimView)?.Target.SetNeedsLayout ();
  619. internal class DimView : Dim {
  620. public View Target { get; init; }
  621. readonly int _side;
  622. public DimView (View view, int side)
  623. {
  624. Target = view;
  625. _side = side;
  626. }
  627. internal override int Anchor (int width) => _side switch {
  628. 0 => Target.Frame.Height,
  629. 1 => Target.Frame.Width,
  630. _ => 0
  631. };
  632. public override string ToString ()
  633. {
  634. if (Target == null) {
  635. throw new NullReferenceException ();
  636. }
  637. string tside = _side switch {
  638. 0 => "Height",
  639. 1 => "Width",
  640. _ => "unknown"
  641. };
  642. return $"View({tside},{Target})";
  643. }
  644. public override int GetHashCode () => Target.GetHashCode ();
  645. public override bool Equals (object other) => other is DimView abs && abs.Target == Target;
  646. }
  647. /// <summary>
  648. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.
  649. /// </summary>
  650. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  651. /// <param name="view">The view that will be tracked.</param>
  652. public static Dim Width (View view) => new DimView (view, 1);
  653. /// <summary>
  654. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.
  655. /// </summary>
  656. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  657. /// <param name="view">The view that will be tracked.</param>
  658. public static Dim Height (View view) => new DimView (view, 0);
  659. /// <summary>Serves as the default hash function. </summary>
  660. /// <returns>A hash code for the current object.</returns>
  661. public override int GetHashCode () => Anchor (0).GetHashCode ();
  662. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  663. /// <param name="other">The object to compare with the current object. </param>
  664. /// <returns>
  665. /// <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />.</returns>
  666. public override bool Equals (object other) => other is Dim abs && abs == this;
  667. }