DimAuto.cs 31 KB

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