View.Drawing.cs 23 KB

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