View.Drawing.cs 24 KB

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