MouseEventArgs.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.ComponentModel;
  2. namespace Terminal.Gui.Input;
  3. /// <summary>
  4. /// Specifies the event arguments for <see cref="MouseEventArgs"/>. This is a higher-level construct than
  5. /// the wrapped <see cref="MouseEventArgs"/> class and is used for the events defined on
  6. /// <see cref="View"/> and subclasses
  7. /// of View (e.g. <see cref="View.MouseEnter"/> and <see cref="View.MouseClick"/>).
  8. /// </summary>
  9. public class MouseEventArgs : HandledEventArgs
  10. {
  11. /// <summary>
  12. /// Flags indicating the state of the mouse buttons and the type of event that occurred.
  13. /// </summary>
  14. public MouseFlags Flags { get; set; }
  15. /// <summary>
  16. /// The screen-relative mouse position.
  17. /// </summary>
  18. public Point ScreenPosition { get; set; }
  19. /// <summary>The deepest View who's <see cref="View.Frame"/> contains <see cref="ScreenPosition"/>.</summary>
  20. public View? View { get; set; }
  21. /// <summary>
  22. /// The position of the mouse in <see cref="View"/>'s Viewport-relative coordinates. Only valid if <see cref="View"/>
  23. /// is set.
  24. /// </summary>
  25. public Point Position { get; set; }
  26. /// <summary>
  27. /// Gets whether <see cref="Flags"/> contains any of the button pressed related flags.
  28. /// </summary>
  29. public bool IsPressed => Flags.HasFlag (MouseFlags.Button1Pressed)
  30. || Flags.HasFlag (MouseFlags.Button2Pressed)
  31. || Flags.HasFlag (MouseFlags.Button3Pressed)
  32. || Flags.HasFlag (MouseFlags.Button4Pressed);
  33. /// <summary>
  34. /// Gets whether <see cref="Flags"/> contains any of the button released related flags.
  35. /// </summary>
  36. public bool IsReleased => Flags.HasFlag (MouseFlags.Button1Released)
  37. || Flags.HasFlag (MouseFlags.Button2Released)
  38. || Flags.HasFlag (MouseFlags.Button3Released)
  39. || Flags.HasFlag (MouseFlags.Button4Released);
  40. /// <summary>
  41. /// Gets whether <see cref="Flags"/> contains any of the single-clicked related flags.
  42. /// </summary>
  43. public bool IsSingleClicked => Flags.HasFlag (MouseFlags.Button1Clicked)
  44. || Flags.HasFlag (MouseFlags.Button2Clicked)
  45. || Flags.HasFlag (MouseFlags.Button3Clicked)
  46. || Flags.HasFlag (MouseFlags.Button4Clicked);
  47. /// <summary>
  48. /// Gets whether <see cref="Flags"/> contains any of the double-clicked related flags.
  49. /// </summary>
  50. public bool IsDoubleClicked => Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  51. || Flags.HasFlag (MouseFlags.Button2DoubleClicked)
  52. || Flags.HasFlag (MouseFlags.Button3DoubleClicked)
  53. || Flags.HasFlag (MouseFlags.Button4DoubleClicked);
  54. /// <summary>
  55. /// Gets whether <see cref="Flags"/> contains any of the triple-clicked related flags.
  56. /// </summary>
  57. public bool IsTripleClicked => Flags.HasFlag (MouseFlags.Button1TripleClicked)
  58. || Flags.HasFlag (MouseFlags.Button2TripleClicked)
  59. || Flags.HasFlag (MouseFlags.Button3TripleClicked)
  60. || Flags.HasFlag (MouseFlags.Button4TripleClicked);
  61. /// <summary>
  62. /// Gets whether <see cref="Flags"/> contains any of the mouse button clicked related flags.
  63. /// </summary>
  64. public bool IsSingleDoubleOrTripleClicked =>
  65. Flags.HasFlag (MouseFlags.Button1Clicked)
  66. || Flags.HasFlag (MouseFlags.Button2Clicked)
  67. || Flags.HasFlag (MouseFlags.Button3Clicked)
  68. || Flags.HasFlag (MouseFlags.Button4Clicked)
  69. || Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  70. || Flags.HasFlag (MouseFlags.Button2DoubleClicked)
  71. || Flags.HasFlag (MouseFlags.Button3DoubleClicked)
  72. || Flags.HasFlag (MouseFlags.Button4DoubleClicked)
  73. || Flags.HasFlag (MouseFlags.Button1TripleClicked)
  74. || Flags.HasFlag (MouseFlags.Button2TripleClicked)
  75. || Flags.HasFlag (MouseFlags.Button3TripleClicked)
  76. || Flags.HasFlag (MouseFlags.Button4TripleClicked);
  77. /// <summary>
  78. /// Gets whether <see cref="Flags"/> contains any of the mouse wheel related flags.
  79. /// </summary>
  80. public bool IsWheel => Flags.HasFlag (MouseFlags.WheeledDown)
  81. || Flags.HasFlag (MouseFlags.WheeledUp)
  82. || Flags.HasFlag (MouseFlags.WheeledLeft)
  83. || Flags.HasFlag (MouseFlags.WheeledRight);
  84. /// <summary>Returns a <see cref="T:System.String"/> that represents the current <see cref="MouseEventArgs"/>.</summary>
  85. /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="MouseEventArgs"/>.</returns>
  86. public override string ToString () { return $"({ScreenPosition}):{Flags}:{View?.Id}:{Position}"; }
  87. }