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