PosDim.cs 34 KB

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