Padding.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. namespace Terminal.Gui.ViewBase;
  2. /// <summary>The Padding for a <see cref="View"/>. Accessed via <see cref="View.Padding"/></summary>
  3. /// <remarks>
  4. /// <para>See the <see cref="Adornment"/> class.</para>
  5. /// </remarks>
  6. public class Padding : Adornment
  7. {
  8. /// <inheritdoc/>
  9. public Padding ()
  10. {
  11. /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  12. }
  13. /// <inheritdoc/>
  14. public Padding (View parent) : base (parent)
  15. {
  16. CanFocus = true;
  17. TabStop = TabBehavior.NoStop;
  18. }
  19. /// <summary>Called when a mouse event occurs within the Padding.</summary>
  20. /// <remarks>
  21. /// <para>
  22. /// The coordinates are relative to <see cref="View.Viewport"/>.
  23. /// </para>
  24. /// <para>
  25. /// A mouse click on the Padding will cause the Parent to focus.
  26. /// </para>
  27. /// </remarks>
  28. /// <param name="mouseEvent"></param>
  29. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  30. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  31. {
  32. if (Parent is null)
  33. {
  34. return false;
  35. }
  36. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
  37. {
  38. if (Parent.CanFocus && !Parent.HasFocus)
  39. {
  40. Parent.SetFocus ();
  41. Parent.SetNeedsDraw ();
  42. return mouseEvent.Handled = true;
  43. }
  44. }
  45. return false;
  46. }
  47. /// <summary>
  48. /// Gets all SubViews of this Padding, optionally including SubViews of the Padding's Parent.
  49. /// </summary>
  50. /// <param name="includeMargin">
  51. /// Ignored.
  52. /// </param>
  53. /// <param name="includeBorder">
  54. /// Ignored.
  55. /// </param>
  56. /// <param name="includePadding">
  57. /// If <see langword="true"/>, includes SubViews from <see cref="Padding"/>. If <see langword="false"/> (default),
  58. /// returns only the direct SubViews
  59. /// of this Padding.
  60. /// </param>
  61. /// <returns>
  62. /// A read-only collection containing all SubViews. If <paramref name="includePadding"/> is
  63. /// <see langword="true"/>, the collection includes SubViews from this Padding's direct SubViews as well
  64. /// as SubViews from the Padding's Parent.
  65. /// </returns>
  66. /// <remarks>
  67. /// <para>
  68. /// This method returns a snapshot of the SubViews at the time of the call. The collection is
  69. /// safe to iterate even if SubViews are added or removed during iteration.
  70. /// </para>
  71. /// <para>
  72. /// The order of SubViews in the returned collection is:
  73. /// <list type="number">
  74. /// <item>Direct SubViews of this Padding</item>
  75. /// <item>SubViews of Parent (if <paramref name="includePadding"/> is <see langword="true"/>)</item>
  76. /// </list>
  77. /// </para>
  78. /// </remarks>
  79. public override IReadOnlyCollection<View> GetSubViews (bool includeMargin = false, bool includeBorder = false, bool includePadding = false)
  80. {
  81. List<View> subViewsOfThisAdornment = new (base.GetSubViews (false, false, includePadding));
  82. if (includePadding && Parent is { })
  83. {
  84. // Include SubViews from Parent. Since we are a Padding of Parent do not
  85. // request Adornments again to avoid infinite recursion.
  86. subViewsOfThisAdornment.AddRange (Parent.GetSubViews (false, false, false));
  87. }
  88. return subViewsOfThisAdornment;
  89. }
  90. }