FakeWindowSizeMonitor.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Microsoft.Extensions.Logging;
  2. namespace Terminal.Gui.Drivers;
  3. internal class FakeWindowSizeMonitor (IConsoleOutput consoleOut, IOutputBuffer outputBuffer) : IWindowSizeMonitor
  4. {
  5. private Size _lastSize = new (0, 0);
  6. /// <summary>Invoked when the terminal's size changed. The new size of the terminal is provided.</summary>
  7. public event EventHandler<SizeChangedEventArgs> SizeChanging;
  8. /// <summary>Raises the <see cref="SizeChanging"/> event with the specified size. Used for testing.</summary>
  9. /// <param name="newSize">The new size to report.</param>
  10. public void RaiseSizeChanging (Size newSize)
  11. {
  12. SizeChanging?.Invoke (this, new (newSize));
  13. }
  14. /// <inheritdoc/>
  15. public bool Poll ()
  16. {
  17. if (ConsoleDriver.RunningUnitTests)
  18. {
  19. return false;
  20. }
  21. Size size = consoleOut.GetWindowSize ();
  22. if (size != _lastSize)
  23. {
  24. Logging.Logger.LogInformation ($"Console size changes from '{_lastSize}' to {size}");
  25. outputBuffer.SetWindowSize (size.Width, size.Height);
  26. _lastSize = size;
  27. SizeChanging?.Invoke (this, new (size));
  28. return true;
  29. }
  30. return false;
  31. }
  32. }