-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
10,881 additions
and
6,351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
dev/DevWinUI.Controls/Controls/TransitioningContentControl/TransitionMode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace DevWinUI; | ||
public enum TransitionMode | ||
{ | ||
Right2Left, | ||
Left2Right, | ||
Bottom2Top, | ||
Top2Bottom, | ||
Right2LeftWithFade, | ||
Left2RightWithFade, | ||
Bottom2TopWithFade, | ||
Top2BottomWithFade, | ||
Fade, | ||
DiagonalSlideWithFade, | ||
ZoomInWithFade, | ||
ScaleDown, | ||
ScaleUp, | ||
Custom | ||
} |
125 changes: 125 additions & 0 deletions
125
dev/DevWinUI.Controls/Controls/TransitioningContentControl/TransitioningContentControl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
namespace DevWinUI; | ||
public partial class TransitioningContentControl : ContentControl | ||
{ | ||
private FrameworkElement _contentPresenter; | ||
private Storyboard _storyboardBuildIn; | ||
private long _visibilityToken; | ||
|
||
public TransitioningContentControl() | ||
{ | ||
Loaded += TransitioningContentControl_Loaded; | ||
Unloaded += TransitioningContentControl_Unloaded; | ||
} | ||
|
||
public static readonly DependencyProperty TransitionModeProperty = DependencyProperty.Register( | ||
nameof(TransitionMode), typeof(TransitionMode), typeof(TransitioningContentControl), | ||
new PropertyMetadata(TransitionMode.Right2Left, OnTransitionModeChanged)); | ||
|
||
private static void OnTransitionModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
var ctl = (TransitioningContentControl)d; | ||
ctl.OnTransitionModeChanged((TransitionMode)e.NewValue); | ||
} | ||
|
||
private void OnTransitionModeChanged(TransitionMode newValue) | ||
{ | ||
_storyboardBuildIn = TryGetResource<Storyboard>($"{newValue}Transition"); | ||
StartTransition(); | ||
} | ||
|
||
public TransitionMode TransitionMode | ||
{ | ||
get => (TransitionMode)GetValue(TransitionModeProperty); | ||
set => SetValue(TransitionModeProperty, value); | ||
} | ||
|
||
public static readonly DependencyProperty TransitionStoryboardProperty = DependencyProperty.Register( | ||
nameof(TransitionStoryboard), typeof(Storyboard), typeof(TransitioningContentControl), new PropertyMetadata(default(Storyboard))); | ||
|
||
public Storyboard TransitionStoryboard | ||
{ | ||
get => (Storyboard)GetValue(TransitionStoryboardProperty); | ||
set => SetValue(TransitionStoryboardProperty, value); | ||
} | ||
|
||
private void TransitioningContentControl_Loaded(object sender, RoutedEventArgs e) | ||
{ | ||
_visibilityToken = RegisterPropertyChangedCallback(VisibilityProperty, OnVisibilityChanged); | ||
} | ||
|
||
private void TransitioningContentControl_Unloaded(object sender, RoutedEventArgs e) | ||
{ | ||
UnregisterPropertyChangedCallback(VisibilityProperty, _visibilityToken); | ||
} | ||
|
||
private void OnVisibilityChanged(DependencyObject sender, DependencyProperty dp) | ||
{ | ||
if (dp == VisibilityProperty) | ||
{ | ||
StartTransition(); | ||
} | ||
} | ||
|
||
private void StartTransition() | ||
{ | ||
if (_contentPresenter == null || Visibility != Visibility.Visible) | ||
return; | ||
|
||
var storyboard = TransitionStoryboard ?? _storyboardBuildIn; | ||
|
||
if (storyboard != null) | ||
{ | ||
try | ||
{ | ||
storyboard.Stop(); // Stop any active animation before starting a new one | ||
Storyboard.SetTarget(storyboard, _contentPresenter); | ||
storyboard.Begin(); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
protected override void OnApplyTemplate() | ||
{ | ||
base.OnApplyTemplate(); | ||
|
||
_contentPresenter = VisualTreeHelper.GetChild(this, 0) as FrameworkElement; | ||
if (_contentPresenter != null) | ||
{ | ||
_contentPresenter.RenderTransformOrigin = new Windows.Foundation.Point(0.5, 0.5); | ||
_contentPresenter.RenderTransform = new TransformGroup | ||
{ | ||
Children = | ||
{ | ||
new ScaleTransform(), | ||
new SkewTransform(), | ||
new RotateTransform(), | ||
new TranslateTransform() | ||
} | ||
}; | ||
} | ||
|
||
StartTransition(); | ||
} | ||
|
||
protected override void OnContentChanged(object oldContent, object newContent) | ||
{ | ||
base.OnContentChanged(oldContent, newContent); | ||
|
||
if (newContent != null) | ||
{ | ||
StartTransition(); | ||
} | ||
} | ||
|
||
private TResource TryGetResource<TResource>(string key) where TResource : class | ||
{ | ||
if (Application.Current.Resources.TryGetValue(key, out var resource) && resource is TResource typedResource) | ||
{ | ||
return typedResource; | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.