PosDim.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 a position 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 Pos. These objects are created using the static methods Percent,
  13. /// AnchorEnd and Center. The Pos objects can be combined with the addition and
  14. /// subtraction operators.
  15. /// </summary>
  16. /// <remarks>
  17. /// <para>
  18. /// Use the 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 Pos conversion), and they can be combined
  23. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the postion
  24. /// of the 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. 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 Pos object
  54. /// </summary>
  55. /// <returns>The percent Pos object.</returns>
  56. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  57. public static Pos Percent (float n)
  58. {
  59. if (n < 0 || n > 100)
  60. throw new ArgumentException ("Percent value must be between 0 and 100");
  61. return new PosFactor (n / 100);
  62. }
  63. static PosAnchorEnd endNoMargin;
  64. class PosAnchorEnd : Pos {
  65. int n;
  66. public PosAnchorEnd (int n)
  67. {
  68. this.n = n;
  69. }
  70. internal override int Anchor (int width)
  71. {
  72. return width - n;
  73. }
  74. public override string ToString ()
  75. {
  76. return $"Pos.AnchorEnd(margin={n})";
  77. }
  78. }
  79. /// <summary>
  80. /// Creates a Pos object that is anchored to the end of the dimension, useful to flush
  81. /// the layout from the end.
  82. /// </summary>
  83. /// <returns>The Pos object anchored to the end (the bottom or the right side).</returns>
  84. /// <param name="margin">Optional margin to set aside.</param>
  85. public static Pos AnchorEnd (int margin = 0)
  86. {
  87. if (margin < 0)
  88. throw new ArgumentException ("Margin must be positive");
  89. if (margin == 0) {
  90. if (endNoMargin == null)
  91. endNoMargin = new PosAnchorEnd (0);
  92. return endNoMargin;
  93. }
  94. return new PosAnchorEnd (margin);
  95. }
  96. internal class PosCenter : Pos {
  97. internal override int Anchor (int width)
  98. {
  99. return width / 2;
  100. }
  101. public override string ToString ()
  102. {
  103. return "Pos.Center";
  104. }
  105. }
  106. static PosCenter pCenter;
  107. /// <summary>
  108. /// Returns a Pos object that can be used to center the views.
  109. /// </summary>
  110. /// <returns>The center Pos.</returns>
  111. public static Pos Center ()
  112. {
  113. if (pCenter == null)
  114. pCenter = new PosCenter ();
  115. return pCenter;
  116. }
  117. class PosAbsolute : Pos {
  118. int n;
  119. public PosAbsolute (int n) { this.n = n; }
  120. public override string ToString ()
  121. {
  122. return $"Pos.Absolute({n})";
  123. }
  124. internal override int Anchor (int width)
  125. {
  126. return n;
  127. }
  128. }
  129. /// <summary>
  130. /// Creates an Absolute Pos from the specified integer value.
  131. /// </summary>
  132. /// <returns>The Absolute Pos.</returns>
  133. /// <param name="n">The value to convert to the pos.</param>
  134. public static implicit operator Pos (int n)
  135. {
  136. return new PosAbsolute (n);
  137. }
  138. /// <summary>
  139. /// Creates an Absolute Pos from the specified integer value.
  140. /// </summary>
  141. /// <returns>The Absolute Pos.</returns>
  142. /// <param name="n">The value to convert to the pos.</param>
  143. public static Pos At (int n)
  144. {
  145. return new PosAbsolute (n);
  146. }
  147. class PosCombine : Pos {
  148. Pos left, right;
  149. bool add;
  150. public PosCombine (bool add, Pos left, Pos right)
  151. {
  152. this.left = left;
  153. this.right = right;
  154. this.add = add;
  155. }
  156. internal override int Anchor (int width)
  157. {
  158. var la = left.Anchor (width);
  159. var ra = right.Anchor (width);
  160. if (add)
  161. return la + ra;
  162. else
  163. return la - ra;
  164. }
  165. public override string ToString ()
  166. {
  167. return $"Pos.Combine ({left.ToString ()}{(add?'+':'-')}{right.ToString ()})";
  168. }
  169. }
  170. static PosCombine posCombine;
  171. /// <summary>
  172. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  173. /// </summary>
  174. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  175. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  176. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  177. public static Pos operator + (Pos left, Pos right)
  178. {
  179. PosCombine newPos = new PosCombine (true, left, right);
  180. if (posCombine?.ToString () != newPos.ToString ()) {
  181. var view = left as PosView;
  182. if (view != null) {
  183. view.Target.SetNeedsLayout ();
  184. }
  185. }
  186. return posCombine = newPos;
  187. }
  188. /// <summary>
  189. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  190. /// </summary>
  191. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  192. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  193. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  194. public static Pos operator - (Pos left, Pos right)
  195. {
  196. PosCombine newPos = new PosCombine (false, left, right);
  197. if (posCombine?.ToString () != newPos.ToString ()) {
  198. var view = left as PosView;
  199. if (view != null)
  200. view.Target.SetNeedsLayout ();
  201. }
  202. return posCombine = newPos;
  203. }
  204. internal class PosView : Pos {
  205. public View Target;
  206. int side;
  207. public PosView (View view, int side)
  208. {
  209. Target = view;
  210. this.side = side;
  211. }
  212. internal override int Anchor (int width)
  213. {
  214. switch (side) {
  215. case 0: return Target.Frame.X;
  216. case 1: return Target.Frame.Y;
  217. case 2: return Target.Frame.Right;
  218. case 3: return Target.Frame.Bottom;
  219. default:
  220. return 0;
  221. }
  222. }
  223. public override string ToString ()
  224. {
  225. string tside;
  226. switch (side) {
  227. case 0: tside = "x"; break;
  228. case 1: tside = "y"; break;
  229. case 2: tside = "right"; break;
  230. case 3: tside = "bottom"; break;
  231. default: tside = "unknown"; break;
  232. }
  233. return $"Pos.View(side={tside}, target={Target.ToString()}";
  234. }
  235. }
  236. /// <summary>
  237. /// Returns a Pos object tracks the Left (X) position of the specified view.
  238. /// </summary>
  239. /// <returns>The Position that depends on the other view.</returns>
  240. /// <param name="view">The view that will be tracked.</param>
  241. public static Pos Left (View view) => new PosView (view, 0);
  242. /// <summary>
  243. /// Returns a Pos object tracks the Left (X) position of the specified view.
  244. /// </summary>
  245. /// <returns>The Position that depends on the other view.</returns>
  246. /// <param name="view">The view that will be tracked.</param>
  247. public static Pos X (View view) => new PosView (view, 0);
  248. /// <summary>
  249. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  250. /// </summary>
  251. /// <returns>The Position that depends on the other view.</returns>
  252. /// <param name="view">The view that will be tracked.</param>
  253. public static Pos Top (View view) => new PosView (view, 1);
  254. /// <summary>
  255. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  256. /// </summary>
  257. /// <returns>The Position that depends on the other view.</returns>
  258. /// <param name="view">The view that will be tracked.</param>
  259. public static Pos Y (View view) => new PosView (view, 1);
  260. /// <summary>
  261. /// Returns a Pos object tracks the Right (X+Width) coordinate of the specified view.
  262. /// </summary>
  263. /// <returns>The Position that depends on the other view.</returns>
  264. /// <param name="view">The view that will be tracked.</param>
  265. public static Pos Right (View view) => new PosView (view, 2);
  266. /// <summary>
  267. /// Returns a Pos object tracks the Bottom (Y+Height) coordinate of the specified view.
  268. /// </summary>
  269. /// <returns>The Position that depends on the other view.</returns>
  270. /// <param name="view">The view that will be tracked.</param>
  271. public static Pos Bottom (View view) => new PosView (view, 3);
  272. }
  273. /// <summary>
  274. /// Dim properties of a view to control the position.
  275. /// </summary>
  276. /// <remarks>
  277. /// <para>
  278. /// Use the Dim objects on the Width or Height properties of a view to control the position.
  279. /// </para>
  280. /// <para>
  281. /// These can be used to set the absolute position, when merely assigning an
  282. /// integer value (via the implicit integer to Pos conversion), and they can be combined
  283. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the postion
  284. /// of the view 3 characters to the left after centering for example.
  285. /// </para>
  286. /// </remarks>
  287. public class Dim {
  288. internal virtual int Anchor (int width)
  289. {
  290. return 0;
  291. }
  292. class DimFactor : Dim {
  293. float factor;
  294. public DimFactor (float n)
  295. {
  296. this.factor = n;
  297. }
  298. internal override int Anchor (int width)
  299. {
  300. return (int)(width * factor);
  301. }
  302. public override string ToString ()
  303. {
  304. return $"Dim.Factor({factor})";
  305. }
  306. public override int GetHashCode () => factor.GetHashCode();
  307. public override bool Equals (object other) => other is DimFactor f && f.factor == factor;
  308. }
  309. /// <summary>
  310. /// Creates a percentage Dim object
  311. /// </summary>
  312. /// <returns>The percent Dim object.</returns>
  313. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  314. public static Dim Percent (float n)
  315. {
  316. if (n < 0 || n > 100)
  317. throw new ArgumentException ("Percent value must be between 0 and 100");
  318. return new DimFactor (n / 100);
  319. }
  320. internal class DimAbsolute : Dim {
  321. int n;
  322. public DimAbsolute (int n) { this.n = n; }
  323. public override string ToString ()
  324. {
  325. return $"Dim.Absolute({n})";
  326. }
  327. internal override int Anchor (int width)
  328. {
  329. return n;
  330. }
  331. public override int GetHashCode () => n.GetHashCode();
  332. public override bool Equals (object other) => other is DimAbsolute abs && abs.n == n;
  333. }
  334. internal class DimFill : Dim {
  335. int margin;
  336. public DimFill (int margin) { this.margin = margin; }
  337. public override string ToString ()
  338. {
  339. return $"Dim.Fill(margin={margin})";
  340. }
  341. internal override int Anchor (int width)
  342. {
  343. return width-margin;
  344. }
  345. public override int GetHashCode () => margin.GetHashCode();
  346. public override bool Equals (object other) => other is DimFill fill && fill.margin == margin;
  347. }
  348. static DimFill zeroMargin;
  349. /// <summary>
  350. /// Creates a Dim object that fills the dimension, but leaves the specified number of colums for a margin.
  351. /// </summary>
  352. /// <returns>The Fill dimension.</returns>
  353. /// <param name="margin">Margin to use.</param>
  354. public static Dim Fill (int margin = 0)
  355. {
  356. if (margin == 0) {
  357. if (zeroMargin == null)
  358. zeroMargin = new DimFill (0);
  359. return zeroMargin;
  360. }
  361. return new DimFill (margin);
  362. }
  363. /// <summary>
  364. /// Creates an Absolute Pos from the specified integer value.
  365. /// </summary>
  366. /// <returns>The Absolute Pos.</returns>
  367. /// <param name="n">The value to convert to the pos.</param>
  368. public static implicit operator Dim (int n)
  369. {
  370. return new DimAbsolute (n);
  371. }
  372. /// <summary>
  373. /// Creates an Absolute Pos from the specified integer value.
  374. /// </summary>
  375. /// <returns>The Absolute Pos.</returns>
  376. /// <param name="n">The value to convert to the pos.</param>
  377. public static Dim Sized (int n)
  378. {
  379. return new DimAbsolute (n);
  380. }
  381. class DimCombine : Dim {
  382. Dim left, right;
  383. bool add;
  384. public DimCombine (bool add, Dim left, Dim right)
  385. {
  386. this.left = left;
  387. this.right = right;
  388. this.add = add;
  389. }
  390. internal override int Anchor (int width)
  391. {
  392. var la = left.Anchor (width);
  393. var ra = right.Anchor (width);
  394. if (add)
  395. return la + ra;
  396. else
  397. return la - ra;
  398. }
  399. }
  400. /// <summary>
  401. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  402. /// </summary>
  403. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  404. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  405. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  406. public static Dim operator + (Dim left, Dim right)
  407. {
  408. return new DimCombine (true, left, right);
  409. }
  410. /// <summary>
  411. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  412. /// </summary>
  413. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  414. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  415. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  416. public static Dim operator - (Dim left, Dim right)
  417. {
  418. return new DimCombine (false, left, right);
  419. }
  420. internal class DimView : Dim {
  421. public View Target;
  422. int side;
  423. public DimView (View view, int side)
  424. {
  425. Target = view;
  426. this.side = side;
  427. }
  428. internal override int Anchor (int width)
  429. {
  430. switch (side) {
  431. case 0: return Target.Frame.Height;
  432. case 1: return Target.Frame.Width;
  433. default:
  434. return 0;
  435. }
  436. }
  437. }
  438. /// <summary>
  439. /// Returns a Dim object tracks the Width of the specified view.
  440. /// </summary>
  441. /// <returns>The dimension of the other view.</returns>
  442. /// <param name="view">The view that will be tracked.</param>
  443. public static Dim Width (View view) => new DimView (view, 1);
  444. /// <summary>
  445. /// Returns a Dim object tracks the Height of the specified view.
  446. /// </summary>
  447. /// <returns>The dimension of the other view.</returns>
  448. /// <param name="view">The view that will be tracked.</param>
  449. public static Dim Height (View view) => new DimView (view, 0);
  450. }
  451. }