Dim.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 Absolute (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 size) { 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. /// <remarks>
  305. /// <para>
  306. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  307. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  308. /// </para>
  309. /// </remarks>
  310. /// <param name="size"></param>
  311. public class DimAbsolute (int size) : Dim
  312. {
  313. /// <summary>
  314. /// Gets the size of the dimension.
  315. /// </summary>
  316. public int Size { get; } = size;
  317. /// <inheritdoc/>
  318. public override bool Equals (object other) { return other is DimAbsolute abs && abs.Size == Size; }
  319. /// <inheritdoc/>
  320. public override int GetHashCode () { return Size.GetHashCode (); }
  321. /// <inheritdoc/>
  322. public override string ToString () { return $"Absolute({Size})"; }
  323. internal override int Anchor (int size) { return Size; }
  324. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  325. {
  326. // DimAbsolute.Anchor (int size) ignores width and returns n
  327. return Math.Max (Anchor (0), 0);
  328. }
  329. }
  330. /// <summary>
  331. /// Represents a dimension that automatically sizes the view to fit all the view's Content, SubViews, and/or Text.
  332. /// </summary>
  333. /// <remarks>
  334. /// <para>
  335. /// See <see cref="DimAutoStyle"/>.
  336. /// </para>
  337. /// <para>
  338. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  339. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  340. /// </para>
  341. /// </remarks>
  342. /// <param name="style">
  343. /// Specifies how <see cref="DimAuto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  344. /// </param>
  345. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  346. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  347. public class DimAuto (DimAutoStyle style, Dim minimumContentDim, Dim maximumContentDim) : Dim
  348. {
  349. /// <summary>
  350. /// Gets the minimum dimension the View's ContentSize will be constrained to.
  351. /// </summary>
  352. public Dim MinimumContentDim { get; } = minimumContentDim;
  353. /// <summary>
  354. /// Gets the maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.
  355. /// </summary>
  356. public Dim MaximumContentDim { get; } = maximumContentDim;
  357. /// <summary>
  358. /// Gets the style of the DimAuto.
  359. /// </summary>
  360. public DimAutoStyle Style { get; } = style;
  361. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  362. {
  363. if (us == null)
  364. {
  365. return MaximumContentDim?.Anchor (0) ?? 0;
  366. }
  367. var textSize = 0;
  368. var subviewsSize = 0;
  369. int autoMin = MinimumContentDim?.Anchor (superviewContentSize) ?? 0;
  370. if (superviewContentSize < autoMin)
  371. {
  372. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  373. //return superviewContentSize;
  374. }
  375. if (Style.HasFlag (DimAutoStyle.Text))
  376. {
  377. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  378. }
  379. if (Style.HasFlag (DimAutoStyle.Content))
  380. {
  381. if (us._contentSize is { })
  382. {
  383. subviewsSize = dimension == Dimension.Width ? us.ContentSize.Width : us.ContentSize.Height;
  384. }
  385. else
  386. {
  387. // TODO: AnchorEnd needs work
  388. // TODO: If _min > 0 we can SetRelativeLayout for the subviews?
  389. subviewsSize = 0;
  390. if (us.Subviews.Count > 0)
  391. {
  392. for (var i = 0; i < us.Subviews.Count; i++)
  393. {
  394. View v = us.Subviews [i];
  395. bool isNotPosAnchorEnd = dimension == Dimension.Width ? v.X is not PosAnchorEnd : v.Y is not PosAnchorEnd;
  396. //if (!isNotPosAnchorEnd)
  397. //{
  398. // v.SetRelativeLayout(dimension == Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin));
  399. //}
  400. if (isNotPosAnchorEnd)
  401. {
  402. int size = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  403. if (size > subviewsSize)
  404. {
  405. subviewsSize = size;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. // All sizes here are content-relative; ignoring adornments.
  413. // We take the larger of text and content.
  414. int max = int.Max (textSize, subviewsSize);
  415. // And, if min: is set, it wins if larger
  416. max = int.Max (max, autoMin);
  417. // Factor in adornments
  418. Thickness thickness = us.GetAdornmentsThickness ();
  419. if (dimension == Dimension.Width)
  420. {
  421. max += thickness.Horizontal;
  422. }
  423. else
  424. {
  425. max += thickness.Vertical;
  426. }
  427. // If max: is set, clamp the return - BUGBUG: Not tested
  428. return int.Min (max, MaximumContentDim?.Anchor (superviewContentSize) ?? max);
  429. }
  430. internal override bool ReferencesOtherViews ()
  431. {
  432. // BUGBUG: This is not correct. _contentSize may be null.
  433. return false; //_style.HasFlag (DimAutoStyle.Content);
  434. }
  435. /// <inheritdoc/>
  436. public override bool Equals (object other)
  437. {
  438. return other is DimAuto auto && auto.MinimumContentDim == MinimumContentDim && auto.MaximumContentDim == MaximumContentDim && auto.Style == Style;
  439. }
  440. /// <inheritdoc/>
  441. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), MinimumContentDim, MaximumContentDim, Style); }
  442. /// <inheritdoc/>
  443. public override string ToString () { return $"Auto({Style},{MinimumContentDim},{MaximumContentDim})"; }
  444. }
  445. /// <summary>
  446. /// Represents a dimension that is a combination of two other dimensions.
  447. /// </summary>
  448. /// <param name="add">
  449. /// Indicates whether the two dimensions are added or subtracted. If <see langword="true"/>, the dimensions are added,
  450. /// otherwise they are subtracted.
  451. /// </param>
  452. /// <remarks>
  453. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  454. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  455. /// </remarks>
  456. /// <param name="left">The left dimension.</param>
  457. /// <param name="right">The right dimension.</param>
  458. public class DimCombine (bool add, Dim left, Dim right) : Dim
  459. {
  460. /// <summary>
  461. /// Gets whether the two dimensions are added or subtracted.
  462. /// </summary>
  463. public bool Add { get; } = add;
  464. /// <summary>
  465. /// Gets the left dimension.
  466. /// </summary>
  467. public Dim Left { get; } = left;
  468. /// <summary>
  469. /// Gets the right dimension.
  470. /// </summary>
  471. public Dim Right { get; } = right;
  472. /// <inheritdoc/>
  473. public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
  474. internal override int Anchor (int size)
  475. {
  476. int la = Left.Anchor (size);
  477. int ra = Right.Anchor (size);
  478. if (Add)
  479. {
  480. return la + ra;
  481. }
  482. return la - ra;
  483. }
  484. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  485. {
  486. int leftNewDim = Left.Calculate (location, superviewContentSize, us, dimension);
  487. int rightNewDim = Right.Calculate (location, superviewContentSize, us, dimension);
  488. int newDimension;
  489. if (Add)
  490. {
  491. newDimension = leftNewDim + rightNewDim;
  492. }
  493. else
  494. {
  495. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  496. }
  497. return newDimension;
  498. }
  499. /// <summary>
  500. /// Diagnostics API to determine if this Dim object references other views.
  501. /// </summary>
  502. /// <returns></returns>
  503. internal override bool ReferencesOtherViews ()
  504. {
  505. if (Left.ReferencesOtherViews ())
  506. {
  507. return true;
  508. }
  509. if (Right.ReferencesOtherViews ())
  510. {
  511. return true;
  512. }
  513. return false;
  514. }
  515. }
  516. /// <summary>
  517. /// Represents a dimension that is a percentage of the width or height of the SuperView.
  518. /// </summary>
  519. /// <remarks>
  520. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  521. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  522. /// </remarks>
  523. /// <param name="percent">The percentage.</param>
  524. /// <param name="usePosition">
  525. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  526. /// <see cref="View.Y"/>).
  527. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  528. /// </param>
  529. public class DimPercent (float percent, bool usePosition = false) : Dim
  530. {
  531. /// <summary>
  532. /// Gets the percentage.
  533. /// </summary>
  534. public new float Percent { get; } = percent;
  535. /// <summary>
  536. /// Gets whether the dimension is computed using the View's position or ContentSize.
  537. /// </summary>
  538. public bool UsePosition { get; } = usePosition;
  539. /// <inheritdoc/>
  540. public override bool Equals (object other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
  541. /// <inheritdoc/>
  542. public override int GetHashCode () { return Percent.GetHashCode (); }
  543. /// <summary>
  544. /// </summary>
  545. /// <returns></returns>
  546. public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
  547. internal override int Anchor (int size) { return (int)(size * Percent); }
  548. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  549. {
  550. return UsePosition ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  551. }
  552. }
  553. /// <summary>
  554. /// Represents a dimension that fills the dimension, leaving the specified margin.
  555. /// </summary>
  556. /// <remarks>
  557. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  558. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  559. /// </remarks>
  560. /// <param name="margin">The margin to not fill.</param>
  561. public class DimFill (int margin) : Dim
  562. {
  563. /// <summary>
  564. /// Gets the margin to not fill.
  565. /// </summary>
  566. public int Margin { get; } = margin;
  567. /// <inheritdoc/>
  568. public override bool Equals (object other) { return other is DimFill fill && fill.Margin == Margin; }
  569. /// <inheritdoc/>
  570. public override int GetHashCode () { return Margin.GetHashCode (); }
  571. /// <inheritdoc/>
  572. public override string ToString () { return $"Fill({Margin})"; }
  573. internal override int Anchor (int size) { return size - Margin; }
  574. }
  575. /// <summary>
  576. /// Represents a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  577. /// </summary>
  578. /// <remarks>
  579. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  580. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  581. /// </remarks>
  582. /// <param name="dim"></param>
  583. public class DimFunc (Func<int> dim) : Dim
  584. {
  585. /// <summary>
  586. /// Gets the function that computes the dimension.
  587. /// </summary>
  588. public Func<int> Func { get; } = dim;
  589. /// <inheritdoc/>
  590. public override bool Equals (object other) { return other is DimFunc f && f.Func () == Func (); }
  591. /// <inheritdoc/>
  592. public override int GetHashCode () { return Func.GetHashCode (); }
  593. /// <inheritdoc/>
  594. public override string ToString () { return $"DimFunc({Func ()})"; }
  595. internal override int Anchor (int size) { return Func (); }
  596. }
  597. /// <summary>
  598. /// Represents a dimension that tracks the Height or Width of the specified View.
  599. /// </summary>
  600. /// <remarks>
  601. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  602. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  603. /// </remarks>
  604. public class DimView : Dim
  605. {
  606. /// <summary>
  607. /// Gets the indicated dimension of the View.
  608. /// </summary>
  609. public Dimension Dimension { get; }
  610. /// <summary>
  611. /// Initializes a new instance of the <see cref="DimView"/> class.
  612. /// </summary>
  613. /// <param name="view">The view the dimension is anchored to.</param>
  614. /// <param name="dimension">Indicates which dimension is tracked.</param>
  615. public DimView (View view, Dimension dimension)
  616. {
  617. Target = view;
  618. Dimension = dimension;
  619. }
  620. /// <summary>
  621. /// Gets the View the dimension is anchored to.
  622. /// </summary>
  623. public View Target { get; init; }
  624. /// <inheritdoc/>
  625. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  626. /// <inheritdoc/>
  627. public override int GetHashCode () { return Target.GetHashCode (); }
  628. /// <inheritdoc/>
  629. public override string ToString ()
  630. {
  631. if (Target == null)
  632. {
  633. throw new NullReferenceException ();
  634. }
  635. string dimString = Dimension switch
  636. {
  637. Dimension.Height => "Height",
  638. Dimension.Width => "Width",
  639. _ => "unknown"
  640. };
  641. return $"View({dimString},{Target})";
  642. }
  643. internal override int Anchor (int size)
  644. {
  645. return Dimension switch
  646. {
  647. Dimension.Height => Target.Frame.Height,
  648. Dimension.Width => Target.Frame.Width,
  649. _ => 0
  650. };
  651. }
  652. internal override bool ReferencesOtherViews () { return true; }
  653. }