Dim.cs 29 KB

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