using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Input;
namespace Xilium.CefGlue.Avalonia
{
///
/// Provides handles for mouse cursors.
///
internal class CursorsProvider
{
private static IDictionary _cache = new ConcurrentDictionary();
static CursorsProvider()
{
foreach(StandardCursorType type in Enum.GetValues(typeof(StandardCursorType)))
{
var cursor = new Cursor(type);
var platformImpl = cursor.PlatformImpl;
if (platformImpl != null)
{
var platformImplType = platformImpl.GetType();
var handleProperty = platformImplType.GetProperties().FirstOrDefault(x => x.Name == "Handle");
if (handleProperty != null)
{
var handle = (IntPtr)handleProperty.GetValue(platformImpl);
_cache[handle] = cursor;
}
}
}
}
public static Cursor GetCursorFromHandle(IntPtr handle)
{
if (_cache.TryGetValue(handle, out var cursor))
{
return cursor;
}
return Cursor.Default;
}
}
}