PosDim.cs 41 KB

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