View.Drawing.cs 24 KB

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