PosDim.cs 22 KB

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