WindowSizeMonitor.cs 1.1 KB

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