PosDim.cs 34 KB

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