Cursor.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using Avalonia.Media.Imaging;
  3. using Avalonia.Platform;
  4. #nullable enable
  5. namespace Avalonia.Input
  6. {
  7. public enum StandardCursorType
  8. {
  9. Arrow,
  10. Ibeam,
  11. Wait,
  12. Cross,
  13. UpArrow,
  14. SizeWestEast,
  15. SizeNorthSouth,
  16. SizeAll,
  17. No,
  18. Hand,
  19. AppStarting,
  20. Help,
  21. TopSide,
  22. BottomSide,
  23. LeftSide,
  24. RightSide,
  25. TopLeftCorner,
  26. TopRightCorner,
  27. BottomLeftCorner,
  28. BottomRightCorner,
  29. DragMove,
  30. DragCopy,
  31. DragLink,
  32. None,
  33. // Not available in GTK directly, see https://www.pixelbeat.org/programming/x_cursors/
  34. // We might enable them later, preferably, by loading pixmax directly from theme with fallback image
  35. // SizeNorthWestSouthEast,
  36. // SizeNorthEastSouthWest,
  37. }
  38. public class Cursor : IDisposable
  39. {
  40. public static readonly Cursor Default = new Cursor(StandardCursorType.Arrow);
  41. internal Cursor(ICursorImpl platformImpl)
  42. {
  43. PlatformImpl = platformImpl;
  44. }
  45. public Cursor(StandardCursorType cursorType)
  46. : this(GetCursorFactory().GetCursor(cursorType))
  47. {
  48. }
  49. public Cursor(IBitmap cursor, PixelPoint hotSpot)
  50. : this(GetCursorFactory().CreateCursor(cursor.PlatformImpl.Item, hotSpot))
  51. {
  52. }
  53. public ICursorImpl PlatformImpl { get; }
  54. public void Dispose() => PlatformImpl.Dispose();
  55. public static Cursor Parse(string s)
  56. {
  57. return Enum.TryParse<StandardCursorType>(s, true, out var t) ?
  58. new Cursor(t) :
  59. throw new ArgumentException($"Unrecognized cursor type '{s}'.");
  60. }
  61. private static ICursorFactory GetCursorFactory()
  62. {
  63. return AvaloniaLocator.Current.GetRequiredService<ICursorFactory>();
  64. }
  65. }
  66. }