Application.Screen.cs 2.1 KB

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