Skip to content

Commit

Permalink
Add TransitioningContentControl
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Dec 24, 2024
1 parent 033ff0e commit 8604e02
Show file tree
Hide file tree
Showing 9 changed files with 10,881 additions and 6,351 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Install-Package DevWinUI
## 🔥 DevWinUI.Controls 🔥
### ⚡ What’s Inside? ⚡

- ✨ TransitioningContentControl
- ✨ DateTimePicker
- ✨ CalendarWithClock
- ✨ Clock
Expand Down Expand Up @@ -254,6 +255,9 @@ Install-Package DevWinUI.ContextMenu

## 🕰️ History 🕰️

### TransitioningContentControl
![DevWinUI](https://raw.githubusercontent.com/ghost1372/DevWinUI-Resources/refs/heads/main/DevWinUI-Docs/TransitioningContentControl.gif)

### DateTimePicker
![DevWinUI](https://raw.githubusercontent.com/ghost1372/DevWinUI-Resources/refs/heads/main/DevWinUI-Docs/DateTimePicker.gif)

Expand Down
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
}
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;
}
}
Loading

0 comments on commit 8604e02

Please sign in to comment.