View.Drawing.cs 23 KB

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