CursorsProvider.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Avalonia.Input;
  6. namespace Xilium.CefGlue.Avalonia
  7. {
  8. /// <summary>
  9. /// Provides handles for mouse cursors.
  10. /// </summary>
  11. internal class CursorsProvider
  12. {
  13. private static IDictionary<IntPtr, Cursor> _cache = new ConcurrentDictionary<IntPtr, Cursor>();
  14. static CursorsProvider()
  15. {
  16. foreach(StandardCursorType type in Enum.GetValues(typeof(StandardCursorType)))
  17. {
  18. var cursor = new Cursor(type);
  19. var platformImpl = cursor.PlatformImpl;
  20. if (platformImpl != null)
  21. {
  22. var platformImplType = platformImpl.GetType();
  23. var handleProperty = platformImplType.GetProperties().FirstOrDefault(x => x.Name == "Handle");
  24. if (handleProperty != null)
  25. {
  26. var handle = (IntPtr)handleProperty.GetValue(platformImpl);
  27. _cache[handle] = cursor;
  28. }
  29. }
  30. }
  31. }
  32. public static Cursor GetCursorFromHandle(IntPtr handle)
  33. {
  34. if (_cache.TryGetValue(handle, out var cursor))
  35. {
  36. return cursor;
  37. }
  38. return Cursor.Default;
  39. }
  40. }
  41. }