2
0

ToplevelTransitionManager.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. namespace Terminal.Gui.App;
  2. // TODO: This whole concept is bogus and over-engineered.
  3. // TODO: Remove it and just let subscribers use the IApplication.Iteration
  4. // TODO: If the requirement is they know if it's the first iteration, they can
  5. // TODO: count invocations.
  6. /// <summary>
  7. /// Handles bespoke behaviours that occur when application top level changes.
  8. /// </summary>
  9. public class ToplevelTransitionManager : IToplevelTransitionManager
  10. {
  11. private readonly HashSet<Toplevel> _readiedTopLevels = new ();
  12. private View? _lastTop;
  13. /// <param name="app"></param>
  14. /// <inheritdoc/>
  15. public void RaiseReadyEventIfNeeded (IApplication? app)
  16. {
  17. Toplevel? top = app?.TopRunnable;
  18. if (top != null && !_readiedTopLevels.Contains (top))
  19. {
  20. top.OnReady ();
  21. _readiedTopLevels.Add (top);
  22. // Views can be closed and opened and run again multiple times, see End_Does_Not_Dispose
  23. top.Closed += (s, e) => _readiedTopLevels.Remove (top);
  24. }
  25. }
  26. /// <param name="app"></param>
  27. /// <inheritdoc/>
  28. public void HandleTopMaybeChanging (IApplication? app)
  29. {
  30. Toplevel? newTop = app?.TopRunnable;
  31. if (_lastTop != null && _lastTop != newTop && newTop != null)
  32. {
  33. newTop.SetNeedsDraw ();
  34. }
  35. _lastTop = app?.TopRunnable;
  36. }
  37. }