Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions components/Media/src/Brushes/AcrylicBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if WINUI2
using CommunityToolkit.WinUI.Media.Pipelines;
#if WINUI3
using Microsoft.UI.Composition;
#endif
using Windows.UI;
#if WINUI2
using Windows.UI.Composition;
#endif

namespace CommunityToolkit.WinUI.Media;

/// <summary>
/// A <see cref="XamlCompositionBrush"/> that implements an acrylic effect with customizable parameters
/// </summary>
public sealed class AcrylicBrush : XamlCompositionEffectBrushBase
public sealed partial class AcrylicBrush : XamlCompositionEffectBrushBase
{
/// <summary>
/// The <see cref="EffectSetter{T}"/> instance in use to set the blur amount
Expand All @@ -30,6 +34,7 @@ public sealed class AcrylicBrush : XamlCompositionEffectBrushBase
/// </summary>
private EffectSetter<float>? tintOpacitySetter;

#if WINUI2
/// <summary>
/// Gets or sets the background source mode for the effect (the default is <see cref="AcrylicBackgroundSource.Backdrop"/>).
/// </summary>
Expand Down Expand Up @@ -62,11 +67,18 @@ private static void OnSourcePropertyChanged(DependencyObject d, DependencyProper
brush.OnConnected();
}
}
#endif

#if WINUI2
/// <summary>
/// Gets or sets the blur amount for the effect (must be a positive value)
/// </summary>
/// <remarks>This property is ignored when the active mode is <see cref="AcrylicBackgroundSource.HostBackdrop"/></remarks>
#else
/// <summary>
/// Gets or sets the blur amount for the effect (must be a positive value)
/// </summary>
#endif
public double BlurAmount
{
get => (double)GetValue(BlurAmountProperty);
Expand All @@ -90,7 +102,9 @@ public double BlurAmount
private static void OnBlurAmountPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is AcrylicBrush brush &&
#if WINUI2
brush.BackgroundSource != AcrylicBackgroundSource.HostBackdrop && // Blur is fixed by OS when using HostBackdrop source.
#endif
brush.CompositionBrush is CompositionBrush target)
{
brush.blurAmountSetter?.Invoke(target, (float)(double)e.NewValue);
Expand Down Expand Up @@ -197,6 +211,7 @@ private static void OnTextureUriPropertyChanged(DependencyObject d, DependencyPr
/// <inheritdoc/>
protected override PipelineBuilder OnPipelineRequested()
{
#if WINUI2
switch (BackgroundSource)
{
case AcrylicBackgroundSource.Backdrop:
Expand All @@ -217,6 +232,15 @@ protected override PipelineBuilder OnPipelineRequested()
TextureUri);
default: throw new ArgumentOutOfRangeException(nameof(BackgroundSource), $"Invalid acrylic source: {BackgroundSource}");
}
#else
return PipelineBuilder.FromBackdropAcrylic(
TintColor,
out this.tintColorSetter,
(float)TintOpacity,
out this.tintOpacitySetter,
(float)BlurAmount,
out blurAmountSetter,
TextureUri);
#endif
}
}
#endif