Application.Screen.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #nullable enable
  2. namespace Terminal.Gui.App;
  3. public static partial class Application // Screen related stuff; intended to hide Driver details
  4. {
  5. private static readonly object _lockScreen = new ();
  6. private static Rectangle? _screen;
  7. /// <summary>
  8. /// Gets or sets the size of the screen. By default, this is the size of the screen as reported by the <see cref="IConsoleDriver"/>.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// If the <see cref="IConsoleDriver"/> has not been initialized, this will return a default size of 2048x2048; useful for unit tests.
  13. /// </para>
  14. /// </remarks>
  15. public static Rectangle Screen
  16. {
  17. get
  18. {
  19. lock (_lockScreen)
  20. {
  21. if (_screen == null)
  22. {
  23. _screen = Driver?.Screen ?? new (new (0, 0), new (2048, 2048));
  24. }
  25. return _screen.Value;
  26. }
  27. }
  28. set
  29. {
  30. if (value is {} && (value.X != 0 || value.Y != 0))
  31. {
  32. throw new NotImplementedException ($"Screen locations other than 0, 0 are not yet supported");
  33. }
  34. lock (_lockScreen)
  35. {
  36. _screen = value;
  37. }
  38. }
  39. }
  40. /// <summary>Invoked when the terminal's size changed. The new size of the terminal is provided.</summary>
  41. /// <remarks>
  42. /// Event handlers can set <see cref="SizeChangedEventArgs.Cancel"/> to <see langword="true"/> to prevent
  43. /// <see cref="Application"/> from changing it's size to match the new terminal size.
  44. /// </remarks>
  45. public static event EventHandler<SizeChangedEventArgs>? SizeChanging;
  46. /// <summary>
  47. /// Called when the application's size changes. Sets the size of all <see cref="Toplevel"/>s and fires the
  48. /// <see cref="SizeChanging"/> event.
  49. /// </summary>
  50. /// <param name="args">The new size.</param>
  51. /// <returns><see lanword="true"/>if the size was changed.</returns>
  52. public static bool OnSizeChanging (SizeChangedEventArgs args)
  53. {
  54. SizeChanging?.Invoke (null, args);
  55. if (args.Cancel || args.Size is null)
  56. {
  57. return false;
  58. }
  59. Screen = new (Point.Empty, args.Size.Value);
  60. foreach (Toplevel t in TopLevels)
  61. {
  62. t.OnSizeChanging (new (args.Size));
  63. t.SetNeedsLayout ();
  64. }
  65. LayoutAndDraw (true);
  66. return true;
  67. }
  68. /// <summary>
  69. /// Gets or sets whether the screen will be cleared, and all Views redrawn, during the next Application iteration.
  70. /// </summary>
  71. /// <remarks>
  72. /// This is typical set to true when a View's <see cref="View.Frame"/> changes and that view has no
  73. /// SuperView (e.g. when <see cref="Application.Top"/> is moved or resized.
  74. /// </remarks>
  75. internal static bool ClearScreenNextIteration { get; set; }
  76. }