View.Drawing.cs 23 KB

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