PosDim.cs 22 KB

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