ViewPopoverExtensions.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. namespace Terminal.Gui.App;
  2. /// <summary>
  3. /// Extension methods for making any <see cref="View"/> into a popover.
  4. /// </summary>
  5. /// <remarks>
  6. /// These extensions provide a fluent API for wrapping views in <see cref="PopoverWrapper{TView}"/>,
  7. /// enabling any View to be shown as a popover without implementing <see cref="IPopover"/>.
  8. /// </remarks>
  9. public static class ViewPopoverExtensions
  10. {
  11. /// <summary>
  12. /// Converts any View into a popover.
  13. /// </summary>
  14. /// <typeparam name="TView">The type of view to make into a popover.</typeparam>
  15. /// <param name="view">The view to wrap. Cannot be null.</param>
  16. /// <returns>A <see cref="PopoverWrapper{TView}"/> that wraps the view.</returns>
  17. /// <exception cref="ArgumentNullException">Thrown if <paramref name="view"/> is null.</exception>
  18. /// <remarks>
  19. /// <para>
  20. /// This method wraps the view in a <see cref="PopoverWrapper{TView}"/> which automatically
  21. /// handles popover behavior including transparency, focus, and quit key handling.
  22. /// </para>
  23. /// <para>
  24. /// After creating the wrapper, register it with <see cref="ApplicationPopover.Register"/>
  25. /// and show it with <see cref="ApplicationPopover.Show"/>.
  26. /// </para>
  27. /// </remarks>
  28. /// <example>
  29. /// <code>
  30. /// // Make a custom view into a popover
  31. /// var myView = new View
  32. /// {
  33. /// X = Pos.Center (),
  34. /// Y = Pos.Center (),
  35. /// Width = 40,
  36. /// Height = 10,
  37. /// BorderStyle = LineStyle.Single
  38. /// };
  39. /// myView.Add (new Label { Text = "Hello Popover!" });
  40. ///
  41. /// var popover = myView.AsPopover();
  42. /// app.Popover.Register (popover);
  43. /// app.Popover.Show (popover);
  44. ///
  45. /// // The wrapped view can still be accessed
  46. /// Console.WriteLine ($"View id: {popover.WrappedView.Id}");
  47. /// </code>
  48. /// </example>
  49. public static PopoverWrapper<TView> AsPopover<TView> (this TView view)
  50. where TView : View
  51. {
  52. if (view is null)
  53. {
  54. throw new ArgumentNullException (nameof (view));
  55. }
  56. return new PopoverWrapper<TView> { WrappedView = view };
  57. }
  58. }