PopoverBaseImpl.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Abstract base class for Popover Views.
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// To show a Popover, use <see cref="ApplicationPopover.Show"/>. To hide a popover,
  9. /// call <see cref="ApplicationPopover.Show"/> with <see langword="null"/> set <see cref="View.Visible"/> to <see langword="false"/>.
  10. /// </para>
  11. /// <para>
  12. /// If the user clicks anywhere not occluded by a SubView of the Popover, presses <see cref="Application.QuitKey"/>,
  13. /// or causes another popover to show, the Popover will be hidden.
  14. /// </para>
  15. /// </remarks>
  16. public abstract class PopoverBaseImpl : View, IPopover
  17. {
  18. /// <summary>
  19. /// Creates a new PopoverBaseImpl.
  20. /// </summary>
  21. protected PopoverBaseImpl ()
  22. {
  23. Id = "popoverBaseImpl";
  24. CanFocus = true;
  25. Width = Dim.Fill ();
  26. Height = Dim.Fill ();
  27. ViewportSettings = ViewportSettings.Transparent | ViewportSettings.TransparentMouse;
  28. // TODO: Add a diagnostic setting for this?
  29. //TextFormatter.VerticalAlignment = Alignment.End;
  30. //TextFormatter.Alignment = Alignment.End;
  31. //base.Text = "popover";
  32. AddCommand (Command.Quit, Quit);
  33. KeyBindings.Add (Application.QuitKey, Command.Quit);
  34. return;
  35. bool? Quit (ICommandContext? ctx)
  36. {
  37. if (!Visible)
  38. {
  39. return null;
  40. }
  41. Visible = false;
  42. return true;
  43. }
  44. }
  45. /// <inheritdoc />
  46. protected override bool OnVisibleChanging ()
  47. {
  48. bool ret = base.OnVisibleChanging ();
  49. if (!ret && !Visible)
  50. {
  51. // Whenever visible is changing to true, we need to resize;
  52. // it's our only chance because we don't get laid out until we're visible
  53. Layout (Application.Screen.Size);
  54. }
  55. return ret;
  56. }
  57. }