2
0

View.NeedsDraw.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. namespace Terminal.Gui.ViewBase;
  2. public partial class View
  3. {
  4. // NOTE: NeedsDrawRect is not currently used to clip drawing to only the invalidated region.
  5. // It is only used within SetNeedsDraw to propagate redraw requests to subviews.
  6. // NOTE: Consider changing NeedsDrawRect from Rectangle to Region for more precise invalidation
  7. // NeedsDraw is already efficiently cached via NeedsDrawRect. It checks:
  8. // 1. NeedsDrawRect (cached by SetNeedsDraw/ClearNeedsDraw)
  9. // 2. Adornment NeedsDraw flags (each cached separately)
  10. /// <summary>
  11. /// INTERNAL: Gets the viewport-relative region that needs to be redrawn.
  12. /// </summary>
  13. internal Rectangle NeedsDrawRect { get; private set; } = Rectangle.Empty;
  14. /// <summary>Gets whether the view needs to be redrawn.</summary>
  15. /// <remarks>
  16. /// <para>
  17. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  18. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  19. /// </para>
  20. /// </remarks>
  21. public bool NeedsDraw => Visible && (NeedsDrawRect != Rectangle.Empty || Margin?.NeedsDraw == true || Border?.NeedsDraw == true || Padding?.NeedsDraw == true);
  22. /// <summary>Sets <see cref="NeedsDraw"/> to <see langword="true"/> indicating the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  23. /// <remarks>
  24. /// If the view is not visible (<see cref="Visible"/> is <see langword="false"/>), this method
  25. /// does nothing.
  26. /// </remarks>
  27. public void SetNeedsDraw ()
  28. {
  29. Rectangle viewport = Viewport;
  30. if (!Visible || (NeedsDrawRect != Rectangle.Empty && viewport.IsEmpty))
  31. {
  32. // This handles the case where the view has not been initialized yet
  33. return;
  34. }
  35. SetNeedsDraw (viewport);
  36. }
  37. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  38. /// <remarks>
  39. /// <para>
  40. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  41. /// </para>
  42. /// <para>
  43. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  44. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  45. /// </para>
  46. /// </remarks>
  47. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  48. public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
  49. {
  50. if (!Visible)
  51. {
  52. return;
  53. }
  54. if (NeedsDrawRect.IsEmpty)
  55. {
  56. NeedsDrawRect = viewPortRelativeRegion;
  57. }
  58. else
  59. {
  60. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  61. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  62. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  63. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  64. NeedsDrawRect = new (x, y, w, h);
  65. }
  66. // Do not set on Margin - it will be drawn in a separate pass.
  67. if (Border is { } && Border.Thickness != Thickness.Empty)
  68. {
  69. Border?.SetNeedsDraw ();
  70. }
  71. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  72. {
  73. Padding?.SetNeedsDraw ();
  74. }
  75. SuperView?.SetSubViewNeedsDrawDownHierarchy ();
  76. if (this is Adornment adornment)
  77. {
  78. adornment.Parent?.SetSubViewNeedsDrawDownHierarchy ();
  79. }
  80. foreach (View subview in InternalSubViews.Snapshot ())
  81. {
  82. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  83. {
  84. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  85. subviewRegion.X -= subview.Frame.X;
  86. subviewRegion.Y -= subview.Frame.Y;
  87. subview.SetNeedsDraw (subviewRegion);
  88. }
  89. }
  90. }
  91. /// <summary>INTERNAL: Clears <see cref="NeedsDraw"/> and <see cref="SubViewNeedsDraw"/> for this view and all SubViews.</summary>
  92. /// <remarks>
  93. /// See <see cref="SubViewNeedsDraw"/> is a cached value that is set when any subview or adornment requests a redraw.
  94. /// It may not always be in sync with the actual state of the subviews.
  95. /// </remarks>
  96. internal void ClearNeedsDraw ()
  97. {
  98. NeedsDrawRect = Rectangle.Empty;
  99. Margin?.ClearNeedsDraw ();
  100. Border?.ClearNeedsDraw ();
  101. Padding?.ClearNeedsDraw ();
  102. foreach (View subview in InternalSubViews.Snapshot ())
  103. {
  104. subview.ClearNeedsDraw ();
  105. }
  106. SubViewNeedsDraw = false;
  107. // This ensures LineCanvas' get redrawn
  108. if (!SuperViewRendersLineCanvas)
  109. {
  110. LineCanvas.Clear ();
  111. }
  112. }
  113. // NOTE: SubViewNeedsDraw is decoupled from the actual state of the subviews (and adornments).
  114. // It is a performance optimization to avoid having to traverse all subviews and adornments to check if any need redraw.
  115. // As a result the code is fragile and can get out of sync; care must be taken to ensure it is set and cleared correctly.
  116. /// <summary>
  117. /// INTERNAL: Gets whether any SubViews need to be redrawn.
  118. /// </summary>
  119. /// <remarks>
  120. /// See <see cref="SubViewNeedsDraw"/> is a cached value that is set when any subview or adornment requests a redraw.
  121. /// It may not always be in sync with the actual state of the subviews.
  122. /// </remarks>
  123. internal bool SubViewNeedsDraw { get; private set; }
  124. /// <summary>INTERNAL: Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
  125. /// <remarks>
  126. /// See <see cref="SubViewNeedsDraw"/> is a cached value that is set when any subview or adornment requests a redraw.
  127. /// It may not always be in sync with the actual state of the subviews.
  128. /// </remarks>
  129. internal void SetSubViewNeedsDrawDownHierarchy ()
  130. {
  131. if (!Visible)
  132. {
  133. return;
  134. }
  135. SubViewNeedsDraw = true;
  136. if (this is Adornment adornment)
  137. {
  138. adornment.Parent?.SetSubViewNeedsDrawDownHierarchy ();
  139. }
  140. if (SuperView is { SubViewNeedsDraw: false })
  141. {
  142. SuperView.SetSubViewNeedsDrawDownHierarchy ();
  143. }
  144. }
  145. }