Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add transitioning content control. #7611

Merged
merged 19 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/Avalonia.Controls/TransitioningContentControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Threading;
using Avalonia.Animation;
using Avalonia.Controls.Templates;
using Avalonia.Threading;

namespace Avalonia.Controls;

/// <summary>
/// Displays <see cref="ContentControl.Content"/> according to a <see cref="FuncDataTemplate"/>.
/// Uses <see cref="PageTransition"/> to move between the old and new content values.
/// </summary>
public class TransitioningContentControl : ContentControl
danwalmsley marked this conversation as resolved.
Show resolved Hide resolved
danwalmsley marked this conversation as resolved.
Show resolved Hide resolved
{
private CancellationTokenSource? _lastTransitionCts;
private object? _currentContent;

/// <summary>
/// Defines the <see cref="PageTransition"/> property.
/// </summary>
public static readonly StyledProperty<IPageTransition?> PageTransitionProperty =
AvaloniaProperty.Register<TransitioningContentControl, IPageTransition?>(nameof(PageTransition),
new CrossFade(TimeSpan.FromSeconds(0.125)));


danwalmsley marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Defines the <see cref="CurrentContent"/> property.
/// </summary>
internal static readonly DirectProperty<TransitioningContentControl, object?> CurrentContentProperty =
AvaloniaProperty.RegisterDirect<TransitioningContentControl, object?>(nameof(CurrentContent),
o => o.CurrentContent);

/// <summary>
/// Gets or sets the animation played when content appears and disappears.
/// </summary>
public IPageTransition? PageTransition
{
get => GetValue(PageTransitionProperty);
set => SetValue(PageTransitionProperty, value);
}

/// <summary>
/// Gets the content currently displayed on the screen.
/// </summary>
internal object? CurrentContent
{
get => _currentContent;
private set => SetAndRaise(CurrentContentProperty, ref _currentContent, value);
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);

Dispatcher.UIThread.Post(() => UpdateContentWithTransition(Content));
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);

_lastTransitionCts?.Cancel();
}

protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);

if (change.Property == ContentProperty)
{
Dispatcher.UIThread.Post(() => UpdateContentWithTransition(Content));
}
}

/// <summary>
/// Updates the content with transitions.
/// </summary>
/// <param name="content">New content to set.</param>
private async void UpdateContentWithTransition(object? content)
{
if (VisualRoot is null)
{
return;
}

_lastTransitionCts?.Cancel();
_lastTransitionCts = new CancellationTokenSource();

if (PageTransition != null)
await PageTransition.Start(this, null, true, _lastTransitionCts.Token);

CurrentContent = content;

if (PageTransition != null)
await PageTransition.Start(null, this, true, _lastTransitionCts.Token);
}
}
1 change: 1 addition & 0 deletions src/Avalonia.ReactiveUI/TransitioningContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Avalonia.ReactiveUI
/// <summary>
/// A ContentControl that animates the transition when its content is changed.
/// </summary>
[Obsolete("Use TransitioningContentControl in Avalonia.Controls namespace")]
public class TransitioningContentControl : ContentControl, IStyleable
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style Selector="TransitioningContentControl">
<!-- Set Defaults -->
<Setter Property="Template">
<ControlTemplate>
<ContentPresenter Name="PART_ContentPresenter"
danwalmsley marked this conversation as resolved.
Show resolved Hide resolved
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding DisplayedContent}"
Padding="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</ControlTemplate>
</Setter>
</Style>
</Styles>
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Default/DefaultTheme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/TabItem.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/TextBox.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/ToggleButton.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/TransitioningContentControl.xaml" />
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/Expander.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/TitleBar.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/TreeView.xaml"/>
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Fluent/Controls/FluentControls.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/TabItem.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/TextBox.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/ToggleButton.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/TransitioningContentControl.xaml" />
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/Expander.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/TitleBar.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/TreeView.xaml"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style Selector="TransitioningContentControl">
<!-- Set Defaults -->
<Setter Property="Template">
<ControlTemplate>
<ContentPresenter Name="PART_ContentPresenter"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding DisplayedContent}"
Padding="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</ControlTemplate>
</Setter>
</Style>
</Styles>