diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs index 144e2d843a..151227dba4 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.Versioning; using Android.App; using Android.Content; using Android.Graphics; @@ -299,93 +300,36 @@ namespace Avalonia.Android.Platform.SkiaPlatform public double Scaling => RenderScaling; - public void SetTransparencyLevelHint(IReadOnlyList transparencyLevel) + public void SetTransparencyLevelHint(IReadOnlyList transparencyLevels) { - ////if (TransparencyLevel != transparencyLevel) - ////{ - //// bool isBelowR = Build.VERSION.SdkInt < BuildVersionCodes.R; - //// bool isAboveR = Build.VERSION.SdkInt > BuildVersionCodes.R; - //// if (_view.Context is AvaloniaMainActivity activity) - //// { - //// if (transparencyLevel == WindowTransparencyLevel.AcrylicBlur || - //// transparencyLevel == WindowTransparencyLevel.Mica || - //// transparencyLevel == WindowTransparencyLevel.None) - //// { - //// if (!isBelowR) - //// { - //// activity.SetTranslucent(false); - //// } - //// if (isAboveR) - //// { - //// activity.Window?.ClearFlags(WindowManagerFlags.BlurBehind); - - //// var attr = activity.Window?.Attributes; - //// if (attr != null) - //// { - //// attr.BlurBehindRadius = 0; - - //// activity.Window.Attributes = attr; - //// } - //// } - //// activity.Window.SetBackgroundDrawable(new ColorDrawable(Color.White)); - - //// if (transparencyLevel != WindowTransparencyLevel.None) - //// { - //// return; - //// } - //// } - - //// if (transparencyLevel == WindowTransparencyLevel.Transparent) - //// { - //// if (!isBelowR) - //// { - //// activity.SetTranslucent(true); - //// } - //// if (isAboveR) - //// { - //// activity.Window?.ClearFlags(WindowManagerFlags.BlurBehind); - - //// var attr = activity.Window?.Attributes; - //// if (attr != null) - //// { - //// attr.BlurBehindRadius = 0; - - //// activity.Window.Attributes = attr; - //// } - //// } - //// activity.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent)); - //// } - - //// if (transparencyLevel == WindowTransparencyLevel.Blur) - //// { - //// if (isAboveR) - //// { - //// activity.SetTranslucent(true); - //// activity.Window?.AddFlags(WindowManagerFlags.BlurBehind); - - //// var attr = activity.Window?.Attributes; - //// if (attr != null) - //// { - //// attr.BlurBehindRadius = 120; - - //// activity.Window.Attributes = attr; - //// } - //// activity.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent)); - //// } - //// else - //// { - //// activity.Window?.ClearFlags(WindowManagerFlags.BlurBehind); - //// activity.Window.SetBackgroundDrawable(new ColorDrawable(Color.White)); - - //// return; - //// } - //// } - - //// TransparencyLevel = transparencyLevel; - //// } - ////} + if (_view.Context is not AvaloniaMainActivity activity) + return; + + foreach (var level in transparencyLevels) + { + if (!IsSupported(level)) + { + continue; + } + + if (level == WindowTransparencyLevel.None && OperatingSystem.IsAndroidVersionAtLeast(30)) + { + activity.SetTranslucent(false); + } + else if (level == WindowTransparencyLevel.Transparent) + { + SetTransparencyTransparent(activity); + } + else if (level == WindowTransparencyLevel.Blur) + { + SetTransparencyBlur(activity); + } + + TransparencyLevel = level; + break; + } } - + public virtual object TryGetFeature(Type featureType) { if (featureType == typeof(IStorageProvider)) @@ -420,6 +364,47 @@ namespace Avalonia.Android.Platform.SkiaPlatform return null; } + + private static bool IsSupported(WindowTransparencyLevel level) + { + if (level == WindowTransparencyLevel.None) + return true; + if (level == WindowTransparencyLevel.Transparent) + return OperatingSystem.IsAndroidVersionAtLeast(30); + if (level == WindowTransparencyLevel.Blur) + return OperatingSystem.IsAndroidVersionAtLeast(31); + return false; + } + + private static void SetTransparencyTransparent(AvaloniaMainActivity activity) + { + if (OperatingSystem.IsAndroidVersionAtLeast(30)) + { + activity.SetTranslucent(true); + SetBlurBehindRadius(activity, 0); + activity.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent)); + } + } + + private static void SetTransparencyBlur(AvaloniaMainActivity activity) + { + if (OperatingSystem.IsAndroidVersionAtLeast(31)) + { + activity.SetTranslucent(true); + activity.Window?.AddFlags(WindowManagerFlags.BlurBehind); + SetBlurBehindRadius(activity, 120); + activity.Window?.SetBackgroundDrawable(new ColorDrawable(Color.Transparent)); + } + } + + private static void SetBlurBehindRadius(AvaloniaMainActivity activity, int radius) + { + if (OperatingSystem.IsAndroidVersionAtLeast(31) && activity.Window?.Attributes is { } attr) + { + attr.BlurBehindRadius = radius; + activity.Window.Attributes = attr; + } + } } internal class AvaloniaInputConnection : BaseInputConnection diff --git a/src/Avalonia.Controls/Platform/ITopLevelImpl.cs b/src/Avalonia.Controls/Platform/ITopLevelImpl.cs index 02b96d0628..5b25e0e551 100644 --- a/src/Avalonia.Controls/Platform/ITopLevelImpl.cs +++ b/src/Avalonia.Controls/Platform/ITopLevelImpl.cs @@ -117,7 +117,7 @@ namespace Avalonia.Platform /// /// Sets the hint of the TopLevel. /// - void SetTransparencyLevelHint(IReadOnlyList transparencyLevel); + void SetTransparencyLevelHint(IReadOnlyList transparencyLevels); /// /// Gets the current of the TopLevel.