ToplevelTransitionManager.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #nullable enable
  2. using Terminal.Gui.Drivers;
  3. namespace Terminal.Gui.App;
  4. /// <summary>
  5. /// Handles bespoke behaviours that occur when application top level changes.
  6. /// </summary>
  7. public class ToplevelTransitionManager : IToplevelTransitionManager
  8. {
  9. private readonly HashSet<Toplevel> _readiedTopLevels = new ();
  10. private View? _lastTop;
  11. /// <inheritdoc/>
  12. public void RaiseReadyEventIfNeeded ()
  13. {
  14. Toplevel? top = Application.Top;
  15. if (top != null && !_readiedTopLevels.Contains (top))
  16. {
  17. top.OnReady ();
  18. _readiedTopLevels.Add (top);
  19. // Views can be closed and opened and run again multiple times, see End_Does_Not_Dispose
  20. top.Closed += (s, e) => _readiedTopLevels.Remove (top);
  21. }
  22. }
  23. /// <inheritdoc/>
  24. public void HandleTopMaybeChanging ()
  25. {
  26. Toplevel? newTop = Application.Top;
  27. if (_lastTop != null && _lastTop != newTop && newTop != null)
  28. {
  29. newTop.SetNeedsDraw ();
  30. }
  31. _lastTop = Application.Top;
  32. }
  33. }