Dim.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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="Func"/>
  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. #region static Dim creation methods
  139. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  140. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  141. /// <param name="size">The value to convert to the <see cref="Dim"/>.</param>
  142. public static Dim Absolute (int size) { return new DimAbsolute (size); }
  143. /// <summary>
  144. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all the view's Content, Subviews, and/or Text.
  145. /// </summary>
  146. /// <remarks>
  147. /// <para>
  148. /// See <see cref="DimAutoStyle"/>.
  149. /// </para>
  150. /// </remarks>
  151. /// <example>
  152. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  153. /// SubViews.
  154. /// <code>
  155. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  156. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  157. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  158. /// view.Add (button, textField);
  159. /// </code>
  160. /// </example>
  161. /// <returns>The <see cref="Dim"/> object.</returns>
  162. /// <param name="style">
  163. /// Specifies how <see cref="Dim.Auto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  164. /// </param>
  165. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  166. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  167. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim minimumContentDim = null, Dim maximumContentDim = null)
  168. {
  169. if (maximumContentDim != null)
  170. {
  171. throw new NotImplementedException (@"maximumContentDim is not implemented");
  172. }
  173. return new DimAuto (style, minimumContentDim, maximumContentDim);
  174. }
  175. /// <summary>
  176. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified margin.
  177. /// </summary>
  178. /// <returns>The Fill dimension.</returns>
  179. /// <param name="margin">Margin to use.</param>
  180. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  181. /// <summary>
  182. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  183. /// The function will be called every time the dimension is needed.
  184. /// </summary>
  185. /// <param name="function">The function to be executed.</param>
  186. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  187. public static Dim Func (Func<int> function) { return new DimFunc (function); }
  188. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  189. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  190. /// <param name="view">The view that will be tracked.</param>
  191. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  192. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  193. /// <returns>The percent <see cref="Dim"/> object.</returns>
  194. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  195. /// <param name="usePosition">
  196. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  197. /// <see cref="View.Y"/>).
  198. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  199. /// </param>
  200. /// <example>
  201. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  202. /// height,
  203. /// and is 80% the width of the SuperView.
  204. /// <code>
  205. /// var textView = new TextField {
  206. /// X = Pos.Center (),
  207. /// Y = Pos.Percent (50),
  208. /// Width = Dim.Percent (80),
  209. /// Height = Dim.Percent (30),
  210. /// };
  211. /// </code>
  212. /// </example>
  213. public static Dim Percent (float percent, bool usePosition = false)
  214. {
  215. if (percent is < 0 or > 100)
  216. {
  217. throw new ArgumentException ("Percent value must be between 0 and 100");
  218. }
  219. return new DimPercent (percent / 100, usePosition);
  220. }
  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. #endregion static Dim creation methods
  226. #region virtual methods
  227. /// <summary>
  228. /// Gets a dimension that is anchored to a certain point in the layout.
  229. /// This method is typically used internally by the layout system to determine the size of a View.
  230. /// </summary>
  231. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  232. /// <returns>
  233. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  234. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  235. /// dimension that is a certain percentage of the super view's size, and so on.
  236. /// </returns>
  237. internal virtual int Anchor (int size) { return 0; }
  238. /// <summary>
  239. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  240. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its
  241. /// content.
  242. /// </summary>
  243. /// <param name="location">
  244. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  245. /// top edge for height calculation.
  246. /// </param>
  247. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  248. /// <param name="us">The View that holds this Pos object.</param>
  249. /// <param name="dimension">Width or Height</param>
  250. /// <returns>
  251. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  252. /// is used.
  253. /// </returns>
  254. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  255. {
  256. return Math.Max (Anchor (superviewContentSize - location), 0);
  257. }
  258. #endregion virtual methods
  259. #region operators
  260. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  261. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  262. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  263. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  264. public static Dim operator + (Dim left, Dim right)
  265. {
  266. if (left is DimAbsolute && right is DimAbsolute)
  267. {
  268. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  269. }
  270. var newDim = new DimCombine (true, left, right);
  271. (left as DimView)?.Target.SetNeedsLayout ();
  272. return newDim;
  273. }
  274. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  275. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  276. /// <param name="n">The value to convert to the pos.</param>
  277. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  278. /// <summary>
  279. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  280. /// <see cref="Dim"/>.
  281. /// </summary>
  282. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  283. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  284. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  285. public static Dim operator - (Dim left, Dim right)
  286. {
  287. if (left is DimAbsolute && right is DimAbsolute)
  288. {
  289. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  290. }
  291. var newDim = new DimCombine (false, left, right);
  292. (left as DimView)?.Target.SetNeedsLayout ();
  293. return newDim;
  294. }
  295. #endregion operators
  296. #region overrides
  297. /// <summary>
  298. /// Diagnostics API to determine if this Dim object references other views.
  299. /// </summary>
  300. /// <returns></returns>
  301. internal virtual bool ReferencesOtherViews () { return false; }
  302. /// <inheritdoc/>
  303. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  304. /// <inheritdoc/>
  305. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  306. #endregion overrides
  307. }
  308. /// <summary>
  309. /// Represents a dimension that is a fixed size.
  310. /// </summary>
  311. /// <remarks>
  312. /// <para>
  313. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  314. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  315. /// </para>
  316. /// </remarks>
  317. /// <param name="size"></param>
  318. public class DimAbsolute (int size) : Dim
  319. {
  320. /// <inheritdoc/>
  321. public override bool Equals (object other) { return other is DimAbsolute abs && abs.Size == Size; }
  322. /// <inheritdoc/>
  323. public override int GetHashCode () { return Size.GetHashCode (); }
  324. /// <summary>
  325. /// Gets the size of the dimension.
  326. /// </summary>
  327. public int Size { get; } = size;
  328. /// <inheritdoc/>
  329. public override string ToString () { return $"Absolute({Size})"; }
  330. internal override int Anchor (int size) { return Size; }
  331. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  332. {
  333. // DimAbsolute.Anchor (int size) ignores width and returns n
  334. return Math.Max (Anchor (0), 0);
  335. }
  336. }
  337. /// <summary>
  338. /// Represents a dimension that automatically sizes the view to fit all the view's Content, SubViews, and/or Text.
  339. /// </summary>
  340. /// <remarks>
  341. /// <para>
  342. /// See <see cref="DimAutoStyle"/>.
  343. /// </para>
  344. /// <para>
  345. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  346. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  347. /// </para>
  348. /// </remarks>
  349. /// <param name="style">
  350. /// Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  351. /// </param>
  352. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  353. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  354. public class DimAuto (DimAutoStyle style, Dim minimumContentDim, Dim maximumContentDim) : Dim
  355. {
  356. /// <inheritdoc/>
  357. public override bool Equals (object other)
  358. {
  359. return other is DimAuto auto && auto.MinimumContentDim == MinimumContentDim && auto.MaximumContentDim == MaximumContentDim && auto.Style == Style;
  360. }
  361. /// <inheritdoc/>
  362. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), MinimumContentDim, MaximumContentDim, Style); }
  363. /// <summary>
  364. /// Gets the maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.
  365. /// </summary>
  366. public Dim MaximumContentDim { get; } = maximumContentDim;
  367. /// <summary>
  368. /// Gets the minimum dimension the View's ContentSize will be constrained to.
  369. /// </summary>
  370. public Dim MinimumContentDim { get; } = minimumContentDim;
  371. /// <summary>
  372. /// Gets the style of the DimAuto.
  373. /// </summary>
  374. public DimAutoStyle Style { get; } = style;
  375. /// <inheritdoc/>
  376. public override string ToString () { return $"Auto({Style},{MinimumContentDim},{MaximumContentDim})"; }
  377. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  378. {
  379. if (us == null)
  380. {
  381. return MaximumContentDim?.Anchor (0) ?? 0;
  382. }
  383. var textSize = 0;
  384. var subviewsSize = 0;
  385. int autoMin = MinimumContentDim?.Anchor (superviewContentSize) ?? 0;
  386. if (superviewContentSize < autoMin)
  387. {
  388. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  389. //return superviewContentSize;
  390. }
  391. if (Style.HasFlag (DimAutoStyle.Text))
  392. {
  393. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  394. }
  395. if (Style.HasFlag (DimAutoStyle.Content))
  396. {
  397. if (us._contentSize is { })
  398. {
  399. subviewsSize = dimension == Dimension.Width ? us.ContentSize.Width : us.ContentSize.Height;
  400. }
  401. else
  402. {
  403. // TODO: AnchorEnd needs work
  404. // TODO: If _min > 0 we can SetRelativeLayout for the subviews?
  405. subviewsSize = 0;
  406. if (us.Subviews.Count > 0)
  407. {
  408. for (var i = 0; i < us.Subviews.Count; i++)
  409. {
  410. View v = us.Subviews [i];
  411. bool isNotPosAnchorEnd = dimension == Dimension.Width ? v.X is not PosAnchorEnd : v.Y is not PosAnchorEnd;
  412. //if (!isNotPosAnchorEnd)
  413. //{
  414. // v.SetRelativeLayout(dimension == Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin));
  415. //}
  416. if (isNotPosAnchorEnd)
  417. {
  418. int size = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  419. if (size > subviewsSize)
  420. {
  421. subviewsSize = size;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. }
  428. // All sizes here are content-relative; ignoring adornments.
  429. // We take the larger of text and content.
  430. int max = int.Max (textSize, subviewsSize);
  431. // And, if min: is set, it wins if larger
  432. max = int.Max (max, autoMin);
  433. // Factor in adornments
  434. Thickness thickness = us.GetAdornmentsThickness ();
  435. if (dimension == Dimension.Width)
  436. {
  437. max += thickness.Horizontal;
  438. }
  439. else
  440. {
  441. max += thickness.Vertical;
  442. }
  443. // If max: is set, clamp the return - BUGBUG: Not tested
  444. return int.Min (max, MaximumContentDim?.Anchor (superviewContentSize) ?? max);
  445. }
  446. internal override bool ReferencesOtherViews ()
  447. {
  448. // BUGBUG: This is not correct. _contentSize may be null.
  449. return false; //_style.HasFlag (DimAutoStyle.Content);
  450. }
  451. }
  452. /// <summary>
  453. /// Represents a dimension that is a combination of two other dimensions.
  454. /// </summary>
  455. /// <param name="add">
  456. /// Indicates whether the two dimensions are added or subtracted. If <see langword="true"/>, the dimensions are added,
  457. /// otherwise they are subtracted.
  458. /// </param>
  459. /// <remarks>
  460. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  461. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  462. /// </remarks>
  463. /// <param name="left">The left dimension.</param>
  464. /// <param name="right">The right dimension.</param>
  465. public class DimCombine (bool add, Dim left, Dim right) : Dim
  466. {
  467. /// <summary>
  468. /// Gets whether the two dimensions are added or subtracted.
  469. /// </summary>
  470. public bool Add { get; } = add;
  471. /// <summary>
  472. /// Gets the left dimension.
  473. /// </summary>
  474. public Dim Left { get; } = left;
  475. /// <summary>
  476. /// Gets the right dimension.
  477. /// </summary>
  478. public Dim Right { get; } = right;
  479. /// <inheritdoc/>
  480. public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
  481. internal override int Anchor (int size)
  482. {
  483. int la = Left.Anchor (size);
  484. int ra = Right.Anchor (size);
  485. if (Add)
  486. {
  487. return la + ra;
  488. }
  489. return la - ra;
  490. }
  491. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  492. {
  493. int leftNewDim = Left.Calculate (location, superviewContentSize, us, dimension);
  494. int rightNewDim = Right.Calculate (location, superviewContentSize, us, dimension);
  495. int newDimension;
  496. if (Add)
  497. {
  498. newDimension = leftNewDim + rightNewDim;
  499. }
  500. else
  501. {
  502. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  503. }
  504. return newDimension;
  505. }
  506. /// <summary>
  507. /// Diagnostics API to determine if this Dim object references other views.
  508. /// </summary>
  509. /// <returns></returns>
  510. internal override bool ReferencesOtherViews ()
  511. {
  512. if (Left.ReferencesOtherViews ())
  513. {
  514. return true;
  515. }
  516. if (Right.ReferencesOtherViews ())
  517. {
  518. return true;
  519. }
  520. return false;
  521. }
  522. }
  523. /// <summary>
  524. /// Represents a dimension that is a percentage of the width or height of the SuperView.
  525. /// </summary>
  526. /// <remarks>
  527. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  528. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  529. /// </remarks>
  530. /// <param name="percent">The percentage.</param>
  531. /// <param name="usePosition">
  532. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  533. /// <see cref="View.Y"/>).
  534. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  535. /// </param>
  536. public class DimPercent (float percent, bool usePosition = false) : Dim
  537. {
  538. /// <inheritdoc/>
  539. public override bool Equals (object other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
  540. /// <inheritdoc/>
  541. public override int GetHashCode () { return Percent.GetHashCode (); }
  542. /// <summary>
  543. /// Gets the percentage.
  544. /// </summary>
  545. public new float Percent { get; } = percent;
  546. /// <summary>
  547. /// </summary>
  548. /// <returns></returns>
  549. public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
  550. /// <summary>
  551. /// Gets whether the dimension is computed using the View's position or ContentSize.
  552. /// </summary>
  553. public bool UsePosition { get; } = usePosition;
  554. internal override int Anchor (int size) { return (int)(size * Percent); }
  555. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  556. {
  557. return UsePosition ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  558. }
  559. }
  560. /// <summary>
  561. /// Represents a dimension that fills the dimension, leaving the specified margin.
  562. /// </summary>
  563. /// <remarks>
  564. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  565. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  566. /// </remarks>
  567. /// <param name="margin">The margin to not fill.</param>
  568. public class DimFill (int margin) : Dim
  569. {
  570. /// <inheritdoc/>
  571. public override bool Equals (object other) { return other is DimFill fill && fill.Margin == Margin; }
  572. /// <inheritdoc/>
  573. public override int GetHashCode () { return Margin.GetHashCode (); }
  574. /// <summary>
  575. /// Gets the margin to not fill.
  576. /// </summary>
  577. public int Margin { get; } = margin;
  578. /// <inheritdoc/>
  579. public override string ToString () { return $"Fill({Margin})"; }
  580. internal override int Anchor (int size) { return size - Margin; }
  581. }
  582. /// <summary>
  583. /// Represents a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  584. /// </summary>
  585. /// <remarks>
  586. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  587. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  588. /// </remarks>
  589. /// <param name="dim"></param>
  590. public class DimFunc (Func<int> dim) : Dim
  591. {
  592. /// <inheritdoc/>
  593. public override bool Equals (object other) { return other is DimFunc f && f.Func () == Func (); }
  594. /// <summary>
  595. /// Gets the function that computes the dimension.
  596. /// </summary>
  597. public Func<int> Func { get; } = dim;
  598. /// <inheritdoc/>
  599. public override int GetHashCode () { return Func.GetHashCode (); }
  600. /// <inheritdoc/>
  601. public override string ToString () { return $"DimFunc({Func ()})"; }
  602. internal override int Anchor (int size) { return Func (); }
  603. }
  604. /// <summary>
  605. /// Represents a dimension that tracks the Height or Width of the specified View.
  606. /// </summary>
  607. /// <remarks>
  608. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  609. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  610. /// </remarks>
  611. public class DimView : Dim
  612. {
  613. /// <summary>
  614. /// Initializes a new instance of the <see cref="DimView"/> class.
  615. /// </summary>
  616. /// <param name="view">The view the dimension is anchored to.</param>
  617. /// <param name="dimension">Indicates which dimension is tracked.</param>
  618. public DimView (View view, Dimension dimension)
  619. {
  620. Target = view;
  621. Dimension = dimension;
  622. }
  623. /// <summary>
  624. /// Gets the indicated dimension of the View.
  625. /// </summary>
  626. public Dimension Dimension { get; }
  627. /// <inheritdoc/>
  628. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target && abs.Dimension == Dimension; }
  629. /// <inheritdoc/>
  630. public override int GetHashCode () { return Target.GetHashCode (); }
  631. /// <summary>
  632. /// Gets the View the dimension is anchored to.
  633. /// </summary>
  634. public View Target { get; init; }
  635. /// <inheritdoc/>
  636. public override string ToString ()
  637. {
  638. if (Target == null)
  639. {
  640. throw new NullReferenceException ();
  641. }
  642. string dimString = Dimension switch
  643. {
  644. Dimension.Height => "Height",
  645. Dimension.Width => "Width",
  646. _ => "unknown"
  647. };
  648. return $"View({dimString},{Target})";
  649. }
  650. internal override int Anchor (int size)
  651. {
  652. return Dimension switch
  653. {
  654. Dimension.Height => Target.Frame.Height,
  655. Dimension.Width => Target.Frame.Width,
  656. _ => 0
  657. };
  658. }
  659. internal override bool ReferencesOtherViews () { return true; }
  660. }