View.Drawing.cs 24 KB

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