GuiTestContext.ContextMenu.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System.Diagnostics;
  2. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  3. namespace TerminalGuiFluentTesting;
  4. public partial class GuiTestContext
  5. {
  6. /// <summary>
  7. /// Registers a right click handler on the <see cref="LastView"/> added view (or root view) that
  8. /// will open the supplied <paramref name="contextMenu"/>.
  9. /// </summary>
  10. /// <param name="contextMenu"></param>
  11. /// <returns></returns>
  12. public GuiTestContext WithContextMenu (PopoverMenu? contextMenu)
  13. {
  14. if (contextMenu?.App is null)
  15. {
  16. Fail (@"PopoverMenu's must have their App property set.");
  17. }
  18. LastView.MouseEvent += (_, e) =>
  19. {
  20. if (e.Flags.HasFlag (MouseFlags.Button3Clicked))
  21. {
  22. // Registering with the PopoverManager will ensure that the context menu is closed when the view is no longer focused
  23. // and the context menu is disposed when it is closed.
  24. App?.Popover?.Register (contextMenu);
  25. contextMenu?.MakeVisible (e.ScreenPosition);
  26. }
  27. };
  28. return this;
  29. }
  30. }