ApplicationPopover.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System.Diagnostics;
  2. namespace Terminal.Gui.App;
  3. /// <summary>
  4. /// Helper class for support of <see cref="IPopover"/> views for <see cref="IApplication"/>. Held by
  5. /// <see cref="IApplication.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. /// <summary>
  14. /// The <see cref="IApplication"/> instance used by this instance.
  15. /// </summary>
  16. public IApplication? App { get; set; }
  17. private readonly List<IPopover> _popovers = [];
  18. /// <summary>
  19. /// Gets the list of popovers registered with the application.
  20. /// </summary>
  21. public IReadOnlyCollection<IPopover> Popovers => _popovers.AsReadOnly ();
  22. /// <summary>
  23. /// Registers <paramref name="popover"/> with the application.
  24. /// This enables the popover to receive keyboard events even when it is not active.
  25. /// </summary>
  26. /// <remarks>
  27. /// When a popover is registered, the View instance lifetime is managed by the application. Call
  28. /// <see cref="DeRegister"/>
  29. /// to manage the lifetime of the popover directly.
  30. /// </remarks>
  31. /// <param name="popover"></param>
  32. /// <returns><paramref name="popover"/>, after it has been registered.</returns>
  33. public IPopover? Register (IPopover? popover)
  34. {
  35. if (popover is { } && !IsRegistered (popover))
  36. {
  37. popover.Current ??= App?.TopRunnableView as IRunnable;
  38. if (popover is View popoverView)
  39. {
  40. popoverView.App = App;
  41. }
  42. _popovers.Add (popover);
  43. }
  44. return popover;
  45. }
  46. /// <summary>
  47. /// Indicates whether a popover has been registered or not.
  48. /// </summary>
  49. /// <param name="popover"></param>
  50. /// <returns></returns>
  51. public bool IsRegistered (IPopover? popover) => popover is { } && _popovers.Contains (popover);
  52. /// <summary>
  53. /// De-registers <paramref name="popover"/> with the application. Use this to remove the popover and it's
  54. /// keyboard bindings from the application.
  55. /// </summary>
  56. /// <remarks>
  57. /// When a popover is registered, the View instance lifetime is managed by the application. Call
  58. /// <see cref="DeRegister"/>
  59. /// to manage the lifetime of the popover directly.
  60. /// </remarks>
  61. /// <param name="popover"></param>
  62. /// <returns></returns>
  63. public bool DeRegister (IPopover? popover)
  64. {
  65. if (popover is null || !IsRegistered (popover))
  66. {
  67. return false;
  68. }
  69. if (GetActivePopover () == popover)
  70. {
  71. _activePopover = null;
  72. }
  73. _popovers.Remove (popover);
  74. return true;
  75. }
  76. private IPopover? _activePopover;
  77. /// <summary>
  78. /// Gets the active popover, if any.
  79. /// </summary>
  80. /// <remarks>
  81. /// Note, the active pop over does not necessarily to be registered with the application.
  82. /// </remarks>
  83. /// <returns></returns>
  84. public IPopover? GetActivePopover () { return _activePopover; }
  85. /// <summary>
  86. /// Shows <paramref name="popover"/>. IPopover implementations should use OnVisibleChanaged/VisibleChanged to be
  87. /// notified when the user has done something to cause the popover to be hidden.
  88. /// </summary>
  89. /// <remarks>
  90. /// <para>
  91. /// This API calls <see cref="Register"/>. To disable the popover from processing keyboard events,
  92. /// either call <see cref="DeRegister"/> to
  93. /// remove the popover from the application or set <see cref="View.Enabled"/> to <see langword="false"/>.
  94. /// </para>
  95. /// </remarks>
  96. /// <param name="popover"></param>
  97. public void Show (IPopover? popover)
  98. {
  99. if (!IsRegistered (popover))
  100. {
  101. throw new InvalidOperationException (@"Popovers must be registered before being shown.");
  102. }
  103. // If there's an existing popover, hide it.
  104. if (_activePopover is View popoverView)
  105. {
  106. popoverView.App = App;
  107. popoverView.Visible = false;
  108. _activePopover = null;
  109. }
  110. if (popover is View newPopover)
  111. {
  112. if (!(newPopover.ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent) &&
  113. newPopover.ViewportSettings.HasFlag (ViewportSettingsFlags.TransparentMouse)))
  114. {
  115. throw new InvalidOperationException ("Popovers must have ViewportSettings.Transparent and ViewportSettings.TransparentMouse set.");
  116. }
  117. if (newPopover.KeyBindings.GetFirstFromCommands (Command.Quit) is null)
  118. {
  119. throw new InvalidOperationException ("Popovers must have a key binding for Command.Quit.");
  120. }
  121. if (!newPopover.IsInitialized)
  122. {
  123. newPopover.BeginInit ();
  124. newPopover.EndInit ();
  125. }
  126. _activePopover = newPopover as IPopover;
  127. newPopover.Enabled = true;
  128. newPopover.Visible = true;
  129. }
  130. }
  131. /// <summary>
  132. /// Causes the specified popover to be hidden.
  133. /// If the popover is derived from <see cref="PopoverBaseImpl"/>, this is the same as setting
  134. /// <see cref="View.Visible"/> to <see langword="false"/>.
  135. /// </summary>
  136. /// <param name="popover"></param>
  137. public void Hide (IPopover? popover)
  138. {
  139. // If there's an existing popover, hide it.
  140. if (_activePopover is View popoverView && popoverView == popover)
  141. {
  142. _activePopover = null;
  143. popoverView.Visible = false;
  144. popoverView.App?.TopRunnableView?.SetNeedsDraw ();
  145. }
  146. }
  147. /// <summary>
  148. /// Hides a popover view if it supports the quit command and is currently visible. It checks for the command's
  149. /// support before hiding.
  150. /// </summary>
  151. /// <param name="visiblePopover">The view that is being checked and potentially hidden based on its visibility and command support.</param>
  152. internal static void HideWithQuitCommand (View visiblePopover)
  153. {
  154. if (visiblePopover.Visible
  155. && (!visiblePopover.GetSupportedCommands ().Contains (Command.Quit)
  156. || (visiblePopover.InvokeCommand (Command.Quit) is true && visiblePopover.Visible)))
  157. {
  158. visiblePopover.Visible = false;
  159. }
  160. }
  161. /// <summary>
  162. /// Called when the user presses a key. Dispatches the key to the active popover, if any,
  163. /// otherwise to the popovers in the order they were registered. Inactive popovers only get hotkeys.
  164. /// </summary>
  165. /// <param name="key"></param>
  166. /// <returns></returns>
  167. internal bool DispatchKeyDown (Key key)
  168. {
  169. // Do active first - Active gets all key down events.
  170. View? activePopover = GetActivePopover () as View;
  171. if (activePopover is { Visible: true })
  172. {
  173. //Logging.Debug ($"Active - Calling NewKeyDownEvent ({key}) on {activePopover.Title}");
  174. if (activePopover.NewKeyDownEvent (key))
  175. {
  176. return true;
  177. }
  178. }
  179. // If the active popover didn't handle the key, try the inactive ones.
  180. // Inactive only get hotkeys
  181. bool? hotKeyHandled = null;
  182. foreach (IPopover popover in _popovers)
  183. {
  184. if (popover == activePopover
  185. || popover is not View popoverView
  186. || (popover.Current is { } && popover.Current != App?.TopRunnableView))
  187. {
  188. continue;
  189. }
  190. // hotKeyHandled = popoverView.InvokeCommandsBoundToHotKey (key);
  191. //Logging.Debug ($"Inactive - Calling NewKeyDownEvent ({key}) on {popoverView.Title}");
  192. popoverView.App ??= App;
  193. hotKeyHandled = popoverView.NewKeyDownEvent (key);
  194. if (hotKeyHandled is true)
  195. {
  196. return true;
  197. }
  198. }
  199. return hotKeyHandled is true;
  200. }
  201. /// <inheritdoc/>
  202. public void Dispose ()
  203. {
  204. foreach (IPopover popover in _popovers)
  205. {
  206. if (popover is View view)
  207. {
  208. view.Dispose ();
  209. }
  210. }
  211. _popovers.Clear ();
  212. }
  213. }