View.Drawing.Clipping.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 
  2. namespace Terminal.Gui.ViewBase;
  3. public partial class View
  4. {
  5. /// <summary>
  6. /// Gets the current Clip region.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// There is a single clip region for the entire application.
  11. /// </para>
  12. /// <para>
  13. /// This method returns the current clip region, not a clone. If there is a need to modify the clip region, clone it first.
  14. /// </para>
  15. /// </remarks>
  16. /// <returns>The current Clip.</returns>
  17. public Region? GetClip () => Driver?.Clip;
  18. /// <summary>
  19. /// Sets the Clip to the specified region.
  20. /// </summary>
  21. /// <remarks>
  22. /// <para>
  23. /// There is a single clip region for the entire application. This method sets the clip region to the specified
  24. /// region.
  25. /// </para>
  26. /// </remarks>
  27. /// <param name="region"></param>
  28. public void SetClip (Region? region)
  29. {
  30. // BUGBUG: If region is null we should set the clip to null.
  31. // BUGBUG: Fixing this probably breaks other things.
  32. if (Driver is { } && region is { })
  33. {
  34. Driver.Clip = region;
  35. }
  36. }
  37. /// <summary>
  38. /// Sets the Clip to be the rectangle of the screen.
  39. /// </summary>
  40. /// <remarks>
  41. /// <para>
  42. /// There is a single clip region for the entire application. This method sets the clip region to the screen.
  43. /// </para>
  44. /// <para>
  45. /// This method returns the current clip region, not a clone. If there is a need to modify the clip region, it is
  46. /// recommended to clone it first.
  47. /// </para>
  48. /// </remarks>
  49. /// <returns>
  50. /// The current Clip, which can be then re-applied <see cref="View.SetClip"/>
  51. /// </returns>
  52. public Region? SetClipToScreen ()
  53. {
  54. Region? previous = GetClip ();
  55. if (Driver is { })
  56. {
  57. Driver.Clip = new (Driver!.Screen);
  58. }
  59. return previous;
  60. }
  61. /// <summary>
  62. /// Removes the specified rectangle from the Clip.
  63. /// </summary>
  64. /// <remarks>
  65. /// <para>
  66. /// There is a single clip region for the entire application.
  67. /// </para>
  68. /// </remarks>
  69. /// <param name="rectangle"></param>
  70. public void ExcludeFromClip (Rectangle rectangle) { Driver?.Clip?.Exclude (rectangle); }
  71. /// <summary>
  72. /// Removes the specified rectangle from the Clip.
  73. /// </summary>
  74. /// <remarks>
  75. /// <para>
  76. /// There is a single clip region for the entire application.
  77. /// </para>
  78. /// </remarks>
  79. /// <param name="region"></param>
  80. public void ExcludeFromClip (Region? region) { Driver?.Clip?.Exclude (region); }
  81. /// <summary>
  82. /// Changes the Clip to the intersection of the current Clip and the <see cref="Frame"/> of this View.
  83. /// </summary>
  84. /// <remarks>
  85. /// <para>
  86. /// This method returns the current clip region, not a clone. If there is a need to modify the clip region, it is
  87. /// recommended to clone it first.
  88. /// </para>
  89. /// </remarks>
  90. /// <returns>
  91. /// The current Clip, which can be then re-applied <see cref="View.SetClip"/>
  92. /// </returns>
  93. internal Region? AddFrameToClip ()
  94. {
  95. if (Driver is null)
  96. {
  97. return null;
  98. }
  99. Region previous = GetClip () ?? new (Driver.Screen);
  100. Region frameRegion = previous.Clone ();
  101. // Translate viewportRegion to screen-relative coords
  102. Rectangle screenRect = FrameToScreen ();
  103. frameRegion.Intersect (screenRect);
  104. if (this is Adornment adornment && adornment.Thickness != Thickness.Empty)
  105. {
  106. // Ensure adornments can't draw outside their thickness
  107. frameRegion.Exclude (adornment.Thickness.GetInside (FrameToScreen()));
  108. }
  109. SetClip (frameRegion);
  110. return previous;
  111. }
  112. /// <summary>Changes the Clip to the intersection of the current Clip and the <see cref="Viewport"/> of this View.</summary>
  113. /// <remarks>
  114. /// <para>
  115. /// By default, sets the Clip to the intersection of the current clip region and the
  116. /// <see cref="Viewport"/>. This ensures that drawing is constrained to the viewport, but allows
  117. /// content to be drawn beyond the viewport.
  118. /// </para>
  119. /// <para>
  120. /// If <see cref="ViewportSettings"/> has <see cref="ViewBase.ViewportSettingsFlags.ClipContentOnly"/> set, clipping will be
  121. /// applied to just the visible content area.
  122. /// </para>
  123. /// <remarks>
  124. /// <para>
  125. /// This method returns the current clip region, not a clone. If there is a need to modify the clip region, it
  126. /// is recommended to clone it first.
  127. /// </para>
  128. /// </remarks>
  129. /// </remarks>
  130. /// <returns>
  131. /// The current Clip, which can be then re-applied <see cref="View.SetClip"/>
  132. /// </returns>
  133. public Region? AddViewportToClip ()
  134. {
  135. if (Driver is null)
  136. {
  137. return null;
  138. }
  139. Region previous = GetClip () ?? new (App!.Screen);
  140. Region viewportRegion = previous.Clone ();
  141. Rectangle viewport = ViewportToScreen (new Rectangle (Point.Empty, Viewport.Size));
  142. viewportRegion?.Intersect (viewport);
  143. if (ViewportSettings.HasFlag (ViewportSettingsFlags.ClipContentOnly))
  144. {
  145. // Clamp the Clip to the just content area that is within the viewport
  146. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  147. viewportRegion?.Intersect (visibleContent);
  148. }
  149. if (this is Adornment adornment && adornment.Thickness != Thickness.Empty)
  150. {
  151. // Ensure adornments can't draw outside their thickness
  152. viewportRegion?.Exclude (adornment.Thickness.GetInside (viewport));
  153. }
  154. SetClip (viewportRegion);
  155. return previous;
  156. }
  157. }