ViewMouse.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. namespace Terminal.Gui;
  2. public partial class View
  3. {
  4. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.</summary>
  5. public virtual bool WantContinuousButtonPressed { get; set; }
  6. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.</summary>
  7. /// <value><see langword="true"/> if want mouse position reports; otherwise, <see langword="false"/>.</value>
  8. public virtual bool WantMousePositionReports { get; set; }
  9. /// <summary>Event fired when a mouse click occurs.</summary>
  10. /// <remarks>
  11. /// <para>
  12. /// Fired when the mouse is either clicked or double-clicked. Check
  13. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  14. /// </para>
  15. /// <para>
  16. /// The coordinates are relative to <see cref="View.Bounds"/>.
  17. /// </para>
  18. /// </remarks>
  19. public event EventHandler<MouseEventEventArgs> MouseClick;
  20. /// <summary>Event fired when the mouse moves into the View's <see cref="Bounds"/>.</summary>
  21. public event EventHandler<MouseEventEventArgs> MouseEnter;
  22. /// <summary>Event fired when the mouse leaves the View's <see cref="Bounds"/>.</summary>
  23. public event EventHandler<MouseEventEventArgs> MouseLeave;
  24. // TODO: OnMouseEnter should not be public virtual, but protected.
  25. /// <summary>
  26. /// Called when the mouse enters the View's <see cref="Bounds"/>. The view will now receive mouse events until the mouse leaves
  27. /// the view. At which time, <see cref="OnMouseLeave(Gui.MouseEvent)"/> will be called.
  28. /// </summary>
  29. /// <remarks>
  30. /// The coordinates are relative to <see cref="View.Bounds"/>.
  31. /// </remarks>
  32. /// <param name="mouseEvent"></param>
  33. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  34. protected internal virtual bool OnMouseEnter (MouseEvent mouseEvent)
  35. {
  36. if (!Enabled)
  37. {
  38. return true;
  39. }
  40. if (!CanBeVisible (this))
  41. {
  42. return false;
  43. }
  44. var args = new MouseEventEventArgs (mouseEvent);
  45. MouseEnter?.Invoke (this, args);
  46. return args.Handled;
  47. }
  48. // TODO: OnMouseLeave should not be public virtual, but protected.
  49. /// <summary>
  50. /// Called when the mouse has moved out of the View's <see cref="Bounds"/>. The view will no longer receive mouse events (until the
  51. /// mouse moves within the view again and <see cref="OnMouseEnter(Gui.MouseEvent)"/> is called).
  52. /// </summary>
  53. /// <remarks>
  54. /// The coordinates are relative to <see cref="View.Bounds"/>.
  55. /// </remarks>
  56. /// <param name="mouseEvent"></param>
  57. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  58. protected internal virtual bool OnMouseLeave (MouseEvent mouseEvent)
  59. {
  60. if (!Enabled)
  61. {
  62. return true;
  63. }
  64. if (!CanBeVisible (this))
  65. {
  66. return false;
  67. }
  68. var args = new MouseEventEventArgs (mouseEvent);
  69. MouseLeave?.Invoke (this, args);
  70. return args.Handled;
  71. }
  72. // TODO: OnMouseEvent should not be public virtual, but protected.
  73. /// <summary>Called when a mouse event occurs within the view's <see cref="Bounds"/>.</summary>
  74. /// <remarks>
  75. /// <para>
  76. /// The coordinates are relative to <see cref="View.Bounds"/>.
  77. /// </para>
  78. /// </remarks>
  79. /// <param name="mouseEvent"></param>
  80. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  81. protected internal virtual bool OnMouseEvent (MouseEvent mouseEvent)
  82. {
  83. if (!Enabled)
  84. {
  85. return true;
  86. }
  87. if (!CanBeVisible (this))
  88. {
  89. return false;
  90. }
  91. var args = new MouseEventEventArgs (mouseEvent);
  92. // Clicked support for all buttons and single and double click
  93. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  94. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  95. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  96. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked))
  97. {
  98. return OnMouseClick (args);
  99. }
  100. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  101. || mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
  102. || mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
  103. || mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked))
  104. {
  105. return OnMouseClick (args);
  106. }
  107. return false;
  108. }
  109. /// <summary>Invokes the MouseClick event.</summary>
  110. /// <remarks>
  111. /// <para>
  112. /// Called when the mouse is either clicked or double-clicked. Check
  113. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  114. /// </para>
  115. /// </remarks>
  116. protected bool OnMouseClick (MouseEventEventArgs args)
  117. {
  118. if (!Enabled)
  119. {
  120. return true;
  121. }
  122. MouseClick?.Invoke (this, args);
  123. if (args.Handled)
  124. {
  125. return true;
  126. }
  127. if (!HasFocus && CanFocus)
  128. {
  129. SetFocus ();
  130. }
  131. return args.Handled;
  132. }
  133. }