Dim.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 (AddOrSubtract.Add, 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 (AddOrSubtract.Subtract, 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. max += dimension switch
  464. {
  465. Dimension.Width => thickness.Horizontal,
  466. Dimension.Height => thickness.Vertical,
  467. Dimension.None => 0,
  468. _ => throw new ArgumentOutOfRangeException (nameof (dimension), dimension, null)
  469. };
  470. return int.Min (max, MaximumContentDim?.GetAnchor (superviewContentSize) ?? max);
  471. }
  472. internal override bool ReferencesOtherViews ()
  473. {
  474. // BUGBUG: This is not correct. _contentSize may be null.
  475. return false; //_style.HasFlag (DimAutoStyle.Content);
  476. }
  477. /// <inheritdoc/>
  478. public override bool Equals (object? other)
  479. {
  480. if (other is not DimAuto auto)
  481. {
  482. return false;
  483. }
  484. return auto.MinimumContentDim == MinimumContentDim &&
  485. auto.MaximumContentDim == MaximumContentDim &&
  486. auto.Style == Style;
  487. }
  488. /// <inheritdoc/>
  489. public override int GetHashCode ()
  490. {
  491. return HashCode.Combine (MinimumContentDim, MaximumContentDim, Style);
  492. }
  493. }
  494. /// <summary>
  495. /// Represents a dimension that is a combination of two other dimensions.
  496. /// </summary>
  497. /// <param name="add">
  498. /// Indicates whether the two dimensions are added or subtracted.
  499. /// </param>
  500. /// <remarks>
  501. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  502. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  503. /// </remarks>
  504. /// <param name="left">The left dimension.</param>
  505. /// <param name="right">The right dimension.</param>
  506. public class DimCombine (AddOrSubtract add, Dim? left, Dim? right) : Dim
  507. {
  508. /// <summary>
  509. /// Gets whether the two dimensions are added or subtracted.
  510. /// </summary>
  511. public AddOrSubtract Add { get; } = add;
  512. /// <summary>
  513. /// Gets the left dimension.
  514. /// </summary>
  515. public Dim? Left { get; } = left;
  516. /// <summary>
  517. /// Gets the right dimension.
  518. /// </summary>
  519. public Dim? Right { get; } = right;
  520. /// <inheritdoc/>
  521. public override string ToString () { return $"Combine({Left}{(Add == AddOrSubtract.Add ? '+' : '-')}{Right})"; }
  522. internal override int GetAnchor (int size)
  523. {
  524. int la = Left!.GetAnchor (size);
  525. int ra = Right!.GetAnchor (size);
  526. if (Add == AddOrSubtract.Add)
  527. {
  528. return la + ra;
  529. }
  530. return la - ra;
  531. }
  532. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  533. {
  534. int leftNewDim = Left!.Calculate (location, superviewContentSize, us, dimension);
  535. int rightNewDim = Right!.Calculate (location, superviewContentSize, us, dimension);
  536. int newDimension;
  537. if (Add == AddOrSubtract.Add)
  538. {
  539. newDimension = leftNewDim + rightNewDim;
  540. }
  541. else
  542. {
  543. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  544. }
  545. return newDimension;
  546. }
  547. /// <summary>
  548. /// Diagnostics API to determine if this Dim object references other views.
  549. /// </summary>
  550. /// <returns></returns>
  551. internal override bool ReferencesOtherViews ()
  552. {
  553. if (Left!.ReferencesOtherViews ())
  554. {
  555. return true;
  556. }
  557. if (Right!.ReferencesOtherViews ())
  558. {
  559. return true;
  560. }
  561. return false;
  562. }
  563. }
  564. /// <summary>
  565. /// Represents a dimension that is a percentage of the width or height of the SuperView.
  566. /// </summary>
  567. /// <remarks>
  568. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  569. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  570. /// </remarks>
  571. /// <param name="percent">The percentage.</param>
  572. /// <param name="usePosition">
  573. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  574. /// <see cref="View.Y"/>).
  575. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  576. /// </param>
  577. public class DimPercent (float percent, bool usePosition = false) : Dim
  578. {
  579. /// <inheritdoc/>
  580. public override bool Equals (object? other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
  581. /// <inheritdoc/>
  582. public override int GetHashCode () { return Percent.GetHashCode (); }
  583. /// <summary>
  584. /// Gets the percentage.
  585. /// </summary>
  586. public new float Percent { get; } = percent;
  587. /// <summary>
  588. /// </summary>
  589. /// <returns></returns>
  590. public override string ToString () { return $"Percent({Percent},{UsePosition})"; }
  591. /// <summary>
  592. /// Gets whether the dimension is computed using the View's position or ContentSize.
  593. /// </summary>
  594. public bool UsePosition { get; } = usePosition;
  595. internal override int GetAnchor (int size) { return (int)(size * Percent); }
  596. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  597. {
  598. return UsePosition ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
  599. }
  600. }
  601. /// <summary>
  602. /// Represents a dimension that fills the dimension, leaving the specified margin.
  603. /// </summary>
  604. /// <remarks>
  605. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  606. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  607. /// </remarks>
  608. /// <param name="margin">The margin to not fill.</param>
  609. public class DimFill (int margin) : Dim
  610. {
  611. /// <inheritdoc/>
  612. public override bool Equals (object? other) { return other is DimFill fill && fill.Margin == Margin; }
  613. /// <inheritdoc/>
  614. public override int GetHashCode () { return Margin.GetHashCode (); }
  615. /// <summary>
  616. /// Gets the margin to not fill.
  617. /// </summary>
  618. public int Margin { get; } = margin;
  619. /// <inheritdoc/>
  620. public override string ToString () { return $"Fill({Margin})"; }
  621. internal override int GetAnchor (int size) { return size - Margin; }
  622. }
  623. /// <summary>
  624. /// Represents a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  625. /// </summary>
  626. /// <remarks>
  627. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  628. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  629. /// </remarks>
  630. /// <param name="dim"></param>
  631. public class DimFunc (Func<int> dim) : Dim
  632. {
  633. /// <inheritdoc/>
  634. public override bool Equals (object? other) { return other is DimFunc f && f.Func () == Func (); }
  635. /// <summary>
  636. /// Gets the function that computes the dimension.
  637. /// </summary>
  638. public new Func<int> Func { get; } = dim;
  639. /// <inheritdoc/>
  640. public override int GetHashCode () { return Func.GetHashCode (); }
  641. /// <inheritdoc/>
  642. public override string ToString () { return $"DimFunc({Func ()})"; }
  643. internal override int GetAnchor (int size) { return Func (); }
  644. }
  645. /// <summary>
  646. /// Represents a dimension that tracks the Height or Width of the specified View.
  647. /// </summary>
  648. /// <remarks>
  649. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  650. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  651. /// </remarks>
  652. public class DimView : Dim
  653. {
  654. /// <summary>
  655. /// Initializes a new instance of the <see cref="DimView"/> class.
  656. /// </summary>
  657. /// <param name="view">The view the dimension is anchored to.</param>
  658. /// <param name="dimension">Indicates which dimension is tracked.</param>
  659. public DimView (View view, Dimension dimension)
  660. {
  661. Target = view;
  662. Dimension = dimension;
  663. }
  664. /// <summary>
  665. /// Gets the indicated dimension of the View.
  666. /// </summary>
  667. public Dimension Dimension { get; }
  668. /// <inheritdoc/>
  669. public override bool Equals (object? other) { return other is DimView abs && abs.Target == Target && abs.Dimension == Dimension; }
  670. /// <inheritdoc/>
  671. public override int GetHashCode () { return Target.GetHashCode (); }
  672. /// <summary>
  673. /// Gets the View the dimension is anchored to.
  674. /// </summary>
  675. public View Target { get; init; }
  676. /// <inheritdoc/>
  677. public override string ToString ()
  678. {
  679. if (Target == null)
  680. {
  681. throw new NullReferenceException ();
  682. }
  683. string dimString = Dimension switch
  684. {
  685. Dimension.Height => "Height",
  686. Dimension.Width => "Width",
  687. _ => "unknown"
  688. };
  689. return $"View({dimString},{Target})";
  690. }
  691. internal override int GetAnchor (int size)
  692. {
  693. return Dimension switch
  694. {
  695. Dimension.Height => Target.Frame.Height,
  696. Dimension.Width => Target.Frame.Width,
  697. _ => 0
  698. };
  699. }
  700. internal override bool ReferencesOtherViews () { return true; }
  701. }