From f2bf50f2cf0e43bf7e6169d1fcdcb63104da42ff Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Mon, 26 Apr 2021 21:57:22 +0800 Subject: [PATCH] CursorInteropHelper --- .../Internal/CursorInteropHelper.Beta.cs | 126 +++++++----------- .../ST.Client.Desktop.Avalonia.csproj | 9 -- 2 files changed, 47 insertions(+), 88 deletions(-) diff --git a/references/CefNet/CefNet.Avalonia/Internal/CursorInteropHelper.Beta.cs b/references/CefNet/CefNet.Avalonia/Internal/CursorInteropHelper.Beta.cs index eaccbe921..326f5194b 100644 --- a/references/CefNet/CefNet.Avalonia/Internal/CursorInteropHelper.Beta.cs +++ b/references/CefNet/CefNet.Avalonia/Internal/CursorInteropHelper.Beta.cs @@ -9,90 +9,58 @@ using System.Text; namespace CefNet.Internal { - public static class CursorInteropHelper - { - private static readonly Dictionary _Cursors = new Dictionary(); - private static readonly Dictionary _StdCursors = new Dictionary(); + public static class CursorInteropHelper + { + private static readonly Dictionary _Cursors = new Dictionary(); + private static readonly Dictionary _StdCursors = new Dictionary(); -#region Avalonia API Change see https://github.com/AvaloniaUI/Avalonia/pull/5763 - static readonly object lock_GetPlatformHandle = new object(); - static readonly Dictionary> _GetPlatformHandle = new Dictionary>(); - static IntPtr GetDefault(ICursorImpl _) => default; - static IntPtr GetPlatformHandle(Cursor cursor) - { - if (cursor != null) - { - if (cursor.PlatformImpl is IPlatformHandle i) - { - return i.Handle; - } - else if (cursor.PlatformImpl != null) - { - var cursorType = cursor.PlatformImpl.GetType(); - if (_GetPlatformHandle.ContainsKey(cursorType)) - { - return _GetPlatformHandle[cursorType](cursor.PlatformImpl); - } - else - { - lock (lock_GetPlatformHandle) - { - Func value; - var p = cursorType.GetProperty(nameof(IPlatformHandle.Handle), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, typeof(IntPtr), Array.Empty(), Array.Empty()); - if (p != null) - { - value = c => (IntPtr)p.GetValue(c); - } - else - { - value = GetDefault; - } - _GetPlatformHandle.Add(cursorType, value); - return value(cursor.PlatformImpl); - } - } - } - } + private static IntPtr GetPlatformHandle(Cursor cursor) + { + if (cursor != null) + { + if (cursor.PlatformImpl is IPlatformHandle i) + { + return i.Handle; + } + } + return default; + } - return default; - } -#endregion + static CursorInteropHelper() + { + foreach (StandardCursorType cursorType in Enum.GetValues(typeof(StandardCursorType))) + { + var cursor = new Cursor(cursorType); + var handle = GetPlatformHandle(cursor); + if (handle == default || _Cursors.ContainsKey(handle)) + continue; - static CursorInteropHelper() - { - foreach (StandardCursorType cursorType in Enum.GetValues(typeof(StandardCursorType))) - { - var cursor = new Cursor(cursorType); - var handle = GetPlatformHandle(cursor); - if (_Cursors.ContainsKey(handle)) - continue; + _Cursors.Add(handle, cursor); + _StdCursors.Add(cursorType, cursor); + } + } - _Cursors.Add(handle, cursor); - _StdCursors.Add(cursorType, cursor); - } - } + public static Cursor Create(IntPtr cursorHandle) + { + if (_Cursors.TryGetValue(cursorHandle, out Cursor cursor)) + return cursor; + return Cursor.Default; + } - public static Cursor Create(IntPtr cursorHandle) - { - if (_Cursors.TryGetValue(cursorHandle, out Cursor cursor)) - return cursor; - return Cursor.Default; - } + public static Cursor Create(StandardCursorType cursorType) + { + Cursor cursor; + lock (_StdCursors) + { + if (!_StdCursors.TryGetValue(cursorType, out cursor)) + { + cursor = new Cursor(cursorType); + _StdCursors[cursorType] = cursor; + } + } + return cursor; + } - public static Cursor Create(StandardCursorType cursorType) - { - Cursor cursor; - lock (_StdCursors) - { - if (!_StdCursors.TryGetValue(cursorType, out cursor)) - { - cursor = new Cursor(cursorType); - _StdCursors[cursorType] = cursor; - } - } - return cursor; - } - - } + } } #endif \ No newline at end of file diff --git a/src/ST.Client.Desktop.Avalonia/ST.Client.Desktop.Avalonia.csproj b/src/ST.Client.Desktop.Avalonia/ST.Client.Desktop.Avalonia.csproj index c9d7e9be2..e7709adea 100644 --- a/src/ST.Client.Desktop.Avalonia/ST.Client.Desktop.Avalonia.csproj +++ b/src/ST.Client.Desktop.Avalonia/ST.Client.Desktop.Avalonia.csproj @@ -48,13 +48,4 @@ - - - - AchievementWindow.axaml - - - ScriptStoreWindow.axaml - - \ No newline at end of file