protected override void OnConnected() { // Delay creating composition resources until they're required. if (CompositionBrush == null) { var backdrop = Window.Current.Compositor.CreateBackdropBrush(); // Use a Win2D blur affect applied to a CompositionBackdropBrush. var graphicsEffect = new GaussianBlurEffect { Name = "Blur", BlurAmount = (float)this.BlurAmount, Source = new CompositionEffectSourceParameter("backdrop") }; var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" }); var effectBrush = effectFactory.CreateBrush(); effectBrush.SetSourceParameter("backdrop", backdrop); CompositionBrush = effectBrush; if (BlurAmountExpression != null) { CompositionBrush.StartAnimation("Blur.BlurAmount", BlurAmountExpression); } } }
void SetCompositionFocus(bool IsGotFocus) { if (CompositionBrush == null) { return; } if (BackgroundSource == AcrylicBackgroundSource.Backdrop) { return; } var tintOpacity = Convert.ToSingle(TintOpacity); if (tintOpacity < 0f) { tintOpacity = 0f; } if (tintOpacity > 1f) { tintOpacity = 1f; } HostOpacityZeroAnimation.SetScalarParameter("TintOpacity", tintOpacity); TintOpacityFillAnimation.SetScalarParameter("TintOpacity", tintOpacity); if (IsGotFocus) { CompositionBrush = Brush; TintOpacityFillAnimation.Direction = AnimationDirection.Reverse; HostOpacityZeroAnimation.Direction = AnimationDirection.Reverse; TintToFallBackAnimation.Direction = AnimationDirection.Reverse; CompositionBrush.StartAnimation("arithmetic.Source2Amount", TintOpacityFillAnimation); CompositionBrush.StartAnimation("arithmetic.Source1Amount", HostOpacityZeroAnimation); CompositionBrush.StartAnimation("tintcolor.Color", TintToFallBackAnimation); } else if (CompositionBrush == Brush) { var scopedBatch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); TintOpacityFillAnimation.Direction = AnimationDirection.Normal; HostOpacityZeroAnimation.Direction = AnimationDirection.Normal; TintToFallBackAnimation.Direction = AnimationDirection.Normal; CompositionBrush.StartAnimation("arithmetic.Source2Amount", TintOpacityFillAnimation); CompositionBrush.StartAnimation("arithmetic.Source1Amount", HostOpacityZeroAnimation); CompositionBrush.StartAnimation("tintcolor.Color", TintToFallBackAnimation); scopedBatch.Completed += (s, a) => CompositionBrush = compositor.CreateColorBrush(FallbackColor); scopedBatch.End(); } else { CompositionBrush = compositor.CreateColorBrush(FallbackColor); } }
private void UpdateBrush() { FrameworkElement root = _root; if (_window != null) { root = ElementCompositionPreview.GetAppWindowContent(_window) as FrameworkElement; } if (root == null || _settings == null || _accessibilitySettings == null || _fastEffects == null || _energySaver == null) { return; } bool useSolidColorFallback = !_settings.AdvancedEffectsEnabled || !_wallpaperBrushSupported || !_windowActivated || _fastEffects == false || _energySaver == true; Compositor compositor = Window.Current.Compositor; ElementTheme currentTheme = root.ActualTheme; Color tintColor = currentTheme == ElementTheme.Light ? Color.FromArgb(255, 243, 243, 243) : Color.FromArgb(255, 32, 32, 32); float tintOpacity = currentTheme == ElementTheme.Light ? 0.5f : 0.8f; if (_accessibilitySettings.HighContrast) { tintColor = _settings.GetColorValue(UIColorType.Background); useSolidColorFallback = true; } FallbackColor = tintColor; CompositionBrush newBrush; if (useSolidColorFallback) { newBrush = compositor.CreateColorBrush(tintColor); } else { newBrush = BuildMicaEffectBrush(compositor, tintColor, tintOpacity, 1.0f); } CompositionBrush oldBrush = CompositionBrush; if (oldBrush == null || (CompositionBrush.Comment == "Crossfade") || (oldBrush is CompositionColorBrush && newBrush is CompositionColorBrush)) { // Set new brush directly if (oldBrush != null) { oldBrush.Dispose(); } CompositionBrush = newBrush; } else { // Crossfade CompositionBrush crossFadeBrush = CreateCrossFadeEffectBrush(compositor, oldBrush, newBrush); ScalarKeyFrameAnimation animation = CreateCrossFadeAnimation(compositor); CompositionBrush = crossFadeBrush; var crossFadeAnimationBatch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation); crossFadeBrush.StartAnimation("CrossFade.CrossFade", animation); crossFadeAnimationBatch.End(); crossFadeAnimationBatch.Completed += (o, a) => { crossFadeBrush.Dispose(); oldBrush.Dispose(); CompositionBrush = newBrush; }; } }