PosDim.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 $"Pos.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 $"Pos.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 $"Pos.AnchorEnd(margin={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 "Pos.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 $"Pos.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. 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 $"Pos.Combine({left.ToString ()}{(add ? '+' : '-')}{right.ToString ()})";
  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. return $"Pos.View(side={tside}, target={Target.ToString ()})";
  293. }
  294. public override int GetHashCode () => Target.GetHashCode ();
  295. public override bool Equals (object other) => other is PosView abs && abs.Target == Target;
  296. }
  297. /// <summary>
  298. /// Returns a <see cref="Pos"/> object tracks the Left (X) position of the specified <see cref="View"/>.
  299. /// </summary>
  300. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  301. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  302. public static Pos Left (View view) => new PosCombine (true, new PosView (view, 0), new Pos.PosAbsolute (0));
  303. /// <summary>
  304. /// Returns a <see cref="Pos"/> object tracks the Left (X) position of the specified <see cref="View"/>.
  305. /// </summary>
  306. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  307. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  308. public static Pos X (View view) => new PosCombine (true, new PosView (view, 0), new Pos.PosAbsolute (0));
  309. /// <summary>
  310. /// Returns a <see cref="Pos"/> object tracks the Top (Y) position of the specified <see cref="View"/>.
  311. /// </summary>
  312. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  313. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  314. public static Pos Top (View view) => new PosCombine (true, new PosView (view, 1), new Pos.PosAbsolute (0));
  315. /// <summary>
  316. /// Returns a <see cref="Pos"/> object tracks the Top (Y) position of the specified <see cref="View"/>.
  317. /// </summary>
  318. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  319. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  320. public static Pos Y (View view) => new PosCombine (true, new PosView (view, 1), new Pos.PosAbsolute (0));
  321. /// <summary>
  322. /// Returns a <see cref="Pos"/> object tracks the Right (X+Width) coordinate of the specified <see cref="View"/>.
  323. /// </summary>
  324. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  325. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  326. public static Pos Right (View view) => new PosCombine (true, new PosView (view, 2), new Pos.PosAbsolute (0));
  327. /// <summary>
  328. /// Returns a <see cref="Pos"/> object tracks the Bottom (Y+Height) coordinate of the specified <see cref="View"/>
  329. /// </summary>
  330. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  331. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  332. public static Pos Bottom (View view) => new PosCombine (true, new PosView (view, 3), new Pos.PosAbsolute (0));
  333. /// <summary>Serves as the default hash function. </summary>
  334. /// <returns>A hash code for the current object.</returns>
  335. public override int GetHashCode () => Anchor (0).GetHashCode ();
  336. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  337. /// <param name="other">The object to compare with the current object. </param>
  338. /// <returns>
  339. /// <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />.</returns>
  340. public override bool Equals (object other) => other is Pos abs && abs == this;
  341. }
  342. /// <summary>
  343. /// Dim properties of a <see cref="View"/> to control the position.
  344. /// </summary>
  345. /// <remarks>
  346. /// <para>
  347. /// Use the Dim objects on the Width or Height properties of a <see cref="View"/> to control the position.
  348. /// </para>
  349. /// <para>
  350. /// These can be used to set the absolute position, when merely assigning an
  351. /// integer value (via the implicit integer to Pos conversion), and they can be combined
  352. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the position
  353. /// of the <see cref="View"/> 3 characters to the left after centering for example.
  354. /// </para>
  355. /// </remarks>
  356. public class Dim {
  357. internal virtual int Anchor (int width)
  358. {
  359. return 0;
  360. }
  361. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  362. internal class DimFunc : Dim {
  363. Func<int> function;
  364. public DimFunc (Func<int> n)
  365. {
  366. this.function = n;
  367. }
  368. internal override int Anchor (int width)
  369. {
  370. return function ();
  371. }
  372. public override string ToString ()
  373. {
  374. return $"Dim.DimFunc({function ()})";
  375. }
  376. public override int GetHashCode () => function.GetHashCode ();
  377. public override bool Equals (object other) => other is DimFunc f && f.function () == function ();
  378. }
  379. /// <summary>
  380. /// Creates a "DimFunc" from the specified function.
  381. /// </summary>
  382. /// <param name="function">The function to be executed.</param>
  383. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  384. public static Dim Function (Func<int> function)
  385. {
  386. return new DimFunc (function);
  387. }
  388. internal class DimFactor : Dim {
  389. float factor;
  390. bool remaining;
  391. public DimFactor (float n, bool r = false)
  392. {
  393. factor = n;
  394. remaining = r;
  395. }
  396. internal override int Anchor (int width)
  397. {
  398. return (int)(width * factor);
  399. }
  400. public bool IsFromRemaining ()
  401. {
  402. return remaining;
  403. }
  404. public override string ToString ()
  405. {
  406. return $"Dim.Factor(factor={factor}, remaining={remaining})";
  407. }
  408. public override int GetHashCode () => factor.GetHashCode ();
  409. public override bool Equals (object other) => other is DimFactor f && f.factor == factor && f.remaining == remaining;
  410. }
  411. /// <summary>
  412. /// Creates a percentage <see cref="Dim"/> object
  413. /// </summary>
  414. /// <returns>The percent <see cref="Dim"/> object.</returns>
  415. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  416. /// <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>
  417. /// <example>
  418. /// This initializes a <see cref="TextField"/>that is centered horizontally, is 50% of the way down,
  419. /// is 30% the height, and is 80% the width of the <see cref="View"/> it added to.
  420. /// <code>
  421. /// var textView = new TextView () {
  422. /// X = Pos.Center (),
  423. /// Y = Pos.Percent (50),
  424. /// Width = Dim.Percent (80),
  425. /// Height = Dim.Percent (30),
  426. /// };
  427. /// </code>
  428. /// </example>
  429. public static Dim Percent (float n, bool r = false)
  430. {
  431. if (n < 0 || n > 100)
  432. throw new ArgumentException ("Percent value must be between 0 and 100");
  433. return new DimFactor (n / 100, r);
  434. }
  435. internal class DimAbsolute : Dim {
  436. int n;
  437. public DimAbsolute (int n) { this.n = n; }
  438. public override string ToString ()
  439. {
  440. return $"Dim.Absolute({n})";
  441. }
  442. internal override int Anchor (int width)
  443. {
  444. return n;
  445. }
  446. public override int GetHashCode () => n.GetHashCode ();
  447. public override bool Equals (object other) => other is DimAbsolute abs && abs.n == n;
  448. }
  449. internal class DimFill : Dim {
  450. int margin;
  451. public DimFill (int margin) { this.margin = margin; }
  452. public override string ToString ()
  453. {
  454. return $"Dim.Fill(margin={margin})";
  455. }
  456. internal override int Anchor (int width)
  457. {
  458. return width - margin;
  459. }
  460. public override int GetHashCode () => margin.GetHashCode ();
  461. public override bool Equals (object other) => other is DimFill fill && fill.margin == margin;
  462. }
  463. /// <summary>
  464. /// Initializes a new instance of the <see cref="Dim"/> class that fills the dimension, but leaves the specified number of colums for a margin.
  465. /// </summary>
  466. /// <returns>The Fill dimension.</returns>
  467. /// <param name="margin">Margin to use.</param>
  468. public static Dim Fill (int margin = 0)
  469. {
  470. return new DimFill (margin);
  471. }
  472. /// <summary>
  473. /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
  474. /// </summary>
  475. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  476. /// <param name="n">The value to convert to the pos.</param>
  477. public static implicit operator Dim (int n)
  478. {
  479. return new DimAbsolute (n);
  480. }
  481. /// <summary>
  482. /// Creates an Absolute <see cref="Dim"/> from the specified integer value.
  483. /// </summary>
  484. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  485. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  486. public static Dim Sized (int n)
  487. {
  488. return new DimAbsolute (n);
  489. }
  490. internal class DimCombine : Dim {
  491. internal Dim left, right;
  492. internal bool add;
  493. public DimCombine (bool add, Dim left, Dim right)
  494. {
  495. this.left = left;
  496. this.right = right;
  497. this.add = add;
  498. }
  499. internal override int Anchor (int width)
  500. {
  501. var la = left.Anchor (width);
  502. var ra = right.Anchor (width);
  503. if (add)
  504. return la + ra;
  505. else
  506. return la - ra;
  507. }
  508. public override string ToString ()
  509. {
  510. return $"Dim.Combine({left.ToString ()}{(add ? '+' : '-')}{right.ToString ()})";
  511. }
  512. }
  513. /// <summary>
  514. /// Adds a <see cref="Terminal.Gui.Dim"/> to a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
  515. /// </summary>
  516. /// <param name="left">The first <see cref="Terminal.Gui.Dim"/> to add.</param>
  517. /// <param name="right">The second <see cref="Terminal.Gui.Dim"/> to add.</param>
  518. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  519. public static Dim operator + (Dim left, Dim right)
  520. {
  521. if (left is DimAbsolute && right is DimAbsolute) {
  522. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  523. }
  524. DimCombine newDim = new DimCombine (true, left, right);
  525. SetDimCombine (left, newDim);
  526. return newDim;
  527. }
  528. /// <summary>
  529. /// Subtracts a <see cref="Terminal.Gui.Dim"/> from a <see cref="Terminal.Gui.Dim"/>, yielding a new <see cref="Dim"/>.
  530. /// </summary>
  531. /// <param name="left">The <see cref="Terminal.Gui.Dim"/> to subtract from (the minuend).</param>
  532. /// <param name="right">The <see cref="Terminal.Gui.Dim"/> to subtract (the subtrahend).</param>
  533. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  534. public static Dim operator - (Dim left, Dim right)
  535. {
  536. if (left is DimAbsolute && right is DimAbsolute) {
  537. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  538. }
  539. DimCombine newDim = new DimCombine (false, left, right);
  540. SetDimCombine (left, newDim);
  541. return newDim;
  542. }
  543. static void SetDimCombine (Dim left, DimCombine newPos)
  544. {
  545. var view = left as DimView;
  546. if (view != null) {
  547. view.Target.SetNeedsLayout ();
  548. }
  549. }
  550. internal class DimView : Dim {
  551. public View Target;
  552. int side;
  553. public DimView (View view, int side)
  554. {
  555. Target = view;
  556. this.side = side;
  557. }
  558. internal override int Anchor (int width)
  559. {
  560. switch (side) {
  561. case 0: return Target.Frame.Height;
  562. case 1: return Target.Frame.Width;
  563. default:
  564. return 0;
  565. }
  566. }
  567. public override string ToString ()
  568. {
  569. string tside;
  570. switch (side) {
  571. case 0: tside = "Height"; break;
  572. case 1: tside = "Width"; break;
  573. default: tside = "unknown"; break;
  574. }
  575. return $"DimView(side={tside}, target={Target.ToString ()})";
  576. }
  577. public override int GetHashCode () => Target.GetHashCode ();
  578. public override bool Equals (object other) => other is DimView abs && abs.Target == Target;
  579. }
  580. /// <summary>
  581. /// Returns a <see cref="Dim"/> object tracks the Width of the specified <see cref="View"/>.
  582. /// </summary>
  583. /// <returns>The <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  584. /// <param name="view">The view that will be tracked.</param>
  585. public static Dim Width (View view) => new DimView (view, 1);
  586. /// <summary>
  587. /// Returns a <see cref="Dim"/> object tracks the Height of the specified <see cref="View"/>.
  588. /// </summary>
  589. /// <returns>The <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  590. /// <param name="view">The view that will be tracked.</param>
  591. public static Dim Height (View view) => new DimView (view, 0);
  592. /// <summary>Serves as the default hash function. </summary>
  593. /// <returns>A hash code for the current object.</returns>
  594. public override int GetHashCode () => Anchor (0).GetHashCode ();
  595. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  596. /// <param name="other">The object to compare with the current object. </param>
  597. /// <returns>
  598. /// <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" />.</returns>
  599. public override bool Equals (object other) => other is Dim abs && abs == this;
  600. }
  601. }