View.Drawing.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. #pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
  324. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  325. /// <returns>
  326. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  327. /// <see cref="Text"/>.
  328. /// </returns>
  329. public event EventHandler<DrawEventArgs>? DrawingText;
  330. #pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
  331. /// <summary>
  332. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  333. /// </summary>
  334. public void DrawText ()
  335. {
  336. if (!string.IsNullOrEmpty (TextFormatter.Text))
  337. {
  338. TextFormatter.NeedsFormat = true;
  339. }
  340. // TODO: If the output is not in the Viewport, do nothing
  341. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  342. TextFormatter?.Draw (
  343. drawRect,
  344. HasFocus ? GetFocusColor () : GetNormalColor (),
  345. HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
  346. Rectangle.Empty
  347. );
  348. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  349. SetSubViewNeedsDraw ();
  350. }
  351. #endregion DrawText
  352. #region DrawContent
  353. private void DoDrawContent ()
  354. {
  355. if (OnDrawingContent ())
  356. {
  357. return;
  358. }
  359. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  360. DrawingContent?.Invoke (this, dev);
  361. if (dev.Cancel)
  362. { }
  363. // Do nothing.
  364. }
  365. /// <summary>
  366. /// Called when the View's content is to be drawn. The default implementation does nothing.
  367. /// </summary>
  368. /// <remarks>
  369. /// </remarks>
  370. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  371. protected virtual bool OnDrawingContent () { return false; }
  372. /// <summary>Raised when the View's content is to be drawn.</summary>
  373. /// <remarks>
  374. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  375. /// <para>
  376. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  377. /// <see cref="View"/> .
  378. /// </para>
  379. /// </remarks>
  380. public event EventHandler<DrawEventArgs>? DrawingContent;
  381. #endregion DrawContent
  382. #region DrawSubviews
  383. private void DoDrawSubviews ()
  384. {
  385. if (OnDrawingSubviews ())
  386. {
  387. return;
  388. }
  389. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  390. DrawingSubviews?.Invoke (this, dev);
  391. if (dev.Cancel)
  392. {
  393. return;
  394. }
  395. if (!SubViewNeedsDraw)
  396. {
  397. return;
  398. }
  399. DrawSubviews ();
  400. }
  401. /// <summary>
  402. /// Called when the <see cref="Subviews"/> are to be drawn.
  403. /// </summary>
  404. /// <returns><see langword="true"/> to stop further drawing of <see cref="Subviews"/>.</returns>
  405. protected virtual bool OnDrawingSubviews () { return false; }
  406. #pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
  407. /// <summary>Raised when the <see cref="Subviews"/> are to be drawn.</summary>
  408. /// <remarks>
  409. /// </remarks>
  410. /// <returns>
  411. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  412. /// <see cref="Subviews"/>.
  413. /// </returns>
  414. public event EventHandler<DrawEventArgs>? DrawingSubviews;
  415. #pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
  416. /// <summary>
  417. /// Draws the <see cref="Subviews"/>.
  418. /// </summary>
  419. public void DrawSubviews ()
  420. {
  421. if (_subviews is null)
  422. {
  423. return;
  424. }
  425. // Draw the subviews in reverse order to leverage clipping.
  426. foreach (View view in _subviews.Where (view => view.Visible).Reverse ())
  427. {
  428. // TODO: HACK - This enables auto line join to work, but is brute force.
  429. if (view.SuperViewRendersLineCanvas)
  430. {
  431. view.SetNeedsDraw ();
  432. }
  433. view.Draw ();
  434. if (view.SuperViewRendersLineCanvas)
  435. {
  436. LineCanvas.Merge (view.LineCanvas);
  437. view.LineCanvas.Clear ();
  438. }
  439. }
  440. }
  441. #endregion DrawSubviews
  442. #region DrawLineCanvas
  443. private void DoRenderLineCanvas ()
  444. {
  445. if (OnRenderingLineCanvas ())
  446. {
  447. return;
  448. }
  449. // TODO: Add event
  450. RenderLineCanvas ();
  451. }
  452. /// <summary>
  453. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  454. /// </summary>
  455. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  456. protected virtual bool OnRenderingLineCanvas () { return false; }
  457. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  458. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  459. public LineCanvas LineCanvas { get; } = new ();
  460. /// <summary>
  461. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  462. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  463. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingBorderAndPadding"/> method will
  464. /// be
  465. /// called to render the borders.
  466. /// </summary>
  467. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  468. /// <summary>
  469. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  470. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  471. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  472. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  473. /// </summary>
  474. public void RenderLineCanvas ()
  475. {
  476. if (Driver is null)
  477. {
  478. return;
  479. }
  480. if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rectangle.Empty)
  481. {
  482. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  483. {
  484. // Get the entire map
  485. if (p.Value is { })
  486. {
  487. SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  488. Driver.Move (p.Key.X, p.Key.Y);
  489. // TODO: #2616 - Support combining sequences that don't normalize
  490. Driver.AddRune (p.Value.Value.Rune);
  491. }
  492. }
  493. LineCanvas.Clear ();
  494. }
  495. }
  496. #endregion DrawLineCanvas
  497. #region DrawComplete
  498. private void DoDrawComplete ()
  499. {
  500. OnDrawComplete ();
  501. DrawComplete?.Invoke (this, new (Viewport, Viewport));
  502. // Default implementation does nothing.
  503. }
  504. /// <summary>
  505. /// Called when the View is completed drawing.
  506. /// </summary>
  507. protected virtual void OnDrawComplete () { }
  508. /// <summary>Raised when the View is completed drawing.</summary>
  509. /// <remarks>
  510. /// </remarks>
  511. public event EventHandler<DrawEventArgs>? DrawComplete;
  512. #endregion DrawComplete
  513. #region NeedsDraw
  514. // TODO: Change NeedsDraw to use a Region instead of Rectangle
  515. // TODO: Make _needsDrawRect nullable instead of relying on Empty
  516. // TODO: If null, it means ?
  517. // TODO: If Empty, it means no need to redraw
  518. // TODO: If not Empty, it means the region that needs to be redrawn
  519. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  520. internal Rectangle _needsDrawRect = Rectangle.Empty;
  521. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  522. /// <remarks>
  523. /// <para>
  524. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  525. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  526. /// </para>
  527. /// <para>
  528. /// Setting has no effect on <see cref="NeedsLayout"/>.
  529. /// </para>
  530. /// </remarks>
  531. public bool NeedsDraw
  532. {
  533. // TODO: Figure out if we can decouple NeedsDraw from NeedsLayout.
  534. get => Visible && (_needsDrawRect != Rectangle.Empty || NeedsLayout);
  535. set
  536. {
  537. if (value)
  538. {
  539. SetNeedsDraw ();
  540. }
  541. else
  542. {
  543. ClearNeedsDraw ();
  544. }
  545. }
  546. }
  547. /// <summary>Gets whether any Subviews need to be redrawn.</summary>
  548. public bool SubViewNeedsDraw { get; private set; }
  549. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  550. /// <remarks>
  551. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  552. /// does nothing.
  553. /// </remarks>
  554. public void SetNeedsDraw ()
  555. {
  556. Rectangle viewport = Viewport;
  557. if (!Visible || (_needsDrawRect != Rectangle.Empty && viewport.IsEmpty))
  558. {
  559. // This handles the case where the view has not been initialized yet
  560. return;
  561. }
  562. SetNeedsDraw (viewport);
  563. }
  564. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  565. /// <remarks>
  566. /// <para>
  567. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  568. /// </para>
  569. /// <para>
  570. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  571. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  572. /// </para>
  573. /// </remarks>
  574. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  575. public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
  576. {
  577. if (!Visible)
  578. {
  579. return;
  580. }
  581. if (_needsDrawRect.IsEmpty)
  582. {
  583. _needsDrawRect = viewPortRelativeRegion;
  584. }
  585. else
  586. {
  587. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  588. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  589. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  590. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  591. _needsDrawRect = new (x, y, w, h);
  592. }
  593. // Do not set on Margin - it will be drawn in a separate pass.
  594. if (Border is { } && Border.Thickness != Thickness.Empty)
  595. {
  596. Border?.SetNeedsDraw ();
  597. }
  598. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  599. {
  600. Padding?.SetNeedsDraw ();
  601. }
  602. SuperView?.SetSubViewNeedsDraw ();
  603. if (this is Adornment adornment)
  604. {
  605. adornment.Parent?.SetSubViewNeedsDraw ();
  606. }
  607. foreach (View subview in Subviews)
  608. {
  609. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  610. {
  611. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  612. subviewRegion.X -= subview.Frame.X;
  613. subviewRegion.Y -= subview.Frame.Y;
  614. subview.SetNeedsDraw (subviewRegion);
  615. }
  616. }
  617. }
  618. /// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
  619. public void SetSubViewNeedsDraw ()
  620. {
  621. if (!Visible)
  622. {
  623. return;
  624. }
  625. SubViewNeedsDraw = true;
  626. if (this is Adornment adornment)
  627. {
  628. adornment.Parent?.SetSubViewNeedsDraw ();
  629. }
  630. if (SuperView is { SubViewNeedsDraw: false })
  631. {
  632. SuperView.SetSubViewNeedsDraw ();
  633. }
  634. }
  635. /// <summary>Clears <see cref="NeedsDraw"/> and <see cref="SubViewNeedsDraw"/>.</summary>
  636. protected void ClearNeedsDraw ()
  637. {
  638. _needsDrawRect = Rectangle.Empty;
  639. SubViewNeedsDraw = false;
  640. if (Margin is { } && Margin.Thickness != Thickness.Empty)
  641. {
  642. Margin?.ClearNeedsDraw ();
  643. }
  644. if (Border is { } && Border.Thickness != Thickness.Empty)
  645. {
  646. Border?.ClearNeedsDraw ();
  647. }
  648. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  649. {
  650. Padding?.ClearNeedsDraw ();
  651. }
  652. foreach (View subview in Subviews)
  653. {
  654. subview.ClearNeedsDraw ();
  655. }
  656. if (SuperView is { })
  657. {
  658. SuperView.SubViewNeedsDraw = false;
  659. }
  660. // This ensures LineCanvas' get redrawn
  661. if (!SuperViewRendersLineCanvas)
  662. {
  663. LineCanvas.Clear ();
  664. }
  665. }
  666. #endregion NeedsDraw
  667. }