InputExtensions.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Avalonia.Input;
  2. using Avalonia.VisualTree;
  3. using System.IO;
  4. namespace Xilium.CefGlue.Avalonia
  5. {
  6. internal static class InputExtensions
  7. {
  8. /// <summary>
  9. /// Convert a mouse event args into a cef mouse event.
  10. /// </summary>
  11. /// <param name="eventArgs">The mouse event args</param>
  12. /// <param name="mouseCoordinatesReferencial">The element used as the positioning referential</param>
  13. /// <returns></returns>
  14. public static CefMouseEvent AsCefMouseEvent(this PointerEventArgs eventArgs, IVisual mousePositionReferential)
  15. {
  16. var cursorPos = mousePositionReferential.IsAttachedToVisualTree ? eventArgs.GetPosition(mousePositionReferential) : new global::Avalonia.Point(0,0);
  17. return new CefMouseEvent((int) cursorPos.X, (int) cursorPos.Y, eventArgs.AsCefEventFlags());
  18. }
  19. /// <summary>
  20. /// Convert a mouse button into a cef mouse button.
  21. /// </summary>
  22. /// <param name="button"></param>
  23. /// <returns></returns>
  24. public static CefMouseButtonType AsCefMouseButtonType(this PointerEventArgs eventArgs)
  25. {
  26. switch (eventArgs.GetCurrentPoint(null).Properties.PointerUpdateKind.GetMouseButton())
  27. {
  28. case MouseButton.Middle:
  29. return CefMouseButtonType.Middle;
  30. case MouseButton.Right:
  31. return CefMouseButtonType.Right;
  32. default:
  33. return CefMouseButtonType.Left;
  34. }
  35. }
  36. /// <summary>
  37. /// Convert a key event into a cef key event.
  38. /// </summary>
  39. /// <param name="eventArgs"></param>
  40. /// <param name="isKeyUp"></param>
  41. /// <returns></returns>
  42. public static CefKeyEvent AsCefKeyEvent(this KeyEventArgs eventArgs, bool isKeyUp)
  43. {
  44. var modifiers = eventArgs.KeyModifiers.AsCefKeyboardModifiers();
  45. return new CefKeyEvent()
  46. {
  47. EventType = isKeyUp ? CefKeyEventType.KeyUp : CefKeyEventType.RawKeyDown,
  48. WindowsKeyCode = KeyInterop.VirtualKeyFromKey(eventArgs.Key),
  49. NativeKeyCode = (int) modifiers,
  50. IsSystemKey = eventArgs.Key == Key.System,
  51. Modifiers = modifiers
  52. };
  53. }
  54. /// <summary>
  55. /// Convert mouse modifiers into cef flags.
  56. /// </summary>
  57. /// <param name="keyboardModifiers"></param>
  58. /// <returns></returns>
  59. public static CefEventFlags AsCefEventFlags(this PointerEventArgs eventArgs)
  60. {
  61. var flags = CefEventFlags.None;
  62. var properties = eventArgs.GetCurrentPoint(null).Properties;
  63. if (properties.IsLeftButtonPressed)
  64. {
  65. flags |= CefEventFlags.LeftMouseButton;
  66. }
  67. if (properties.IsMiddleButtonPressed)
  68. {
  69. flags |= CefEventFlags.MiddleMouseButton;
  70. }
  71. if (properties.IsRightButtonPressed)
  72. {
  73. flags |= CefEventFlags.RightMouseButton;
  74. }
  75. return flags;
  76. }
  77. /// <summary>
  78. /// Convert keyboard modifiers into cef flags.
  79. /// </summary>
  80. /// <param name="keyboardModifiers"></param>
  81. /// <returns></returns>
  82. public static CefEventFlags AsCefKeyboardModifiers(this KeyModifiers keyboardModifiers)
  83. {
  84. var modifiers = new CefEventFlags();
  85. if (keyboardModifiers.HasFlag(KeyModifiers.Alt))
  86. {
  87. modifiers |= CefEventFlags.AltDown;
  88. }
  89. if (keyboardModifiers.HasFlag(KeyModifiers.Control))
  90. {
  91. modifiers |= CefEventFlags.ControlDown;
  92. }
  93. if (keyboardModifiers.HasFlag(KeyModifiers.Shift))
  94. {
  95. modifiers |= CefEventFlags.ShiftDown;
  96. }
  97. return modifiers;
  98. }
  99. /// <summary>
  100. /// Convert keyboard modifiers into cef flags.
  101. /// </summary>
  102. /// <param name="keyboardModifiers"></param>
  103. /// <returns></returns>
  104. public static CefEventFlags AsCefKeyboardModifiers(this InputModifiers keyboardModifiers)
  105. {
  106. var modifiers = new CefEventFlags();
  107. if (keyboardModifiers.HasFlag(InputModifiers.Alt))
  108. {
  109. modifiers |= CefEventFlags.AltDown;
  110. }
  111. if (keyboardModifiers.HasFlag(InputModifiers.Control))
  112. {
  113. modifiers |= CefEventFlags.ControlDown;
  114. }
  115. if (keyboardModifiers.HasFlag(InputModifiers.Shift))
  116. {
  117. modifiers |= CefEventFlags.ShiftDown;
  118. }
  119. return modifiers;
  120. }
  121. /// <summary>
  122. /// Convert a drag event args into a cef mouse event.
  123. /// </summary>
  124. /// <param name="eventArgs">The drag event args</param>
  125. /// <param name="mouseCoordinatesReferencial">The element used as the positioning referential</param>
  126. /// <returns></returns>
  127. public static CefMouseEvent AsCefMouseEvent(this DragEventArgs eventArgs, IVisual mouseCoordinatesReferencial)
  128. {
  129. var cursorPos = eventArgs.GetPosition(mouseCoordinatesReferencial);
  130. return new CefMouseEvent((int)cursorPos.X, (int)cursorPos.Y, eventArgs.Modifiers.AsCefKeyboardModifiers());
  131. }
  132. /// <summary>
  133. /// Converts a drag drop effects to Cef Drag Operations
  134. /// </summary>
  135. /// <param name="dragDropEffects">The drag drop effects.</param>
  136. /// <returns></returns>
  137. public static CefDragOperationsMask AsCefDragOperationsMask(this DragDropEffects dragDropEffects)
  138. {
  139. var operationsCount = 0;
  140. var operations = CefDragOperationsMask.None;
  141. if (dragDropEffects.HasFlag(DragDropEffects.Copy))
  142. {
  143. operations |= CefDragOperationsMask.Copy;
  144. operationsCount++;
  145. }
  146. if (dragDropEffects.HasFlag(DragDropEffects.Move))
  147. {
  148. operations |= CefDragOperationsMask.Move;
  149. operationsCount++;
  150. }
  151. if (dragDropEffects.HasFlag(DragDropEffects.Link))
  152. {
  153. operations |= CefDragOperationsMask.Link;
  154. operationsCount++;
  155. }
  156. if (operationsCount == 3)
  157. {
  158. return CefDragOperationsMask.Every;
  159. }
  160. return operations;
  161. }
  162. /// <summary>
  163. /// Gets the drag effects.
  164. /// </summary>
  165. /// <param name="mask">The mask.</param>
  166. /// <returns></returns>
  167. public static DragDropEffects AsDragDropEffects(this CefDragOperationsMask mask)
  168. {
  169. if (mask.HasFlag(CefDragOperationsMask.Every))
  170. {
  171. return DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link;
  172. }
  173. if (mask.HasFlag(CefDragOperationsMask.Copy))
  174. {
  175. return DragDropEffects.Copy;
  176. }
  177. if (mask.HasFlag(CefDragOperationsMask.Move))
  178. {
  179. return DragDropEffects.Move;
  180. }
  181. if (mask.HasFlag(CefDragOperationsMask.Link))
  182. {
  183. return DragDropEffects.Link;
  184. }
  185. return DragDropEffects.None;
  186. }
  187. /// <summary>
  188. /// Gets the drag data
  189. /// </summary>
  190. /// <param name="e">The <see cref="DragEventArgs"/> instance containing the event data.</param>
  191. /// <returns></returns>
  192. public static CefDragData GetDragData(this DragEventArgs e)
  193. {
  194. var dragData = CefDragData.Create();
  195. // Files
  196. if (e.Data.Contains(DataFormats.FileNames))
  197. {
  198. var files = (string[])e.Data.GetFileNames();
  199. foreach (var filePath in files)
  200. {
  201. var displayName = Path.GetFileName(filePath);
  202. dragData.AddFile(filePath.Replace("\\", "/"), displayName);
  203. }
  204. }
  205. // Text
  206. if (e.Data.Contains(DataFormats.Text))
  207. {
  208. dragData.SetFragmentText(e.Data.GetText());
  209. }
  210. return dragData;
  211. }
  212. }
  213. }