PopoverBaseImpl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #nullable enable
  2. namespace Terminal.Gui.App;
  3. /// <summary>
  4. /// Abstract base class for popover views in Terminal.Gui.
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// <b>Popover Lifecycle:</b><br/>
  9. /// To display a popover, use <see cref="ApplicationPopover.Show"/>. To hide a popover, either call
  10. /// <see cref="ApplicationPopover.Hide"/>,
  11. /// set <see cref="View.Visible"/> to <see langword="false"/>, or show another popover.
  12. /// </para>
  13. /// <para>
  14. /// <b>Focus and Input:</b><br/>
  15. /// When visible, a popover receives focus and input events. If the user clicks outside the popover (and not on a
  16. /// subview),
  17. /// presses <see cref="Application.QuitKey"/>, or another popover is shown, the popover will be hidden
  18. /// automatically.
  19. /// </para>
  20. /// <para>
  21. /// <b>Layout:</b><br/>
  22. /// When the popover becomes visible, it is automatically laid out to fill the screen by default. You can override
  23. /// this behavior
  24. /// by setting <see cref="View.Width"/> and <see cref="View.Height"/> in your derived class.
  25. /// </para>
  26. /// <para>
  27. /// <b>Mouse:</b><br/>
  28. /// Popovers are transparent to mouse events (see <see cref="ViewportSettingsFlags.TransparentMouse"/>),
  29. /// meaning mouse events in a popover that are not also within a subview of the popover will not be captured.
  30. /// </para>
  31. /// <para>
  32. /// <b>Custom Popovers:</b><br/>
  33. /// To create a custom popover, inherit from <see cref="PopoverBaseImpl"/> and add your own content and logic.
  34. /// </para>
  35. /// </remarks>
  36. public abstract class PopoverBaseImpl : View, IPopover
  37. {
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="PopoverBaseImpl"/> class.
  40. /// </summary>
  41. /// <remarks>
  42. /// By default, the popover fills the available screen area and is focusable.
  43. /// </remarks>
  44. protected PopoverBaseImpl ()
  45. {
  46. Id = "popoverBaseImpl";
  47. CanFocus = true;
  48. Width = Dim.Fill ();
  49. Height = Dim.Fill ();
  50. ViewportSettings = ViewportSettingsFlags.Transparent | ViewportSettingsFlags.TransparentMouse;
  51. // TODO: Add a diagnostic setting for this?
  52. //TextFormatter.VerticalAlignment = Alignment.End;
  53. //TextFormatter.Alignment = Alignment.End;
  54. //base.Text = "popover";
  55. AddCommand (Command.Quit, Quit);
  56. KeyBindings.Add (Application.QuitKey, Command.Quit);
  57. return;
  58. bool? Quit (ICommandContext? ctx)
  59. {
  60. if (!Visible)
  61. {
  62. return false;
  63. }
  64. Visible = false;
  65. return true;
  66. }
  67. }
  68. /// <inheritdoc/>
  69. public Toplevel? Toplevel { get; set; }
  70. /// <summary>
  71. /// Called when the <see cref="View.Visible"/> property is changing.
  72. /// </summary>
  73. /// <remarks>
  74. /// When becoming visible, the popover is laid out to fit the screen.
  75. /// When becoming hidden, focus is restored to the previous view.
  76. /// </remarks>
  77. /// <returns>
  78. /// <see langword="true"/> to cancel the visibility change; otherwise, <see langword="false"/>.
  79. /// </returns>
  80. protected override bool OnVisibleChanging ()
  81. {
  82. bool ret = base.OnVisibleChanging ();
  83. if (ret)
  84. {
  85. return ret;
  86. }
  87. if (!Visible)
  88. {
  89. // Whenever visible is changing to true, we need to resize;
  90. // it's our only chance because we don't get laid out until we're visible
  91. Layout (Application.Screen.Size);
  92. }
  93. else
  94. {
  95. // Whenever visible is changing to false, we need to reset the focus
  96. if (ApplicationNavigation.IsInHierarchy (this, Application.Navigation?.GetFocused ()))
  97. {
  98. Application.Navigation?.SetFocused (Application.Top?.MostFocused);
  99. }
  100. }
  101. return ret;
  102. }
  103. }