Application.Screen.cs 2.4 KB

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