ContextRequestedEventArgs.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Avalonia.Input;
  2. using Avalonia.Interactivity;
  3. namespace Avalonia.Controls
  4. {
  5. /// <summary>
  6. /// Provides event data for the ContextRequested event.
  7. /// </summary>
  8. public class ContextRequestedEventArgs : RoutedEventArgs
  9. {
  10. private readonly PointerEventArgs? _pointerEventArgs;
  11. /// <summary>
  12. /// Initializes a new instance of the ContextRequestedEventArgs class.
  13. /// </summary>
  14. public ContextRequestedEventArgs()
  15. : base(Control.ContextRequestedEvent)
  16. {
  17. }
  18. /// <inheritdoc cref="ContextRequestedEventArgs()" />
  19. public ContextRequestedEventArgs(PointerEventArgs pointerEventArgs)
  20. : this()
  21. {
  22. _pointerEventArgs = pointerEventArgs;
  23. }
  24. /// <summary>
  25. /// Gets the x- and y-coordinates of the pointer position, optionally evaluated against a coordinate origin of a supplied <see cref="Control"/>.
  26. /// </summary>
  27. /// <param name="relativeTo">
  28. /// Any <see cref="Control"/>-derived object that is connected to the same object tree.
  29. /// To specify the object relative to the overall coordinate system, use a relativeTo value of null.
  30. /// </param>
  31. /// <param name="point">
  32. /// A <see cref="Point"/> that represents the current x- and y-coordinates of the mouse pointer position.
  33. /// If null was passed as relativeTo, this coordinate is for the overall window.
  34. /// If a relativeTo value other than null was passed, this coordinate is relative to the object referenced by relativeTo.
  35. /// </param>
  36. /// <returns>
  37. /// true if the context request was initiated by a pointer device; otherwise, false.
  38. /// </returns>
  39. public bool TryGetPosition(Control? relativeTo, out Point point)
  40. {
  41. if (_pointerEventArgs is null)
  42. {
  43. point = default;
  44. return false;
  45. }
  46. point = _pointerEventArgs.GetPosition(relativeTo);
  47. return true;
  48. }
  49. }
  50. }