View.Drawing.cs 24 KB

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