ToplevelTransitionManager.cs 1.0 KB

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