Dim.cs 29 KB

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