PosDim.cs 22 KB

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