MouseEventArgs.cs 5.0 KB

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