ApplicationPopover.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Helper class for support of <see cref="IPopover"/> views for <see cref="Application"/>. Held by
  5. /// <see cref="Application.Popover"/>
  6. /// </summary>
  7. public sealed class ApplicationPopover : IDisposable
  8. {
  9. /// <summary>
  10. /// Initializes a new instance of the <see cref="ApplicationPopover"/> class.
  11. /// </summary>
  12. public ApplicationPopover () { }
  13. private readonly List<IPopover> _popovers = [];
  14. /// <summary>
  15. /// Gets the list of popovers registered with the application.
  16. /// </summary>
  17. public IReadOnlyCollection<IPopover> Popovers => _popovers.AsReadOnly ();
  18. /// <summary>
  19. /// Registers <paramref name="popover"/> with the application.
  20. /// This enables the popover to receive keyboard events even when it is not active.
  21. /// </summary>
  22. /// <remarks>
  23. /// When a popover is registered, the View instance lifetime is managed by the application. Call
  24. /// <see cref="DeRegister"/>
  25. /// to manage the lifetime of the popover directly.
  26. /// </remarks>
  27. /// <param name="popover"></param>
  28. /// <returns><paramref name="popover"/>, after it has been registered.</returns>
  29. public IPopover? Register (IPopover? popover)
  30. {
  31. if (popover is { } && !_popovers.Contains (popover))
  32. {
  33. _popovers.Add (popover);
  34. }
  35. return popover;
  36. }
  37. /// <summary>
  38. /// De-registers <paramref name="popover"/> with the application. Use this to remove the popover and it's
  39. /// keyboard bindings from the application.
  40. /// </summary>
  41. /// <remarks>
  42. /// When a popover is registered, the View instance lifetime is managed by the application. Call
  43. /// <see cref="DeRegister"/>
  44. /// to manage the lifetime of the popover directly.
  45. /// </remarks>
  46. /// <param name="popover"></param>
  47. /// <returns></returns>
  48. public bool DeRegister (IPopover? popover)
  49. {
  50. if (popover is { } && _popovers.Contains (popover))
  51. {
  52. if (GetActivePopover () == popover)
  53. {
  54. _activePopover = null;
  55. }
  56. _popovers.Remove (popover);
  57. return true;
  58. }
  59. return false;
  60. }
  61. private IPopover? _activePopover;
  62. /// <summary>
  63. /// Gets the active popover, if any.
  64. /// </summary>
  65. /// <remarks>
  66. /// Note, the active pop over does not necessarily to be registered with the application.
  67. /// </remarks>
  68. /// <returns></returns>
  69. public IPopover? GetActivePopover () { return _activePopover; }
  70. /// <summary>
  71. /// Shows <paramref name="popover"/>. IPopover implementations should use OnVisibleChanaged/VisibleChanged to be
  72. /// notified when the user has done something to cause the popover to be hidden.
  73. /// </summary>
  74. /// <remarks>
  75. /// <para>
  76. /// This API calls <see cref="Register"/>. To disable the popover from processing keyboard events,
  77. /// either call <see cref="DeRegister"/> to
  78. /// remove the popover from the application or set <see cref="View.Enabled"/> to <see langword="false"/>.
  79. /// </para>
  80. /// </remarks>
  81. /// <param name="popover"></param>
  82. public void Show (IPopover? popover)
  83. {
  84. // If there's an existing popover, hide it.
  85. if (_activePopover is View popoverView)
  86. {
  87. popoverView.Visible = false;
  88. _activePopover = null;
  89. }
  90. if (popover is View newPopover)
  91. {
  92. if (!newPopover.IsInitialized)
  93. {
  94. newPopover.BeginInit ();
  95. newPopover.EndInit ();
  96. }
  97. _activePopover = newPopover as IPopover;
  98. newPopover.Enabled = true;
  99. newPopover.Visible = true;
  100. }
  101. }
  102. /// <summary>
  103. /// Causes the specified popover to be hidden.
  104. /// If the popover is dervied from <see cref="PopoverBaseImpl"/>, this is the same as setting
  105. /// <see cref="View.Visible"/> to <see langword="false"/>.
  106. /// </summary>
  107. /// <param name="popover"></param>
  108. public void Hide (IPopover? popover)
  109. {
  110. // If there's an existing popover, hide it.
  111. if (_activePopover is View popoverView && popoverView == popover)
  112. {
  113. popoverView.Visible = false;
  114. _activePopover = null;
  115. Application.Top?.SetNeedsDraw ();
  116. }
  117. }
  118. /// <summary>
  119. /// Called when the user presses a key. Dispatches the key to the active popover, if any,
  120. /// otherwise to the popovers in the order they were registered. Inactive popovers only get hotkeys.
  121. /// </summary>
  122. /// <param name="key"></param>
  123. /// <returns></returns>
  124. internal bool DispatchKeyDown (Key key)
  125. {
  126. // Do active first - Active gets all key down events.
  127. var activePopover = GetActivePopover () as View;
  128. if (activePopover is { Visible: true })
  129. {
  130. if (activePopover.NewKeyDownEvent (key))
  131. {
  132. return true;
  133. }
  134. }
  135. // If the active popover didn't handle the key, try the inactive ones.
  136. // Inactive only get hotkeys
  137. bool? hotKeyHandled = null;
  138. foreach (IPopover popover in _popovers)
  139. {
  140. if (popover == activePopover || popover is not View popoverView)
  141. {
  142. continue;
  143. }
  144. // hotKeyHandled = popoverView.InvokeCommandsBoundToHotKey (key);
  145. hotKeyHandled = popoverView.NewKeyDownEvent (key);
  146. if (hotKeyHandled is true)
  147. {
  148. return true;
  149. }
  150. }
  151. return hotKeyHandled is true;
  152. }
  153. /// <inheritdoc/>
  154. public void Dispose ()
  155. {
  156. foreach (IPopover popover in _popovers)
  157. {
  158. if (popover is View view)
  159. {
  160. view.Dispose ();
  161. }
  162. }
  163. _popovers.Clear ();
  164. }
  165. }