Dim.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Specifies how <see cref="Dim.Auto"/> will compute the dimension.
  5. /// </summary>
  6. [Flags]
  7. public enum DimAutoStyle
  8. {
  9. /// <summary>
  10. /// The dimension will be computed using both the view's <see cref="View.Text"/> and
  11. /// <see cref="View.Subviews"/> (whichever is larger).
  12. /// </summary>
  13. Auto = Content | Text,
  14. /// <summary>
  15. /// The dimensions will be computed based on the View's non-Text content.
  16. /// <para>
  17. /// If <see cref="View.ContentSize"/> is explicitly set (is not <see langword="null"/>) then
  18. /// <see cref="View.ContentSize"/>
  19. /// will be used to determine the dimension.
  20. /// </para>
  21. /// <para>
  22. /// Otherwise, the Subview in <see cref="View.Subviews"/> with the largest corresponding position plus dimension
  23. /// will determine the dimension.
  24. /// </para>
  25. /// <para>
  26. /// The corresponding dimension of the view's <see cref="View.Text"/> will be ignored.
  27. /// </para>
  28. /// </summary>
  29. Content = 1,
  30. /// <summary>
  31. /// <para>
  32. /// The corresponding dimension of the view's <see cref="View.Text"/>, formatted using the
  33. /// <see cref="View.TextFormatter"/> settings,
  34. /// will be used to determine the dimension.
  35. /// </para>
  36. /// <para>
  37. /// The corresponding dimensions of the <see cref="View.Subviews"/> will be ignored.
  38. /// </para>
  39. /// </summary>
  40. Text = 2
  41. }
  42. /// <summary>
  43. /// Indicates the dimension for <see cref="Dim"/> operations.
  44. /// </summary>
  45. public enum Dimension
  46. {
  47. /// <summary>
  48. /// No dimension specified.
  49. /// </summary>
  50. None = 0,
  51. /// <summary>
  52. /// The height dimension.
  53. /// </summary>
  54. Height = 1,
  55. /// <summary>
  56. /// The width dimension.
  57. /// </summary>
  58. Width = 2
  59. }
  60. /// <summary>
  61. /// <para>
  62. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  63. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  64. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  65. /// </para>
  66. /// <para>
  67. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  68. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  69. /// subtraction operators.
  70. /// </para>
  71. /// </summary>
  72. /// <remarks>
  73. /// <para>
  74. /// <list type="table">
  75. /// <listheader>
  76. /// <term>Dim Object</term> <description>Description</description>
  77. /// </listheader>
  78. /// <item>
  79. /// <term>
  80. /// <see cref="Dim.Auto"/>
  81. /// </term>
  82. /// <description>
  83. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit
  84. /// the view's Text, SubViews, or ContentArea.
  85. /// </description>
  86. /// </item>
  87. /// <item>
  88. /// <term>
  89. /// <see cref="Dim.Function(Func{int})"/>
  90. /// </term>
  91. /// <description>
  92. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  93. /// function. The function will be called every time the dimension is needed.
  94. /// </description>
  95. /// </item>
  96. /// <item>
  97. /// <term>
  98. /// <see cref="Dim.Percent(float, bool)"/>
  99. /// </term>
  100. /// <description>
  101. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  102. /// SuperView.
  103. /// </description>
  104. /// </item>
  105. /// <item>
  106. /// <term>
  107. /// <see cref="Dim.Fill(int)"/>
  108. /// </term>
  109. /// <description>
  110. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  111. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  112. /// </description>
  113. /// </item>
  114. /// <item>
  115. /// <term>
  116. /// <see cref="Dim.Width(View)"/>
  117. /// </term>
  118. /// <description>
  119. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  120. /// <see cref="View"/>.
  121. /// </description>
  122. /// </item>
  123. /// <item>
  124. /// <term>
  125. /// <see cref="Dim.Height(View)"/>
  126. /// </term>
  127. /// <description>
  128. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  129. /// <see cref="View"/>.
  130. /// </description>
  131. /// </item>
  132. /// </list>
  133. /// </para>
  134. /// <para></para>
  135. /// </remarks>
  136. public class Dim
  137. {
  138. /// <summary>
  139. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all the view's SubViews and/or Text.
  140. /// </summary>
  141. /// <remarks>
  142. /// <para>
  143. /// See <see cref="DimAutoStyle"/>.
  144. /// </para>
  145. /// </remarks>
  146. /// <example>
  147. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  148. /// SubViews.
  149. /// <code>
  150. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  151. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  152. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  153. /// view.Add (button, textField);
  154. /// </code>
  155. /// </example>
  156. /// <returns>The <see cref="Dim"/> object.</returns>
  157. /// <param name="style">
  158. /// Specifies how <see cref="Dim.Auto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  159. /// </param>
  160. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  161. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  162. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim minimumContentDim = null, Dim maximumContentDim = null)
  163. {
  164. if (maximumContentDim != null)
  165. {
  166. throw new NotImplementedException (@"maximumContentDim is not implemented");
  167. }
  168. return new DimAuto (style, minimumContentDim, maximumContentDim);
  169. }
  170. /// <summary>
  171. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  172. /// margin.
  173. /// </summary>
  174. /// <returns>The Fill dimension.</returns>
  175. /// <param name="margin">Margin to use.</param>
  176. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  177. /// <summary>
  178. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  179. /// The function will be called every time the dimension is needed.
  180. /// </summary>
  181. /// <param name="function">The function to be executed.</param>
  182. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  183. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  184. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  185. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  186. /// <param name="view">The view that will be tracked.</param>
  187. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  188. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  189. /// <returns>The percent <see cref="Dim"/> object.</returns>
  190. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  191. /// <param name="usePosition">
  192. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  193. /// <see cref="View.Y"/>).
  194. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  195. /// </param>
  196. /// <example>
  197. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  198. /// height,
  199. /// and is 80% the width of the SuperView.
  200. /// <code>
  201. /// var textView = new TextField {
  202. /// X = Pos.Center (),
  203. /// Y = Pos.Percent (50),
  204. /// Width = Dim.Percent (80),
  205. /// Height = Dim.Percent (30),
  206. /// };
  207. /// </code>
  208. /// </example>
  209. public static Dim Percent (float percent, bool usePosition = false)
  210. {
  211. if (percent is < 0 or > 100)
  212. {
  213. throw new ArgumentException ("Percent value must be between 0 and 100");
  214. }
  215. return new DimPercent (percent / 100, usePosition);
  216. }
  217. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  218. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  219. /// <param name="size">The value to convert to the <see cref="Dim"/>.</param>
  220. public static Dim Sized (int size) { return new DimAbsolute (size); }
  221. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  222. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  223. /// <param name="view">The view that will be tracked.</param>
  224. public static Dim Width (View view) { return new DimView (view, Dimension.Width); }
  225. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  226. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  227. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  228. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  229. public static Dim operator + (Dim left, Dim right)
  230. {
  231. if (left is DimAbsolute && right is DimAbsolute)
  232. {
  233. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  234. }
  235. var newDim = new DimCombine (true, left, right);
  236. (left as DimView)?.Target.SetNeedsLayout ();
  237. return newDim;
  238. }
  239. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  240. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  241. /// <param name="n">The value to convert to the pos.</param>
  242. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  243. /// <summary>
  244. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  245. /// <see cref="Dim"/>.
  246. /// </summary>
  247. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  248. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  249. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  250. public static Dim operator - (Dim left, Dim right)
  251. {
  252. if (left is DimAbsolute && right is DimAbsolute)
  253. {
  254. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  255. }
  256. var newDim = new DimCombine (false, left, right);
  257. (left as DimView)?.Target.SetNeedsLayout ();
  258. return newDim;
  259. }
  260. /// <summary>
  261. /// Gets a dimension that is anchored to a certain point in the layout.
  262. /// This method is typically used internally by the layout system to determine the size of a View.
  263. /// </summary>
  264. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  265. /// <returns>
  266. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  267. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  268. /// dimension that is a certain percentage of the super view's size, and so on.
  269. /// </returns>
  270. internal virtual int Anchor (int width) { return 0; }
  271. /// <summary>
  272. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  273. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its
  274. /// content.
  275. /// </summary>
  276. /// <param name="location">
  277. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  278. /// top edge for height calculation.
  279. /// </param>
  280. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  281. /// <param name="us">The View that holds this Pos object.</param>
  282. /// <param name="dimension">Width or Height</param>
  283. /// <returns>
  284. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  285. /// is used.
  286. /// </returns>
  287. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  288. {
  289. return Math.Max (Anchor (superviewContentSize - location), 0);
  290. }
  291. /// <summary>
  292. /// Diagnostics API to determine if this Dim object references other views.
  293. /// </summary>
  294. /// <returns></returns>
  295. internal virtual bool ReferencesOtherViews () { return false; }
  296. /// <inheritdoc />
  297. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  298. /// <inheritdoc />
  299. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  300. }
  301. /// <summary>
  302. /// Represents a dimension that is a fixed size.
  303. /// </summary>
  304. /// <param name="size"></param>
  305. public class DimAbsolute (int size) : Dim
  306. {
  307. /// <summary>
  308. /// Gets the size of the dimension.
  309. /// </summary>
  310. public int Size { get; } = size;
  311. /// <inheritdoc />
  312. public override bool Equals (object other) { return other is DimAbsolute abs && abs.Size == Size; }
  313. /// <inheritdoc />
  314. public override int GetHashCode () { return Size.GetHashCode (); }
  315. /// <inheritdoc />
  316. public override string ToString () { return $"Absolute({Size})"; }
  317. internal override int Anchor (int width) { return Size; }
  318. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  319. {
  320. // DimAbsolute.Anchor (int width) ignores width and returns n
  321. return Math.Max (Anchor (0), 0);
  322. }
  323. }
  324. /// <summary>
  325. /// Represents a dimension that automatically sizes the view to fit all the view's Content, SubViews, and/or Text.
  326. /// </summary>
  327. /// <remarks>
  328. /// <para>
  329. /// See <see cref="DimAutoStyle"/>.
  330. /// </para>
  331. /// </remarks>
  332. /// <param name="style">
  333. /// Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  334. /// </param>
  335. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  336. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  337. public class DimAuto (DimAutoStyle style, Dim minimumContentDim, Dim maximumContentDim) : Dim
  338. {
  339. /// <summary>
  340. /// Gets the minimum dimension the View's ContentSize will be constrained to.
  341. /// </summary>
  342. public Dim MinimumContentDim { get; } = minimumContentDim;
  343. /// <summary>
  344. /// Gets the maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.
  345. /// </summary>
  346. public Dim MaximumContentDim { get; } = maximumContentDim;
  347. /// <summary>
  348. /// Gets the style of the DimAuto.
  349. /// </summary>
  350. public DimAutoStyle Style { get; } = style;
  351. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  352. {
  353. if (us == null)
  354. {
  355. return MaximumContentDim?.Anchor (0) ?? 0;
  356. }
  357. var textSize = 0;
  358. var subviewsSize = 0;
  359. int autoMin = MinimumContentDim?.Anchor (superviewContentSize) ?? 0;
  360. if (superviewContentSize < autoMin)
  361. {
  362. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  363. return superviewContentSize;
  364. }
  365. if (Style.HasFlag (DimAutoStyle.Text))
  366. {
  367. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  368. }
  369. if (Style.HasFlag (DimAutoStyle.Content))
  370. {
  371. if (us._contentSize is { })
  372. {
  373. subviewsSize = dimension == Dimension.Width ? us.ContentSize.Width : us.ContentSize.Height;
  374. }
  375. else
  376. {
  377. // TODO: AnchorEnd needs work
  378. // TODO: If _min > 0 we can SetRelativeLayout for the subviews?
  379. subviewsSize = 0;
  380. if (us.Subviews.Count > 0)
  381. {
  382. for (var i = 0; i < us.Subviews.Count; i++)
  383. {
  384. View v = us.Subviews [i];
  385. bool isNotPosAnchorEnd = dimension == Dimension.Width ? v.X is not PosAnchorEnd : v.Y is not PosAnchorEnd;
  386. //if (!isNotPosAnchorEnd)
  387. //{
  388. // v.SetRelativeLayout(dimension == Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin));
  389. //}
  390. if (isNotPosAnchorEnd)
  391. {
  392. int size = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  393. if (size > subviewsSize)
  394. {
  395. subviewsSize = size;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. }
  402. // All sizes here are content-relative; ignoring adornments.
  403. // We take the larger of text and content.
  404. int max = int.Max (textSize, subviewsSize);
  405. // And, if min: is set, it wins if larger
  406. max = int.Max (max, autoMin);
  407. // Factor in adornments
  408. Thickness thickness = us.GetAdornmentsThickness ();
  409. if (dimension == Dimension.Width)
  410. {
  411. max += thickness.Horizontal;
  412. }
  413. else
  414. {
  415. max += thickness.Vertical;
  416. }
  417. // If max: is set, clamp the return - BUGBUG: Not tested
  418. return int.Min (max, MaximumContentDim?.Anchor (superviewContentSize) ?? superviewContentSize);
  419. }
  420. internal override bool ReferencesOtherViews ()
  421. {
  422. // BUGBUG: This is not correct. _contentSize may be null.
  423. return false; //_style.HasFlag (DimAutoStyle.Content);
  424. }
  425. /// <inheritdoc/>
  426. public override bool Equals (object other)
  427. {
  428. return other is DimAuto auto && auto.MinimumContentDim == MinimumContentDim && auto.MaximumContentDim == MaximumContentDim && auto.Style == Style;
  429. }
  430. /// <inheritdoc/>
  431. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), MinimumContentDim, MaximumContentDim, Style); }
  432. /// <inheritdoc/>
  433. public override string ToString () { return $"Auto({Style},{MinimumContentDim},{MaximumContentDim})"; }
  434. }
  435. /// <summary>
  436. /// Represents a dimension that is a combination of two other dimensions.
  437. /// </summary>
  438. /// <param name="add">Indicates whether the two dimensions are added or subtracted. If <see langword="true"/>, the dimensions are added, otherwise they are subtracted. </param>
  439. /// <param name="left">The left dimension.</param>
  440. /// <param name="right">The right dimension.</param>
  441. public class DimCombine (bool add, Dim left, Dim right) : Dim
  442. {
  443. /// <summary>
  444. /// Gets whether the two dimensions are added or subtracted.
  445. /// </summary>
  446. public bool Add { get; } = add;
  447. /// <summary>
  448. /// Gets the left dimension.
  449. /// </summary>
  450. public Dim Left { get; } = left;
  451. /// <summary>
  452. /// Gets the right dimension.
  453. /// </summary>
  454. public Dim Right { get; } = right;
  455. /// <inheritdoc />
  456. public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
  457. internal override int Anchor (int width)
  458. {
  459. int la = Left.Anchor (width);
  460. int ra = Right.Anchor (width);
  461. if (Add)
  462. {
  463. return la + ra;
  464. }
  465. return la - ra;
  466. }
  467. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  468. {
  469. int leftNewDim = Left.Calculate (location, superviewContentSize, us, dimension);
  470. int rightNewDim = Right.Calculate (location, superviewContentSize, us, dimension);
  471. int newDimension;
  472. if (Add)
  473. {
  474. newDimension = leftNewDim + rightNewDim;
  475. }
  476. else
  477. {
  478. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  479. }
  480. return newDimension;
  481. }
  482. /// <summary>
  483. /// Diagnostics API to determine if this Dim object references other views.
  484. /// </summary>
  485. /// <returns></returns>
  486. internal override bool ReferencesOtherViews ()
  487. {
  488. if (Left.ReferencesOtherViews ())
  489. {
  490. return true;
  491. }
  492. if (Right.ReferencesOtherViews ())
  493. {
  494. return true;
  495. }
  496. return false;
  497. }
  498. }
  499. /// <summary>
  500. /// Represents a dimension that is a percentage of the width or height of the SuperView.
  501. /// </summary>
  502. /// <param name="percent">The percentage.</param>
  503. /// <param name="usePosition">
  504. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  505. /// <see cref="View.Y"/>).
  506. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  507. /// </param>
  508. public class DimPercent (float percent, bool usePosition = false) : Dim
  509. {
  510. /// <summary>
  511. /// Gets the percentage.
  512. /// </summary>
  513. public new float Percent { get; } = percent;
  514. /// <summary>
  515. /// Gets whether the dimension is computed using the View's position or ContentSize.
  516. /// </summary>
  517. public bool UsePosition { get; } = usePosition;
  518. /// <inheritdoc />
  519. public override bool Equals (object other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
  520. /// <inheritdoc />
  521. public override int GetHashCode () { return Percent.GetHashCode (); }
  522. /// <summary>
  523. ///
  524. /// </summary>
  525. /// <returns></returns>
  526. public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
  527. internal override int Anchor (int width) { return (int)(width * Percent); }
  528. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  529. {
  530. return UsePosition ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  531. }
  532. }
  533. /// <summary>
  534. /// Represents a dimension that fills the dimension, leaving the specified margin.
  535. /// </summary>
  536. /// <param name="margin">The margin to not fill.</param>
  537. public class DimFill (int margin) : Dim
  538. {
  539. /// <summary>
  540. /// Gets the margin to not fill.
  541. /// </summary>
  542. public int Margin { get; } = margin;
  543. /// <inheritdoc />
  544. public override bool Equals (object other) { return other is DimFill fill && fill.Margin == Margin; }
  545. /// <inheritdoc />
  546. public override int GetHashCode () { return Margin.GetHashCode (); }
  547. /// <inheritdoc />
  548. public override string ToString () { return $"Fill({Margin})"; }
  549. internal override int Anchor (int width) { return width - Margin; }
  550. }
  551. /// <summary>
  552. /// Represents a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  553. /// </summary>
  554. /// <param name="dim"></param>
  555. public class DimFunc (Func<int> dim) : Dim
  556. {
  557. /// <summary>
  558. /// Gets the function that computes the dimension.
  559. /// </summary>
  560. public Func<int> Func { get; } = dim;
  561. /// <inheritdoc />
  562. public override bool Equals (object other) { return other is DimFunc f && f.Func () == Func (); }
  563. /// <inheritdoc />
  564. public override int GetHashCode () { return Func.GetHashCode (); }
  565. /// <inheritdoc />
  566. public override string ToString () { return $"DimFunc({Func ()})"; }
  567. internal override int Anchor (int width) { return Func (); }
  568. }
  569. internal class DimView : Dim
  570. {
  571. private readonly Dimension _side;
  572. internal DimView (View view, Dimension side)
  573. {
  574. Target = view;
  575. _side = side;
  576. }
  577. public View Target { get; init; }
  578. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  579. public override int GetHashCode () { return Target.GetHashCode (); }
  580. public override string ToString ()
  581. {
  582. if (Target == null)
  583. {
  584. throw new NullReferenceException ();
  585. }
  586. string sideString = _side switch
  587. {
  588. Dimension.Height => "Height",
  589. Dimension.Width => "Width",
  590. _ => "unknown"
  591. };
  592. return $"View({sideString},{Target})";
  593. }
  594. internal override int Anchor (int width)
  595. {
  596. return _side switch
  597. {
  598. Dimension.Height => Target.Frame.Height,
  599. Dimension.Width => Target.Frame.Width,
  600. _ => 0
  601. };
  602. }
  603. internal override bool ReferencesOtherViews () { return true; }
  604. }