DimAuto.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. #nullable enable
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Represents a dimension that automatically sizes the view to fit all the view's Content, SubViews, and/or Text.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// See <see cref="DimAutoStyle"/>.
  11. /// </para>
  12. /// <para>
  13. /// This is a low-level API that is typically used internally by the layout system. Use the various static
  14. /// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
  15. /// </para>
  16. /// </remarks>
  17. public class DimAuto () : Dim
  18. {
  19. private readonly Dim? _maximumContentDim;
  20. /// <summary>
  21. /// Gets the maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.
  22. /// </summary>
  23. // ReSharper disable once ConvertToAutoProperty
  24. public required Dim? MaximumContentDim
  25. {
  26. get => _maximumContentDim;
  27. init => _maximumContentDim = value;
  28. }
  29. private readonly Dim? _minimumContentDim;
  30. /// <summary>
  31. /// Gets the minimum dimension the View's ContentSize will be constrained to.
  32. /// </summary>
  33. // ReSharper disable once ConvertToAutoProperty
  34. public required Dim? MinimumContentDim
  35. {
  36. get => _minimumContentDim;
  37. init => _minimumContentDim = value;
  38. }
  39. private readonly DimAutoStyle _style;
  40. /// <summary>
  41. /// Gets the style of the DimAuto.
  42. /// </summary>
  43. // ReSharper disable once ConvertToAutoProperty
  44. public required DimAutoStyle Style
  45. {
  46. get => _style;
  47. init => _style = value;
  48. }
  49. /// <inheritdoc/>
  50. public override string ToString () { return $"Auto({Style},{MinimumContentDim},{MaximumContentDim})"; }
  51. internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
  52. {
  53. var textSize = 0;
  54. var maxCalculatedSize = 0;
  55. int autoMin = MinimumContentDim?.GetAnchor (superviewContentSize) ?? 0;
  56. int autoMax = MaximumContentDim?.GetAnchor (superviewContentSize) ?? (dimension == Dimension.Width ? Application.Driver.Screen.Width : Application.Driver.Screen.Height);
  57. if (Style.FastHasFlags (DimAutoStyle.Text))
  58. {
  59. if (dimension == Dimension.Width)
  60. {
  61. //us.TextFormatter.Size = us.GetContentSize ();
  62. textSize = int.Max (autoMin, us.TextFormatter.FormatAndGetSize ().Width);
  63. }
  64. else
  65. {
  66. us.TextFormatter.Size = us.GetContentSize () with { Height = Application.Driver.Screen.Height };
  67. textSize = int.Max (autoMin, us.TextFormatter.FormatAndGetSize ().Height);
  68. }
  69. }
  70. if (Style.FastHasFlags (DimAutoStyle.Content))
  71. {
  72. if (!us.ContentSizeTracksViewport)
  73. {
  74. // ContentSize was explicitly set. Use `us.ContentSize` to determine size.
  75. maxCalculatedSize = dimension == Dimension.Width ? us.GetContentSize ().Width : us.GetContentSize ().Height;
  76. }
  77. else
  78. {
  79. maxCalculatedSize = textSize;
  80. // ContentSize was NOT explicitly set. Use `us.Subviews` to determine size.
  81. List<View> includedSubviews = us.Subviews.ToList ();
  82. // If [x] it can cause `us.ContentSize` to change.
  83. // If [ ] it doesn't need special processing for us to determine `us.ContentSize`.
  84. // -------------------- Pos types that are dependent on `us.Subviews`
  85. // [ ] PosAlign - Position is dependent on other views with `GroupId` AND `us.ContentSize`
  86. // [x] PosView - Position is dependent on `subview.Target` - it can cause a change in `us.ContentSize`
  87. // [x] PosCombine - Position is dependent if `Pos.Has ([one of the above]` - it can cause a change in `us.ContentSize`
  88. // -------------------- Pos types that are dependent on `us.ContentSize`
  89. // [ ] PosAlign - Position is dependent on other views with `GroupId` AND `us.ContentSize`
  90. // [x] PosAnchorEnd - Position is dependent on `us.ContentSize` AND `subview.Frame` - it can cause a change in `us.ContentSize`
  91. // [ ] PosCenter - Position is dependent `us.ContentSize` AND `subview.Frame`
  92. // [ ] PosPercent - Position is dependent `us.ContentSize`
  93. // [x] PosCombine - Position is dependent if `Pos.Has ([one of the above]` - it can cause a change in `us.ContentSize`
  94. // -------------------- Pos types that are not dependent on either `us.Subviews` or `us.ContentSize`
  95. // [ ] PosAbsolute - Position is fixed.
  96. // [ ] PosFunc - Position is internally calculated.
  97. // -------------------- Dim types that are dependent on `us.Subviews`
  98. // [x] DimView - Dimension is dependent on `subview.Target`
  99. // [x] DimCombine - Dimension is dependent if `Dim.Has ([one of the above]` - it can cause a change in `us.ContentSize`
  100. // -------------------- Dim types that are dependent on `us.ContentSize`
  101. // [ ] DimFill - Dimension is dependent on `us.ContentSize`
  102. // [ ] DimPercent - Dimension is dependent on `us.ContentSize`
  103. // [ ] DimCombine - Dimension is dependent if `Dim.Has ([one of the above]`
  104. // -------------------- Dim types that are not dependent on either `us.Subviews` or `us.ContentSize`
  105. // [ ] DimAuto - Dimension is internally calculated
  106. // [ ] DimAbsolute - Dimension is fixed
  107. // [ ] DimFunc - Dimension is internally calculated
  108. // ======================================================
  109. // Do the easy stuff first - subviews whose position and size are not dependent on other views or content size
  110. // ======================================================
  111. // [ ] PosAbsolute - Position is fixed.
  112. // [ ] PosFunc - Position is internally calculated
  113. // [ ] DimAuto - Dimension is internally calculated
  114. // [ ] DimAbsolute - Dimension is fixed
  115. // [ ] DimFunc - Dimension is internally calculated
  116. List<View> notDependentSubViews;
  117. if (dimension == Dimension.Width)
  118. {
  119. notDependentSubViews = includedSubviews.Where (v => v.Width is { } &&
  120. (v.X is PosAbsolute or PosFunc || v.Width is DimAuto or DimAbsolute or DimFunc) &&
  121. !v.X.Has (typeof (PosAnchorEnd), out _) &&
  122. !v.X.Has (typeof (PosAlign), out _) &&
  123. !v.X.Has (typeof (PosView), out _) &&
  124. !v.Width.Has (typeof (DimView), out _) &&
  125. !v.X.Has (typeof (PosCenter), out _)).ToList ();
  126. }
  127. else
  128. {
  129. notDependentSubViews = includedSubviews.Where (v => v.Height is { } &&
  130. (v.Y is PosAbsolute or PosFunc || v.Height is DimAuto or DimAbsolute or DimFunc) &&
  131. !v.Y.Has (typeof (PosAnchorEnd), out _) &&
  132. !v.Y.Has (typeof (PosAlign), out _) &&
  133. !v.Y.Has (typeof (PosView), out _) &&
  134. !v.Height.Has (typeof (DimView), out _) &&
  135. !v.Y.Has (typeof (PosCenter), out _)).ToList ();
  136. }
  137. for (var i = 0; i < notDependentSubViews.Count; i++)
  138. {
  139. View v = notDependentSubViews [i];
  140. int size = 0;
  141. if (dimension == Dimension.Width)
  142. {
  143. int width = v.Width!.Calculate (0, 0, v, dimension);
  144. size = v.X.GetAnchor (0) + width;
  145. }
  146. else
  147. {
  148. int height = v.Height!.Calculate (0, 0, v, dimension);
  149. size = v.Y.GetAnchor (0) + height;
  150. }
  151. if (size > maxCalculatedSize)
  152. {
  153. maxCalculatedSize = size;
  154. }
  155. }
  156. // ************** We now have some idea of `us.ContentSize` ***************
  157. #region Centered
  158. // [ ] PosCenter - Position is dependent `us.ContentSize` AND `subview.Frame`
  159. List<View> centeredSubViews;
  160. if (dimension == Dimension.Width)
  161. {
  162. centeredSubViews = us.Subviews.Where (v => v.X.Has (typeof (PosCenter), out _)).ToList ();
  163. }
  164. else
  165. {
  166. centeredSubViews = us.Subviews.Where (v => v.Y.Has (typeof (PosCenter), out _)).ToList ();
  167. }
  168. int maxCentered = 0;
  169. for (var i = 0; i < centeredSubViews.Count; i++)
  170. {
  171. View v = centeredSubViews [i];
  172. if (dimension == Dimension.Width)
  173. {
  174. int width = v.Width!.Calculate (0, 0, v, dimension);
  175. maxCentered = (v.X.GetAnchor (0) + width) * 2;
  176. }
  177. else
  178. {
  179. int height = v.Height!.Calculate (0, 0, v, dimension);
  180. maxCentered = (v.Y.GetAnchor (0) + height) * 2;
  181. }
  182. }
  183. maxCalculatedSize = int.Max (maxCalculatedSize, maxCentered);
  184. #endregion Centered
  185. #region Percent
  186. // [ ] DimPercent - Dimension is dependent on `us.ContentSize`
  187. List<View> percentSubViews;
  188. if (dimension == Dimension.Width)
  189. {
  190. percentSubViews = us.Subviews.Where (v => v.Width.Has (typeof (DimPercent), out _)).ToList ();
  191. }
  192. else
  193. {
  194. percentSubViews = us.Subviews.Where (v => v.Height.Has (typeof (DimPercent), out _)).ToList ();
  195. }
  196. int maxPercent = 0;
  197. for (var i = 0; i < percentSubViews.Count; i++)
  198. {
  199. View v = percentSubViews [i];
  200. if (dimension == Dimension.Width)
  201. {
  202. int width = v.Width!.Calculate (0, 0, v, dimension);
  203. maxPercent = (v.X.GetAnchor (0) + width);
  204. }
  205. else
  206. {
  207. int height = v.Height!.Calculate (0, 0, v, dimension);
  208. maxPercent = (v.Y.GetAnchor (0) + height);
  209. }
  210. }
  211. maxCalculatedSize = int.Max (maxCalculatedSize, maxPercent);
  212. #endregion Percent
  213. #region Aligned
  214. // [ ] PosAlign - Position is dependent on other views with `GroupId` AND `us.ContentSize`
  215. int maxAlign = 0;
  216. // Use Linq to get a list of distinct GroupIds from the subviews
  217. List<int> groupIds = includedSubviews.Select (
  218. v =>
  219. {
  220. if (dimension == Dimension.Width)
  221. {
  222. if (v.X.Has (typeof (PosAlign), out Pos posAlign))
  223. {
  224. return ((PosAlign)posAlign).GroupId;
  225. }
  226. }
  227. else
  228. {
  229. if (v.Y.Has (typeof (PosAlign), out Pos posAlign))
  230. {
  231. return ((PosAlign)posAlign).GroupId;
  232. }
  233. }
  234. return -1;
  235. }).Distinct ().ToList ();
  236. foreach (var groupId in groupIds.Where (g => g != -1))
  237. {
  238. // PERF: If this proves a perf issue, consider caching a ref to this list in each item
  239. List<PosAlign?> posAlignsInGroup = includedSubviews.Where (
  240. v =>
  241. {
  242. return dimension switch
  243. {
  244. Dimension.Width when v.X is PosAlign alignX => alignX.GroupId == groupId,
  245. Dimension.Height when v.Y is PosAlign alignY => alignY.GroupId == groupId,
  246. _ => false
  247. };
  248. })
  249. .Select (v => dimension == Dimension.Width ? v.X as PosAlign : v.Y as PosAlign)
  250. .ToList ();
  251. if (posAlignsInGroup.Count == 0)
  252. {
  253. continue;
  254. }
  255. maxAlign = PosAlign.CalculateMinDimension (groupId, includedSubviews, dimension);
  256. }
  257. maxCalculatedSize = int.Max (maxCalculatedSize, maxAlign);
  258. #endregion Aligned
  259. #region Anchored
  260. // [x] PosAnchorEnd - Position is dependent on `us.ContentSize` AND `subview.Frame`
  261. List<View> anchoredSubViews;
  262. if (dimension == Dimension.Width)
  263. {
  264. anchoredSubViews = includedSubviews.Where (v => v.X.Has (typeof (PosAnchorEnd), out _)).ToList ();
  265. }
  266. else
  267. {
  268. anchoredSubViews = includedSubviews.Where (v => v.Y.Has (typeof (PosAnchorEnd), out _)).ToList ();
  269. }
  270. int maxAnchorEnd = 0;
  271. for (var i = 0; i < anchoredSubViews.Count; i++)
  272. {
  273. View v = anchoredSubViews [i];
  274. // Need to set the relative layout for PosAnchorEnd subviews to calculate the size
  275. if (dimension == Dimension.Width)
  276. {
  277. v.SetRelativeLayout (new Size (maxCalculatedSize, 0));
  278. }
  279. else
  280. {
  281. v.SetRelativeLayout (new Size (0, maxCalculatedSize));
  282. }
  283. maxAnchorEnd = dimension == Dimension.Width ? v.X.GetAnchor (maxCalculatedSize) + v.Frame.Width : v.Y.GetAnchor (maxCalculatedSize) + v.Frame.Height;
  284. }
  285. maxCalculatedSize = Math.Max (maxCalculatedSize, maxAnchorEnd);
  286. #endregion Anchored
  287. #region PosView
  288. // [x] PosView - Position is dependent on `subview.Target` - it can cause a change in `us.ContentSize`
  289. List<View> posViewSubViews;
  290. if (dimension == Dimension.Width)
  291. {
  292. posViewSubViews = includedSubviews.Where (v => v.X.Has (typeof (PosView), out _)).ToList ();
  293. }
  294. else
  295. {
  296. posViewSubViews = includedSubviews.Where (v => v.Y.Has (typeof (PosView), out _)).ToList ();
  297. }
  298. for (var i = 0; i < posViewSubViews.Count; i++)
  299. {
  300. View v = posViewSubViews [i];
  301. // BUGBUG: The order may not be correct. May need to call TopologicalSort?
  302. if (dimension == Dimension.Width)
  303. {
  304. v.SetRelativeLayout (new Size (maxCalculatedSize, 0));
  305. }
  306. else
  307. {
  308. v.SetRelativeLayout (new Size (0, maxCalculatedSize));
  309. }
  310. int maxPosView = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  311. if (maxPosView > maxCalculatedSize)
  312. {
  313. maxCalculatedSize = maxPosView;
  314. }
  315. }
  316. #endregion PosView
  317. // [x] PosCombine - Position is dependent if `Pos.Has ([one of the above]` - it can cause a change in `us.ContentSize`
  318. #region DimView
  319. // [x] DimView - Dimension is dependent on `subview.Target` - it can cause a change in `us.ContentSize`
  320. List<View> dimViewSubViews;
  321. if (dimension == Dimension.Width)
  322. {
  323. dimViewSubViews = includedSubviews.Where (v => v.Width is { } && v.Width.Has (typeof (DimView), out _)).ToList ();
  324. }
  325. else
  326. {
  327. dimViewSubViews = includedSubviews.Where (v => v.Height is { } && v.Height.Has (typeof (DimView), out _)).ToList ();
  328. }
  329. for (var i = 0; i < dimViewSubViews.Count; i++)
  330. {
  331. View v = dimViewSubViews [i];
  332. // BUGBUG: The order may not be correct. May need to call TopologicalSort?
  333. if (dimension == Dimension.Width)
  334. {
  335. v.SetRelativeLayout (new Size (maxCalculatedSize, 0));
  336. }
  337. else
  338. {
  339. v.SetRelativeLayout (new Size (0, maxCalculatedSize));
  340. }
  341. int maxDimView = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  342. if (maxDimView > maxCalculatedSize)
  343. {
  344. maxCalculatedSize = maxDimView;
  345. }
  346. }
  347. #endregion DimView
  348. // [x] DimCombine - Dimension is dependent if `Dim.Has ([one of the above]` - it can cause a change in `us.ContentSize`
  349. // // ======================================================
  350. // // Now do PosAlign - It's dependent on other views with `GroupId` AND `us.ContentSize`
  351. // // ======================================================
  352. // // [ ] PosAlign - Position is dependent on other views with `GroupId` AND `us.ContentSize`
  353. // #region Aligned
  354. // int maxAlign = 0;
  355. // if (dimension == Dimension.Width)
  356. // {
  357. // // Use Linq to get a list of distinct GroupIds from the subviews
  358. // List<int> groupIds = includedSubviews.Select (v => v.X is PosAlign posAlign ? posAlign.GroupId : -1).Distinct ().ToList ();
  359. // foreach (var groupId in groupIds)
  360. // {
  361. // List<int> dimensionsList = new ();
  362. // // PERF: If this proves a perf issue, consider caching a ref to this list in each item
  363. // List<PosAlign?> posAlignsInGroup = includedSubviews.Where (
  364. // v =>
  365. // {
  366. // return dimension switch
  367. // {
  368. // Dimension.Width when v.X is PosAlign alignX => alignX.GroupId == groupId,
  369. // Dimension.Height when v.Y is PosAlign alignY => alignY.GroupId == groupId,
  370. // _ => false
  371. // };
  372. // })
  373. // .Select (v => dimension == Dimension.Width ? v.X as PosAlign : v.Y as PosAlign)
  374. // .ToList ();
  375. // if (posAlignsInGroup.Count == 0)
  376. // {
  377. // continue;
  378. // }
  379. // // BUGBUG: ignores adornments
  380. // maxAlign = PosAlign.CalculateMinDimension (groupId, includedSubviews, dimension);
  381. // }
  382. // }
  383. // else
  384. // {
  385. // // BUGBUG: Incompletge
  386. // subviews = includedSubviews.Where (v => v.Y is PosAlign).ToList ();
  387. // }
  388. // maxCalculatedSize = int.Max (maxCalculatedSize, maxAlign);
  389. // #endregion Aligned
  390. // // TODO: This whole body of code is a WIP (forhttps://github.com/gui-cs/Terminal.Gui/issues/3499).
  391. // List<View> subviews;
  392. // #region Not Anchored and Are Not Dependent
  393. // // Start with subviews that are not anchored to the end, aligned, or dependent on content size
  394. // // [x] PosAnchorEnd
  395. // // [x] PosAlign
  396. // // [ ] PosCenter
  397. // // [ ] PosPercent
  398. // // [ ] PosView
  399. // // [ ] PosFunc
  400. // // [x] DimFill
  401. // // [ ] DimPercent
  402. // // [ ] DimFunc
  403. // // [ ] DimView
  404. // if (dimension == Dimension.Width)
  405. // {
  406. // subviews = includedSubviews.Where (v => v.X is not PosAnchorEnd
  407. // && v.X is not PosAlign
  408. // // && v.X is not PosCenter
  409. // && v.Width is not DimAuto
  410. // && v.Width is not DimFill).ToList ();
  411. // }
  412. // else
  413. // {
  414. // subviews = includedSubviews.Where (v => v.Y is not PosAnchorEnd
  415. // && v.Y is not PosAlign
  416. // // && v.Y is not PosCenter
  417. // && v.Height is not DimAuto
  418. // && v.Height is not DimFill).ToList ();
  419. // }
  420. // for (var i = 0; i < subviews.Count; i++)
  421. // {
  422. // View v = subviews [i];
  423. // int size = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  424. // if (size > maxCalculatedSize)
  425. // {
  426. // // BUGBUG: Should we break here? Or choose min/max?
  427. // maxCalculatedSize = size;
  428. // }
  429. // }
  430. // #endregion Not Anchored and Are Not Dependent
  431. // #region Auto
  432. // #endregion Auto
  433. // //#region Center
  434. // //// Now, handle subviews that are Centered
  435. // //if (dimension == Dimension.Width)
  436. // //{
  437. // // subviews = us.Subviews.Where (v => v.X is PosCenter).ToList ();
  438. // //}
  439. // //else
  440. // //{
  441. // // subviews = us.Subviews.Where (v => v.Y is PosCenter).ToList ();
  442. // //}
  443. // //int maxCenter = 0;
  444. // //for (var i = 0; i < subviews.Count; i++)
  445. // //{
  446. // // View v = subviews [i];
  447. // // maxCenter = dimension == Dimension.Width ? v.Frame.Width : v.Frame.Height;
  448. // //}
  449. // //subviewsSize += maxCenter;
  450. // //#endregion Center
  451. // #region Are Dependent
  452. // // Now, go back to those that are dependent on content size
  453. // // Set relative layout for all DimAuto subviews
  454. // List<View> dimAutoSubViews;
  455. // int maxAuto = 0;
  456. // if (dimension == Dimension.Width)
  457. // {
  458. // dimAutoSubViews = includedSubviews.Where (v => v.Width is DimAuto).ToList ();
  459. // }
  460. // else
  461. // {
  462. // dimAutoSubViews = includedSubviews.Where (v => v.Height is DimAuto).ToList ();
  463. // }
  464. // for (var i = 0; i < dimAutoSubViews.Count; i++)
  465. // {
  466. // View v = dimAutoSubViews [i];
  467. // if (dimension == Dimension.Width)
  468. // {
  469. // // BUGBUG: ignores adornments
  470. // v.SetRelativeLayout (new Size (autoMax - maxCalculatedSize, 0));
  471. // }
  472. // else
  473. // {
  474. // // BUGBUG: ignores adornments
  475. // v.SetRelativeLayout (new Size (0, autoMax - maxCalculatedSize));
  476. // }
  477. // maxAuto = dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height;
  478. // if (maxAuto > maxCalculatedSize)
  479. // {
  480. // // BUGBUG: Should we break here? Or choose min/max?
  481. // maxCalculatedSize = maxAuto;
  482. // }
  483. // }
  484. // // [x] DimFill
  485. // // [ ] DimPercent
  486. // if (dimension == Dimension.Width)
  487. // {
  488. // subviews = includedSubviews.Where (v => v.Width is DimFill).ToList ();
  489. // }
  490. // else
  491. // {
  492. // subviews = includedSubviews.Where (v => v.Height is DimFill).ToList ();
  493. // }
  494. // int maxFill = 0;
  495. // for (var i = 0; i < subviews.Count; i++)
  496. // {
  497. // View v = subviews [i];
  498. // if (autoMax == int.MaxValue)
  499. // {
  500. // autoMax = superviewContentSize;
  501. // }
  502. // if (dimension == Dimension.Width)
  503. // {
  504. // // BUGBUG: ignores adornments
  505. // v.SetRelativeLayout (new Size (autoMax - maxCalculatedSize, 0));
  506. // }
  507. // else
  508. // {
  509. // // BUGBUG: ignores adornments
  510. // v.SetRelativeLayout (new Size (0, autoMax - maxCalculatedSize));
  511. // }
  512. // maxFill = dimension == Dimension.Width ? v.Frame.Width : v.Frame.Height;
  513. // }
  514. // maxCalculatedSize += maxFill;
  515. // #endregion Are Dependent
  516. }
  517. }
  518. // All sizes here are content-relative; ignoring adornments.
  519. // We take the largest of text and content.
  520. int max = int.Max (textSize, maxCalculatedSize);
  521. // And, if min: is set, it wins if larger
  522. max = int.Max (max, autoMin);
  523. // And, if max: is set, it wins if smaller
  524. max = int.Min (max, autoMax);
  525. // ************** We now definitively know `us.ContentSize` ***************
  526. foreach (var v in us.Subviews)
  527. {
  528. if (dimension == Dimension.Width)
  529. {
  530. v.SetRelativeLayout (new Size (max, Application.Driver.Screen.Width));
  531. }
  532. else
  533. {
  534. v.SetRelativeLayout (new Size (Application.Driver.Screen.Height, max));
  535. }
  536. }
  537. // Factor in adornments
  538. Thickness thickness = us.GetAdornmentsThickness ();
  539. var adornmentThickness = dimension switch
  540. {
  541. Dimension.Width => thickness.Horizontal,
  542. Dimension.Height => thickness.Vertical,
  543. Dimension.None => 0,
  544. _ => throw new ArgumentOutOfRangeException (nameof (dimension), dimension, null)
  545. };
  546. max += adornmentThickness;
  547. return max;
  548. }
  549. internal override bool ReferencesOtherViews ()
  550. {
  551. // BUGBUG: This is not correct. _contentSize may be null.
  552. return false; //_style.HasFlag (DimAutoStyle.Content);
  553. }
  554. /// <inheritdoc/>
  555. public override bool Equals (object? other)
  556. {
  557. if (other is not DimAuto auto)
  558. {
  559. return false;
  560. }
  561. return auto.MinimumContentDim == MinimumContentDim &&
  562. auto.MaximumContentDim == MaximumContentDim &&
  563. auto.Style == Style;
  564. }
  565. /// <inheritdoc/>
  566. public override int GetHashCode ()
  567. {
  568. return HashCode.Combine (MinimumContentDim, MaximumContentDim, Style);
  569. }
  570. }