Application.Screen.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public static partial class Application // Screen related stuff
  4. {
  5. private static Rectangle? _screen;
  6. /// <summary>
  7. /// Gets or sets the size of the screen. By default, this is the size of the screen as reported by the <see cref="ConsoleDriver"/>.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// If the <see cref="ConsoleDriver"/> has not been initialized, this will return a default size of 2048x2048; useful for unit tests.
  12. /// </para>
  13. /// </remarks>
  14. public static Rectangle Screen
  15. {
  16. get
  17. {
  18. if (_screen == null)
  19. {
  20. _screen = Driver?.Screen ?? new (new (0, 0), new (2048, 2048));
  21. }
  22. return _screen.Value;
  23. }
  24. set
  25. {
  26. if (value is {} && (value.X != 0 || value.Y != 0))
  27. {
  28. throw new NotImplementedException ($"Screen locations other than 0, 0 are not yet supported");
  29. }
  30. _screen = value;
  31. }
  32. }
  33. /// <summary>Invoked when the terminal's size changed. The new size of the terminal is provided.</summary>
  34. /// <remarks>
  35. /// Event handlers can set <see cref="SizeChangedEventArgs.Cancel"/> to <see langword="true"/> to prevent
  36. /// <see cref="Application"/> from changing it's size to match the new terminal size.
  37. /// </remarks>
  38. public static event EventHandler<SizeChangedEventArgs>? SizeChanging;
  39. /// <summary>
  40. /// Called when the application's size changes. Sets the size of all <see cref="Toplevel"/>s and fires the
  41. /// <see cref="SizeChanging"/> event.
  42. /// </summary>
  43. /// <param name="args">The new size.</param>
  44. /// <returns><see lanword="true"/>if the size was changed.</returns>
  45. public static bool OnSizeChanging (SizeChangedEventArgs args)
  46. {
  47. SizeChanging?.Invoke (null, args);
  48. if (args.Cancel || args.Size is null)
  49. {
  50. return false;
  51. }
  52. Screen = new (Point.Empty, args.Size.Value);
  53. foreach (Toplevel t in TopLevels)
  54. {
  55. t.OnSizeChanging (new (args.Size));
  56. t.SetNeedsLayout ();
  57. }
  58. LayoutAndDrawToplevels ();
  59. return true;
  60. }
  61. #region Clip Support
  62. /// <summary>
  63. /// Sets the clip region to the screen.
  64. /// </summary>
  65. /// <returns>The previous clip region.</returns>>
  66. public static Region? ClipToScreen ()
  67. {
  68. Region? previous = Driver?.Clip;
  69. if (Driver is { })
  70. {
  71. Driver.Clip = new (Screen);
  72. }
  73. return previous;
  74. }
  75. /// <summary>
  76. /// Sets the clip to the specified region.
  77. /// </summary>
  78. /// <param name="region"></param>
  79. public static void SetClip (Region? region)
  80. {
  81. if (Driver is { } && region is {})
  82. {
  83. Driver.Clip = region;
  84. }
  85. }
  86. /// <summary>
  87. /// Removes the specified rectangle from the Clip.
  88. /// </summary>
  89. /// <param name="rectangle"></param>
  90. public static void ExcludeFromClip (Rectangle rectangle)
  91. {
  92. Driver?.Clip?.Exclude (rectangle);
  93. }
  94. #endregion Clip Support
  95. }