View.Drawing.Clipping.cs 1.7 KB

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