Skip to content

[X11] Use XcursorLibraryLoadCursor for dnd cursors #18286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/Avalonia.X11/X11CursorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal partial class X11CursorFactory : ICursorFactory
private static IntPtr _nullCursor;

private readonly IntPtr _display;
private Dictionary<CursorFontShape, IntPtr> _cursors;
private Dictionary<StandardCursorType, IntPtr> _cursors;

private static readonly Dictionary<StandardCursorType, CursorFontShape> s_mapping =
new Dictionary<StandardCursorType, CursorFontShape>
Expand Down Expand Up @@ -45,29 +45,29 @@ internal partial class X11CursorFactory : ICursorFactory
{StandardCursorType.TopRightCorner, CursorFontShape.XC_top_right_corner},
};

private static readonly Dictionary<StandardCursorType, string> s_libraryCursors = new()
{
{StandardCursorType.DragCopy, "dnd-copy"},
{StandardCursorType.DragLink, "dnd-link"},
{StandardCursorType.DragMove, "dnd-move"},
// TODO: Check if other platforms have dnd-none, dnd-no-drop and dnd-ask
};

public X11CursorFactory(IntPtr display)
{
_display = display;
_nullCursor = GetNullCursor(display);

// 78 = number of items in CursorFontShape enum
// Unlikely to change, but, do we have a Src Gen for this?
_cursors = new Dictionary<CursorFontShape, IntPtr>(78);
_cursors = new Dictionary<StandardCursorType, IntPtr>();
}

public ICursorImpl GetCursor(StandardCursorType cursorType)
{
IntPtr handle;
if (cursorType == StandardCursorType.None)
{
handle = _nullCursor;
}
else
{
handle = s_mapping.TryGetValue(cursorType, out var shape)
? GetCursorHandleLazy(shape)
: GetCursorHandleLazy(CursorFontShape.XC_left_ptr);
}
handle = GetCursorHandleCached(cursorType);
return new CursorImpl(handle);
}

Expand Down Expand Up @@ -139,10 +139,25 @@ public ILockedFramebuffer Lock()
public IFramebufferRenderTarget CreateFramebufferRenderTarget() => new FuncFramebufferRenderTarget(Lock);
}

private nint GetCursorHandleLazy(CursorFontShape shape)
private nint GetCursorHandleCached(StandardCursorType type)
{
if (!_cursors.TryGetValue(shape, out var handle))
_cursors[shape] = handle = XLib.XCreateFontCursor(_display, shape);
if (!_cursors.TryGetValue(type, out var handle))
{
if(s_libraryCursors.TryGetValue(type, out var cursorName))
handle = XLib.XcursorLibraryLoadCursor(_display, cursorName);
else if(s_mapping.TryGetValue(type, out var cursorShape))
handle = XLib.XCreateFontCursor(_display, cursorShape);

if (handle == IntPtr.Zero)
{
if (type != StandardCursorType.Arrow)
handle = GetCursorHandleCached(StandardCursorType.Arrow);
else
handle = _nullCursor;
}

_cursors[type] = handle;
}

return handle;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.X11/XLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ public static extern int XGetWindowProperty(IntPtr display, IntPtr window, IntPt
[DllImport(libX11)]
public static extern IntPtr XCreateFontCursor(IntPtr display, CursorFontShape shape);

[DllImport(libXCursor)]
public static extern IntPtr XcursorLibraryLoadCursor(IntPtr display, string name);

[DllImport(libX11)]
public static extern IntPtr XCreatePixmapCursor(IntPtr display, IntPtr source, IntPtr mask,
ref XColor foreground_color, ref XColor background_color, int x_hot, int y_hot);
Expand Down
Loading