ViewMouse.cs 5.6 KB

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