View.Drawing.cs 24 KB

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