WindowSizeMonitor.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Microsoft.Extensions.Logging;
  2. namespace Terminal.Gui;
  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. Size size = _consoleOut.GetWindowSize ();
  19. if (size != _lastSize)
  20. {
  21. Logging.Logger.LogInformation ($"Console size changes from '{_lastSize}' to {size}");
  22. _outputBuffer.SetWindowSize (size.Width, size.Height);
  23. _lastSize = size;
  24. SizeChanging?.Invoke (this, new (size));
  25. return true;
  26. }
  27. return false;
  28. }
  29. }