View.Drawing.Clipping.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. namespace Terminal.Gui;
  2. public partial class View
  3. {
  4. /// <summary>Sets the <see cref="ConsoleDriver"/>'s clip region to <see cref="Viewport"/>.</summary>
  5. /// <remarks>
  6. /// <para>
  7. /// By default, the clip rectangle is set to the intersection of the current clip region and the
  8. /// <see cref="Viewport"/>. This ensures that drawing is constrained to the viewport, but allows
  9. /// content to be drawn beyond the viewport.
  10. /// </para>
  11. /// <para>
  12. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClipContentOnly"/> set, clipping will be
  13. /// applied to just the visible content area.
  14. /// </para>
  15. /// </remarks>
  16. /// <returns>
  17. /// The current screen-relative clip region, which can be then re-applied by setting
  18. /// <see cref="ConsoleDriver.Clip"/>.
  19. /// </returns>
  20. public Rectangle SetClip ()
  21. {
  22. if (Driver is null)
  23. {
  24. return Rectangle.Empty;
  25. }
  26. Rectangle previous = Driver.Clip;
  27. // Clamp the Clip to the entire visible area
  28. Rectangle clip = Rectangle.Intersect (ViewportToScreen (Viewport with { Location = Point.Empty }), previous);
  29. if (ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly))
  30. {
  31. // Clamp the Clip to the just content area that is within the viewport
  32. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  33. clip = Rectangle.Intersect (clip, visibleContent);
  34. }
  35. Driver.Clip = clip;
  36. return previous;
  37. }
  38. }