Dim.cs 30 KB

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