Application.Screen.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }