Application.Screen.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public static partial class Application // Screen related stuff
  4. {
  5. /// <summary>
  6. /// Gets the size of the screen. This is the size of the screen as reported by the <see cref="ConsoleDriver"/>.
  7. /// </summary>
  8. /// <remarks>
  9. /// If the <see cref="ConsoleDriver"/> has not been initialized, this will return a default size of 2048x2048; useful for unit tests.
  10. /// </remarks>
  11. public static Rectangle Screen => Driver?.Screen ?? new (0, 0, 2048, 2048);
  12. /// <summary>Invoked when the terminal's size changed. The new size of the terminal is provided.</summary>
  13. /// <remarks>
  14. /// Event handlers can set <see cref="SizeChangedEventArgs.Cancel"/> to <see langword="true"/> to prevent
  15. /// <see cref="Application"/> from changing it's size to match the new terminal size.
  16. /// </remarks>
  17. public static event EventHandler<SizeChangedEventArgs>? SizeChanging;
  18. /// <summary>
  19. /// Called when the application's size changes. Sets the size of all <see cref="Toplevel"/>s and fires the
  20. /// <see cref="SizeChanging"/> event.
  21. /// </summary>
  22. /// <param name="args">The new size.</param>
  23. /// <returns><see lanword="true"/>if the size was changed.</returns>
  24. public static bool OnSizeChanging (SizeChangedEventArgs args)
  25. {
  26. SizeChanging?.Invoke (null, args);
  27. if (args.Cancel || args.Size is null)
  28. {
  29. return false;
  30. }
  31. foreach (Toplevel t in TopLevels)
  32. {
  33. if (t.SetRelativeLayout (args.Size.Value))
  34. {
  35. t.LayoutSubviews ();
  36. }
  37. t.OnSizeChanging (new (args.Size));
  38. }
  39. Refresh ();
  40. return true;
  41. }
  42. }