View.Drawing.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using Microsoft.CodeAnalysis.Operations;
  5. namespace Terminal.Gui;
  6. public partial class View // Drawing APIs
  7. {
  8. /// <summary>
  9. /// Draws the view if it needs to be drawn.
  10. /// </summary>
  11. /// <remarks>
  12. /// <para>
  13. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDisplay"/>,
  14. /// <see cref="SubViewNeedsDisplay"/>,
  15. /// or <see cref="NeedsLayout"/> set.
  16. /// </para>
  17. /// <para>
  18. /// // TODO: Add docs for the drawing process.
  19. /// </para>
  20. /// </remarks>
  21. public void Draw ()
  22. {
  23. if (!CanBeVisible (this) || (!NeedsDisplay && !SubViewNeedsDisplay))
  24. {
  25. return;
  26. }
  27. DoDrawAdornments ();
  28. DoSetAttribute ();
  29. // By default, we clip to the viewport preventing drawing outside the viewport
  30. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  31. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  32. Rectangle prevClip = SetClip ();
  33. DoClearViewport (Viewport);
  34. DoDrawText (Viewport);
  35. DoDrawContent (Viewport);
  36. DoDrawSubviews (Viewport);
  37. // Restore the clip before rendering the line canvas and adornment subviews
  38. // because they may draw outside the viewport.
  39. if (Driver is { })
  40. {
  41. Driver.Clip = prevClip;
  42. }
  43. DoRenderLineCanvas ();
  44. DoDrawAdornmentSubViews ();
  45. ClearNeedsDisplay ();
  46. // We're done
  47. DoDrawComplete ();
  48. }
  49. #region DrawAdornments
  50. private void DoDrawAdornmentSubViews ()
  51. {
  52. // This causes the Adornment's subviews to be REDRAWN
  53. // TODO: Figure out how to make this more efficient
  54. if (Margin?.Subviews is { })
  55. {
  56. foreach (View subview in Margin.Subviews)
  57. {
  58. subview.SetNeedsDisplay ();
  59. }
  60. Margin?.DoDrawSubviews (Margin.Viewport);
  61. }
  62. if (Border?.Subviews is { })
  63. {
  64. foreach (View subview in Border.Subviews)
  65. {
  66. subview.SetNeedsDisplay ();
  67. }
  68. Border?.DoDrawSubviews (Border.Viewport);
  69. }
  70. if (Padding?.Subviews is { })
  71. {
  72. foreach (View subview in Padding.Subviews)
  73. {
  74. subview.SetNeedsDisplay ();
  75. }
  76. Padding?.DoDrawSubviews (Padding.Viewport);
  77. }
  78. }
  79. private void DoDrawAdornments ()
  80. {
  81. if (OnDrawingAdornments ())
  82. {
  83. return;
  84. }
  85. // TODO: add event.
  86. // Subviews of Adornments count as subviews
  87. if (!NeedsDisplay && !SubViewNeedsDisplay)
  88. {
  89. return;
  90. }
  91. DrawAdornments ();
  92. }
  93. /// <summary>
  94. /// Causes each of the View's Adornments to be drawn. This includes the <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>.
  95. /// </summary>
  96. public void DrawAdornments ()
  97. {
  98. // Each of these renders lines to either this View's LineCanvas
  99. // Those lines will be finally rendered in OnRenderLineCanvas
  100. Margin?.Draw ();
  101. Border?.Draw ();
  102. Padding?.Draw ();
  103. }
  104. /// <summary>
  105. /// Called when the View's Adornments are to be drawn. Prepares <see cref="View.LineCanvas"/>. If
  106. /// <see cref="SuperViewRendersLineCanvas"/> is true, only the
  107. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  108. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  109. /// </summary>
  110. /// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
  111. protected virtual bool OnDrawingAdornments () { return false; }
  112. #endregion DrawAdornments
  113. #region SetAttribute
  114. private void DoSetAttribute ()
  115. {
  116. if (OnSettingAttribute ())
  117. {
  118. return;
  119. }
  120. var args = new CancelEventArgs ();
  121. SettingAttribute?.Invoke (this, args);
  122. if (args.Cancel)
  123. {
  124. return;
  125. }
  126. if (!NeedsDisplay && !SubViewNeedsDisplay)
  127. {
  128. return;
  129. }
  130. SetNormalAttribute ();
  131. }
  132. /// <summary>
  133. /// Called when the normal attribute for the View is to be set. This is called before the View is drawn.
  134. /// </summary>
  135. /// <returns><see langword="true"/> to stop default behavior.</returns>
  136. protected virtual bool OnSettingAttribute () { return false; }
  137. /// <summary>Raised when the normal attribute for the View is to be set. This is raised before the View is drawn.</summary>
  138. /// <returns>
  139. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop default behavior.
  140. /// </returns>
  141. public event EventHandler<CancelEventArgs>? SettingAttribute;
  142. /// <summary>
  143. /// Sets the attribute for the View. This is called before the View is drawn.
  144. /// </summary>
  145. public void SetNormalAttribute ()
  146. {
  147. if (ColorScheme is { })
  148. {
  149. SetAttribute (GetNormalColor ());
  150. }
  151. }
  152. #endregion
  153. #region ClearViewport
  154. private void DoClearViewport (Rectangle viewport)
  155. {
  156. Debug.Assert (viewport == Viewport);
  157. if (OnClearingViewport (Viewport))
  158. {
  159. return;
  160. }
  161. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  162. ClearingViewport?.Invoke (this, dev);
  163. if (dev.Cancel)
  164. {
  165. return;
  166. }
  167. if (!NeedsDisplay)
  168. {
  169. return;
  170. }
  171. ClearViewport ();
  172. }
  173. /// <summary>
  174. /// Called when the <see cref="Viewport"/> is to be cleared.
  175. /// </summary>
  176. /// <param name="viewport"></param>
  177. /// <returns><see langword="true"/> to stop further clearing.</returns>
  178. protected virtual bool OnClearingViewport (Rectangle viewport) { return false; }
  179. /// <summary>Event invoked when the content area of the View is to be drawn.</summary>
  180. /// <remarks>
  181. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  182. /// <para>
  183. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  184. /// <see cref="View"/> .
  185. /// </para>
  186. /// </remarks>
  187. public event EventHandler<DrawEventArgs>? ClearingViewport;
  188. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  189. /// <remarks>
  190. /// <para>
  191. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClearContentOnly"/> only
  192. /// the portion of the content
  193. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  194. /// a
  195. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettings.AllowNegativeLocation"/> is
  196. /// enabled) and want
  197. /// the area outside the content to be visually distinct.
  198. /// </para>
  199. /// </remarks>
  200. public void ClearViewport ()
  201. {
  202. if (Driver is null)
  203. {
  204. return;
  205. }
  206. // Get screen-relative coords
  207. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  208. Rectangle prevClip = Driver.Clip;
  209. if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
  210. {
  211. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  212. toClear = Rectangle.Intersect (toClear, visibleContent);
  213. }
  214. Attribute prev = SetAttribute (GetNormalColor ());
  215. Driver.FillRect (toClear);
  216. SetAttribute (prev);
  217. Driver.Clip = prevClip;
  218. SetNeedsDisplay ();
  219. }
  220. #endregion ClearViewport
  221. #region DrawText
  222. private void DoDrawText (Rectangle viewport)
  223. {
  224. Debug.Assert (viewport == Viewport);
  225. if (OnDrawingText (Viewport))
  226. {
  227. return;
  228. }
  229. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  230. DrawingText?.Invoke (this, dev);
  231. if (dev.Cancel)
  232. {
  233. return;
  234. }
  235. if (!NeedsDisplay)
  236. {
  237. return;
  238. }
  239. DrawText ();
  240. }
  241. /// <summary>
  242. /// Called when the <see cref="Text"/> of the View is to be drawn.
  243. /// </summary>
  244. /// <param name="viewport"></param>
  245. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  246. protected virtual bool OnDrawingText (Rectangle viewport) { return false; }
  247. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  248. /// <returns>
  249. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  250. /// <see cref="Text"/>.
  251. /// </returns>
  252. public event EventHandler<DrawEventArgs>? DrawingText;
  253. /// <summary>
  254. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  255. /// </summary>
  256. public void DrawText ()
  257. {
  258. if (!string.IsNullOrEmpty (TextFormatter.Text))
  259. {
  260. TextFormatter.NeedsFormat = true;
  261. }
  262. // This should NOT clear
  263. // TODO: If the output is not in the Viewport, do nothing
  264. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  265. TextFormatter?.Draw (
  266. drawRect,
  267. HasFocus ? GetFocusColor () : GetNormalColor (),
  268. HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
  269. Rectangle.Empty
  270. );
  271. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  272. SetSubViewNeedsDisplay ();
  273. }
  274. #endregion DrawText
  275. #region DrawContent
  276. private void DoDrawContent (Rectangle viewport)
  277. {
  278. Debug.Assert (viewport == Viewport);
  279. if (OnDrawingContent (Viewport))
  280. {
  281. return;
  282. }
  283. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  284. DrawingContent?.Invoke (this, dev);
  285. if (dev.Cancel)
  286. { }
  287. // Do nothing.
  288. }
  289. /// <summary>
  290. /// Called when the View's content is to be drawn. The default implementation does nothing.
  291. /// </summary>
  292. /// <remarks>
  293. /// </remarks>
  294. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  295. protected virtual bool OnDrawingContent (Rectangle viewport) { return false; }
  296. /// <summary>Raised when the View's content is to be drawn.</summary>
  297. /// <remarks>
  298. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  299. /// <para>
  300. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  301. /// <see cref="View"/> .
  302. /// </para>
  303. /// </remarks>
  304. public event EventHandler<DrawEventArgs>? DrawingContent;
  305. #endregion DrawContent
  306. #region DrawSubviews
  307. private void DoDrawSubviews (Rectangle viewport)
  308. {
  309. Debug.Assert (viewport == Viewport);
  310. if (OnDrawingSubviews (Viewport))
  311. {
  312. return;
  313. }
  314. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  315. DrawingSubviews?.Invoke (this, dev);
  316. if (dev.Cancel)
  317. {
  318. return;
  319. }
  320. if (!SubViewNeedsDisplay)
  321. {
  322. return;
  323. }
  324. DrawSubviews ();
  325. }
  326. /// <summary>
  327. /// Called when the <see cref="Subviews"/> are to be drawn.
  328. /// </summary>
  329. /// <param name="viewport"></param>
  330. /// <returns><see langword="true"/> to stop further drawing of <see cref="Subviews"/>.</returns>
  331. protected virtual bool OnDrawingSubviews (Rectangle viewport) { return false; }
  332. /// <summary>Raised when the <see cref="Subviews"/> are to be drawn.</summary>
  333. /// <remarks>
  334. /// </remarks>
  335. /// <returns>
  336. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  337. /// <see cref="Subviews"/>.
  338. /// </returns>
  339. public event EventHandler<DrawEventArgs>? DrawingSubviews;
  340. /// <summary>
  341. /// Draws the <see cref="Subviews"/>.
  342. /// </summary>
  343. public void DrawSubviews ()
  344. {
  345. if (_subviews is null || !SubViewNeedsDisplay)
  346. {
  347. return;
  348. }
  349. IEnumerable<View> subviewsNeedingDraw = _subviews.Where (view => (view.Visible && (view.NeedsDisplay || view.SubViewNeedsDisplay))
  350. );//|| view.Arrangement.HasFlag (ViewArrangement.Overlapped));
  351. foreach (View view in subviewsNeedingDraw)
  352. {
  353. if (view.Arrangement.HasFlag (ViewArrangement.Overlapped))
  354. {
  355. //view.SetNeedsDisplay ();
  356. }
  357. view.Draw ();
  358. }
  359. //var overlapped = subviewsNeedingDraw;
  360. //foreach (View view in overlapped)
  361. //{
  362. // view.Draw ();
  363. //}
  364. }
  365. #endregion DrawSubviews
  366. #region DrawLineCanvas
  367. private void DoRenderLineCanvas ()
  368. {
  369. if (OnRenderingLineCanvas ())
  370. {
  371. return;
  372. }
  373. // TODO: Add event
  374. RenderLineCanvas ();
  375. }
  376. /// <summary>
  377. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  378. /// </summary>
  379. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  380. protected virtual bool OnRenderingLineCanvas () { return false; }
  381. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  382. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  383. public LineCanvas LineCanvas { get; } = new ();
  384. /// <summary>
  385. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  386. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  387. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will be
  388. /// called to render the borders.
  389. /// </summary>
  390. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  391. /// <summary>
  392. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  393. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  394. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  395. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  396. /// </summary>
  397. public void RenderLineCanvas ()
  398. {
  399. // TODO: This is super confusing and needs to be refactored.
  400. if (Driver is null)
  401. {
  402. return;
  403. }
  404. // If we have a SuperView, it'll render our frames.
  405. if (!SuperViewRendersLineCanvas && LineCanvas.Viewport != Rectangle.Empty)
  406. {
  407. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  408. {
  409. // Get the entire map
  410. if (p.Value is { })
  411. {
  412. SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  413. Driver.Move (p.Key.X, p.Key.Y);
  414. // TODO: #2616 - Support combining sequences that don't normalize
  415. Driver.AddRune (p.Value.Value.Rune);
  416. }
  417. }
  418. LineCanvas.Clear ();
  419. }
  420. if (Subviews.Any (s => s.SuperViewRendersLineCanvas))
  421. {
  422. foreach (View subview in Subviews.Where (s => s.SuperViewRendersLineCanvas))
  423. {
  424. // Combine the LineCanvas'
  425. LineCanvas.Merge (subview.LineCanvas);
  426. subview.LineCanvas.Clear ();
  427. }
  428. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  429. {
  430. // Get the entire map
  431. if (p.Value is { })
  432. {
  433. SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  434. Driver.Move (p.Key.X, p.Key.Y);
  435. // TODO: #2616 - Support combining sequences that don't normalize
  436. Driver.AddRune (p.Value.Value.Rune);
  437. }
  438. }
  439. LineCanvas.Clear ();
  440. }
  441. }
  442. #endregion DrawLineCanvas
  443. #region DrawComplete
  444. private void DoDrawComplete ()
  445. {
  446. OnDrawComplete ();
  447. DrawComplete?.Invoke (this, EventArgs.Empty);
  448. // Default implementation does nothing.
  449. }
  450. /// <summary>
  451. /// Called when the View is completed drawing.
  452. /// </summary>
  453. protected virtual void OnDrawComplete () { }
  454. /// <summary>Raised when the View is completed drawing.</summary>
  455. /// <remarks>
  456. /// </remarks>
  457. public event EventHandler<EventArgs>? DrawComplete;
  458. #endregion DrawComplete
  459. #region NeedsDisplay
  460. // TODO: Make _needsDisplayRect nullable instead of relying on Empty
  461. // TODO: If null, it means ?
  462. // TODO: If Empty, it means no need to redraw
  463. // TODO: If not Empty, it means the region that needs to be redrawn
  464. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  465. internal Rectangle _needsDisplayRect = Rectangle.Empty;
  466. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  467. /// <remarks>
  468. /// <para>
  469. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  470. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  471. /// </para>
  472. /// <para>
  473. /// Setting has no effect on <see cref="NeedsLayout"/>.
  474. /// </para>
  475. /// </remarks>
  476. public bool NeedsDisplay
  477. {
  478. // TODO: Figure out if we can decouple NeedsDisplay from NeedsLayout. This is a temporary fix.
  479. get => _needsDisplayRect != Rectangle.Empty || NeedsLayout;
  480. set
  481. {
  482. if (value)
  483. {
  484. SetNeedsDisplay ();
  485. }
  486. else
  487. {
  488. ClearNeedsDisplay ();
  489. }
  490. }
  491. }
  492. /// <summary>Gets whether any Subviews need to be redrawn.</summary>
  493. public bool SubViewNeedsDisplay { get; private set; }
  494. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  495. /// <remarks>
  496. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  497. /// does nothing.
  498. /// </remarks>
  499. public void SetNeedsDisplay ()
  500. {
  501. Rectangle viewport = Viewport;
  502. if (_needsDisplayRect != Rectangle.Empty && viewport.IsEmpty)
  503. {
  504. // This handles the case where the view has not been initialized yet
  505. return;
  506. }
  507. SetNeedsDisplay (viewport);
  508. }
  509. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  510. /// <remarks>
  511. /// <para>
  512. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  513. /// </para>
  514. /// <para>
  515. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  516. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  517. /// </para>
  518. /// </remarks>
  519. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  520. public void SetNeedsDisplay (Rectangle viewPortRelativeRegion)
  521. {
  522. if (_needsDisplayRect.IsEmpty)
  523. {
  524. _needsDisplayRect = viewPortRelativeRegion;
  525. }
  526. else
  527. {
  528. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  529. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  530. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  531. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  532. _needsDisplayRect = new (x, y, w, h);
  533. }
  534. Margin?.SetNeedsDisplay ();
  535. Border?.SetNeedsDisplay ();
  536. Padding?.SetNeedsDisplay ();
  537. SuperView?.SetSubViewNeedsDisplay ();
  538. if (this is Adornment adornment)
  539. {
  540. adornment.Parent?.SetSubViewNeedsDisplay ();
  541. }
  542. foreach (View subview in Subviews)
  543. {
  544. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  545. {
  546. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  547. subviewRegion.X -= subview.Frame.X;
  548. subviewRegion.Y -= subview.Frame.Y;
  549. subview.SetNeedsDisplay (subviewRegion);
  550. }
  551. }
  552. }
  553. /// <summary>Sets <see cref="SubViewNeedsDisplay"/> to <see langword="true"/> for this View and all Superviews.</summary>
  554. public void SetSubViewNeedsDisplay ()
  555. {
  556. SubViewNeedsDisplay = true;
  557. if (this is Adornment adornment)
  558. {
  559. adornment.Parent?.SetSubViewNeedsDisplay ();
  560. }
  561. if (SuperView is { SubViewNeedsDisplay: false })
  562. {
  563. SuperView.SetSubViewNeedsDisplay ();
  564. }
  565. }
  566. /// <summary>Clears <see cref="NeedsDisplay"/> and <see cref="SubViewNeedsDisplay"/>.</summary>
  567. protected void ClearNeedsDisplay ()
  568. {
  569. _needsDisplayRect = Rectangle.Empty;
  570. SubViewNeedsDisplay = false;
  571. Margin?.ClearNeedsDisplay ();
  572. Border?.ClearNeedsDisplay ();
  573. Padding?.ClearNeedsDisplay ();
  574. foreach (View subview in Subviews)
  575. {
  576. subview.ClearNeedsDisplay ();
  577. }
  578. }
  579. #endregion NeedsDisplay
  580. }