PosDim.cs 20 KB

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