Skip to content

Commit

Permalink
Did some modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumzhao committed Aug 24, 2020
1 parent 078064a commit 8d4f7f1
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Ripple/RippleEffect/RippleEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Avalonia;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Media;
using Avalonia.Styling;
using System;

namespace Ripple
{
public class RippleEffect : ContentControl
{
#region Styled properties

public static readonly StyledProperty<Brush> RippleFillProperty =
AvaloniaProperty.Register<RippleEffect, Brush>(nameof(RippleFill));

public Brush RippleFill
{
get { return GetValue(RippleFillProperty); }
set { SetValue(RippleFillProperty, value); }
}

public static readonly StyledProperty<double> RippleOpacityProperty =
AvaloniaProperty.Register<RippleEffect, double>(nameof(RippleOpacity));

public double RippleOpacity
{
get { return GetValue(RippleOpacityProperty); }
set { SetValue(RippleOpacityProperty, value); }
}

public static readonly StyledProperty<Action> PressedActionProperty =
AvaloniaProperty.Register<RippleEffect, Action>(nameof(PressedAction));
public Action PressedAction
{
get => GetValue(PressedActionProperty);
set => SetValue(PressedActionProperty, value);
}

#endregion Styled properties

private Ellipse _circle;
private Animation _ripple;
private Point _pointer;
private IAnimationSetter _toWidth;
private IAnimationSetter _fromMargin;
private IAnimationSetter _toMargin;
private bool _isRunning;

public RippleEffect()
{
this.AddHandler(PointerPressedEvent, async (s, e) =>
{
if (_isRunning)
{
return;
}
_pointer = e.GetPosition(this);
_isRunning = true;
var maxWidth = Math.Max(Bounds.Width, Bounds.Height) * 2.2D;
_toWidth.Value = maxWidth;
_fromMargin.Value = _circle.Margin = new Thickness(_pointer.X, _pointer.Y, 0, 0);
_toMargin.Value = new Thickness(_pointer.X - maxWidth / 2, _pointer.Y - maxWidth / 2, 0, 0);

await _ripple.RunAsync(_circle);

_isRunning = false;

PressedAction.Invoke();
});
}

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

_circle = e.NameScope.Find<Ellipse>("Circle");

var style = _circle.Styles[0] as Style;
_ripple = style.Animations[0] as Animation;
_toWidth = _ripple.Children[1].Setters[1];
_fromMargin = _ripple.Children[0].Setters[0];
_toMargin = _ripple.Children[1].Setters[0];

style.Animations.Remove(_ripple);
}
}
}

0 comments on commit 8d4f7f1

Please sign in to comment.