View.Layout.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. public partial class View // Layout APIs
  5. {
  6. #region Frame/Position/Dimension
  7. /// <summary>
  8. /// Indicates whether the specified SuperView-relative coordinates are within the View's <see cref="Frame"/>.
  9. /// </summary>
  10. /// <param name="location">SuperView-relative coordinate</param>
  11. /// <returns><see langword="true"/> if the specified SuperView-relative coordinates are within the View.</returns>
  12. public virtual bool Contains (in Point location) { return Frame.Contains (location); }
  13. private Rectangle? _frame;
  14. /// <summary>Gets or sets the absolute location and dimension of the view.</summary>
  15. /// <value>
  16. /// The rectangle describing absolute location and dimension of the view, in coordinates relative to the
  17. /// <see cref="SuperView"/>'s Content, which is bound by <see cref="GetContentSize ()"/>.
  18. /// </value>
  19. /// <remarks>
  20. /// <para>
  21. /// See the View Layout Deep Dive for more information:
  22. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  23. /// </para>
  24. /// <para>
  25. /// Frame is relative to the <see cref="SuperView"/>'s Content, which is bound by <see cref="GetContentSize ()"/>
  26. /// .
  27. /// </para>
  28. /// <para>
  29. /// Setting Frame will set <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> to
  30. /// absolute values.
  31. /// </para>
  32. /// <para>
  33. /// Changing this property will result in <see cref="NeedsLayout"/> and <see cref="NeedsDraw"/> to be set,
  34. /// resulting in the
  35. /// view being laid out and redrawn as appropriate in the next iteration of the <see cref="MainLoop"/>.
  36. /// </para>
  37. /// </remarks>
  38. public Rectangle Frame
  39. {
  40. get
  41. {
  42. if (NeedsLayout)
  43. {
  44. //Debug.WriteLine("Frame_get with _layoutNeeded");
  45. }
  46. return _frame ?? Rectangle.Empty;
  47. }
  48. set
  49. {
  50. // This will set _frame, call SetsNeedsLayout, and raise OnViewportChanged/ViewportChanged
  51. if (SetFrame (value with { Width = Math.Max (value.Width, 0), Height = Math.Max (value.Height, 0) }))
  52. {
  53. // If Frame gets set, set all Pos/Dim to Absolute values.
  54. _x = _frame!.Value.X;
  55. _y = _frame!.Value.Y;
  56. _width = _frame!.Value.Width;
  57. _height = _frame!.Value.Height;
  58. // Implicit layout is ok here because we are setting the Frame directly.
  59. Layout ();
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// INTERNAL API - Sets _frame, calls SetsNeedsLayout, and raises OnViewportChanged/ViewportChanged
  65. /// </summary>
  66. /// <param name="frame"></param>
  67. /// <returns><see langword="true"/> if the frame was changed.</returns>
  68. private bool SetFrame (in Rectangle frame)
  69. {
  70. if (_frame == frame)
  71. {
  72. return false;
  73. }
  74. var oldViewport = Rectangle.Empty;
  75. if (IsInitialized)
  76. {
  77. oldViewport = Viewport;
  78. }
  79. // This is the only place where _frame should be set directly. Use Frame = or SetFrame instead.
  80. _frame = frame;
  81. SetAdornmentFrames ();
  82. SetNeedsDraw ();
  83. SetNeedsLayout ();
  84. // BUGBUG: When SetFrame is called from Frame_set, this event gets raised BEFORE OnResizeNeeded. Is that OK?
  85. OnFrameChanged (in frame);
  86. FrameChanged?.Invoke (this, new (in frame));
  87. if (oldViewport != Viewport)
  88. {
  89. RaiseViewportChangedEvent (oldViewport);
  90. }
  91. return true;
  92. }
  93. /// <summary>
  94. /// Called when <see cref="Frame"/> changes.
  95. /// </summary>
  96. /// <param name="frame">The new Frame.</param>
  97. protected virtual void OnFrameChanged (in Rectangle frame) { }
  98. /// <summary>
  99. /// Raised when the <see cref="Frame"/> changes. This event is raised after the <see cref="Frame"/> has been
  100. /// updated.
  101. /// </summary>
  102. public event EventHandler<EventArgs<Rectangle>>? FrameChanged;
  103. /// <summary>Gets the <see cref="Frame"/> with a screen-relative location.</summary>
  104. /// <returns>The location and size of the view in screen-relative coordinates.</returns>
  105. public virtual Rectangle FrameToScreen ()
  106. {
  107. Rectangle screen = Frame;
  108. View? current = SuperView;
  109. while (current is { })
  110. {
  111. if (current is Adornment adornment)
  112. {
  113. // Adornments don't have SuperViews; use Adornment.FrameToScreen override
  114. // which will give us the screen coordinates of the parent
  115. Rectangle parentScreen = adornment.FrameToScreen ();
  116. // Now add our Frame location
  117. parentScreen.Offset (screen.X, screen.Y);
  118. return parentScreen with { Size = Frame.Size };
  119. }
  120. Point viewportOffset = current.GetViewportOffsetFromFrame ();
  121. viewportOffset.Offset (current.Frame.X - current.Viewport.X, current.Frame.Y - current.Viewport.Y);
  122. screen.X += viewportOffset.X;
  123. screen.Y += viewportOffset.Y;
  124. current = current.SuperView;
  125. }
  126. return screen;
  127. }
  128. /// <summary>
  129. /// Converts a screen-relative coordinate to a Frame-relative coordinate. Frame-relative means relative to the
  130. /// View's <see cref="SuperView"/>'s <see cref="Viewport"/>.
  131. /// </summary>
  132. /// <returns>The coordinate relative to the <see cref="SuperView"/>'s <see cref="Viewport"/>.</returns>
  133. /// <param name="location">Screen-relative coordinate.</param>
  134. public virtual Point ScreenToFrame (in Point location)
  135. {
  136. if (SuperView is null)
  137. {
  138. return new (location.X - Frame.X, location.Y - Frame.Y);
  139. }
  140. Point superViewViewportOffset = SuperView.GetViewportOffsetFromFrame ();
  141. superViewViewportOffset.Offset (-SuperView.Viewport.X, -SuperView.Viewport.Y);
  142. Point frame = location;
  143. frame.Offset (-superViewViewportOffset.X, -superViewViewportOffset.Y);
  144. frame = SuperView.ScreenToFrame (frame);
  145. frame.Offset (-Frame.X, -Frame.Y);
  146. return frame;
  147. }
  148. // helper for X, Y, Width, Height setters to ensure consistency
  149. private void PosDimSet ()
  150. {
  151. SetNeedsLayout ();
  152. if (_x is PosAbsolute && _y is PosAbsolute && _width is DimAbsolute && _height is DimAbsolute)
  153. {
  154. // Implicit layout is ok here because all Pos/Dim are Absolute values.
  155. Layout ();
  156. if (SuperView is { } || this is Adornment { Parent: null })
  157. {
  158. // Ensure the next Application iteration tries to layout again
  159. SetNeedsLayout ();
  160. }
  161. }
  162. }
  163. private Pos _x = Pos.Absolute (0);
  164. /// <summary>Gets or sets the X position for the view (the column).</summary>
  165. /// <value>The <see cref="Pos"/> object representing the X position.</value>
  166. /// <remarks>
  167. /// <para>
  168. /// See the View Layout Deep Dive for more information:
  169. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  170. /// </para>
  171. /// <para>
  172. /// The position is relative to the <see cref="SuperView"/>'s Content, which is bound by
  173. /// <see cref="GetContentSize ()"/>.
  174. /// </para>
  175. /// <para>
  176. /// If set to a relative value (e.g. <see cref="Pos.Center"/>) the value is indeterminate until the view has been
  177. /// laid out (e.g. <see cref="Layout(System.Drawing.Size)"/> has been called).
  178. /// </para>
  179. /// <para>
  180. /// Changing this property will result in <see cref="NeedsLayout"/> and <see cref="NeedsDraw"/> to be set,
  181. /// resulting in the
  182. /// view being laid out and redrawn as appropriate in the next iteration of the <see cref="MainLoop"/>.
  183. /// </para>
  184. /// <para>
  185. /// Changing this property will cause <see cref="Frame"/> to be updated.
  186. /// </para>
  187. /// <para>The default value is <c>Pos.At (0)</c>.</para>
  188. /// </remarks>
  189. public Pos X
  190. {
  191. get => VerifyIsInitialized (_x, nameof (X));
  192. set
  193. {
  194. if (Equals (_x, value))
  195. {
  196. return;
  197. }
  198. _x = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (X)} cannot be null");
  199. PosDimSet ();
  200. }
  201. }
  202. private Pos _y = Pos.Absolute (0);
  203. /// <summary>Gets or sets the Y position for the view (the row).</summary>
  204. /// <value>The <see cref="Pos"/> object representing the Y position.</value>
  205. /// <remarks>
  206. /// <para>
  207. /// See the View Layout Deep Dive for more information:
  208. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  209. /// </para>
  210. /// <para>
  211. /// The position is relative to the <see cref="SuperView"/>'s Content, which is bound by
  212. /// <see cref="GetContentSize ()"/>.
  213. /// </para>
  214. /// <para>
  215. /// If set to a relative value (e.g. <see cref="Pos.Center"/>) the value is indeterminate until the view has been
  216. /// laid out (e.g. <see cref="Layout(System.Drawing.Size)"/> has been called).
  217. /// </para>
  218. /// <para>
  219. /// Changing this property will result in <see cref="NeedsLayout"/> and <see cref="NeedsDraw"/> to be set,
  220. /// resulting in the
  221. /// view being laid out and redrawn as appropriate in the next iteration of the <see cref="MainLoop"/>.
  222. /// </para>
  223. /// <para>
  224. /// Changing this property will cause <see cref="Frame"/> to be updated.
  225. /// </para>
  226. /// <para>The default value is <c>Pos.At (0)</c>.</para>
  227. /// </remarks>
  228. public Pos Y
  229. {
  230. get => VerifyIsInitialized (_y, nameof (Y));
  231. set
  232. {
  233. if (Equals (_y, value))
  234. {
  235. return;
  236. }
  237. _y = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (Y)} cannot be null");
  238. PosDimSet ();
  239. }
  240. }
  241. private Dim? _height = Dim.Absolute (0);
  242. /// <summary>Gets or sets the height dimension of the view.</summary>
  243. /// <value>The <see cref="Dim"/> object representing the height of the view (the number of rows).</value>
  244. /// <remarks>
  245. /// <para>
  246. /// See the View Layout Deep Dive for more information:
  247. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  248. /// </para>
  249. /// <para>
  250. /// The dimension is relative to the <see cref="SuperView"/>'s Content, which is bound by
  251. /// <see cref="GetContentSize ()"/> .
  252. /// </para>
  253. /// <para>
  254. /// If set to a relative value (e.g. <see cref="DimFill"/>) the value is indeterminate until the view has been
  255. /// laid out (e.g. <see cref="Layout(System.Drawing.Size)"/> has been called).
  256. /// </para>
  257. /// <para>
  258. /// Changing this property will result in <see cref="NeedsLayout"/> and <see cref="NeedsDraw"/> to be set,
  259. /// resulting in the
  260. /// view being laid out and redrawn as appropriate in the next iteration of the <see cref="MainLoop"/>.
  261. /// </para>
  262. /// <para>
  263. /// Changing this property will cause <see cref="Frame"/> to be updated.
  264. /// </para>
  265. /// <para>The default value is <c>Dim.Sized (0)</c>.</para>
  266. /// </remarks>
  267. public Dim? Height
  268. {
  269. get => VerifyIsInitialized (_height, nameof (Height));
  270. set
  271. {
  272. if (Equals (_height, value))
  273. {
  274. return;
  275. }
  276. _height = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (Height)} cannot be null");
  277. // Reset TextFormatter - Will be recalculated in SetTextFormatterSize
  278. TextFormatter.ConstrainToHeight = null;
  279. PosDimSet ();
  280. }
  281. }
  282. private Dim? _width = Dim.Absolute (0);
  283. /// <summary>Gets or sets the width dimension of the view.</summary>
  284. /// <value>The <see cref="Dim"/> object representing the width of the view (the number of columns).</value>
  285. /// <remarks>
  286. /// <para>
  287. /// See the View Layout Deep Dive for more information:
  288. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  289. /// </para>
  290. /// <para>
  291. /// The dimension is relative to the <see cref="SuperView"/>'s Content, which is bound by
  292. /// <see cref="GetContentSize ()"/>
  293. /// .
  294. /// </para>
  295. /// <para>
  296. /// If set to a relative value (e.g. <see cref="DimFill"/>) the value is indeterminate until the view has been
  297. /// laid out (e.g. <see cref="Layout(System.Drawing.Size)"/> has been called).
  298. /// </para>
  299. /// <para>
  300. /// Changing this property will result in <see cref="NeedsLayout"/> and <see cref="NeedsDraw"/> to be set,
  301. /// resulting in the
  302. /// view being laid out and redrawn as appropriate in the next iteration of the <see cref="MainLoop"/>.
  303. /// </para>
  304. /// <para>
  305. /// Changing this property will cause <see cref="Frame"/> to be updated.
  306. /// </para>
  307. /// <para>The default value is <c>Dim.Sized (0)</c>.</para>
  308. /// </remarks>
  309. public Dim? Width
  310. {
  311. get => VerifyIsInitialized (_width, nameof (Width));
  312. set
  313. {
  314. if (Equals (_width, value))
  315. {
  316. return;
  317. }
  318. _width = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (Width)} cannot be null");
  319. // Reset TextFormatter - Will be recalculated in SetTextFormatterSize
  320. TextFormatter.ConstrainToWidth = null;
  321. PosDimSet ();
  322. }
  323. }
  324. #endregion Frame/Position/Dimension
  325. #region Core Layout API
  326. /// <summary>
  327. /// INTERNAL API - Performs layout of the specified views within the specified content size. Called by the Application
  328. /// main loop.
  329. /// </summary>
  330. /// <param name="views">The views to layout.</param>
  331. /// <param name="contentSize">The size to bound the views by.</param>
  332. /// <returns><see langword="true"/>If any of the views needed to be laid out.</returns>
  333. internal static bool Layout (IEnumerable<View> views, Size contentSize)
  334. {
  335. var neededLayout = false;
  336. foreach (View v in views)
  337. {
  338. if (v.NeedsLayout)
  339. {
  340. neededLayout = true;
  341. v.Layout (contentSize);
  342. }
  343. }
  344. return neededLayout;
  345. }
  346. /// <summary>
  347. /// Performs layout of the view and its subviews within the specified content size.
  348. /// </summary>
  349. /// <remarks>
  350. /// <para>
  351. /// See the View Layout Deep Dive for more information:
  352. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  353. /// </para>
  354. /// <para>
  355. /// This method is intended to be called by the layout engine to
  356. /// prepare the view for layout and is exposed as a public API primarily for testing purposes.
  357. /// </para>
  358. /// </remarks>
  359. /// <param name="contentSize"></param>
  360. /// <returns><see langword="false"/>If the view could not be laid out (typically because a dependencies was not ready). </returns>
  361. public bool Layout (Size contentSize)
  362. {
  363. if (SetRelativeLayout (contentSize))
  364. {
  365. LayoutSubviews ();
  366. // Debug.Assert(!NeedsLayout);
  367. return true;
  368. }
  369. return false;
  370. }
  371. /// <summary>
  372. /// Performs layout of the view and its subviews using the content size of either the <see cref="SuperView"/> or
  373. /// <see cref="Application.Screen"/>.
  374. /// </summary>
  375. /// <remarks>
  376. /// <para>
  377. /// See the View Layout Deep Dive for more information:
  378. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  379. /// </para>
  380. /// <para>
  381. /// This method is intended to be called by the layout engine to
  382. /// prepare the view for layout and is exposed as a public API primarily for testing purposes.
  383. /// </para>
  384. /// </remarks>
  385. /// <returns><see langword="false"/>If the view could not be laid out (typically because dependency was not ready). </returns>
  386. public bool Layout () { return Layout (GetContainerSize ()); }
  387. /// <summary>
  388. /// Sets the position and size of this view, relative to the SuperView's ContentSize (nominally the same as
  389. /// <c>this.SuperView.GetContentSize ()</c>) based on the values of <see cref="X"/>, <see cref="Y"/>,
  390. /// <see cref="Width"/>,
  391. /// and <see cref="Height"/>.
  392. /// </summary>
  393. /// <remarks>
  394. /// <para>
  395. /// If <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, or <see cref="Height"/> are
  396. /// absolute, they will be updated to reflect the new size and position of the view. Otherwise, they
  397. /// are left unchanged.
  398. /// </para>
  399. /// <para>
  400. /// This method does not arrange subviews or adornments. It is intended to be called by the layout engine to
  401. /// prepare the view for layout and is exposed as a public API primarily for testing purposes.
  402. /// </para>
  403. /// <para>
  404. /// Some subviews may have SetRelativeLayout called on them as a side effect, particularly in DimAuto scenarios.
  405. /// </para>
  406. /// </remarks>
  407. /// <param name="superviewContentSize">
  408. /// The size of the SuperView's content (nominally the same as <c>this.SuperView.GetContentSize ()</c>).
  409. /// </param>
  410. /// <returns><see langword="true"/> if successful. <see langword="false"/> means a dependent View still needs layout.</returns>
  411. public bool SetRelativeLayout (Size superviewContentSize)
  412. {
  413. Debug.Assert (_x is { });
  414. Debug.Assert (_y is { });
  415. Debug.Assert (_width is { });
  416. Debug.Assert (_height is { });
  417. CheckDimAuto ();
  418. // TODO: Should move to View.LayoutSubviews?
  419. SetTextFormatterSize ();
  420. int newX, newW, newY, newH;
  421. try
  422. {
  423. // Calculate the new X, Y, Width, and Height
  424. // If the Width or Height is Dim.Auto, calculate the Width or Height first. Otherwise, calculate the X or Y first.
  425. if (_width.Has<DimAuto> (out _))
  426. {
  427. newW = _width.Calculate (0, superviewContentSize.Width, this, Dimension.Width);
  428. newX = _x.Calculate (superviewContentSize.Width, newW, this, Dimension.Width);
  429. if (newW != Frame.Width)
  430. {
  431. // Pos.Calculate gave us a new position. We need to redo dimension
  432. newW = _width.Calculate (newX, superviewContentSize.Width, this, Dimension.Width);
  433. }
  434. }
  435. else
  436. {
  437. newX = _x.Calculate (superviewContentSize.Width, _width, this, Dimension.Width);
  438. newW = _width.Calculate (newX, superviewContentSize.Width, this, Dimension.Width);
  439. }
  440. if (_height.Has<DimAuto> (out _))
  441. {
  442. newH = _height.Calculate (0, superviewContentSize.Height, this, Dimension.Height);
  443. newY = _y.Calculate (superviewContentSize.Height, newH, this, Dimension.Height);
  444. if (newH != Frame.Height)
  445. {
  446. // Pos.Calculate gave us a new position. We need to redo dimension
  447. newH = _height.Calculate (newY, superviewContentSize.Height, this, Dimension.Height);
  448. }
  449. }
  450. else
  451. {
  452. newY = _y.Calculate (superviewContentSize.Height, _height, this, Dimension.Height);
  453. newH = _height.Calculate (newY, superviewContentSize.Height, this, Dimension.Height);
  454. }
  455. }
  456. catch (LayoutException)
  457. {
  458. //Debug.WriteLine ($"A Dim/PosFunc function threw (typically this is because a dependent View was not laid out)\n{le}.");
  459. return false;
  460. }
  461. Rectangle newFrame = new (newX, newY, newW, newH);
  462. if (Frame != newFrame)
  463. {
  464. // Set the frame. Do NOT use `Frame` as it overwrites X, Y, Width, and Height
  465. // This will set _frame, call SetsNeedsLayout, and raise OnViewportChanged/ViewportChanged
  466. SetFrame (newFrame);
  467. if (_x is PosAbsolute)
  468. {
  469. _x = Frame.X;
  470. }
  471. if (_y is PosAbsolute)
  472. {
  473. _y = Frame.Y;
  474. }
  475. if (_width is DimAbsolute)
  476. {
  477. _width = Frame.Width;
  478. }
  479. if (_height is DimAbsolute)
  480. {
  481. _height = Frame.Height;
  482. }
  483. if (!string.IsNullOrEmpty (Title))
  484. {
  485. SetTitleTextFormatterSize ();
  486. }
  487. if (SuperView is { })
  488. {
  489. SuperView?.SetNeedsDraw ();
  490. }
  491. else
  492. {
  493. Application.ClearScreenNextIteration = true;
  494. }
  495. }
  496. if (TextFormatter.ConstrainToWidth is null)
  497. {
  498. TextFormatter.ConstrainToWidth = GetContentSize ().Width;
  499. }
  500. if (TextFormatter.ConstrainToHeight is null)
  501. {
  502. TextFormatter.ConstrainToHeight = GetContentSize ().Height;
  503. }
  504. return true;
  505. }
  506. /// <summary>
  507. /// INTERNAL API - Causes the view's subviews and adornments to be laid out within the view's content areas. Assumes
  508. /// the view's relative layout has been set via <see cref="SetRelativeLayout"/>.
  509. /// </summary>
  510. /// <remarks>
  511. /// <para>
  512. /// See the View Layout Deep Dive for more information:
  513. /// <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/layout.html"/>
  514. /// </para>
  515. /// <para>
  516. /// The position and dimensions of the view are indeterminate until the view has been initialized. Therefore, the
  517. /// behavior of this method is indeterminate if <see cref="IsInitialized"/> is <see langword="false"/>.
  518. /// </para>
  519. /// <para>Raises the <see cref="SubviewsLaidOut"/> event before it returns.</para>
  520. /// </remarks>
  521. internal void LayoutSubviews ()
  522. {
  523. if (!NeedsLayout)
  524. {
  525. return;
  526. }
  527. CheckDimAuto ();
  528. Size contentSize = GetContentSize ();
  529. OnSubviewLayout (new (contentSize));
  530. SubviewLayout?.Invoke (this, new (contentSize));
  531. // The Adornments already have their Frame's set by SetRelativeLayout so we call LayoutSubViews vs. Layout here.
  532. if (Margin is { Subviews.Count: > 0 })
  533. {
  534. Margin.LayoutSubviews ();
  535. }
  536. if (Border is { Subviews.Count: > 0 })
  537. {
  538. Border.LayoutSubviews ();
  539. }
  540. if (Padding is { Subviews.Count: > 0 })
  541. {
  542. Padding.LayoutSubviews ();
  543. }
  544. // Sort out the dependencies of the X, Y, Width, Height properties
  545. HashSet<View> nodes = new ();
  546. HashSet<(View, View)> edges = new ();
  547. CollectAll (this, ref nodes, ref edges);
  548. List<View> ordered = TopologicalSort (SuperView!, nodes, edges);
  549. List<View> redo = new ();
  550. foreach (View v in ordered)
  551. {
  552. if (!v.Layout (contentSize))
  553. {
  554. redo.Add (v);
  555. }
  556. }
  557. var layoutStillNeeded = false;
  558. if (redo.Count > 0)
  559. {
  560. foreach (View v in ordered)
  561. {
  562. if (!v.Layout (contentSize))
  563. {
  564. layoutStillNeeded = true;
  565. }
  566. }
  567. }
  568. // If the 'to' is rooted to 'from' it's a special-case.
  569. // Use Layout with the ContentSize of the 'from'.
  570. // See the Nested_SubViews_Ref_Topmost_SuperView unit test
  571. if (edges.Count > 0 && GetTopSuperView () is { })
  572. {
  573. foreach ((View from, View to) in edges)
  574. {
  575. // QUESTION: Do we test this with adornments well enough?
  576. to.Layout (from.GetContentSize ());
  577. }
  578. }
  579. NeedsLayout = layoutStillNeeded;
  580. OnSubviewsLaidOut (new (contentSize));
  581. SubviewsLaidOut?.Invoke (this, new (contentSize));
  582. }
  583. /// <summary>
  584. /// Called from <see cref="LayoutSubviews"/> before any subviews
  585. /// have been laid out.
  586. /// </summary>
  587. /// <remarks>
  588. /// Override to perform tasks when the layout is changing.
  589. /// </remarks>
  590. protected virtual void OnSubviewLayout (LayoutEventArgs args) { }
  591. /// <summary>
  592. /// Raised by <see cref="LayoutSubviews"/> before any subviews
  593. /// have been laid out.
  594. /// </summary>
  595. /// <remarks>
  596. /// Subscribe to this event to perform tasks when the layout is changing.
  597. /// </remarks>
  598. public event EventHandler<LayoutEventArgs>? SubviewLayout;
  599. /// <summary>
  600. /// Called from <see cref="LayoutSubviews"/> after all sub-views
  601. /// have been laid out.
  602. /// </summary>
  603. /// <remarks>
  604. /// Override to perform tasks after the <see cref="View"/> has been resized or the layout has
  605. /// otherwise changed.
  606. /// </remarks>
  607. protected virtual void OnSubviewsLaidOut (LayoutEventArgs args) { }
  608. /// <summary>Raised after all sub-views have been laid out.</summary>
  609. /// <remarks>
  610. /// Subscribe to this event to perform tasks after the <see cref="View"/> has been resized or the layout has
  611. /// otherwise changed.
  612. /// </remarks>
  613. public event EventHandler<LayoutEventArgs>? SubviewsLaidOut;
  614. #endregion Core Layout API
  615. #region NeedsLayout
  616. // We expose no setter for this to ensure that the ONLY place it's changed is in SetNeedsLayout
  617. /// <summary>
  618. /// Indicates the View's Frame or the layout of the View's subviews (including Adornments) have
  619. /// changed since the last time the View was laid out.
  620. /// </summary>
  621. /// <remarks>
  622. /// <para>
  623. /// Used to prevent <see cref="Layout()"/> from needlessly computing
  624. /// layout.
  625. /// </para>
  626. /// </remarks>
  627. /// <value>
  628. /// <see langword="true"/> if layout is needed.
  629. /// </value>
  630. public bool NeedsLayout { get; private set; } = true;
  631. /// <summary>
  632. /// Sets <see cref="NeedsLayout"/> to return <see langword="true"/>, indicating this View and all of it's subviews
  633. /// (including adornments) need to be laid out in the next Application iteration.
  634. /// </summary>
  635. /// <remarks>
  636. /// <para>
  637. /// The <see cref="MainLoop"/> will cause <see cref="Layout()"/> to be called on the next
  638. /// <see cref="Application.Iteration"/> so there is normally no reason to call see <see cref="Layout()"/>.
  639. /// </para>
  640. /// </remarks>
  641. public void SetNeedsLayout ()
  642. {
  643. NeedsLayout = true;
  644. if (Margin is { Subviews.Count: > 0 })
  645. {
  646. Margin.SetNeedsLayout ();
  647. }
  648. if (Border is { Subviews.Count: > 0 })
  649. {
  650. Border.SetNeedsLayout ();
  651. }
  652. if (Padding is { Subviews.Count: > 0 })
  653. {
  654. Padding.SetNeedsLayout ();
  655. }
  656. // Use a stack to avoid recursion
  657. Stack<View> stack = new (Subviews);
  658. while (stack.Count > 0)
  659. {
  660. View current = stack.Pop ();
  661. if (!current.NeedsLayout)
  662. {
  663. current.NeedsLayout = true;
  664. if (current.Margin is { Subviews.Count: > 0 })
  665. {
  666. current.Margin.SetNeedsLayout ();
  667. }
  668. if (current.Border is { Subviews.Count: > 0 })
  669. {
  670. current.Border.SetNeedsLayout ();
  671. }
  672. if (current.Padding is { Subviews.Count: > 0 })
  673. {
  674. current.Padding.SetNeedsLayout ();
  675. }
  676. foreach (View subview in current.Subviews)
  677. {
  678. stack.Push (subview);
  679. }
  680. }
  681. }
  682. TextFormatter.NeedsFormat = true;
  683. if (SuperView is { NeedsLayout: false })
  684. {
  685. SuperView?.SetNeedsLayout ();
  686. }
  687. if (SuperView is null)
  688. {
  689. foreach (Toplevel tl in Application.TopLevels)
  690. {
  691. // tl.SetNeedsDraw ();
  692. }
  693. }
  694. if (this is not Adornment adornment)
  695. {
  696. return;
  697. }
  698. if (adornment.Parent is { NeedsLayout: false })
  699. {
  700. adornment.Parent?.SetNeedsLayout ();
  701. }
  702. }
  703. #endregion NeedsLayout
  704. #region Topological Sort
  705. /// <summary>
  706. /// INTERNAL API - Collects all views and their dependencies from a given starting view for layout purposes. Used by
  707. /// <see cref="TopologicalSort"/> to create an ordered list of views to layout.
  708. /// </summary>
  709. /// <param name="from">The starting view from which to collect dependencies.</param>
  710. /// <param name="nNodes">A reference to a set of views representing nodes in the layout graph.</param>
  711. /// <param name="nEdges">
  712. /// A reference to a set of tuples representing edges in the layout graph, where each tuple consists of a pair of views
  713. /// indicating a dependency.
  714. /// </param>
  715. internal void CollectAll (View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
  716. {
  717. foreach (View? v in from.InternalSubviews)
  718. {
  719. nNodes.Add (v);
  720. CollectPos (v.X, v, ref nNodes, ref nEdges);
  721. CollectPos (v.Y, v, ref nNodes, ref nEdges);
  722. CollectDim (v.Width, v, ref nNodes, ref nEdges);
  723. CollectDim (v.Height, v, ref nNodes, ref nEdges);
  724. }
  725. }
  726. /// <summary>
  727. /// INTERNAL API - Collects dimension (where Width or Height is `DimView`) dependencies for a given view.
  728. /// </summary>
  729. /// <param name="dim">The dimension (width or height) to collect dependencies for.</param>
  730. /// <param name="from">The view for which to collect dimension dependencies.</param>
  731. /// <param name="nNodes">A reference to a set of views representing nodes in the layout graph.</param>
  732. /// <param name="nEdges">
  733. /// A reference to a set of tuples representing edges in the layout graph, where each tuple consists of a pair of views
  734. /// indicating a dependency.
  735. /// </param>
  736. internal void CollectDim (Dim? dim, View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
  737. {
  738. if (dim!.Has (out DimView dv))
  739. {
  740. if (dv.Target != this)
  741. {
  742. nEdges.Add ((dv.Target!, from));
  743. }
  744. }
  745. if (dim!.Has (out DimCombine dc))
  746. {
  747. CollectDim (dc.Left, from, ref nNodes, ref nEdges);
  748. CollectDim (dc.Right, from, ref nNodes, ref nEdges);
  749. }
  750. }
  751. /// <summary>
  752. /// INTERNAL API - Collects position (where X or Y is `PosView`) dependencies for a given view.
  753. /// </summary>
  754. /// <param name="pos">The position (X or Y) to collect dependencies for.</param>
  755. /// <param name="from">The view for which to collect position dependencies.</param>
  756. /// <param name="nNodes">A reference to a set of views representing nodes in the layout graph.</param>
  757. /// <param name="nEdges">
  758. /// A reference to a set of tuples representing edges in the layout graph, where each tuple consists of a pair of views
  759. /// indicating a dependency.
  760. /// </param>
  761. internal void CollectPos (Pos pos, View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
  762. {
  763. // TODO: Use Pos.Has<T> instead.
  764. switch (pos)
  765. {
  766. case PosView pv:
  767. Debug.Assert (pv.Target is { });
  768. if (pv.Target != this)
  769. {
  770. nEdges.Add ((pv.Target!, from));
  771. }
  772. return;
  773. case PosCombine pc:
  774. CollectPos (pc.Left, from, ref nNodes, ref nEdges);
  775. CollectPos (pc.Right, from, ref nNodes, ref nEdges);
  776. break;
  777. }
  778. }
  779. // https://en.wikipedia.org/wiki/Topological_sorting
  780. internal static List<View> TopologicalSort (
  781. View superView,
  782. IEnumerable<View> nodes,
  783. ICollection<(View From, View To)> edges
  784. )
  785. {
  786. List<View> result = new ();
  787. // Set of all nodes with no incoming edges
  788. HashSet<View> noEdgeNodes = new (nodes.Where (n => edges.All (e => !e.To.Equals (n))));
  789. while (noEdgeNodes.Any ())
  790. {
  791. // remove a node n from S
  792. View n = noEdgeNodes.First ();
  793. noEdgeNodes.Remove (n);
  794. // add n to tail of L
  795. if (n != superView)
  796. {
  797. result.Add (n);
  798. }
  799. // for each node m with an edge e from n to m do
  800. foreach ((View From, View To) e in edges.Where (e => e.From.Equals (n)).ToArray ())
  801. {
  802. View m = e.To;
  803. // remove edge e from the graph
  804. edges.Remove (e);
  805. // if m has no other incoming edges then
  806. if (edges.All (me => !me.To.Equals (m)) && m != superView)
  807. {
  808. // insert m into S
  809. noEdgeNodes.Add (m);
  810. }
  811. }
  812. }
  813. if (!edges.Any ())
  814. {
  815. return result;
  816. }
  817. foreach ((View from, View to) in edges)
  818. {
  819. if (from == to)
  820. {
  821. // if not yet added to the result, add it and remove from edge
  822. if (result.Find (v => v == from) is null)
  823. {
  824. result.Add (from);
  825. }
  826. edges.Remove ((from, to));
  827. }
  828. else if (from.SuperView == to.SuperView)
  829. {
  830. // if 'from' is not yet added to the result, add it
  831. if (result.Find (v => v == from) is null)
  832. {
  833. result.Add (from);
  834. }
  835. // if 'to' is not yet added to the result, add it
  836. if (result.Find (v => v == to) is null)
  837. {
  838. result.Add (to);
  839. }
  840. // remove from edge
  841. edges.Remove ((from, to));
  842. }
  843. else if (from != superView?.GetTopSuperView (to, from) && !ReferenceEquals (from, to))
  844. {
  845. if (ReferenceEquals (from.SuperView, to))
  846. {
  847. throw new LayoutException (
  848. $"ComputedLayout for \"{superView}\": \"{to}\" "
  849. + $"references a SubView (\"{from}\")."
  850. );
  851. }
  852. throw new LayoutException (
  853. $"ComputedLayout for \"{superView}\": \"{from}\" "
  854. + $"linked with \"{to}\" was not found. Did you forget to add it to {superView}?"
  855. );
  856. }
  857. }
  858. // return L (a topologically sorted order)
  859. return result;
  860. } // TopologicalSort
  861. #endregion Topological Sort
  862. #region Utilities
  863. /// <summary>
  864. /// INTERNAL API - Gets the size of the SuperView's content (nominally the same as
  865. /// the SuperView's <see cref="GetContentSize ()"/>) or the screen size if there's no SuperView.
  866. /// </summary>
  867. /// <returns></returns>
  868. private Size GetContainerSize ()
  869. {
  870. // TODO: Get rid of refs to Top
  871. Size superViewContentSize = SuperView?.GetContentSize ()
  872. ?? (Application.Top is { } && Application.Top != this && Application.Top.IsInitialized
  873. ? Application.Top.GetContentSize ()
  874. : Application.Screen.Size);
  875. return superViewContentSize;
  876. }
  877. // BUGBUG: This method interferes with Dialog/MessageBox default min/max size.
  878. // TODO: Get rid of MenuBar coupling as part of https://github.com/gui-cs/Terminal.Gui/issues/2975
  879. /// <summary>
  880. /// Gets a new location of the <see cref="View"/> that is within the Viewport of the <paramref name="viewToMove"/>'s
  881. /// <see cref="View.SuperView"/> (e.g. for dragging a Window). The `out` parameters are the new X and Y coordinates.
  882. /// </summary>
  883. /// <remarks>
  884. /// If <paramref name="viewToMove"/> does not have a <see cref="View.SuperView"/> or it's SuperView is not
  885. /// <see cref="Application.Top"/> the position will be bound by <see cref="Application.Screen"/>.
  886. /// </remarks>
  887. /// <param name="viewToMove">The View that is to be moved.</param>
  888. /// <param name="targetX">The target x location.</param>
  889. /// <param name="targetY">The target y location.</param>
  890. /// <param name="nx">The new x location that will ensure <paramref name="viewToMove"/> will be fully visible.</param>
  891. /// <param name="ny">The new y location that will ensure <paramref name="viewToMove"/> will be fully visible.</param>
  892. /// <returns>
  893. /// Either <see cref="Application.Top"/> (if <paramref name="viewToMove"/> does not have a Super View) or
  894. /// <paramref name="viewToMove"/>'s SuperView. This can be used to ensure LayoutSubviews is called on the correct View.
  895. /// </returns>
  896. internal static View? GetLocationEnsuringFullVisibility (
  897. View viewToMove,
  898. int targetX,
  899. int targetY,
  900. out int nx,
  901. out int ny
  902. )
  903. {
  904. int maxDimension;
  905. View? superView;
  906. if (viewToMove is not Toplevel || viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top)
  907. {
  908. maxDimension = Application.Screen.Width;
  909. superView = Application.Top;
  910. }
  911. else
  912. {
  913. // Use the SuperView's Viewport, not Frame
  914. maxDimension = viewToMove!.SuperView.Viewport.Width;
  915. superView = viewToMove.SuperView;
  916. }
  917. if (superView?.Margin is { } && superView == viewToMove!.SuperView)
  918. {
  919. maxDimension -= superView.GetAdornmentsThickness ().Left + superView.GetAdornmentsThickness ().Right;
  920. }
  921. if (viewToMove!.Frame.Width <= maxDimension)
  922. {
  923. nx = Math.Max (targetX, 0);
  924. nx = nx + viewToMove.Frame.Width > maxDimension ? Math.Max (maxDimension - viewToMove.Frame.Width, 0) : nx;
  925. if (nx > viewToMove.Frame.X + viewToMove.Frame.Width)
  926. {
  927. nx = Math.Max (viewToMove.Frame.Right, 0);
  928. }
  929. }
  930. else
  931. {
  932. nx = targetX;
  933. }
  934. //System.Diagnostics.Debug.WriteLine ($"nx:{nx}, rWidth:{rWidth}");
  935. var menuVisible = false;
  936. var statusVisible = false;
  937. if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top)
  938. {
  939. menuVisible = Application.Top?.MenuBar?.Visible == true;
  940. }
  941. else
  942. {
  943. View? t = viewToMove!.SuperView;
  944. while (t is { } and not Toplevel)
  945. {
  946. t = t.SuperView;
  947. }
  948. if (t is Toplevel topLevel)
  949. {
  950. menuVisible = topLevel.MenuBar?.Visible == true;
  951. }
  952. }
  953. if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top)
  954. {
  955. maxDimension = menuVisible ? 1 : 0;
  956. }
  957. else
  958. {
  959. maxDimension = 0;
  960. }
  961. ny = Math.Max (targetY, maxDimension);
  962. if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top)
  963. {
  964. maxDimension = statusVisible ? Application.Screen.Height - 1 : Application.Screen.Height;
  965. }
  966. else
  967. {
  968. maxDimension = statusVisible ? viewToMove!.SuperView.Viewport.Height - 1 : viewToMove!.SuperView.Viewport.Height;
  969. }
  970. if (superView?.Margin is { } && superView == viewToMove?.SuperView)
  971. {
  972. maxDimension -= superView.GetAdornmentsThickness ().Top + superView.GetAdornmentsThickness ().Bottom;
  973. }
  974. ny = Math.Min (ny, maxDimension);
  975. if (viewToMove?.Frame.Height <= maxDimension)
  976. {
  977. ny = ny + viewToMove.Frame.Height > maxDimension
  978. ? Math.Max (maxDimension - viewToMove.Frame.Height, menuVisible ? 1 : 0)
  979. : ny;
  980. if (ny > viewToMove.Frame.Y + viewToMove.Frame.Height)
  981. {
  982. ny = Math.Max (viewToMove.Frame.Bottom, 0);
  983. }
  984. }
  985. //System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}");
  986. return superView!;
  987. }
  988. #endregion Utilities
  989. #region Diagnostics and Verification
  990. // Diagnostics to highlight when X or Y is read before the view has been initialized
  991. private Pos VerifyIsInitialized (Pos pos, string member)
  992. {
  993. //#if DEBUG
  994. // if (pos.ReferencesOtherViews () && !IsInitialized)
  995. // {
  996. // Debug.WriteLine (
  997. // $"WARNING: {member} = {pos} of {this} is dependent on other views and {member} "
  998. // + $"is being accessed before the View has been initialized. This is likely a bug."
  999. // );
  1000. // }
  1001. //#endif // DEBUG
  1002. return pos;
  1003. }
  1004. // Diagnostics to highlight when Width or Height is read before the view has been initialized
  1005. private Dim? VerifyIsInitialized (Dim? dim, string member)
  1006. {
  1007. //#if DEBUG
  1008. // if (dim.ReferencesOtherViews () && !IsInitialized)
  1009. // {
  1010. // Debug.WriteLine (
  1011. // $"WARNING: {member} = {dim} of {this} is dependent on other views and {member} "
  1012. // + $"is being accessed before the View has been initialized. This is likely a bug."
  1013. // );
  1014. // }
  1015. //#endif // DEBUG
  1016. return dim;
  1017. }
  1018. /// <summary>Gets or sets whether validation of <see cref="Pos"/> and <see cref="Dim"/> occurs.</summary>
  1019. /// <remarks>
  1020. /// Setting this to <see langword="true"/> will enable validation of <see cref="X"/>, <see cref="Y"/>,
  1021. /// <see cref="Width"/>, and <see cref="Height"/> during set operations and in <see cref="LayoutSubviews"/>. If invalid
  1022. /// settings are discovered exceptions will be thrown indicating the error. This will impose a performance penalty and
  1023. /// thus should only be used for debugging.
  1024. /// </remarks>
  1025. public bool ValidatePosDim { get; set; }
  1026. // TODO: Move this logic into the Pos/Dim classes
  1027. /// <summary>
  1028. /// Throws an <see cref="InvalidOperationException"/> if any SubViews are using Dim objects that depend on this
  1029. /// Views dimensions.
  1030. /// </summary>
  1031. /// <exception cref="InvalidOperationException"></exception>
  1032. private void CheckDimAuto ()
  1033. {
  1034. if (!ValidatePosDim || !IsInitialized)
  1035. {
  1036. return;
  1037. }
  1038. var widthAuto = Width as DimAuto;
  1039. var heightAuto = Height as DimAuto;
  1040. // Verify none of the subviews are using Dim objects that depend on the SuperView's dimensions.
  1041. foreach (View view in Subviews)
  1042. {
  1043. if (widthAuto is { } && widthAuto.Style.FastHasFlags (DimAutoStyle.Content) && ContentSizeTracksViewport)
  1044. {
  1045. ThrowInvalid (view, view.Width, nameof (view.Width));
  1046. ThrowInvalid (view, view.X, nameof (view.X));
  1047. }
  1048. if (heightAuto is { } && heightAuto.Style.FastHasFlags (DimAutoStyle.Content) && ContentSizeTracksViewport)
  1049. {
  1050. ThrowInvalid (view, view.Height, nameof (view.Height));
  1051. ThrowInvalid (view, view.Y, nameof (view.Y));
  1052. }
  1053. }
  1054. return;
  1055. void ThrowInvalid (View view, object? checkPosDim, string name)
  1056. {
  1057. object? bad = null;
  1058. switch (checkPosDim)
  1059. {
  1060. case Pos pos and PosAnchorEnd:
  1061. break;
  1062. case Pos pos and not PosAbsolute and not PosView and not PosCombine:
  1063. bad = pos;
  1064. break;
  1065. case Pos pos and PosCombine:
  1066. // Recursively check for not Absolute or not View
  1067. ThrowInvalid (view, (pos as PosCombine)?.Left, name);
  1068. ThrowInvalid (view, (pos as PosCombine)?.Right, name);
  1069. break;
  1070. case Dim dim and DimAuto:
  1071. break;
  1072. case Dim dim and DimFill:
  1073. break;
  1074. case Dim dim and not DimAbsolute and not DimView and not DimCombine:
  1075. bad = dim;
  1076. break;
  1077. case Dim dim and DimCombine:
  1078. // Recursively check for not Absolute or not View
  1079. ThrowInvalid (view, (dim as DimCombine)?.Left, name);
  1080. ThrowInvalid (view, (dim as DimCombine)?.Right, name);
  1081. break;
  1082. }
  1083. if (bad != null)
  1084. {
  1085. throw new LayoutException (
  1086. $"{view.GetType ().Name}.{name} = {bad.GetType ().Name} "
  1087. + $"which depends on the SuperView's dimensions and the SuperView uses Dim.Auto."
  1088. );
  1089. }
  1090. }
  1091. }
  1092. #endregion Diagnostics and Verification
  1093. }