PosDim.cs 28 KB

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