View.NeedsDraw.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. namespace Terminal.Gui.ViewBase;
  2. public partial class View
  3. {
  4. // TODO: Change NeedsDraw to use a Region instead of Rectangle
  5. // TODO: Make _needsDrawRect nullable instead of relying on Empty
  6. // TODO: If null, it means ?
  7. // TODO: If Empty, it means no need to redraw
  8. // TODO: If not Empty, it means the region that needs to be redrawn
  9. /// <summary>
  10. /// The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  11. /// </summary>
  12. internal Rectangle NeedsDrawRect { get; set; } = Rectangle.Empty;
  13. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  14. /// <remarks>
  15. /// <para>
  16. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  17. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  18. /// </para>
  19. /// <para>
  20. /// Setting has no effect on <see cref="NeedsLayout"/>.
  21. /// </para>
  22. /// </remarks>
  23. public bool NeedsDraw
  24. {
  25. get => Visible && (NeedsDrawRect != Rectangle.Empty || Margin?.NeedsDraw == true || Border?.NeedsDraw == true || Padding?.NeedsDraw == true);
  26. set
  27. {
  28. if (value)
  29. {
  30. SetNeedsDraw ();
  31. }
  32. else
  33. {
  34. ClearNeedsDraw ();
  35. }
  36. }
  37. }
  38. /// <summary>Gets whether any SubViews need to be redrawn.</summary>
  39. public bool SubViewNeedsDraw { get; private set; }
  40. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  41. /// <remarks>
  42. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  43. /// does nothing.
  44. /// </remarks>
  45. public void SetNeedsDraw ()
  46. {
  47. Rectangle viewport = Viewport;
  48. if (!Visible || (NeedsDrawRect != Rectangle.Empty && viewport.IsEmpty))
  49. {
  50. // This handles the case where the view has not been initialized yet
  51. return;
  52. }
  53. SetNeedsDraw (viewport);
  54. }
  55. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  56. /// <remarks>
  57. /// <para>
  58. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  59. /// </para>
  60. /// <para>
  61. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  62. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  63. /// </para>
  64. /// </remarks>
  65. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  66. public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
  67. {
  68. if (!Visible)
  69. {
  70. return;
  71. }
  72. if (NeedsDrawRect.IsEmpty)
  73. {
  74. NeedsDrawRect = viewPortRelativeRegion;
  75. }
  76. else
  77. {
  78. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  79. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  80. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  81. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  82. NeedsDrawRect = new (x, y, w, h);
  83. }
  84. // Do not set on Margin - it will be drawn in a separate pass.
  85. if (Border is { } && Border.Thickness != Thickness.Empty)
  86. {
  87. Border?.SetNeedsDraw ();
  88. }
  89. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  90. {
  91. Padding?.SetNeedsDraw ();
  92. }
  93. SuperView?.SetSubViewNeedsDraw ();
  94. if (this is Adornment adornment)
  95. {
  96. adornment.Parent?.SetSubViewNeedsDraw ();
  97. }
  98. // There was multiple enumeration error here, so calling new snapshot collection - probably a stop gap
  99. foreach (View subview in InternalSubViews.Snapshot ())
  100. {
  101. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  102. {
  103. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  104. subviewRegion.X -= subview.Frame.X;
  105. subviewRegion.Y -= subview.Frame.Y;
  106. subview.SetNeedsDraw (subviewRegion);
  107. }
  108. }
  109. }
  110. /// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
  111. public void SetSubViewNeedsDraw ()
  112. {
  113. if (!Visible)
  114. {
  115. return;
  116. }
  117. SubViewNeedsDraw = true;
  118. if (this is Adornment adornment)
  119. {
  120. adornment.Parent?.SetSubViewNeedsDraw ();
  121. }
  122. if (SuperView is { SubViewNeedsDraw: false })
  123. {
  124. SuperView.SetSubViewNeedsDraw ();
  125. }
  126. }
  127. /// <summary>Clears <see cref="NeedsDraw"/> and <see cref="SubViewNeedsDraw"/>.</summary>
  128. protected void ClearNeedsDraw ()
  129. {
  130. NeedsDrawRect = Rectangle.Empty;
  131. Margin?.ClearNeedsDraw ();
  132. Border?.ClearNeedsDraw ();
  133. Padding?.ClearNeedsDraw ();
  134. // There was multiple enumeration error here, so calling new snapshot collection - probably a stop gap
  135. foreach (View subview in InternalSubViews.Snapshot ())
  136. {
  137. subview.ClearNeedsDraw ();
  138. }
  139. SubViewNeedsDraw = false;
  140. if (SuperView is { })
  141. {
  142. SuperView.SubViewNeedsDraw = false;
  143. }
  144. // This ensures LineCanvas' get redrawn
  145. if (!SuperViewRendersLineCanvas)
  146. {
  147. LineCanvas.Clear ();
  148. }
  149. }
  150. }