Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Add custom events behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Jun 18, 2022
1 parent efb2812 commit 507eb74
Show file tree
Hide file tree
Showing 21 changed files with 1,163 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Avalonia.Xaml.Interactions/Events/DoubleTappedEventBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Events;

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class DoubleTappedEventBehavior<T> : Behavior<T> where T : Control
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
AvaloniaProperty.Register<DoubleTappedEventBehavior<T>, RoutingStrategies>(
nameof(RoutingStrategies),
RoutingStrategies.Bubble);

/// <summary>
///
/// </summary>
public RoutingStrategies RoutingStrategies
{
get => GetValue(RoutingStrategiesProperty);
set => SetValue(RoutingStrategiesProperty, value);
}


/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
AssociatedObject?.AddHandler(Gestures.DoubleTappedEvent, DoubleTapped, RoutingStrategies);
}

/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
AssociatedObject?.RemoveHandler(Gestures.DoubleTappedEvent, DoubleTapped);
}

private void DoubleTapped(object? sender, RoutedEventArgs e)
{
OnDoubleTapped(sender, e);
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnDoubleTapped(object? sender, RoutedEventArgs e)
{
}
}
57 changes: 57 additions & 0 deletions src/Avalonia.Xaml.Interactions/Events/GotFocusEventBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Events;

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class GotFocusEventBehavior<T> : Behavior<T> where T : Control
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
AvaloniaProperty.Register<GotFocusEventBehavior<T>, RoutingStrategies>(
nameof(RoutingStrategies),
RoutingStrategies.Bubble);


/// <summary>
///
/// </summary>
public RoutingStrategies RoutingStrategies
{
get => GetValue(RoutingStrategiesProperty);
set => SetValue(RoutingStrategiesProperty, value);
}

/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
AssociatedObject?.AddHandler(InputElement.GotFocusEvent, GotFocus, RoutingStrategies);
}

/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
AssociatedObject?.RemoveHandler(InputElement.GotFocusEvent, GotFocus);
}

private void GotFocus(object? sender, GotFocusEventArgs e)
{
OnGotFocus(sender, e);
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnGotFocus(object? sender, GotFocusEventArgs e)
{
}
}
56 changes: 56 additions & 0 deletions src/Avalonia.Xaml.Interactions/Events/KeyDownEventBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Events;

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class KeyDownEventBehavior<T> : Behavior<T> where T : Control
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
AvaloniaProperty.Register<KeyDownEventBehavior<T>, RoutingStrategies>(
nameof(RoutingStrategies),
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);

/// <summary>
///
/// </summary>
public RoutingStrategies RoutingStrategies
{
get => GetValue(RoutingStrategiesProperty);
set => SetValue(RoutingStrategiesProperty, value);
}

/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
AssociatedObject?.AddHandler(InputElement.KeyDownEvent, KeyDown, RoutingStrategies);
}

/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
AssociatedObject?.RemoveHandler(InputElement.KeyDownEvent, KeyDown);
}

private void KeyDown(object? sender, KeyEventArgs e)
{
OnKeyDown(sender, e);
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnKeyDown(object? sender, KeyEventArgs e)
{
}
}
56 changes: 56 additions & 0 deletions src/Avalonia.Xaml.Interactions/Events/KeyUpEventBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Events;

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class KeyUpEventBehavior<T> : Behavior<T> where T : Control
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
AvaloniaProperty.Register<KeyUpEventBehavior<T>, RoutingStrategies>(
nameof(RoutingStrategies),
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);

/// <summary>
///
/// </summary>
public RoutingStrategies RoutingStrategies
{
get => GetValue(RoutingStrategiesProperty);
set => SetValue(RoutingStrategiesProperty, value);
}

/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
AssociatedObject?.AddHandler(InputElement.KeyUpEvent, KeyUp, RoutingStrategies);
}

/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
AssociatedObject?.RemoveHandler(InputElement.KeyUpEvent, KeyUp);
}

private void KeyUp(object? sender, KeyEventArgs e)
{
OnKeyUp(sender, e);
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnKeyUp(object? sender, KeyEventArgs e)
{
}
}
56 changes: 56 additions & 0 deletions src/Avalonia.Xaml.Interactions/Events/LostFocusEventBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Events;

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class LostFocusEventBehavior<T> : Behavior<T> where T : Control
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
AvaloniaProperty.Register<LostFocusEventBehavior<T>, RoutingStrategies>(
nameof(RoutingStrategies),
RoutingStrategies.Bubble);

/// <summary>
///
/// </summary>
public RoutingStrategies RoutingStrategies
{
get => GetValue(RoutingStrategiesProperty);
set => SetValue(RoutingStrategiesProperty, value);
}

/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
AssociatedObject?.AddHandler(InputElement.LostFocusEvent, LostFocus, RoutingStrategies);
}

/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
AssociatedObject?.RemoveHandler(InputElement.LostFocusEvent, LostFocus);
}

private void LostFocus(object? sender, RoutedEventArgs e)
{
OnLostFocus(sender, e);
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnLostFocus(object? sender, RoutedEventArgs e)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Events;

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class PointerCaptureLostEventBehavior<T> : Behavior<T> where T : Control
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
AvaloniaProperty.Register<PointerCaptureLostEventBehavior<T>, RoutingStrategies>(
nameof(RoutingStrategies),
RoutingStrategies.Direct);

/// <summary>
///
/// </summary>
public RoutingStrategies RoutingStrategies
{
get => GetValue(RoutingStrategiesProperty);
set => SetValue(RoutingStrategiesProperty, value);
}

/// <inheritdoc />
protected override void OnAttachedToVisualTree()
{
AssociatedObject?.AddHandler(InputElement.PointerCaptureLostEvent, PointerCaptureLost, RoutingStrategies);
}

/// <inheritdoc />
protected override void OnDetachedFromVisualTree()
{
AssociatedObject?.RemoveHandler(InputElement.PointerCaptureLostEvent, PointerCaptureLost);
}

private void PointerCaptureLost(object? sender, PointerCaptureLostEventArgs e)
{
OnPointerCaptureLost(sender, e);
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnPointerCaptureLost(object? sender, PointerCaptureLostEventArgs e)
{
}
}
Loading

0 comments on commit 507eb74

Please sign in to comment.