Application.Screen.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Internal helper method for ApplicationImpl.ResetState to clear this event
  25. internal static void ClearSizeChangingEvent ()
  26. {
  27. SizeChanging = null;
  28. }
  29. /// <summary>
  30. /// Called when the application's size changes. Sets the size of all <see cref="Toplevel"/>s and fires the
  31. /// <see cref="SizeChanging"/> event.
  32. /// </summary>
  33. /// <param name="args">The new size.</param>
  34. /// <returns><see lanword="true"/>if the size was changed.</returns>
  35. public static bool OnSizeChanging (SizeChangedEventArgs args)
  36. {
  37. SizeChanging?.Invoke (null, args);
  38. if (args.Cancel || args.Size is null)
  39. {
  40. return false;
  41. }
  42. Screen = new (Point.Empty, args.Size.Value);
  43. foreach (Toplevel t in TopLevels)
  44. {
  45. t.OnSizeChanging (new (args.Size));
  46. t.SetNeedsLayout ();
  47. }
  48. LayoutAndDraw (true);
  49. return true;
  50. }
  51. /// <summary>
  52. /// Gets or sets whether the screen will be cleared, and all Views redrawn, during the next Application iteration.
  53. /// </summary>
  54. /// <remarks>
  55. /// This is typical set to true when a View's <see cref="View.Frame"/> changes and that view has no
  56. /// SuperView (e.g. when <see cref="Application.Top"/> is moved or resized.
  57. /// </remarks>
  58. internal static bool ClearScreenNextIteration
  59. {
  60. get => ApplicationImpl.Instance.ClearScreenNextIteration;
  61. set => ApplicationImpl.Instance.ClearScreenNextIteration = value;
  62. }
  63. }