Skip to content

Commit

Permalink
sorry for that
Browse files Browse the repository at this point in the history
  • Loading branch information
kikipoulet committed May 31, 2024
1 parent 50f5cd2 commit ed21ac3
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 12 deletions.
29 changes: 18 additions & 11 deletions SukiUI/Controls/GlassMorphism/GlassCard.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
<Setter Property="Padding" Value="20" />
<Setter Property="Template">
<ControlTemplate>

<Panel Name="RootPanel" Opacity="0">
<Border Name="PART_BorderCard"
<Border Name="PART_BorderCard" RenderTransformOrigin="50%,50%"
Background="{DynamicResource SukiGlassCardBackground}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
ClipToBounds="{TemplateBinding ClipToBounds}"
CornerRadius="{TemplateBinding CornerRadius}"
Opacity="{DynamicResource GlassOpacity}">

<Border.Transitions>
<Transitions>
<DoubleTransition Property="Width"
Duration="0:0:0.5"
Easing="QuadraticEaseOut"/>
<DoubleTransition Property="Height"
Duration="0:0:0.5"
Easing="QuadraticEaseOut"/>
</Transitions>
</Border.Transitions>

<Border.Transitions>
<Transitions>
<BrushTransition Property="Background" Duration="0:0:0.15" />
Expand Down Expand Up @@ -65,16 +78,10 @@
</Style>

<Style Selector="^[IsVisible=True] /template/ Panel#RootPanel">
<Style.Animations>
<Animation FillMode="Forward" Duration="0:0:0.3">
<KeyFrame Cue="0%">
<Setter Property="Opacity" Value="0.0"/>
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="Opacity" Value="1.0"/>
</KeyFrame>
</Animation>
</Style.Animations>
<Setter Property="Opacity" Value="1" />
</Style>
<Style Selector="^[IsVisible=False] /template/ Panel#RootPanel">
<Setter Property="Opacity" Value="0" />
</Style>
</ControlTheme>
<ControlTheme x:Key="{x:Type suki:GlassCard}"
Expand Down
98 changes: 97 additions & 1 deletion SukiUI/Controls/GlassMorphism/GlassCard.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Rendering.Composition;
using Avalonia.Rendering.Composition.Animations;

namespace SukiUI.Controls;

Expand Down Expand Up @@ -68,6 +72,98 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
ContextMenu.Opening += ContextMenuOnOpening;
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
var b = e.NameScope.Get<Panel>("RootPanel");
b.Loaded += (sender, args) =>
{
var v = ElementComposition.GetElementVisual(b);
MakeOpacityAnimated(v);
};

var b2 = e.NameScope.Get<Border>("PART_BorderCard");
b2.Loaded += (sender, args) =>
{
var v = ElementComposition.GetElementVisual(b2);
MakeSizeAnimated(v);
};



}


public static void MakeOpacityAnimated(CompositionVisual compositionVisual)
{
if (compositionVisual == null)
return;

Compositor compositor = compositionVisual.Compositor;

var animationGroup = compositor.CreateAnimationGroup();


ScalarKeyFrameAnimation opacityAnimation = compositor.CreateScalarKeyFrameAnimation();
opacityAnimation.Target = "Opacity";
opacityAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
opacityAnimation.Duration = TimeSpan.FromMilliseconds(700);



Vector3KeyFrameAnimation offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.Target = "Offset";
offsetAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
offsetAnimation.Duration = TimeSpan.FromMilliseconds(700);


animationGroup.Add(offsetAnimation);
animationGroup.Add(opacityAnimation);


ImplicitAnimationCollection implicitAnimationCollection = compositor.CreateImplicitAnimationCollection();
implicitAnimationCollection["Opacity"] = animationGroup;
implicitAnimationCollection["Offset"] = animationGroup;


compositionVisual.ImplicitAnimations = implicitAnimationCollection;

}

public static void MakeSizeAnimated(CompositionVisual compositionVisual)
{
if (compositionVisual == null)
return;

Compositor compositor = compositionVisual.Compositor;

var animationGroup = compositor.CreateAnimationGroup();

Vector2KeyFrameAnimation sizeAnimation = compositor.CreateVector2KeyFrameAnimation();
sizeAnimation.Target = "Size";
sizeAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
sizeAnimation.Duration = TimeSpan.FromMilliseconds(450);



Vector3KeyFrameAnimation offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.Target = "Offset";
offsetAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
offsetAnimation.Duration = TimeSpan.FromMilliseconds(450);

animationGroup.Add(sizeAnimation);

animationGroup.Add(offsetAnimation);

ImplicitAnimationCollection implicitAnimationCollection = compositor.CreateImplicitAnimationCollection();
implicitAnimationCollection["Size"] = animationGroup;
implicitAnimationCollection["Offset"] = animationGroup;


compositionVisual.ImplicitAnimations = implicitAnimationCollection;

}

private void ContextMenuOnOpening(object sender, CancelEventArgs e)
{
PseudoClasses.Set(":pointerdown", false);
Expand Down

0 comments on commit ed21ac3

Please sign in to comment.