@@ -9,7 +9,7 @@ public partial class BlurEffectManager : IDisposable
99 private SpriteVisual _blurVisual ;
1010 private CompositionEffectBrush _blurBrush ;
1111 private ManagedSurface _noiseSurface ;
12-
12+ private LinearEasingFunction _linearEasing ;
1313
1414 private bool _isTintEnabled = false ;
1515 public bool IsTintEnabled
@@ -427,46 +427,91 @@ public void StartBlurAnimation(double targetBlurAmount, TimeSpan duration)
427427 {
428428 if ( _blurBrush == null ) return ;
429429
430- var blurAnimation = _compositor . CreateScalarKeyFrameAnimation ( ) ;
431- blurAnimation . Duration = duration ;
432- blurAnimation . InsertKeyFrame ( 0f , 0f ) ;
433- blurAnimation . InsertKeyFrame ( 1f , ( float ) targetBlurAmount ) ;
430+ PrepareForNewAnimation ( ) ;
431+
432+ _blurBrush . Properties . TryGetScalar ( "Blur.BlurAmount" , out float current ) ;
433+
434+ var easing = GetEasing ( ) ;
435+
436+ var blurAnim = _compositor . CreateScalarKeyFrameAnimation ( ) ;
437+ blurAnim . Duration = duration ;
438+ blurAnim . InsertKeyFrame ( 0f , current , easing ) ;
439+ blurAnim . InsertKeyFrame ( 1f , ( float ) targetBlurAmount , easing ) ;
434440
435- _blurBrush . Properties . StartAnimation ( "Blur.BlurAmount" , blurAnimation ) ;
441+ _blurBrush . Properties . StartAnimation ( "Blur.BlurAmount" , blurAnim ) ;
436442
437443 if ( IsTintEnabled )
438444 {
439- var colorAnimation = _compositor . CreateColorKeyFrameAnimation ( ) ;
440- colorAnimation . Duration = duration ;
441- colorAnimation . InsertKeyFrame ( 0f , Colors . Transparent ) ;
442- colorAnimation . InsertKeyFrame ( 1f , TintColor ) ;
443- _blurBrush . Properties . StartAnimation ( "Tint.Color" , colorAnimation ) ;
445+ Color transparent = Color . FromArgb ( 0 , TintColor . R , TintColor . G , TintColor . B ) ;
446+
447+ var tintAnim = _compositor . CreateColorKeyFrameAnimation ( ) ;
448+ tintAnim . Duration = duration ;
449+ tintAnim . InsertKeyFrame ( 0f , transparent , easing ) ;
450+ tintAnim . InsertKeyFrame ( 1f , TintColor , easing ) ;
451+
452+ _blurBrush . Properties . StartAnimation ( "Tint.Color" , tintAnim ) ;
444453 }
445454 }
446455
447456 public void StartBlurReverseAnimation ( ) => StartBlurReverseAnimation ( BlurAmount , TimeSpan . FromMilliseconds ( 300 ) ) ;
448457 public void StartBlurReverseAnimation ( TimeSpan duration ) => StartBlurReverseAnimation ( BlurAmount , duration ) ;
449458
450- public void StartBlurReverseAnimation ( double currentBlurAmount , TimeSpan duration )
459+ public void StartBlurReverseAnimation ( double fallbackBlur , TimeSpan duration )
451460 {
452461 if ( _blurBrush == null ) return ;
453462
454- var blurAnimation = _compositor . CreateScalarKeyFrameAnimation ( ) ;
455- blurAnimation . Duration = duration ;
456- blurAnimation . InsertKeyFrame ( 0f , ( float ) currentBlurAmount ) ;
457- blurAnimation . InsertKeyFrame ( 1f , 0f ) ;
458- _blurBrush . Properties . StartAnimation ( propertyName : "Blur.BlurAmount" , blurAnimation ) ;
463+ PrepareForNewAnimation ( ) ;
464+
465+ float current = ( float ) fallbackBlur ;
466+ _blurBrush . Properties . TryGetScalar ( "Blur.BlurAmount" , out current ) ;
467+
468+ var easing = GetEasing ( ) ;
469+
470+ var blurAnim = _compositor . CreateScalarKeyFrameAnimation ( ) ;
471+ blurAnim . Duration = duration ;
472+ blurAnim . InsertKeyFrame ( 0f , current , easing ) ;
473+ blurAnim . InsertKeyFrame ( 1f , 0f , easing ) ;
474+
475+ _blurBrush . Properties . StartAnimation ( "Blur.BlurAmount" , blurAnim ) ;
459476
460477 if ( IsTintEnabled )
461478 {
462- var colorAnimation = _compositor . CreateColorKeyFrameAnimation ( ) ;
463- colorAnimation . Duration = duration ;
464- colorAnimation . InsertKeyFrame ( 0f , TintColor ) ;
465- colorAnimation . InsertKeyFrame ( 1f , Colors . Transparent ) ;
466- _blurBrush . Properties . StartAnimation ( "Tint.Color" , colorAnimation ) ;
479+ _blurBrush . Properties . TryGetColor ( "Tint.Color" , out Color startColor ) ;
480+ Color transparent = Color . FromArgb ( 0 , startColor . R , startColor . G , startColor . B ) ;
481+
482+ var tintAnim = _compositor . CreateColorKeyFrameAnimation ( ) ;
483+ tintAnim . Duration = duration ;
484+ tintAnim . InsertKeyFrame ( 0f , startColor , easing ) ;
485+ tintAnim . InsertKeyFrame ( 1f , transparent , easing ) ;
486+
487+ _blurBrush . Properties . StartAnimation ( "Tint.Color" , tintAnim ) ;
467488 }
468489 }
469490
491+ private void PrepareForNewAnimation ( )
492+ {
493+ if ( _blurBrush == null ) return ;
494+
495+ _blurBrush . Properties . StopAnimation ( "Blur.BlurAmount" ) ;
496+ if ( IsTintEnabled )
497+ _blurBrush . Properties . StopAnimation ( "Tint.Color" ) ;
498+
499+ // Force compositor to apply the final animated value before starting new one
500+ _blurBrush . Properties . InsertScalar ( "Blur.BlurAmount" ,
501+ _blurBrush . Properties . TryGetScalar ( "Blur.BlurAmount" , out float v1 ) == CompositionGetValueStatus . Succeeded ? v1 : 0f ) ;
502+
503+ if ( IsTintEnabled )
504+ {
505+ if ( _blurBrush . Properties . TryGetColor ( "Tint.Color" , out Color c ) == CompositionGetValueStatus . Succeeded )
506+ _blurBrush . Properties . InsertColor ( "Tint.Color" , c ) ;
507+ else
508+ _blurBrush . Properties . InsertColor ( "Tint.Color" , TintColor ) ;
509+ }
510+ }
511+ private LinearEasingFunction GetEasing ( )
512+ {
513+ return _linearEasing ??= _compositor . CreateLinearEasingFunction ( ) ;
514+ }
470515 public void StopBlurAnimation ( )
471516 {
472517 if ( _blurBrush == null ) return ;
0 commit comments