PosDim.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. ((PosView)left).Target.SetNeedsLayout ();
  199. return posCombine = newPos;
  200. }
  201. internal class PosView : Pos {
  202. public View Target;
  203. int side;
  204. public PosView (View view, int side)
  205. {
  206. Target = view;
  207. this.side = side;
  208. }
  209. internal override int Anchor (int width)
  210. {
  211. switch (side) {
  212. case 0: return Target.Frame.X;
  213. case 1: return Target.Frame.Y;
  214. case 2: return Target.Frame.Right;
  215. case 3: return Target.Frame.Bottom;
  216. default:
  217. return 0;
  218. }
  219. }
  220. public override string ToString ()
  221. {
  222. string tside;
  223. switch (side) {
  224. case 0: tside = "x"; break;
  225. case 1: tside = "y"; break;
  226. case 2: tside = "right"; break;
  227. case 3: tside = "bottom"; break;
  228. default: tside = "unknown"; break;
  229. }
  230. return $"Pos.View(side={tside}, target={Target.ToString()}";
  231. }
  232. }
  233. /// <summary>
  234. /// Returns a Pos object tracks the Left (X) position of the specified view.
  235. /// </summary>
  236. /// <returns>The Position that depends on the other view.</returns>
  237. /// <param name="view">The view that will be tracked.</param>
  238. public static Pos Left (View view) => new PosView (view, 0);
  239. /// <summary>
  240. /// Returns a Pos object tracks the Left (X) position of the specified view.
  241. /// </summary>
  242. /// <returns>The Position that depends on the other view.</returns>
  243. /// <param name="view">The view that will be tracked.</param>
  244. public static Pos X (View view) => new PosView (view, 0);
  245. /// <summary>
  246. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  247. /// </summary>
  248. /// <returns>The Position that depends on the other view.</returns>
  249. /// <param name="view">The view that will be tracked.</param>
  250. public static Pos Top (View view) => new PosView (view, 1);
  251. /// <summary>
  252. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  253. /// </summary>
  254. /// <returns>The Position that depends on the other view.</returns>
  255. /// <param name="view">The view that will be tracked.</param>
  256. public static Pos Y (View view) => new PosView (view, 1);
  257. /// <summary>
  258. /// Returns a Pos object tracks the Right (X+Width) coordinate of the specified view.
  259. /// </summary>
  260. /// <returns>The Position that depends on the other view.</returns>
  261. /// <param name="view">The view that will be tracked.</param>
  262. public static Pos Right (View view) => new PosView (view, 2);
  263. /// <summary>
  264. /// Returns a Pos object tracks the Bottom (Y+Height) coordinate of the specified view.
  265. /// </summary>
  266. /// <returns>The Position that depends on the other view.</returns>
  267. /// <param name="view">The view that will be tracked.</param>
  268. public static Pos Bottom (View view) => new PosView (view, 3);
  269. }
  270. /// <summary>
  271. /// Dim properties of a view to control the position.
  272. /// </summary>
  273. /// <remarks>
  274. /// <para>
  275. /// Use the Dim objects on the Width or Height properties of a view to control the position.
  276. /// </para>
  277. /// <para>
  278. /// These can be used to set the absolute position, when merely assigning an
  279. /// integer value (via the implicit integer to Pos conversion), and they can be combined
  280. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the postion
  281. /// of the view 3 characters to the left after centering for example.
  282. /// </para>
  283. /// </remarks>
  284. public class Dim {
  285. internal virtual int Anchor (int width)
  286. {
  287. return 0;
  288. }
  289. class DimFactor : Dim {
  290. float factor;
  291. public DimFactor (float n)
  292. {
  293. this.factor = n;
  294. }
  295. internal override int Anchor (int width)
  296. {
  297. return (int)(width * factor);
  298. }
  299. public override string ToString ()
  300. {
  301. return $"Dim.Factor({factor})";
  302. }
  303. public override int GetHashCode () => factor.GetHashCode();
  304. public override bool Equals (object other) => other is DimFactor f && f.factor == factor;
  305. }
  306. /// <summary>
  307. /// Creates a percentage Dim object
  308. /// </summary>
  309. /// <returns>The percent Dim object.</returns>
  310. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  311. public static Dim Percent (float n)
  312. {
  313. if (n < 0 || n > 100)
  314. throw new ArgumentException ("Percent value must be between 0 and 100");
  315. return new DimFactor (n / 100);
  316. }
  317. internal class DimAbsolute : Dim {
  318. int n;
  319. public DimAbsolute (int n) { this.n = n; }
  320. public override string ToString ()
  321. {
  322. return $"Dim.Absolute({n})";
  323. }
  324. internal override int Anchor (int width)
  325. {
  326. return n;
  327. }
  328. public override int GetHashCode () => n.GetHashCode();
  329. public override bool Equals (object other) => other is DimAbsolute abs && abs.n == n;
  330. }
  331. internal class DimFill : Dim {
  332. int margin;
  333. public DimFill (int margin) { this.margin = margin; }
  334. public override string ToString ()
  335. {
  336. return $"Dim.Fill(margin={margin})";
  337. }
  338. internal override int Anchor (int width)
  339. {
  340. return width-margin;
  341. }
  342. public override int GetHashCode () => margin.GetHashCode();
  343. public override bool Equals (object other) => other is DimFill fill && fill.margin == margin;
  344. }
  345. static DimFill zeroMargin;
  346. /// <summary>
  347. /// Creates a Dim object that fills the dimension, but leaves the specified number of colums for a margin.
  348. /// </summary>
  349. /// <returns>The Fill dimension.</returns>
  350. /// <param name="margin">Margin to use.</param>
  351. public static Dim Fill (int margin = 0)
  352. {
  353. if (margin == 0) {
  354. if (zeroMargin == null)
  355. zeroMargin = new DimFill (0);
  356. return zeroMargin;
  357. }
  358. return new DimFill (margin);
  359. }
  360. /// <summary>
  361. /// Creates an Absolute Pos from the specified integer value.
  362. /// </summary>
  363. /// <returns>The Absolute Pos.</returns>
  364. /// <param name="n">The value to convert to the pos.</param>
  365. public static implicit operator Dim (int n)
  366. {
  367. return new DimAbsolute (n);
  368. }
  369. /// <summary>
  370. /// Creates an Absolute Pos from the specified integer value.
  371. /// </summary>
  372. /// <returns>The Absolute Pos.</returns>
  373. /// <param name="n">The value to convert to the pos.</param>
  374. public static Dim Sized (int n)
  375. {
  376. return new DimAbsolute (n);
  377. }
  378. class DimCombine : Dim {
  379. Dim left, right;
  380. bool add;
  381. public DimCombine (bool add, Dim left, Dim right)
  382. {
  383. this.left = left;
  384. this.right = right;
  385. this.add = add;
  386. }
  387. internal override int Anchor (int width)
  388. {
  389. var la = left.Anchor (width);
  390. var ra = right.Anchor (width);
  391. if (add)
  392. return la + ra;
  393. else
  394. return la - ra;
  395. }
  396. }
  397. /// <summary>
  398. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  399. /// </summary>
  400. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  401. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  402. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  403. public static Dim operator + (Dim left, Dim right)
  404. {
  405. return new DimCombine (true, left, right);
  406. }
  407. /// <summary>
  408. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  409. /// </summary>
  410. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  411. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  412. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  413. public static Dim operator - (Dim left, Dim right)
  414. {
  415. return new DimCombine (false, left, right);
  416. }
  417. internal class DimView : Dim {
  418. public View Target;
  419. int side;
  420. public DimView (View view, int side)
  421. {
  422. Target = view;
  423. this.side = side;
  424. }
  425. internal override int Anchor (int width)
  426. {
  427. switch (side) {
  428. case 0: return Target.Frame.Height;
  429. case 1: return Target.Frame.Width;
  430. default:
  431. return 0;
  432. }
  433. }
  434. }
  435. /// <summary>
  436. /// Returns a Dim object tracks the Width of the specified view.
  437. /// </summary>
  438. /// <returns>The dimension of the other view.</returns>
  439. /// <param name="view">The view that will be tracked.</param>
  440. public static Dim Width (View view) => new DimView (view, 1);
  441. /// <summary>
  442. /// Returns a Dim object tracks the Height of the specified view.
  443. /// </summary>
  444. /// <returns>The dimension of the other view.</returns>
  445. /// <param name="view">The view that will be tracked.</param>
  446. public static Dim Height (View view) => new DimView (view, 0);
  447. }
  448. }