PopoverBaseImpl.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 false;
  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 is not true)
  50. {
  51. if (!Visible)
  52. {
  53. // Whenever visible is changing to true, we need to resize;
  54. // it's our only chance because we don't get laid out until we're visible
  55. Layout (Application.Screen.Size);
  56. }
  57. else
  58. {
  59. // Whenever visible is changing to false, we need to reset the focus
  60. if (ApplicationNavigation.IsInHierarchy(this, Application.Navigation?.GetFocused ()))
  61. {
  62. Application.Navigation?.SetFocused (Application.Top?.MostFocused);
  63. }
  64. }
  65. }
  66. return ret;
  67. }
  68. }