View.Drawing.cs 25 KB

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