-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
078064a
commit 8d4f7f1
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |