2
0

PosDim.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Describes the position of a <see cref="View"/> which can be an absolute value, a percentage, centered, or
  4. /// relative to the ending dimension. Integer values are implicitly convertible to an absolute <see cref="Pos"/>. These
  5. /// objects are created using the static methods Percent, AnchorEnd, and Center. The <see cref="Pos"/> objects can be
  6. /// combined with the addition and subtraction operators.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>Use the <see cref="Pos"/> objects on the X or Y properties of a view to control the position.</para>
  10. /// <para>
  11. /// These can be used to set the absolute position, when merely assigning an integer value (via the implicit
  12. /// integer to <see cref="Pos"/> conversion), and they can be combined to produce more useful layouts, like:
  13. /// Pos.Center - 3, which would shift the position of the <see cref="View"/> 3 characters to the left after
  14. /// centering for example.
  15. /// </para>
  16. /// <para>
  17. /// Reference coordinates of another view by using the methods Left(View), Right(View), Bottom(View), Top(View).
  18. /// The X(View) and Y(View) are aliases to Left(View) and Top(View) respectively.
  19. /// </para>
  20. /// <para>
  21. /// <list type="table">
  22. /// <listheader>
  23. /// <term>Pos Object</term> <description>Description</description>
  24. /// </listheader>
  25. /// <item>
  26. /// <term>
  27. /// <see cref="Pos.Function(Func{int})"/>
  28. /// </term>
  29. /// <description>
  30. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided
  31. /// function. The function will be called every time the position is needed.
  32. /// </description>
  33. /// </item>
  34. /// <item>
  35. /// <term>
  36. /// <see cref="Pos.Percent(float)"/>
  37. /// </term>
  38. /// <description>
  39. /// Creates a <see cref="Pos"/> object that is a percentage of the width or height of the
  40. /// SuperView.
  41. /// </description>
  42. /// </item>
  43. /// <item>
  44. /// <term>
  45. /// <see cref="Pos.AnchorEnd(int)"/>
  46. /// </term>
  47. /// <description>
  48. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of
  49. /// the dimension, useful to flush the layout from the right or bottom.
  50. /// </description>
  51. /// </item>
  52. /// <item>
  53. /// <term>
  54. /// <see cref="Pos.Center"/>
  55. /// </term>
  56. /// <description>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</description>
  57. /// </item>
  58. /// <item>
  59. /// <term>
  60. /// <see cref="Pos.At(int)"/>
  61. /// </term>
  62. /// <description>
  63. /// Creates a <see cref="Pos"/> object that is an absolute position based on the specified
  64. /// integer value.
  65. /// </description>
  66. /// </item>
  67. /// <item>
  68. /// <term>
  69. /// <see cref="Pos.Left"/>
  70. /// </term>
  71. /// <description>
  72. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  73. /// <see cref="View"/>.
  74. /// </description>
  75. /// </item>
  76. /// <item>
  77. /// <term>
  78. /// <see cref="Pos.X(View)"/>
  79. /// </term>
  80. /// <description>
  81. /// Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified
  82. /// <see cref="View"/>.
  83. /// </description>
  84. /// </item>
  85. /// <item>
  86. /// <term>
  87. /// <see cref="Pos.Top(View)"/>
  88. /// </term>
  89. /// <description>
  90. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  91. /// <see cref="View"/>.
  92. /// </description>
  93. /// </item>
  94. /// <item>
  95. /// <term>
  96. /// <see cref="Pos.Y(View)"/>
  97. /// </term>
  98. /// <description>
  99. /// Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified
  100. /// <see cref="View"/>.
  101. /// </description>
  102. /// </item>
  103. /// <item>
  104. /// <term>
  105. /// <see cref="Pos.Right(View)"/>
  106. /// </term>
  107. /// <description>
  108. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the
  109. /// specified <see cref="View"/>.
  110. /// </description>
  111. /// </item>
  112. /// <item>
  113. /// <term>
  114. /// <see cref="Pos.Bottom(View)"/>
  115. /// </term>
  116. /// <description>
  117. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the
  118. /// specified <see cref="View"/>
  119. /// </description>
  120. /// </item>
  121. /// </list>
  122. /// </para>
  123. /// </remarks>
  124. public class Pos
  125. {
  126. /// <summary>
  127. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or
  128. /// bottom) of the SuperView, minus the respective dimension of the View. This is equivalent to using
  129. /// <see cref="Pos.AnchorEnd(int)"/>,
  130. /// with an offset equivalent to the View's respective dimension.
  131. /// </summary>
  132. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side) minus the View's dimension.</returns>
  133. /// <example>
  134. /// This sample shows how align a <see cref="Button"/> to the bottom-right the SuperView.
  135. /// <code>
  136. /// anchorButton.X = Pos.AnchorEnd ();
  137. /// anchorButton.Y = Pos.AnchorEnd ();
  138. /// </code>
  139. /// </example>
  140. public static Pos AnchorEnd () { return new PosAnchorEnd (); }
  141. /// <summary>
  142. /// Creates a <see cref="Pos"/> object that is anchored to the end (right side or bottom) of the SuperView,
  143. /// useful to flush the layout from the right or bottom.
  144. /// </summary>
  145. /// <returns>The <see cref="Pos"/> object anchored to the end (the bottom or the right side).</returns>
  146. /// <param name="offset">The view will be shifted left or up by the amount specified.</param>
  147. /// <example>
  148. /// This sample shows how align a 10 column wide <see cref="Button"/> to the bottom-right the SuperView.
  149. /// <code>
  150. /// anchorButton.X = Pos.AnchorEnd (10);
  151. /// anchorButton.Y = 1
  152. /// </code>
  153. /// </example>
  154. public static Pos AnchorEnd (int offset)
  155. {
  156. if (offset < 0)
  157. {
  158. throw new ArgumentException (@"Must be positive", nameof (offset));
  159. }
  160. return new PosAnchorEnd (offset);
  161. }
  162. /// <summary>Creates a <see cref="Pos"/> object that is an absolute position based on the specified integer value.</summary>
  163. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  164. /// <param name="n">The value to convert to the <see cref="Pos"/>.</param>
  165. public static Pos At (int n) { return new PosAbsolute (n); }
  166. /// <summary>
  167. /// Creates a <see cref="Pos"/> object that tracks the Bottom (Y+Height) coordinate of the specified
  168. /// <see cref="View"/>
  169. /// </summary>
  170. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  171. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  172. public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); }
  173. /// <summary>Creates a <see cref="Pos"/> object that can be used to center the <see cref="View"/>.</summary>
  174. /// <returns>The center Pos.</returns>
  175. /// <example>
  176. /// This creates a <see cref="TextView"/> centered horizontally, is 50% of the way down, is 30% the height, and
  177. /// is 80% the width of the <see cref="View"/> it added to.
  178. /// <code>
  179. /// var textView = new TextView () {
  180. /// X = Pos.Center (),
  181. /// Y = Pos.Percent (50),
  182. /// Width = Dim.Percent (80),
  183. /// Height = Dim.Percent (30),
  184. /// };
  185. /// </code>
  186. /// </example>
  187. public static Pos Center () { return new PosCenter (); }
  188. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  189. /// <param name="other">The object to compare with the current object. </param>
  190. /// <returns>
  191. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  192. /// <see langword="false"/>.
  193. /// </returns>
  194. public override bool Equals (object other) { return other is Pos abs && abs == this; }
  195. /// <summary>
  196. /// Creates a <see cref="Pos"/> object that computes the position by executing the provided function. The function
  197. /// will be called every time the position is needed.
  198. /// </summary>
  199. /// <param name="function">The function to be executed.</param>
  200. /// <returns>The <see cref="Pos"/> returned from the function.</returns>
  201. public static Pos Function (Func<int> function) { return new PosFunc (function); }
  202. /// <summary>Serves as the default hash function. </summary>
  203. /// <returns>A hash code for the current object.</returns>
  204. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  205. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  206. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  207. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  208. public static Pos Left (View view) { return new PosView (view, Side.X); }
  209. /// <summary>Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="Pos"/>.</summary>
  210. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  211. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  212. /// <returns>The <see cref="Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  213. public static Pos operator + (Pos left, Pos right)
  214. {
  215. if (left is PosAbsolute && right is PosAbsolute)
  216. {
  217. return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
  218. }
  219. var newPos = new PosCombine (true, left, right);
  220. if (left is PosView view)
  221. {
  222. view.Target.SetNeedsLayout ();
  223. }
  224. return newPos;
  225. }
  226. /// <summary>Creates an Absolute <see cref="Pos"/> from the specified integer value.</summary>
  227. /// <returns>The Absolute <see cref="Pos"/>.</returns>
  228. /// <param name="n">The value to convert to the <see cref="Pos"/> .</param>
  229. public static implicit operator Pos (int n) { return new PosAbsolute (n); }
  230. /// <summary>
  231. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new
  232. /// <see cref="Pos"/>.
  233. /// </summary>
  234. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  235. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  236. /// <returns>The <see cref="Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  237. public static Pos operator - (Pos left, Pos right)
  238. {
  239. if (left is PosAbsolute && right is PosAbsolute)
  240. {
  241. return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
  242. }
  243. var newPos = new PosCombine (false, left, right);
  244. if (left is PosView view)
  245. {
  246. view.Target.SetNeedsLayout ();
  247. }
  248. return newPos;
  249. }
  250. /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
  251. /// <returns>The percent <see cref="Pos"/> object.</returns>
  252. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  253. /// <example>
  254. /// This creates a <see cref="TextField"/> centered horizontally, is 50% of the way down, is 30% the height, and
  255. /// is 80% the width of the <see cref="View"/> it added to.
  256. /// <code>
  257. /// var textView = new TextField {
  258. /// X = Pos.Center (),
  259. /// Y = Pos.Percent (50),
  260. /// Width = Dim.Percent (80),
  261. /// Height = Dim.Percent (30),
  262. /// };
  263. /// </code>
  264. /// </example>
  265. public static Pos Percent (float percent)
  266. {
  267. if (percent is < 0 or > 100)
  268. {
  269. throw new ArgumentException ("Percent value must be between 0 and 100.");
  270. }
  271. return new PosFactor (percent / 100);
  272. }
  273. /// <summary>
  274. /// Creates a <see cref="Pos"/> object that tracks the Right (X+Width) coordinate of the specified
  275. /// <see cref="View"/>.
  276. /// </summary>
  277. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  278. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  279. public static Pos Right (View view) { return new PosView (view, Side.Right); }
  280. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</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 Top (View view) { return new PosView (view, Side.Y); }
  284. /// <summary>Creates a <see cref="Pos"/> object that tracks the Left (X) position of the specified <see cref="View"/>.</summary>
  285. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  286. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  287. public static Pos X (View view) { return new PosView (view, Side.X); }
  288. /// <summary>Creates a <see cref="Pos"/> object that tracks the Top (Y) position of the specified <see cref="View"/>.</summary>
  289. /// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
  290. /// <param name="view">The <see cref="View"/> that will be tracked.</param>
  291. public static Pos Y (View view) { return new PosView (view, Side.Y); }
  292. internal virtual int Anchor (int width) { return 0; }
  293. internal virtual int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize) { return Anchor (superviewDimension); }
  294. internal class PosAbsolute (int n) : Pos
  295. {
  296. private readonly int _n = n;
  297. public override bool Equals (object other) { return other is PosAbsolute abs && abs._n == _n; }
  298. public override int GetHashCode () { return _n.GetHashCode (); }
  299. public override string ToString () { return $"Absolute({_n})"; }
  300. internal override int Anchor (int width) { return _n; }
  301. }
  302. internal class PosAnchorEnd : Pos
  303. {
  304. private readonly int _offset;
  305. public PosAnchorEnd () { UseDimForOffset = true; }
  306. public PosAnchorEnd (int offset) { _offset = offset; }
  307. public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd._offset == _offset; }
  308. public override int GetHashCode () { return _offset.GetHashCode (); }
  309. /// <summary>
  310. /// If true, the offset is the width of the view, if false, the offset is the offset value.
  311. /// </summary>
  312. internal bool UseDimForOffset { get; set; }
  313. public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({_offset})"; }
  314. internal override int Anchor (int width)
  315. {
  316. if (UseDimForOffset)
  317. {
  318. return width;
  319. }
  320. return width - _offset;
  321. }
  322. internal override int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize)
  323. {
  324. int newLocation = Anchor (superviewDimension);
  325. if (UseDimForOffset)
  326. {
  327. newLocation -= dim.Anchor (superviewDimension);
  328. }
  329. return newLocation;
  330. }
  331. }
  332. internal class PosCenter : Pos
  333. {
  334. public override string ToString () { return "Center"; }
  335. internal override int Anchor (int width) { return width / 2; }
  336. internal override int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize)
  337. {
  338. int newDimension = Math.Max (dim.GetDimension (0, superviewDimension, autosize, autoSize), 0);
  339. return Anchor (superviewDimension - newDimension);
  340. }
  341. }
  342. internal class PosCombine (bool add, Pos left, Pos right) : Pos
  343. {
  344. internal bool _add = add;
  345. internal Pos _left = left, _right = right;
  346. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  347. internal override int Anchor (int width)
  348. {
  349. int la = _left.Anchor (width);
  350. int ra = _right.Anchor (width);
  351. if (_add)
  352. {
  353. return la + ra;
  354. }
  355. return la - ra;
  356. }
  357. internal override int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize)
  358. {
  359. int newDimension = dim.GetDimension (0, superviewDimension, autosize, autoSize);
  360. int left = _left.GetLocation (superviewDimension, dim, autosize, autoSize);
  361. int right = _right.GetLocation (superviewDimension, dim, autosize, autoSize);
  362. if (_add)
  363. {
  364. return left + right;
  365. }
  366. return left - right;
  367. }
  368. }
  369. internal class PosFactor (float factor) : Pos
  370. {
  371. private readonly float _factor = factor;
  372. public override bool Equals (object other) { return other is PosFactor f && f._factor == _factor; }
  373. public override int GetHashCode () { return _factor.GetHashCode (); }
  374. public override string ToString () { return $"Factor({_factor})"; }
  375. internal override int Anchor (int width) { return (int)(width * _factor); }
  376. }
  377. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  378. internal class PosFunc (Func<int> n) : Pos
  379. {
  380. private readonly Func<int> _function = n;
  381. public override bool Equals (object other) { return other is PosFunc f && f._function () == _function (); }
  382. public override int GetHashCode () { return _function.GetHashCode (); }
  383. public override string ToString () { return $"PosFunc({_function ()})"; }
  384. internal override int Anchor (int width) { return _function (); }
  385. }
  386. internal enum Side
  387. {
  388. X = 0,
  389. Y = 1,
  390. Right = 2,
  391. Bottom = 3
  392. }
  393. internal class PosView (View view, Side side) : Pos
  394. {
  395. public readonly View Target = view;
  396. public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; }
  397. public override int GetHashCode () { return Target.GetHashCode (); }
  398. public override string ToString ()
  399. {
  400. string sideString = side switch
  401. {
  402. Side.X => "x",
  403. Side.Y => "y",
  404. Side.Right => "right",
  405. Side.Bottom => "bottom",
  406. _ => "unknown"
  407. };
  408. if (Target == null)
  409. {
  410. throw new NullReferenceException (nameof (Target));
  411. }
  412. return $"View(side={sideString},target={Target})";
  413. }
  414. internal override int Anchor (int width)
  415. {
  416. return side switch
  417. {
  418. Side.X => Target.Frame.X,
  419. Side.Y => Target.Frame.Y,
  420. Side.Right => Target.Frame.Right,
  421. Side.Bottom => Target.Frame.Bottom,
  422. _ => 0
  423. };
  424. }
  425. }
  426. }
  427. /// <summary>
  428. /// <para>
  429. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  430. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  431. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  432. /// </para>
  433. /// <para>
  434. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  435. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  436. /// subtraction operators.
  437. /// </para>
  438. /// </summary>
  439. /// <remarks>
  440. /// <para>
  441. /// <list type="table">
  442. /// <listheader>
  443. /// <term>Dim Object</term> <description>Description</description>
  444. /// </listheader>
  445. /// <item>
  446. /// <term>
  447. /// <see cref="Dim.Function(Func{int})"/>
  448. /// </term>
  449. /// <description>
  450. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  451. /// function. The function will be called every time the dimension is needed.
  452. /// </description>
  453. /// </item>
  454. /// <item>
  455. /// <term>
  456. /// <see cref="Dim.Percent(float, bool)"/>
  457. /// </term>
  458. /// <description>
  459. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  460. /// SuperView.
  461. /// </description>
  462. /// </item>
  463. /// <item>
  464. /// <term>
  465. /// <see cref="Dim.Fill(int)"/>
  466. /// </term>
  467. /// <description>
  468. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  469. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  470. /// </description>
  471. /// </item>
  472. /// <item>
  473. /// <term>
  474. /// <see cref="Dim.Width(View)"/>
  475. /// </term>
  476. /// <description>
  477. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  478. /// <see cref="View"/>.
  479. /// </description>
  480. /// </item>
  481. /// <item>
  482. /// <term>
  483. /// <see cref="Dim.Height(View)"/>
  484. /// </term>
  485. /// <description>
  486. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  487. /// <see cref="View"/>.
  488. /// </description>
  489. /// </item>
  490. /// </list>
  491. /// </para>
  492. /// <para></para>
  493. /// </remarks>
  494. public class Dim
  495. {
  496. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  497. /// <param name="other">The object to compare with the current object. </param>
  498. /// <returns>
  499. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  500. /// <see langword="false"/>.
  501. /// </returns>
  502. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  503. /// <summary>
  504. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  505. /// margin.
  506. /// </summary>
  507. /// <returns>The Fill dimension.</returns>
  508. /// <param name="margin">Margin to use.</param>
  509. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  510. /// <summary>
  511. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  512. /// The function will be called every time the dimension is needed.
  513. /// </summary>
  514. /// <param name="function">The function to be executed.</param>
  515. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  516. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  517. /// <summary>Serves as the default hash function. </summary>
  518. /// <returns>A hash code for the current object.</returns>
  519. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  520. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  521. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  522. /// <param name="view">The view that will be tracked.</param>
  523. public static Dim Height (View view) { return new DimView (view, Side.Height); }
  524. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  525. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  526. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  527. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  528. public static Dim operator + (Dim left, Dim right)
  529. {
  530. if (left is DimAbsolute && right is DimAbsolute)
  531. {
  532. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  533. }
  534. var newDim = new DimCombine (true, left, right);
  535. (left as DimView)?.Target.SetNeedsLayout ();
  536. return newDim;
  537. }
  538. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  539. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  540. /// <param name="n">The value to convert to the pos.</param>
  541. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  542. /// <summary>
  543. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  544. /// <see cref="Dim"/>.
  545. /// </summary>
  546. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  547. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  548. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  549. public static Dim operator - (Dim left, Dim right)
  550. {
  551. if (left is DimAbsolute && right is DimAbsolute)
  552. {
  553. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  554. }
  555. var newDim = new DimCombine (false, left, right);
  556. (left as DimView)?.Target.SetNeedsLayout ();
  557. return newDim;
  558. }
  559. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  560. /// <returns>The percent <see cref="Dim"/> object.</returns>
  561. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  562. /// <param name="usePosition">
  563. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  564. /// <see cref="View.Y"/>).
  565. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  566. /// </param>
  567. /// <example>
  568. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  569. /// height,
  570. /// and is 80% the width of the SuperView.
  571. /// <code>
  572. /// var textView = new TextField {
  573. /// X = Pos.Center (),
  574. /// Y = Pos.Percent (50),
  575. /// Width = Dim.Percent (80),
  576. /// Height = Dim.Percent (30),
  577. /// };
  578. /// </code>
  579. /// </example>
  580. public static Dim Percent (float percent, bool usePosition = false)
  581. {
  582. if (percent is < 0 or > 100)
  583. {
  584. throw new ArgumentException ("Percent value must be between 0 and 100");
  585. }
  586. return new DimFactor (percent / 100, usePosition);
  587. }
  588. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  589. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  590. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  591. public static Dim Sized (int n) { return new DimAbsolute (n); }
  592. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  593. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  594. /// <param name="view">The view that will be tracked.</param>
  595. public static Dim Width (View view) { return new DimView (view, Side.Width); }
  596. internal virtual int Anchor (int width) { return 0; }
  597. internal virtual int GetDimension (int location, int dimension, int autosize, bool autoSize)
  598. {
  599. int newDimension = Math.Max (Anchor (dimension - location), 0);
  600. return autoSize && autosize > newDimension ? autosize : newDimension;
  601. }
  602. internal class DimAbsolute (int n) : Dim
  603. {
  604. private readonly int _n = n;
  605. public override bool Equals (object other) { return other is DimAbsolute abs && abs._n == _n; }
  606. public override int GetHashCode () { return _n.GetHashCode (); }
  607. public override string ToString () { return $"Absolute({_n})"; }
  608. internal override int Anchor (int width) { return _n; }
  609. internal override int GetDimension (int location, int dimension, int autosize, bool autoSize)
  610. {
  611. // DimAbsolute.Anchor (int width) ignores width and returns n
  612. int newDimension = Math.Max (Anchor (0), 0);
  613. return autoSize && autosize > newDimension ? autosize : newDimension;
  614. }
  615. }
  616. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  617. {
  618. internal bool _add = add;
  619. internal Dim _left = left, _right = right;
  620. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  621. internal override int Anchor (int width)
  622. {
  623. int la = _left.Anchor (width);
  624. int ra = _right.Anchor (width);
  625. if (_add)
  626. {
  627. return la + ra;
  628. }
  629. return la - ra;
  630. }
  631. internal override int GetDimension (int location, int dimension, int autosize, bool autoSize)
  632. {
  633. int leftNewDim = _left.GetDimension (location, dimension, autosize, autoSize);
  634. int rightNewDim = _right.GetDimension (location, dimension, autosize, autoSize);
  635. int newDimension;
  636. if (_add)
  637. {
  638. newDimension = leftNewDim + rightNewDim;
  639. }
  640. else
  641. {
  642. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  643. }
  644. return autoSize && autosize > newDimension ? autosize : newDimension;
  645. }
  646. }
  647. internal class DimFactor (float factor, bool remaining = false) : Dim
  648. {
  649. private readonly float _factor = factor;
  650. private readonly bool _remaining = remaining;
  651. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  652. public override int GetHashCode () { return _factor.GetHashCode (); }
  653. public bool IsFromRemaining () { return _remaining; }
  654. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  655. internal override int Anchor (int width) { return (int)(width * _factor); }
  656. internal override int GetDimension (int location, int dimension, int autosize, bool autoSize)
  657. {
  658. int newDimension = _remaining ? Math.Max (Anchor (dimension - location), 0) : Anchor (dimension);
  659. return autoSize && autosize > newDimension ? autosize : newDimension;
  660. }
  661. }
  662. internal class DimFill (int margin) : Dim
  663. {
  664. private readonly int _margin = margin;
  665. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  666. public override int GetHashCode () { return _margin.GetHashCode (); }
  667. public override string ToString () { return $"Fill({_margin})"; }
  668. internal override int Anchor (int width) { return width - _margin; }
  669. }
  670. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  671. internal class DimFunc (Func<int> n) : Dim
  672. {
  673. private readonly Func<int> _function = n;
  674. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  675. public override int GetHashCode () { return _function.GetHashCode (); }
  676. public override string ToString () { return $"DimFunc({_function ()})"; }
  677. internal override int Anchor (int width) { return _function (); }
  678. }
  679. internal enum Side
  680. {
  681. Height = 0,
  682. Width = 1
  683. }
  684. internal class DimView : Dim
  685. {
  686. private readonly Side _side;
  687. internal DimView (View view, Side side)
  688. {
  689. Target = view;
  690. _side = side;
  691. }
  692. public View Target { get; init; }
  693. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  694. public override int GetHashCode () { return Target.GetHashCode (); }
  695. public override string ToString ()
  696. {
  697. if (Target == null)
  698. {
  699. throw new NullReferenceException ();
  700. }
  701. string sideString = _side switch
  702. {
  703. Side.Height => "Height",
  704. Side.Width => "Width",
  705. _ => "unknown"
  706. };
  707. return $"View({sideString},{Target})";
  708. }
  709. internal override int Anchor (int width)
  710. {
  711. return _side switch
  712. {
  713. Side.Height => Target.Frame.Height,
  714. Side.Width => Target.Frame.Width,
  715. _ => 0
  716. };
  717. }
  718. }
  719. }