-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
25 additions
and
24 deletions.
There are no files selected for viewing
49 changes: 25 additions & 24 deletions
49
src/Uno/Prism.Uno/Extensions/DependencyObjectExtensions.Uno.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Data; | ||
using Windows.Foundation.Metadata; | ||
|
||
namespace Prism | ||
{ | ||
internal static partial class DependencyObjectExtensions | ||
{ | ||
#if HAS_UNO_WINUI | ||
// Related to : | ||
// https://github.com/dotnet/runtime/issues/76959 | ||
// https://github.com/unoplatform/uno/blob/56d3f6ece16b2dba51776e95a3c847c9c4b68e8b/src/Uno.UI.Dispatching/Core/CoreDispatcher.wasm.cs#L17 | ||
private static bool IsWebAssemblyThreadingSupported { get; } | ||
= Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES") | ||
?.Split(',').Any(v => v.Equals("threads", StringComparison.OrdinalIgnoreCase)) ?? false; | ||
|
||
private static bool IsWebAssembly { get; } | ||
= RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")); | ||
#endif | ||
|
||
/// <summary> | ||
/// Compatibility method to determine if the current thread can access a <see cref="DependencyObject"/> | ||
/// </summary> | ||
/// <param name="instance">The instance to check</param> | ||
/// <returns><c>true</c> if the current thread has access to the instance, otherwise <c>false</c></returns> | ||
public static bool CheckAccess(this DependencyObject instance) | ||
#if __WASM__ | ||
// This needs to evolve, once threading is supported | ||
// See https://github.com/unoplatform/uno/issues/2302 | ||
=> System.Threading.Thread.CurrentThread.ManagedThreadId == 1; | ||
#elif HAS_WINUI | ||
#if NETCOREAPP | ||
=> instance.DispatcherQueue.HasThreadAccess; | ||
#else | ||
#if HAS_WINUI | ||
#if HAS_UNO_WINUI | ||
{ | ||
if (IsWebAssembly && !IsWebAssemblyThreadingSupported) | ||
{ | ||
// Dispatcher queue HasThreadAccess is not yet implemented in Uno, we can fall back on CoreDispatcher | ||
// See https://github.com/unoplatform/uno/issues/5818 | ||
if(ApiInformation.IsPropertyPresent("Microsoft.UI.Dispatching.DispatcherQueue", nameof(Microsoft.UI.Dispatching.DispatcherQueue.HasThreadAccess))) | ||
{ | ||
return instance.DispatcherQueue.HasThreadAccess; | ||
} | ||
else | ||
{ | ||
return instance.Dispatcher.HasThreadAccess; | ||
} | ||
// When threading is disabled, we need to return true | ||
return true; | ||
} | ||
|
||
return instance.DispatcherQueue.HasThreadAccess; | ||
} | ||
#else | ||
=> instance.DispatcherQueue.HasThreadAccess; | ||
#endif | ||
#else | ||
=> instance.Dispatcher.HasThreadAccess; | ||
=> instance.Dispatcher.HasThreadAccess; | ||
#endif | ||
} | ||
} |