Dim.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// <para>
  5. /// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
  6. /// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
  7. /// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
  8. /// </para>
  9. /// <para>
  10. /// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
  11. /// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
  12. /// subtraction operators.
  13. /// </para>
  14. /// </summary>
  15. /// <remarks>
  16. /// <para>
  17. /// <list type="table">
  18. /// <listheader>
  19. /// <term>Dim Object</term> <description>Description</description>
  20. /// </listheader>
  21. /// <item>
  22. /// <term>
  23. /// <see cref="Dim.Auto"/>
  24. /// </term>
  25. /// <description>
  26. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit
  27. /// the view's Text, SubViews, or ContentArea.
  28. /// </description>
  29. /// </item>
  30. /// <item>
  31. /// <term>
  32. /// <see cref="Dim.Function(Func{int})"/>
  33. /// </term>
  34. /// <description>
  35. /// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
  36. /// function. The function will be called every time the dimension is needed.
  37. /// </description>
  38. /// </item>
  39. /// <item>
  40. /// <term>
  41. /// <see cref="Dim.Percent(float, bool)"/>
  42. /// </term>
  43. /// <description>
  44. /// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
  45. /// SuperView.
  46. /// </description>
  47. /// </item>
  48. /// <item>
  49. /// <term>
  50. /// <see cref="Dim.Fill(int)"/>
  51. /// </term>
  52. /// <description>
  53. /// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
  54. /// to the end of the super view's width, leaving the specified number of columns for a margin.
  55. /// </description>
  56. /// </item>
  57. /// <item>
  58. /// <term>
  59. /// <see cref="Dim.Width(View)"/>
  60. /// </term>
  61. /// <description>
  62. /// Creates a <see cref="Dim"/> object that tracks the Width of the specified
  63. /// <see cref="View"/>.
  64. /// </description>
  65. /// </item>
  66. /// <item>
  67. /// <term>
  68. /// <see cref="Dim.Height(View)"/>
  69. /// </term>
  70. /// <description>
  71. /// Creates a <see cref="Dim"/> object that tracks the Height of the specified
  72. /// <see cref="View"/>.
  73. /// </description>
  74. /// </item>
  75. /// </list>
  76. /// </para>
  77. /// <para></para>
  78. /// </remarks>
  79. public class Dim
  80. {
  81. /// <summary>
  82. /// Specifies how <see cref="Dim.Auto"/> will compute the dimension.
  83. /// </summary>
  84. [Flags]
  85. public enum DimAutoStyle
  86. {
  87. /// <summary>
  88. /// The dimension will be computed using both the view's <see cref="View.Text"/> and
  89. /// <see cref="View.Subviews"/> (whichever is larger).
  90. /// </summary>
  91. Auto = Content | Text,
  92. /// <summary>
  93. /// The dimensions will be computed based on the View's non-Text content.
  94. /// <para>
  95. /// If <see cref="View.ContentSize"/> is explicitly set (is not <see langword="null"/>) then <see cref="View.ContentSize"/>
  96. /// will be used to determine the dimension.
  97. /// </para>
  98. /// <para>
  99. /// Otherwise, the Subview in <see cref="View.Subviews"/> with the largest corresponding position plus dimension
  100. /// will determine the dimension.
  101. /// </para>
  102. /// <para>
  103. /// The corresponding dimension of the view's <see cref="View.Text"/> will be ignored.
  104. /// </para>
  105. /// </summary>
  106. Content = 1,
  107. /// <summary>
  108. /// <para>
  109. /// The corresponding dimension of the view's <see cref="View.Text"/>, formatted using the
  110. /// <see cref="View.TextFormatter"/> settings,
  111. /// will be used to determine the dimension.
  112. /// </para>
  113. /// <para>
  114. /// The corresponding dimensions of the <see cref="View.Subviews"/> will be ignored.
  115. /// </para>
  116. /// </summary>
  117. Text = 2
  118. }
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. public enum Dimension
  123. {
  124. /// <summary>
  125. /// No dimension specified.
  126. /// </summary>
  127. None = 0,
  128. /// <summary>
  129. /// The height dimension.
  130. /// </summary>
  131. Height = 1,
  132. /// <summary>
  133. /// The width dimension.
  134. /// </summary>
  135. Width = 2
  136. }
  137. /// <summary>
  138. /// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all the view's SubViews and/or Text.
  139. /// </summary>
  140. /// <remarks>
  141. /// <para>
  142. /// See <see cref="DimAutoStyle"/>.
  143. /// </para>
  144. /// </remarks>
  145. /// <example>
  146. /// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
  147. /// SubViews.
  148. /// <code>
  149. /// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
  150. /// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
  151. /// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
  152. /// view.Add (button, textField);
  153. /// </code>
  154. /// </example>
  155. /// <returns>The <see cref="Dim"/> object.</returns>
  156. /// <param name="style">
  157. /// Specifies how <see cref="Dim.Auto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
  158. /// </param>
  159. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  160. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  161. public static Dim Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim minimumContentDim = null, Dim maximumContentDim = null)
  162. {
  163. if (maximumContentDim != null)
  164. {
  165. throw new NotImplementedException (@"maximumContentDim is not implemented");
  166. }
  167. return new DimAuto (style, minimumContentDim, maximumContentDim);
  168. }
  169. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  170. /// <param name="other">The object to compare with the current object. </param>
  171. /// <returns>
  172. /// <see langword="true"/> if the specified object is equal to the current object; otherwise,
  173. /// <see langword="false"/>.
  174. /// </returns>
  175. public override bool Equals (object other) { return other is Dim abs && abs == this; }
  176. /// <summary>
  177. /// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified number of columns for a
  178. /// margin.
  179. /// </summary>
  180. /// <returns>The Fill dimension.</returns>
  181. /// <param name="margin">Margin to use.</param>
  182. public static Dim Fill (int margin = 0) { return new DimFill (margin); }
  183. /// <summary>
  184. /// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
  185. /// The function will be called every time the dimension is needed.
  186. /// </summary>
  187. /// <param name="function">The function to be executed.</param>
  188. /// <returns>The <see cref="Dim"/> returned from the function.</returns>
  189. public static Dim Function (Func<int> function) { return new DimFunc (function); }
  190. /// <summary>Serves as the default hash function. </summary>
  191. /// <returns>A hash code for the current object.</returns>
  192. public override int GetHashCode () { return Anchor (0).GetHashCode (); }
  193. /// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
  194. /// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  195. /// <param name="view">The view that will be tracked.</param>
  196. public static Dim Height (View view) { return new DimView (view, Dimension.Height); }
  197. /// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
  198. /// <param name="left">The first <see cref="Dim"/> to add.</param>
  199. /// <param name="right">The second <see cref="Dim"/> to add.</param>
  200. /// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  201. public static Dim operator + (Dim left, Dim right)
  202. {
  203. if (left is DimAbsolute && right is DimAbsolute)
  204. {
  205. return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
  206. }
  207. var newDim = new DimCombine (true, left, right);
  208. (left as DimView)?.Target.SetNeedsLayout ();
  209. return newDim;
  210. }
  211. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  212. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  213. /// <param name="n">The value to convert to the pos.</param>
  214. public static implicit operator Dim (int n) { return new DimAbsolute (n); }
  215. /// <summary>
  216. /// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
  217. /// <see cref="Dim"/>.
  218. /// </summary>
  219. /// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
  220. /// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
  221. /// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
  222. public static Dim operator - (Dim left, Dim right)
  223. {
  224. if (left is DimAbsolute && right is DimAbsolute)
  225. {
  226. return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
  227. }
  228. var newDim = new DimCombine (false, left, right);
  229. (left as DimView)?.Target.SetNeedsLayout ();
  230. return newDim;
  231. }
  232. /// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
  233. /// <returns>The percent <see cref="Dim"/> object.</returns>
  234. /// <param name="percent">A value between 0 and 100 representing the percentage.</param>
  235. /// <param name="usePosition">
  236. /// If <see langword="true"/> the dimension is computed using the View's position (<see cref="View.X"/> or
  237. /// <see cref="View.Y"/>).
  238. /// If <see langword="false"/> the dimension is computed using the View's <see cref="View.ContentSize"/>.
  239. /// </param>
  240. /// <example>
  241. /// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
  242. /// height,
  243. /// and is 80% the width of the SuperView.
  244. /// <code>
  245. /// var textView = new TextField {
  246. /// X = Pos.Center (),
  247. /// Y = Pos.Percent (50),
  248. /// Width = Dim.Percent (80),
  249. /// Height = Dim.Percent (30),
  250. /// };
  251. /// </code>
  252. /// </example>
  253. public static Dim Percent (float percent, bool usePosition = false)
  254. {
  255. if (percent is < 0 or > 100)
  256. {
  257. throw new ArgumentException ("Percent value must be between 0 and 100");
  258. }
  259. return new DimFactor (percent / 100, usePosition);
  260. }
  261. /// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
  262. /// <returns>The Absolute <see cref="Dim"/>.</returns>
  263. /// <param name="n">The value to convert to the <see cref="Dim"/>.</param>
  264. public static Dim Sized (int n) { return new DimAbsolute (n); }
  265. /// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
  266. /// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
  267. /// <param name="view">The view that will be tracked.</param>
  268. public static Dim Width (View view) { return new DimView (view, Dimension.Width); }
  269. /// <summary>
  270. /// Gets a dimension that is anchored to a certain point in the layout.
  271. /// This method is typically used internally by the layout system to determine the size of a View.
  272. /// </summary>
  273. /// <param name="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
  274. /// <returns>
  275. /// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
  276. /// subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
  277. /// dimension that is a certain percentage of the super view's size, and so on.
  278. /// </returns>
  279. internal virtual int Anchor (int width) { return 0; }
  280. /// <summary>
  281. /// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
  282. /// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its content.
  283. /// </summary>
  284. /// <param name="location">
  285. /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the
  286. /// top edge for height calculation.
  287. /// </param>
  288. /// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
  289. /// <param name="us">The View that holds this Pos object.</param>
  290. /// <param name="dimension">Width or Height</param>
  291. /// <returns>
  292. /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
  293. /// is used.
  294. /// </returns>
  295. internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  296. {
  297. return Math.Max (Anchor (superviewContentSize - location), 0);
  298. }
  299. /// <summary>
  300. /// Diagnostics API to determine if this Dim object references other views.
  301. /// </summary>
  302. /// <returns></returns>
  303. internal virtual bool ReferencesOtherViews ()
  304. {
  305. return false;
  306. }
  307. internal class DimAbsolute (int n) : Dim
  308. {
  309. private readonly int _n = n;
  310. public override bool Equals (object other) { return other is DimAbsolute abs && abs._n == _n; }
  311. public override int GetHashCode () { return _n.GetHashCode (); }
  312. public override string ToString () { return $"Absolute({_n})"; }
  313. internal override int Anchor (int width) { return _n; }
  314. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  315. {
  316. // DimAbsolute.Anchor (int width) ignores width and returns n
  317. return Math.Max (Anchor (0), 0);
  318. }
  319. }
  320. /// <summary>
  321. /// A <see cref="Dim"/> object that automatically sizes the view to fit all the view's SubViews and/or Text.
  322. /// </summary>
  323. /// <remarks>
  324. /// <para>
  325. /// See <see cref="Dim.DimAutoStyle"/>.
  326. /// </para>
  327. /// </remarks>
  328. /// <param name="style">
  329. /// Specifies how <see cref="Dim.DimAuto"/> will compute the dimension. The default is <see cref="Dim.DimAutoStyle.Auto"/>.
  330. /// </param>
  331. /// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
  332. /// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
  333. public class DimAuto (DimAutoStyle style, Dim minimumContentDim, Dim maximumContentDim) : Dim
  334. {
  335. internal readonly Dim _minContentDim = minimumContentDim;
  336. internal readonly Dim _maxContentDim = maximumContentDim;
  337. internal readonly DimAutoStyle _style = style;
  338. internal int _size;
  339. /// <inheritdoc />
  340. public override bool Equals (object other) { return other is DimAuto auto && auto._minContentDim == _minContentDim && auto._maxContentDim == _maxContentDim && auto._style == _style; }
  341. /// <inheritdoc />
  342. public override int GetHashCode () { return HashCode.Combine (base.GetHashCode (), _minContentDim, _maxContentDim, _style); }
  343. /// <inheritdoc />
  344. public override string ToString () { return $"Auto({_style},{_minContentDim},{_maxContentDim})"; }
  345. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  346. {
  347. if (us == null)
  348. {
  349. return _maxContentDim?.Anchor (0) ?? 0;
  350. }
  351. var textSize = 0;
  352. var subviewsSize = 0;
  353. int autoMin = _minContentDim?.Anchor (superviewContentSize) ?? 0;
  354. if (superviewContentSize < autoMin)
  355. {
  356. Debug.WriteLine ($"WARNING: DimAuto specifies a min size ({autoMin}), but the SuperView's bounds are smaller ({superviewContentSize}).");
  357. return superviewContentSize;
  358. }
  359. if (_style.HasFlag (Dim.DimAutoStyle.Text))
  360. {
  361. textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
  362. }
  363. if (_style.HasFlag (DimAutoStyle.Content))
  364. {
  365. if (us._contentSize is { })
  366. {
  367. subviewsSize = dimension == Dimension.Width ? us.ContentSize.Width : us.ContentSize.Height;
  368. }
  369. else
  370. {
  371. // TODO: AnchorEnd needs work
  372. // TODO: If _min > 0 we can SetRelativeLayout for the subviews?
  373. subviewsSize = 0;
  374. if (us.Subviews.Count > 0)
  375. {
  376. for (int i = 0; i < us.Subviews.Count; i++)
  377. {
  378. var v = us.Subviews [i];
  379. bool isNotPosAnchorEnd = dimension == Dim.Dimension.Width ? v.X is not Pos.PosAnchorEnd : v.Y is not Pos.PosAnchorEnd;
  380. //if (!isNotPosAnchorEnd)
  381. //{
  382. // v.SetRelativeLayout(dimension == Dim.Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin));
  383. //}
  384. if (isNotPosAnchorEnd)
  385. {
  386. int size = dimension == Dim.Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  387. if (size > subviewsSize)
  388. {
  389. subviewsSize = size;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. // All sizes here are content-relative; ignoring adornments.
  397. // We take the larger of text and content.
  398. int max = int.Max (textSize, subviewsSize);
  399. // And, if min: is set, it wins if larger
  400. max = int.Max (max, autoMin);
  401. // Factor in adornments
  402. Thickness thickness = us.GetAdornmentsThickness ();
  403. if (dimension == Dimension.Width)
  404. {
  405. max += thickness.Horizontal;
  406. }
  407. else
  408. {
  409. max += thickness.Vertical;
  410. }
  411. // If max: is set, clamp the return - BUGBUG: Not tested
  412. return int.Min (max, _maxContentDim?.Anchor (superviewContentSize) ?? superviewContentSize);
  413. }
  414. /// <summary>
  415. /// Diagnostics API to determine if this Dim object references other views.
  416. /// </summary>
  417. /// <returns></returns>
  418. internal override bool ReferencesOtherViews ()
  419. {
  420. // BUGBUG: This is not correct. _contentSize may be null.
  421. return false;//_style.HasFlag (Dim.DimAutoStyle.Content);
  422. }
  423. }
  424. internal class DimCombine (bool add, Dim left, Dim right) : Dim
  425. {
  426. internal bool _add = add;
  427. internal Dim _left = left, _right = right;
  428. public override string ToString () { return $"Combine({_left}{(_add ? '+' : '-')}{_right})"; }
  429. internal override int Anchor (int width)
  430. {
  431. int la = _left.Anchor (width);
  432. int ra = _right.Anchor (width);
  433. if (_add)
  434. {
  435. return la + ra;
  436. }
  437. return la - ra;
  438. }
  439. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  440. {
  441. int leftNewDim = _left.Calculate (location, superviewContentSize, us, dimension);
  442. int rightNewDim = _right.Calculate (location, superviewContentSize, us, dimension);
  443. int newDimension;
  444. if (_add)
  445. {
  446. newDimension = leftNewDim + rightNewDim;
  447. }
  448. else
  449. {
  450. newDimension = Math.Max (0, leftNewDim - rightNewDim);
  451. }
  452. return newDimension;
  453. }
  454. /// <summary>
  455. /// Diagnostics API to determine if this Dim object references other views.
  456. /// </summary>
  457. /// <returns></returns>
  458. internal override bool ReferencesOtherViews ()
  459. {
  460. if (_left.ReferencesOtherViews ())
  461. {
  462. return true;
  463. }
  464. if (_right.ReferencesOtherViews ())
  465. {
  466. return true;
  467. }
  468. return false;
  469. }
  470. }
  471. internal class DimFactor (float factor, bool remaining = false) : Dim
  472. {
  473. private readonly float _factor = factor;
  474. private readonly bool _remaining = remaining;
  475. public override bool Equals (object other) { return other is DimFactor f && f._factor == _factor && f._remaining == _remaining; }
  476. public override int GetHashCode () { return _factor.GetHashCode (); }
  477. public bool IsFromRemaining () { return _remaining; }
  478. public override string ToString () { return $"Factor({_factor},{_remaining})"; }
  479. internal override int Anchor (int width) { return (int)(width * _factor); }
  480. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  481. {
  482. return _remaining ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
  483. }
  484. }
  485. internal class DimFill (int margin) : Dim
  486. {
  487. private readonly int _margin = margin;
  488. public override bool Equals (object other) { return other is DimFill fill && fill._margin == _margin; }
  489. public override int GetHashCode () { return _margin.GetHashCode (); }
  490. public override string ToString () { return $"Fill({_margin})"; }
  491. internal override int Anchor (int width) { return width - _margin; }
  492. }
  493. // Helper class to provide dynamic value by the execution of a function that returns an integer.
  494. internal class DimFunc (Func<int> n) : Dim
  495. {
  496. private readonly Func<int> _function = n;
  497. public override bool Equals (object other) { return other is DimFunc f && f._function () == _function (); }
  498. public override int GetHashCode () { return _function.GetHashCode (); }
  499. public override string ToString () { return $"DimFunc({_function ()})"; }
  500. internal override int Anchor (int width) { return _function (); }
  501. }
  502. internal class DimView : Dim
  503. {
  504. private readonly Dimension _side;
  505. internal DimView (View view, Dimension side)
  506. {
  507. Target = view;
  508. _side = side;
  509. }
  510. public View Target { get; init; }
  511. public override bool Equals (object other) { return other is DimView abs && abs.Target == Target; }
  512. public override int GetHashCode () { return Target.GetHashCode (); }
  513. public override string ToString ()
  514. {
  515. if (Target == null)
  516. {
  517. throw new NullReferenceException ();
  518. }
  519. string sideString = _side switch
  520. {
  521. Dimension.Height => "Height",
  522. Dimension.Width => "Width",
  523. _ => "unknown"
  524. };
  525. return $"View({sideString},{Target})";
  526. }
  527. internal override int Anchor (int width)
  528. {
  529. return _side switch
  530. {
  531. Dimension.Height => Target.Frame.Height,
  532. Dimension.Width => Target.Frame.Width,
  533. _ => 0
  534. };
  535. }
  536. internal override bool ReferencesOtherViews ()
  537. {
  538. return true;
  539. }
  540. }
  541. }