View.Drawing.cs 23 KB

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