View.Drawing.cs 23 KB

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