ViewMouse.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 is generated.</summary>
  10. public event EventHandler<MouseEventEventArgs> MouseClick;
  11. /// <summary>Event fired when the view receives the mouse event for the first time.</summary>
  12. public event EventHandler<MouseEventEventArgs> MouseEnter;
  13. /// <summary>Event fired when the view receives a mouse event for the last time.</summary>
  14. public event EventHandler<MouseEventEventArgs> MouseLeave;
  15. /// <inheritdoc/>
  16. public override bool OnMouseEnter (MouseEvent mouseEvent)
  17. {
  18. if (!Enabled)
  19. {
  20. return true;
  21. }
  22. if (!CanBeVisible (this))
  23. {
  24. return false;
  25. }
  26. var args = new MouseEventEventArgs (mouseEvent);
  27. MouseEnter?.Invoke (this, args);
  28. return args.Handled || base.OnMouseEnter (mouseEvent);
  29. }
  30. /// <summary>Method invoked when a mouse event is generated</summary>
  31. /// <remarks>
  32. /// The coordinates are relative to <see cref="View.Bounds"/>.
  33. /// </remarks>
  34. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  35. /// <param name="mouseEvent">Contains the details about the mouse event.</param>
  36. //public virtual bool MouseEvent (MouseEvent mouseEvent) { return false; }
  37. /// <summary>Method invoked when a mouse event is generated</summary>
  38. /// <param name="mouseEvent"></param>
  39. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  40. public virtual bool OnMouseEvent (MouseEvent mouseEvent)
  41. {
  42. if (!Enabled)
  43. {
  44. return true;
  45. }
  46. if (!CanBeVisible (this))
  47. {
  48. return false;
  49. }
  50. var args = new MouseEventEventArgs (mouseEvent);
  51. //if (MouseEvent (mouseEvent))
  52. //{
  53. // return true;
  54. //}
  55. if (mouseEvent.Flags == MouseFlags.Button1Clicked)
  56. {
  57. if (CanFocus && !HasFocus && SuperView is { })
  58. {
  59. SuperView.SetFocus (this);
  60. SetNeedsDisplay ();
  61. }
  62. return OnMouseClick (args);
  63. }
  64. if (mouseEvent.Flags == MouseFlags.Button2Clicked)
  65. {
  66. return OnMouseClick (args);
  67. }
  68. if (mouseEvent.Flags == MouseFlags.Button3Clicked)
  69. {
  70. return OnMouseClick (args);
  71. }
  72. if (mouseEvent.Flags == MouseFlags.Button4Clicked)
  73. {
  74. return OnMouseClick (args);
  75. }
  76. return false;
  77. }
  78. /// <inheritdoc/>
  79. public override bool OnMouseLeave (MouseEvent mouseEvent)
  80. {
  81. if (!Enabled)
  82. {
  83. return true;
  84. }
  85. if (!CanBeVisible (this))
  86. {
  87. return false;
  88. }
  89. var args = new MouseEventEventArgs (mouseEvent);
  90. MouseLeave?.Invoke (this, args);
  91. return args.Handled || base.OnMouseLeave (mouseEvent);
  92. }
  93. /// <summary>Invokes the MouseClick event.</summary>
  94. protected bool OnMouseClick (MouseEventEventArgs args)
  95. {
  96. if (!Enabled)
  97. {
  98. return true;
  99. }
  100. MouseClick?.Invoke (this, args);
  101. return args.Handled;
  102. }
  103. }