|
|
|
@ -1,3 +1,4 @@ |
|
|
|
using System; |
|
|
|
using System.Numerics; |
|
|
|
using MicroCom.Runtime; |
|
|
|
|
|
|
|
@ -5,16 +6,72 @@ namespace Avalonia.Win32.WinRT.Composition; |
|
|
|
|
|
|
|
internal static class WinUiCompositionUtils |
|
|
|
{ |
|
|
|
public static ICompositionBrush? CreateMicaBackdropBrush(ICompositor compositor) |
|
|
|
public static ICompositionBrush? CreateMicaBackdropBrush(ICompositor compositor, float color, float opacity) |
|
|
|
{ |
|
|
|
if (Win32Platform.WindowsVersion.Build < 22000) |
|
|
|
return null; |
|
|
|
|
|
|
|
using var backDropParameterFactory = |
|
|
|
NativeWinRTMethods.CreateActivationFactory<ICompositionEffectSourceParameterFactory>( |
|
|
|
"Windows.UI.Composition.CompositionEffectSourceParameter"); |
|
|
|
|
|
|
|
|
|
|
|
var tint = new[] { color / 255f, color / 255f, color / 255f, 255f / 255f }; |
|
|
|
|
|
|
|
using var tintColorEffect = new ColorSourceEffect(tint); |
|
|
|
|
|
|
|
|
|
|
|
using var tintOpacityEffect = new OpacityEffect(1.0f, tintColorEffect); |
|
|
|
using var tintOpacityEffectFactory = compositor.CreateEffectFactory(tintOpacityEffect); |
|
|
|
using var tintOpacityEffectBrushEffect = tintOpacityEffectFactory.CreateBrush(); |
|
|
|
using var tintOpacityEffectBrush = tintOpacityEffectBrushEffect.QueryInterface<ICompositionBrush>(); |
|
|
|
|
|
|
|
using var luminosityColorEffect = new ColorSourceEffect(tint); |
|
|
|
|
|
|
|
using var luminosityOpacityEffect = new OpacityEffect(opacity, luminosityColorEffect); |
|
|
|
using var luminosityOpacityEffectFactory = compositor.CreateEffectFactory(luminosityOpacityEffect); |
|
|
|
using var luminosityOpacityEffectBrushEffect = luminosityOpacityEffectFactory.CreateBrush(); |
|
|
|
using var luminosityOpacityEffectBrush = |
|
|
|
luminosityOpacityEffectBrushEffect.QueryInterface<ICompositionBrush>(); |
|
|
|
|
|
|
|
using var compositorWithBlurredWallpaperBackdropBrush = |
|
|
|
compositor.QueryInterface<ICompositorWithBlurredWallpaperBackdropBrush>(); |
|
|
|
using var blurredWallpaperBackdropBrush = |
|
|
|
compositorWithBlurredWallpaperBackdropBrush?.TryCreateBlurredWallpaperBackdropBrush(); |
|
|
|
return blurredWallpaperBackdropBrush?.QueryInterface<ICompositionBrush>(); |
|
|
|
using var micaBackdropBrush = blurredWallpaperBackdropBrush?.QueryInterface<ICompositionBrush>(); |
|
|
|
|
|
|
|
|
|
|
|
using var backgroundParameterAsSource = |
|
|
|
GetParameterSource("Background", backDropParameterFactory, out var backgroundHandle); |
|
|
|
using var foregroundParameterAsSource = |
|
|
|
GetParameterSource("Foreground", backDropParameterFactory, out var foregroundHandle); |
|
|
|
|
|
|
|
using var luminosityBlendEffect = |
|
|
|
new BlendEffect(23, backgroundParameterAsSource, foregroundParameterAsSource); |
|
|
|
using var luminosityBlendEffectFactory = compositor.CreateEffectFactory(luminosityBlendEffect); |
|
|
|
using var luminosityBlendEffectBrush = luminosityBlendEffectFactory.CreateBrush(); |
|
|
|
using var luminosityBlendEffectBrush1 = luminosityBlendEffectBrush.QueryInterface<ICompositionBrush>(); |
|
|
|
luminosityBlendEffectBrush.SetSourceParameter(backgroundHandle, micaBackdropBrush); |
|
|
|
luminosityBlendEffectBrush.SetSourceParameter(foregroundHandle, luminosityOpacityEffectBrush); |
|
|
|
|
|
|
|
|
|
|
|
using var backgroundParameterAsSource1 = |
|
|
|
GetParameterSource("Background", backDropParameterFactory, out var backgroundHandle1); |
|
|
|
using var foregroundParameterAsSource1 = |
|
|
|
GetParameterSource("Foreground", backDropParameterFactory, out var foregroundHandle1); |
|
|
|
|
|
|
|
using var colorBlendEffect = |
|
|
|
new BlendEffect(22, backgroundParameterAsSource1, foregroundParameterAsSource1); |
|
|
|
using var colorBlendEffectFactory = compositor.CreateEffectFactory(colorBlendEffect); |
|
|
|
using var colorBlendEffectBrush = colorBlendEffectFactory.CreateBrush(); |
|
|
|
colorBlendEffectBrush.SetSourceParameter(backgroundHandle1, luminosityBlendEffectBrush1); |
|
|
|
colorBlendEffectBrush.SetSourceParameter(foregroundHandle1, tintOpacityEffectBrush); |
|
|
|
|
|
|
|
|
|
|
|
// colorBlendEffectBrush.SetSourceParameter(backgroundHandle, micaBackdropBrush);
|
|
|
|
|
|
|
|
using var micaBackdropBrush1 = colorBlendEffectBrush.QueryInterface<ICompositionBrush>(); |
|
|
|
return micaBackdropBrush1.CloneReference(); |
|
|
|
} |
|
|
|
|
|
|
|
public static ICompositionBrush CreateAcrylicBlurBackdropBrush(ICompositor compositor) |
|
|
|
@ -97,4 +154,15 @@ internal static class WinUiCompositionUtils |
|
|
|
brush?.Dispose(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static IGraphicsEffectSource GetParameterSource(string name, |
|
|
|
ICompositionEffectSourceParameterFactory backDropParameterFactory, out IntPtr handle) |
|
|
|
{ |
|
|
|
var backdropString = new HStringInterop(name); |
|
|
|
var backDropParameter = |
|
|
|
backDropParameterFactory.Create(backdropString.Handle); |
|
|
|
var backDropParameterAsSource = backDropParameter.QueryInterface<IGraphicsEffectSource>(); |
|
|
|
handle = backdropString.Handle; |
|
|
|
return backDropParameterAsSource; |
|
|
|
} |
|
|
|
} |
|
|
|
|