ToplevelTransitionManager.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. /// <param name="app"></param>
  10. /// <inheritdoc/>
  11. public void RaiseReadyEventIfNeeded (IApplication? app)
  12. {
  13. Toplevel? top = app?.Current;
  14. if (top != null && !_readiedTopLevels.Contains (top))
  15. {
  16. top.OnReady ();
  17. _readiedTopLevels.Add (top);
  18. // Views can be closed and opened and run again multiple times, see End_Does_Not_Dispose
  19. top.Closed += (s, e) => _readiedTopLevels.Remove (top);
  20. }
  21. }
  22. /// <param name="app"></param>
  23. /// <inheritdoc/>
  24. public void HandleTopMaybeChanging (IApplication? app)
  25. {
  26. Toplevel? newTop = app?.Current;
  27. if (_lastTop != null && _lastTop != newTop && newTop != null)
  28. {
  29. newTop.SetNeedsDraw ();
  30. }
  31. _lastTop = app?.Current;
  32. }
  33. }