View.Layout.cs 46 KB

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