diff --git a/src/Avalonia.X11/X11CursorFactory.cs b/src/Avalonia.X11/X11CursorFactory.cs index 22254ada18..bd0413b1db 100644 --- a/src/Avalonia.X11/X11CursorFactory.cs +++ b/src/Avalonia.X11/X11CursorFactory.cs @@ -1,13 +1,10 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Runtime.InteropServices; using Avalonia.Controls.Platform.Surfaces; using Avalonia.Input; using Avalonia.Platform; using Avalonia.Platform.Internal; -using Avalonia.SourceGenerator; -using Avalonia.Utilities; #nullable enable @@ -50,15 +47,14 @@ namespace Avalonia.X11 {StandardCursorType.TopRightCorner, CursorFontShape.XC_top_right_corner}, }; - [GenerateEnumValueList] - private static partial CursorFontShape[] GetAllCursorShapes(); - public X11CursorFactory(IntPtr display) { _display = display; _nullCursor = GetNullCursor(display); - _cursors = GetAllCursorShapes() - .ToDictionary(id => id, id => XLib.XCreateFontCursor(_display, id)); + + // 78 = number of items in CursorFontShape enum + // Unlikely to change, but, do we have a Src Gen for this? + _cursors = new Dictionary(78); } public ICursorImpl GetCursor(StandardCursorType cursorType) @@ -71,8 +67,8 @@ namespace Avalonia.X11 else { handle = s_mapping.TryGetValue(cursorType, out var shape) - ? _cursors[shape] - : _cursors[CursorFontShape.XC_left_ptr]; + ? GetCursorHandleLazy(shape) + : GetCursorHandleLazy(CursorFontShape.XC_left_ptr); } return new CursorImpl(handle); } @@ -144,6 +140,14 @@ namespace Avalonia.X11 public IFramebufferRenderTarget CreateFramebufferRenderTarget() => new FuncFramebufferRenderTarget(Lock); } + + private nint GetCursorHandleLazy(CursorFontShape shape) + { + if (!_cursors.TryGetValue(shape, out var handle)) + _cursors[shape] = handle = XLib.XCreateFontCursor(_display, shape); + + return handle; + } } internal class CursorImpl : ICursorImpl diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index 905e9e1346..582608045f 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -2,8 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; -using Avalonia.Controls; using Avalonia.Controls.Platform; using Avalonia.FreeDesktop; using Avalonia.FreeDesktop.DBusIme;