@@ -108,6 +108,15 @@ public class MetroWindow : Window
108
108
public static readonly DependencyProperty OverlayBrushProperty = DependencyProperty . Register ( "OverlayBrush" , typeof ( Brush ) , typeof ( MetroWindow ) , new PropertyMetadata ( new SolidColorBrush ( Color . FromScRgb ( 255 , 0 , 0 , 0 ) ) ) ) ; // BlackColorBrush
109
109
public static readonly DependencyProperty OverlayOpacityProperty = DependencyProperty . Register ( "OverlayOpacity" , typeof ( double ) , typeof ( MetroWindow ) , new PropertyMetadata ( 0.7d ) ) ;
110
110
111
+ /// <summary>
112
+ /// Identifies the <see cref="OverlayFadeIn"/> dependency property.
113
+ /// </summary>
114
+ public static readonly DependencyProperty OverlayFadeInProperty = DependencyProperty . Register ( "OverlayFadeIn" , typeof ( Storyboard ) , typeof ( MetroWindow ) , new PropertyMetadata ( default ( Storyboard ) ) ) ;
115
+ /// <summary>
116
+ /// Identifies the <see cref="OverlayFadeOut"/> dependency property.
117
+ /// </summary>
118
+ public static readonly DependencyProperty OverlayFadeOutProperty = DependencyProperty . Register ( "OverlayFadeOut" , typeof ( Storyboard ) , typeof ( MetroWindow ) , new PropertyMetadata ( default ( Storyboard ) ) ) ;
119
+
111
120
public static readonly DependencyProperty IconTemplateProperty = DependencyProperty . Register ( "IconTemplate" , typeof ( DataTemplate ) , typeof ( MetroWindow ) , new PropertyMetadata ( null ) ) ;
112
121
public static readonly DependencyProperty TitleTemplateProperty = DependencyProperty . Register ( "TitleTemplate" , typeof ( DataTemplate ) , typeof ( MetroWindow ) , new PropertyMetadata ( null ) ) ;
113
122
@@ -756,12 +765,54 @@ public double OverlayOpacity
756
765
set { SetValue ( OverlayOpacityProperty , value ) ; }
757
766
}
758
767
768
+ /// <summary>
769
+ /// Gets or sets the overlay fade in storyboard.
770
+ /// </summary>
771
+ public Storyboard OverlayFadeIn
772
+ {
773
+ get { return ( Storyboard ) GetValue ( OverlayFadeInProperty ) ; }
774
+ set { SetValue ( OverlayFadeInProperty , value ) ; }
775
+ }
776
+
777
+ /// <summary>
778
+ /// Gets or sets the overlay fade out storyboard.
779
+ /// </summary>
780
+ public Storyboard OverlayFadeOut
781
+ {
782
+ get { return ( Storyboard ) GetValue ( OverlayFadeOutProperty ) ; }
783
+ set { SetValue ( OverlayFadeOutProperty , value ) ; }
784
+ }
785
+
759
786
[ Obsolete ( "This property will be deleted in the next release." ) ]
760
787
public string WindowTitle
761
788
{
762
789
get { return TitleCaps ? Title . ToUpper ( ) : Title ; }
763
790
}
764
791
792
+ private bool CanUseOverlayFadingStoryboard ( Storyboard sb , out DoubleAnimation animation )
793
+ {
794
+ animation = null ;
795
+ if ( null == sb )
796
+ {
797
+ return false ;
798
+ }
799
+
800
+ sb . Dispatcher . VerifyAccess ( ) ;
801
+
802
+ animation = sb . Children . OfType < DoubleAnimation > ( ) . FirstOrDefault ( ) ;
803
+ if ( null == animation )
804
+ {
805
+ return false ;
806
+ }
807
+
808
+ return ( sb . Duration . HasTimeSpan && sb . Duration . TimeSpan . Ticks > 0 )
809
+ || ( sb . AccelerationRatio > 0 )
810
+ || ( sb . DecelerationRatio > 0 )
811
+ || ( animation . Duration . HasTimeSpan && animation . Duration . TimeSpan . Ticks > 0 )
812
+ || animation . AccelerationRatio > 0
813
+ || animation . DecelerationRatio > 0 ;
814
+ }
815
+
765
816
/// <summary>
766
817
/// Begins to show the MetroWindow's overlay effect.
767
818
/// </summary>
@@ -781,32 +832,39 @@ public System.Threading.Tasks.Task ShowOverlayAsync()
781
832
782
833
Dispatcher . VerifyAccess ( ) ;
783
834
784
- overlayBox . Visibility = Visibility . Visible ;
785
-
786
- var sb = ( ( Storyboard ) this . Template . Resources [ "OverlayFastSemiFadeIn" ] ) . Clone ( ) ;
787
- ( ( DoubleAnimation ) sb . Children [ 0 ] ) . To = this . OverlayOpacity ;
788
-
789
- EventHandler completionHandler = null ;
790
- completionHandler = ( sender , args ) =>
835
+ var sb = OverlayFadeIn ? . Clone ( ) ;
836
+ overlayStoryboard = sb ;
837
+ DoubleAnimation animation ;
838
+ if ( CanUseOverlayFadingStoryboard ( sb , out animation ) )
791
839
{
792
- sb . Completed -= completionHandler ;
840
+ this . overlayBox . SetCurrentValue ( VisibilityProperty , Visibility . Visible ) ;
793
841
794
- if ( overlayStoryboard == sb )
795
- {
796
- overlayStoryboard = null ;
797
- }
842
+ animation . To = this . OverlayOpacity ;
798
843
799
- tcs . TrySetResult ( null ) ;
800
- } ;
801
-
802
- sb . Completed += completionHandler ;
844
+ EventHandler completionHandler = null ;
845
+ completionHandler = ( sender , args ) =>
846
+ {
847
+ sb . Completed -= completionHandler ;
848
+ if ( overlayStoryboard == sb )
849
+ {
850
+ overlayStoryboard = null ;
851
+ }
803
852
804
- overlayBox . BeginStoryboard ( sb ) ;
853
+ tcs . TrySetResult ( null ) ;
854
+ } ;
805
855
806
- overlayStoryboard = sb ;
856
+ sb . Completed += completionHandler ;
857
+ overlayBox . BeginStoryboard ( sb ) ;
858
+ }
859
+ else
860
+ {
861
+ ShowOverlay ( ) ;
862
+ tcs . TrySetResult ( null ) ;
863
+ }
807
864
808
865
return tcs . Task ;
809
866
}
867
+
810
868
/// <summary>
811
869
/// Begins to hide the MetroWindow's overlay effect.
812
870
/// </summary>
@@ -817,55 +875,65 @@ public System.Threading.Tasks.Task HideOverlayAsync()
817
875
818
876
var tcs = new System . Threading . Tasks . TaskCompletionSource < object > ( ) ;
819
877
820
- if ( overlayBox . Visibility == Visibility . Visible && overlayBox . Opacity = = 0.0 )
878
+ if ( overlayBox . Visibility == Visibility . Visible && overlayBox . Opacity < = 0.0 )
821
879
{
822
880
//No Task.FromResult in .NET 4.
881
+ this . overlayBox . SetCurrentValue ( VisibilityProperty , Visibility . Hidden ) ;
823
882
tcs . SetResult ( null ) ;
824
883
return tcs . Task ;
825
884
}
826
885
827
886
Dispatcher . VerifyAccess ( ) ;
828
887
829
- var sb = ( ( Storyboard ) this . Template . Resources [ "OverlayFastSemiFadeOut" ] ) . Clone ( ) ;
830
- ( ( DoubleAnimation ) sb . Children [ 0 ] ) . To = 0d ;
831
-
832
- EventHandler completionHandler = null ;
833
- completionHandler = ( sender , args ) =>
888
+ var sb = OverlayFadeOut ? . Clone ( ) ;
889
+ overlayStoryboard = sb ;
890
+ DoubleAnimation animation ;
891
+ if ( CanUseOverlayFadingStoryboard ( sb , out animation ) )
834
892
{
835
- sb . Completed -= completionHandler ;
836
-
837
- if ( overlayStoryboard == sb )
838
- {
839
- overlayBox . Visibility = Visibility . Hidden ;
840
- overlayStoryboard = null ;
841
- }
893
+ animation . To = 0d ;
842
894
843
- tcs . TrySetResult ( null ) ;
844
- } ;
845
-
846
- sb . Completed += completionHandler ;
895
+ EventHandler completionHandler = null ;
896
+ completionHandler = ( sender , args ) =>
897
+ {
898
+ sb . Completed -= completionHandler ;
899
+ if ( overlayStoryboard == sb )
900
+ {
901
+ this . overlayBox . SetCurrentValue ( VisibilityProperty , Visibility . Hidden ) ;
902
+ overlayStoryboard = null ;
903
+ }
847
904
848
- overlayBox . BeginStoryboard ( sb ) ;
905
+ tcs . TrySetResult ( null ) ;
906
+ } ;
849
907
850
- overlayStoryboard = sb ;
908
+ sb . Completed += completionHandler ;
909
+ overlayBox . BeginStoryboard ( sb ) ;
910
+ }
911
+ else
912
+ {
913
+ HideOverlay ( ) ;
914
+ tcs . TrySetResult ( null ) ;
915
+ }
851
916
852
917
return tcs . Task ;
853
918
}
919
+
854
920
public bool IsOverlayVisible ( )
855
921
{
856
922
if ( overlayBox == null ) throw new InvalidOperationException ( "OverlayBox can not be founded in this MetroWindow's template. Are you calling this before the window has loaded?" ) ;
857
923
858
924
return overlayBox . Visibility == Visibility . Visible && overlayBox . Opacity >= this . OverlayOpacity ;
859
925
}
926
+
860
927
public void ShowOverlay ( )
861
928
{
862
- overlayBox . Visibility = Visibility . Visible ;
929
+ this . overlayBox . SetCurrentValue ( VisibilityProperty , Visibility . Visible ) ;
863
930
overlayBox . SetCurrentValue ( Grid . OpacityProperty , this . OverlayOpacity ) ;
864
931
}
932
+
865
933
public void HideOverlay ( )
866
934
{
867
- overlayBox . SetCurrentValue ( Grid . OpacityProperty , 0.0 ) ;
868
- overlayBox . Visibility = System . Windows . Visibility . Hidden ;
935
+ overlayBox . SetCurrentValue ( Grid . OpacityProperty , 0d ) ;
936
+ this . overlayBox . SetCurrentValue ( VisibilityProperty , Visibility . Hidden ) ;
869
937
}
870
938
871
939
/// <summary>
0 commit comments