Skip to content

Commit

Permalink
尝试优化一些动画的性能
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Jul 29, 2024
1 parent 6c1797f commit 637f7f7
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 144 deletions.
2 changes: 1 addition & 1 deletion WonderLab/Services/Game/LaunchService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace WonderLab.Services.Game;

public sealed class LaunchService {
}
}
9 changes: 1 addition & 8 deletions WonderLab/ViewModels/Windows/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public sealed partial class MainWindowViewModel : ViewModelBase {
[ObservableProperty] private bool _isOpenTaskListPanel;
[ObservableProperty] private bool _isOpenBackgroundPanel;

[ObservableProperty] private NavigationPageData _activePanelPage;
[ObservableProperty] private ReadOnlyObservableCollection<ITaskJob> _tasks;
[ObservableProperty] private ReadOnlyObservableCollection<INotification> _notifications;

Expand All @@ -51,14 +50,8 @@ public MainWindowViewModel(
_settingService = settingService;
_navigationService = navigationService;
_notificationService = notificationService;

_navigationService.NavigationRequest += p => {
if (p.PageKey is "HomePage") {
ActivePage = p.Page;
} else {
ActivePanelPage = null;
ActivePanelPage = p;
}
ActivePage = p.Page;
};

WeakReferenceMessenger.Default.Register<BlurEnableMessage>(this, BlurEnableValueHandle);
Expand Down
2 changes: 1 addition & 1 deletion WonderLab/ViewModels/Windows/OobeWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed partial class OobeWindowViewModel : ViewModelBase {

public OobeWindowViewModel(OobeNavigationService navigationService) {
IsTitleBarVisible = EnvironmentUtil.IsWindow;
Task.Run(async () => {
RunBackgroundWork(async () => {
await Task.Delay(800);
IsOpenBackgroundPanel = true;
});
Expand Down
88 changes: 30 additions & 58 deletions WonderLab/Views/Controls/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,22 @@
using Avalonia.Animation.Easings;
using Avalonia.Styling;
using Avalonia.Media.Transformation;
using Avalonia.Controls.Presenters;

namespace WonderLab.Views.Controls;

[PseudoClasses(":ispanelopen", ":ispanelclose")]
public sealed class NavigationView : SelectingItemsControl {
public sealed class NavigationViewTemplateSettings : AvaloniaObject {
private double _actualPx;
private ContentPresenter _PART_ContentPresenter;
private Border _PART_Border;

public static readonly DirectProperty<NavigationViewTemplateSettings, double> ActualPxProperty =
AvaloniaProperty.RegisterDirect<NavigationViewTemplateSettings, double>(nameof(ActualPx), p => p.ActualPx,
(p, o) => p.ActualPx = o);
private bool _isRunPanelAnimation;

public double ActualPx {
get => _actualPx;
set => SetAndRaise(ActualPxProperty, ref _actualPx,
value);
}
}

private Frame PART_Frame;
private Frame PART_PanelFrame;
private LayoutTransformControl PART_LayoutTransformControl;

private Border _backgroundPanel;
private bool _oldIsOpenBackgroundPanel;

public NavigationViewTemplateSettings TemplateSettings { get; } = new();
private event EventHandler AnimationCompleted;

public static readonly StyledProperty<object> ContentProperty =
AvaloniaProperty.Register<NavigationView, object>(nameof(Content));

public static readonly StyledProperty<object> PanelContentProperty =
AvaloniaProperty.Register<NavigationView, object>(nameof(PanelContent));

public static readonly StyledProperty<object> FooterContentProperty =
AvaloniaProperty.Register<NavigationView, object>(nameof(FooterContent));

Expand All @@ -58,11 +40,6 @@ public object Content {
set => SetValue(ContentProperty, value);
}

public object PanelContent {
get => GetValue(PanelContentProperty);
set => SetValue(PanelContentProperty, value);
}

public object FooterContent {
get => GetValue(FooterContentProperty);
set => SetValue(FooterContentProperty, value);
Expand All @@ -73,51 +50,46 @@ public bool IsOpenBackgroundPanel {
set => SetValue(IsOpenBackgroundPanelProperty, value);
}

private DispatcherOperation RunAnimation() {
var px = IsOpenBackgroundPanel ? 0 : _backgroundPanel.Bounds.Height + 15;

Dispatcher.UIThread.VerifyAccess();
return Dispatcher.UIThread.InvokeAsync(() => {
PART_LayoutTransformControl.Opacity = IsOpenBackgroundPanel ? 1 : 0;
PART_LayoutTransformControl.RenderTransform = TransformOperations.Parse($"translateY({px}px)");
});

private void OnAnimationCompleted(object sender, EventArgs e) {
_isRunPanelAnimation = true;
Dispatcher.UIThread.Post(() => _PART_ContentPresenter.Content = Content, DispatcherPriority.ApplicationIdle);
}

protected override async void OnApplyTemplate(TemplateAppliedEventArgs e) {
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) {
base.OnApplyTemplate(e);

//Layouts
PART_Frame = e.NameScope.Find<Frame>("PART_Frame");
PART_PanelFrame = e.NameScope.Find<Frame>("PART_PanelFrame");
PART_LayoutTransformControl = e.NameScope.Find<LayoutTransformControl>("PART_LayoutTransformControl");
_backgroundPanel = e.NameScope.Find<Border>("BackgroundPanel");
_PART_ContentPresenter = e.NameScope.Find<ContentPresenter>("PART_ContentPresenter");
_PART_Border = e.NameScope.Find<Border>("PART_Border");

await Dispatcher.UIThread.InvokeAsync(() => {
PART_LayoutTransformControl.Opacity = 0;
PART_LayoutTransformControl.RenderTransform = TransformOperations.Parse($"translateY({_backgroundPanel.Bounds.Height + 15}px)");
});
AnimationCompleted += OnAnimationCompleted;
}

protected override async void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) {
base.OnPropertyChanged(change);

if (change.Property == IsOpenBackgroundPanelProperty) {
await RunAnimation();
var @bool = change.GetNewValue<bool>();

await Task.Delay(TimeSpan.Parse("0:0:0.3"));
await Dispatcher.UIThread.InvokeAsync(() => PART_PanelFrame.Content = PanelContent, DispatcherPriority.ApplicationIdle);
}
Dispatcher.UIThread.Post(() => {
_PART_Border.Opacity = @bool ? 1d : 0d;
_PART_Border.RenderTransform = TransformOperations.Parse($"translateY({(@bool ? 0 : 15)}px)");
}, DispatcherPriority.Send);

await Dispatcher.UIThread.InvokeAsync(() => {
_PART_ContentPresenter.Content = null;
}, DispatcherPriority.ApplicationIdle);

if (change.Property == PanelContentProperty) {
var dispatcherOperation = RunAnimation();
await Task.Delay(TimeSpan.Parse("0:0:0.38"));
AnimationCompleted?.Invoke(this, EventArgs.Empty);
}

dispatcherOperation.Completed += async (_, _) => {
await Task.Delay(TimeSpan.Parse("0:0:0.3"));
await Dispatcher.UIThread.InvokeAsync(() => PART_PanelFrame.Content = PanelContent, DispatcherPriority.ApplicationIdle);
};
if (change.Property == ContentProperty) {
if (_isRunPanelAnimation) {
_isRunPanelAnimation = false;
return;
}

await dispatcherOperation;
Dispatcher.UIThread.Post(() => _PART_ContentPresenter.Content = change.NewValue, DispatcherPriority.ApplicationIdle);
}
}
}
Expand Down
131 changes: 63 additions & 68 deletions WonderLab/Views/Controls/Themes/Controls/NavigationViewTheme.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,72 +25,67 @@

<Setter Property="Template">
<ControlTemplate>
<Grid Background="Transparent"
RowDefinitions="1*, Auto">
<controls:Frame Grid.Row="0"
Name="PART_Frame"
Margin="0 0 12 15"
ClipToBounds="False"
Background="Transparent"
Content="{TemplateBinding Content}"/>

<!-- ContentPanel -->
<LayoutTransformControl UseRenderTransform="True"
Name="PART_LayoutTransformControl">
<LayoutTransformControl.Transitions>
<Transitions>
<DoubleTransition Property="Opacity"
Duration="0:0:0.35"
Easing="ExponentialEaseInOut"/>

<TransformOperationsTransition Duration="0:0:0.35"
Property="RenderTransform"
Easing="ExponentialEaseInOut"/>
</Transitions>
</LayoutTransformControl.Transitions>
<Border Grid.Row="0"
CornerRadius="12"
Margin="14 5 14 15"
BorderThickness="1"
Name="BackgroundPanel"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}">
<controls:Frame ClipToBounds="True"
Name="PART_PanelFrame"
Background="Transparent"/>
</Border>
</LayoutTransformControl>

<Border Opacity="1"
Grid.Row="2"
BorderThickness="1"
VerticalAlignment="Bottom"
HorizontalAlignment="Stretch"
MinHeight="{TemplateBinding MinHeight}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{DynamicResource BottomBarBackground}">
<Grid ColumnDefinitions="Auto, 1*, Auto">
<ScrollViewer Margin="16 0 -10 0"
Name="PART_ScrollViewer"
HorizontalAlignment="Left"
AllowAutoHide="{TemplateBinding (ScrollViewer.AllowAutoHide)}"
VerticalSnapPointsType="{TemplateBinding (ScrollViewer.VerticalSnapPointsType)}"
IsScrollChainingEnabled="{TemplateBinding (ScrollViewer.IsScrollChainingEnabled)}"
HorizontalSnapPointsType="{TemplateBinding (ScrollViewer.HorizontalSnapPointsType)}"
BringIntoViewOnFocusChange="{TemplateBinding (ScrollViewer.BringIntoViewOnFocusChange)}"
VerticalScrollBarVisibility="{TemplateBinding (ScrollViewer.VerticalScrollBarVisibility)}"
HorizontalScrollBarVisibility="{TemplateBinding (ScrollViewer.HorizontalScrollBarVisibility)}">
<ItemsPresenter Name="PART_ItemsPresenter"
Margin="{TemplateBinding Padding}"
ItemsPanel="{TemplateBinding ItemsPanel}" />
</ScrollViewer>

<ContentPresenter Grid.Column="2"
Margin="0 0 10 0"
Content="{TemplateBinding FooterContent}"/>
</Grid>
</Border>
</Grid>
<Grid Background="Transparent"
RowDefinitions="1*, Auto">
<Border Opacity="0"
Grid.Row="0"
CornerRadius="12"
Name="PART_Border"
Margin="14 5 14 15"
BorderThickness="1"
RenderTransform="translateY(15px)"
Effect="{StaticResource UnderlineShadow}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}">
<Border.Transitions>
<Transitions>
<DoubleTransition Property="Opacity"
Duration="0:0:0.35"
Easing="ExponentialEaseInOut"/>

<TransformOperationsTransition Duration="0:0:0.35"
Property="RenderTransform"
Easing="ExponentialEaseInOut"/>
</Transitions>
</Border.Transitions>
</Border>

<ContentPresenter Grid.Row="0"
Margin="14 5 14 15"
Background="Transparent"
Name="PART_ContentPresenter"/>

<!--navbar-->
<Border Opacity="1"
Grid.Row="2"
BorderThickness="1"
VerticalAlignment="Bottom"
HorizontalAlignment="Stretch"
MinHeight="{TemplateBinding MinHeight}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{DynamicResource BottomBarBackground}">
<Grid ColumnDefinitions="Auto, 1*, Auto">
<ScrollViewer Margin="16 0 0 0"
Name="PART_ScrollViewer"
HorizontalAlignment="Left"
AllowAutoHide="{TemplateBinding (ScrollViewer.AllowAutoHide)}"
VerticalSnapPointsType="{TemplateBinding (ScrollViewer.VerticalSnapPointsType)}"
IsScrollChainingEnabled="{TemplateBinding (ScrollViewer.IsScrollChainingEnabled)}"
HorizontalSnapPointsType="{TemplateBinding (ScrollViewer.HorizontalSnapPointsType)}"
BringIntoViewOnFocusChange="{TemplateBinding (ScrollViewer.BringIntoViewOnFocusChange)}"
VerticalScrollBarVisibility="{TemplateBinding (ScrollViewer.VerticalScrollBarVisibility)}"
HorizontalScrollBarVisibility="{TemplateBinding (ScrollViewer.HorizontalScrollBarVisibility)}">
<ItemsPresenter Name="PART_ItemsPresenter"
Margin="{TemplateBinding Padding}"
ItemsPanel="{TemplateBinding ItemsPanel}" />
</ScrollViewer>

<ContentPresenter Grid.Column="2"
Margin="0 0 10 0"
Content="{TemplateBinding FooterContent}"/>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter>

Expand All @@ -110,7 +105,7 @@
</Style.Animations>
</Style>-->

<Style Selector="^ /template/ controls|Frame#PART_PanelFrame">
<!--<Style Selector="^ /template/ controls|Frame#PART_PanelFrame">
<Style.Animations>
<Animation Duration="0:0:0.35" FillMode="Forward">
<KeyFrame Cue="0%">
Expand All @@ -121,7 +116,7 @@
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
</Style>-->

<!--IsPanelOpen-->
<!--<Style Selector="^:ispanelopen /template/ LayoutTransformControl#PART_LayoutTransformControl">
Expand Down
3 changes: 1 addition & 2 deletions WonderLab/Views/Pages/HomePage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
x:DataType="vm:HomePageViewModel"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="WonderLab.Views.Pages.HomePage">
<Grid Margin="16 0 0 0"
VerticalAlignment="Bottom"
<Grid VerticalAlignment="Bottom"
HorizontalAlignment="Left"
RowDefinitions="1*, 10, Auto">
<ui:GameManagerPanel IsGameEmpty="{Binding IsGameEmpty}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</Border>

<controls:Frame Grid.Row="1"
Margin="36 0"
Margin="36 0" IsVisible="False"
Content="{Binding ActivePage.Page}"/>
</Grid>
</UserControl>
7 changes: 3 additions & 4 deletions WonderLab/Views/Windows/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
<ui:NavigationView Grid.Row="1"
ClipToBounds="False"
Content="{Binding ActivePage}"
PanelContent="{Binding ActivePanelPage.Page}"
IsOpenBackgroundPanel="{Binding IsOpenBackgroundPanel}">
<ui:NavigationView.Items>
<ui:NavigationViewItem Icon="&#xEA8A;"
Expand All @@ -62,21 +61,21 @@
Foreground="{DynamicResource PrimaryBaseColor}"/>

<ui:NavigationViewItem Icon="&#xEDDC;"
Content="{DynamicResource Main_Download}"
Command="{Binding NavigationToCommand}"
Content="{DynamicResource Main_Download}"
CommandParameter="DownloadNavigationPage"
Foreground="{DynamicResource SuccessBaseColor}"/>

<ui:NavigationViewItem Icon="&#xF0B9;"
Content="{DynamicResource Main_Multiplayer}"
CommandParameter="MultiplayerPage"
Command="{Binding NavigationToCommand}"
Content="{DynamicResource Main_Multiplayer}"
Foreground="{DynamicResource WarningBaseColor}"/>

<ui:NavigationViewItem Icon="&#xF8B0;"
Content="{DynamicResource Main_Settings}"
Command="{Binding NavigationToCommand}"
CommandParameter="SettingNavigationPage"
Content="{DynamicResource Main_Settings}"
Foreground="{DynamicResource SecondaryColor600}"/>
</ui:NavigationView.Items>

Expand Down
2 changes: 1 addition & 1 deletion WonderLab/Views/Windows/OobeWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ui:NavigationView Grid.Row="1"
ClipToBounds="True"
PanelContent="{Binding ActivePage}"
Content="{Binding ActivePage}"
SelectedIndex="{Binding CurrentPageId}"
IsOpenBackgroundPanel="{Binding IsOpenBackgroundPanel}">
<ui:NavigationView.Items>
Expand Down

0 comments on commit 637f7f7

Please sign in to comment.