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

Commit

Permalink
Update behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Apr 24, 2024
1 parent 752fbbf commit 59a7f2d
Show file tree
Hide file tree
Showing 40 changed files with 382 additions and 324 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,16 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Avalonia.VisualTree;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public class ButtonExecuteCommandOnKeyDownBehavior : AttachedToVisualTreeBehavior<Button>
public class ButtonExecuteCommandOnKeyDownBehavior : ExecuteCommandOnKeyBehaviorBase
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<bool> IsEnabledProperty =
AvaloniaProperty.Register<ButtonExecuteCommandOnKeyDownBehavior, bool>(nameof(IsEnabled));

/// <summary>
///
/// </summary>
public static readonly StyledProperty<Key?> KeyProperty =
AvaloniaProperty.Register<ButtonExecuteCommandOnKeyDownBehavior, Key?>(nameof(Key));

/// <summary>
///
/// </summary>
public static readonly StyledProperty<KeyGesture?> GestureProperty =
AvaloniaProperty.Register<ButtonExecuteCommandOnKeyDownBehavior, KeyGesture?>(nameof(Gesture));

/// <summary>
///
/// </summary>
public bool IsEnabled
{
get => GetValue(IsEnabledProperty);
set => SetValue(IsEnabledProperty, value);
}

/// <summary>
///
/// </summary>
public Key? Key
{
get => GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}

/// <summary>
///
/// </summary>
public KeyGesture? Gesture
{
get => GetValue(GestureProperty);
set => SetValue(GestureProperty, value);
}

/// <summary>
///
/// </summary>
Expand All @@ -79,12 +35,12 @@ private void RootDefaultKeyDown(object? sender, KeyEventArgs e)
return;
}

if (AssociatedObject is { } button)
if (AssociatedObject is Button button)
{
ExecuteCommand(button);
}
}

private bool ExecuteCommand(Button button)
{
if (!IsEnabled)
Expand All @@ -102,6 +58,16 @@ private bool ExecuteCommand(Button button)
return false;
}

if (FocusTopLevel)
{
Dispatcher.UIThread.Post(() => (AssociatedObject?.GetVisualRoot() as TopLevel)?.Focus());
}

if (FocusControl is { } focusControl)
{
Dispatcher.UIThread.Post(() => focusControl.Focus());
}

button.Command.Execute(button.CommandParameter);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Control? FocusControl
///
/// </summary>
/// <returns></returns>
protected bool ExecuteCommand()
protected virtual bool ExecuteCommand()
{
if (!IsEnabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Avalonia.Xaml.Interactions.Custom;
/// <summary>
///
/// </summary>
public class ExecuteCommandOnDoubleTappedBehavior : ExecuteCommandBehaviorBase
public class ExecuteCommandOnDoubleTappedBehavior : ExecuteCommandRoutedEventBehaviorBase
{
/// <summary>
///
Expand All @@ -18,16 +18,16 @@ protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
var dispose = AssociatedObject?
.AddDisposableHandler(
Gestures.DoubleTappedEvent,
AssociatedObject_DoubleTapped,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
OnDoubleTapped,
EventRoutingStrategy);

if (dispose is not null)
{
disposable.Add(dispose);
}
}

private void AssociatedObject_DoubleTapped(object? sender, RoutedEventArgs e)
private void OnDoubleTapped(object? sender, RoutedEventArgs e)
{
if (e.Handled)
{
Expand All @@ -36,7 +36,7 @@ private void AssociatedObject_DoubleTapped(object? sender, RoutedEventArgs e)

if (ExecuteCommand())
{
e.Handled = true;
e.Handled = MarkAsHandled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Avalonia.Xaml.Interactions.Custom;
/// <summary>
///
/// </summary>
public class ExecuteCommandOnGotFocusBehavior : ExecuteCommandBehaviorBase
public class ExecuteCommandOnGotFocusBehavior : ExecuteCommandRoutedEventBehaviorBase
{
/// <summary>
///
Expand All @@ -18,16 +18,16 @@ protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
var dispose = AssociatedObject?
.AddDisposableHandler(
InputElement.GotFocusEvent,
AssociatedObject_GotFocus,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
OnGotFocus,
EventRoutingStrategy);

if (dispose is not null)
{
disposable.Add(dispose);
}
}

private void AssociatedObject_GotFocus(object? sender, RoutedEventArgs e)
private void OnGotFocus(object? sender, RoutedEventArgs e)
{
if (e.Handled)
{
Expand All @@ -36,7 +36,7 @@ private void AssociatedObject_GotFocus(object? sender, RoutedEventArgs e)

if (ExecuteCommand())
{
e.Handled = true;
e.Handled = MarkAsHandled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Avalonia.Xaml.Interactions.Custom;
/// <summary>
///
/// </summary>
public class ExecuteCommandOnHoldingBehavior : ExecuteCommandBehaviorBase
public class ExecuteCommandOnHoldingBehavior : ExecuteCommandRoutedEventBehaviorBase
{
/// <summary>
///
Expand All @@ -18,16 +18,16 @@ protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
var dispose = AssociatedObject?
.AddDisposableHandler(
Gestures.HoldingEvent,
AssociatedObject_Holding,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
OnHolding,
EventRoutingStrategy);

if (dispose is not null)
{
disposable.Add(dispose);
}
}

private void AssociatedObject_Holding(object? sender, RoutedEventArgs e)
private void OnHolding(object? sender, RoutedEventArgs e)
{
if (e.Handled)
{
Expand All @@ -36,7 +36,7 @@ private void AssociatedObject_Holding(object? sender, RoutedEventArgs e)

if (ExecuteCommand())
{
e.Handled = true;
e.Handled = MarkAsHandled;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Avalonia.Input;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public abstract class ExecuteCommandOnKeyBehaviorBase : ExecuteCommandRoutedEventBehaviorBase
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<Key?> KeyProperty =
AvaloniaProperty.Register<ExecuteCommandOnKeyBehaviorBase, Key?>(nameof(Key));

/// <summary>
///
/// </summary>
public static readonly StyledProperty<KeyGesture?> GestureProperty =
AvaloniaProperty.Register<ExecuteCommandOnKeyBehaviorBase, KeyGesture?>(nameof(Gesture));

/// <summary>
///
/// </summary>
public Key? Key
{
get => GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}

/// <summary>
///
/// </summary>
public KeyGesture? Gesture
{
get => GetValue(GestureProperty);
set => SetValue(GestureProperty, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,8 @@ namespace Avalonia.Xaml.Interactions.Custom;
/// <summary>
///
/// </summary>
public class ExecuteCommandOnKeyDownBehavior : ExecuteCommandBehaviorBase
public class ExecuteCommandOnKeyDownBehavior : ExecuteCommandOnKeyBehaviorBase
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<Key?> KeyProperty =
AvaloniaProperty.Register<ExecuteCommandOnKeyDownBehavior, Key?>(nameof(Key));

/// <summary>
///
/// </summary>
public static readonly StyledProperty<KeyGesture?> GestureProperty =
AvaloniaProperty.Register<ExecuteCommandOnKeyDownBehavior, KeyGesture?>(nameof(Gesture));

/// <summary>
///
/// </summary>
public Key? Key
{
get => GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}

/// <summary>
///
/// </summary>
public KeyGesture? Gesture
{
get => GetValue(GestureProperty);
set => SetValue(GestureProperty, value);
}

/// <summary>
///
/// </summary>
Expand All @@ -49,7 +19,7 @@ protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
.AddDisposableHandler(
InputElement.KeyDownEvent,
OnKeyDown,
RoutingStrategies.Bubble);
EventRoutingStrategy);

if (dispose is not null)
{
Expand All @@ -74,82 +44,7 @@ private void OnKeyDown(object? sender, KeyEventArgs e)

if (ExecuteCommand())
{
e.Handled = true;
}
}
}

/// <summary>
///
/// </summary>
public class ExecuteCommandOnKeyUpBehavior : ExecuteCommandBehaviorBase
{
/// <summary>
///
/// </summary>
public static readonly StyledProperty<Key?> KeyProperty =
AvaloniaProperty.Register<ExecuteCommandOnKeyDownBehavior, Key?>(nameof(Key));

/// <summary>
///
/// </summary>
public static readonly StyledProperty<KeyGesture?> GestureProperty =
AvaloniaProperty.Register<ExecuteCommandOnKeyDownBehavior, KeyGesture?>(nameof(Gesture));

/// <summary>
///
/// </summary>
public Key? Key
{
get => GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}

/// <summary>
///
/// </summary>
public KeyGesture? Gesture
{
get => GetValue(GestureProperty);
set => SetValue(GestureProperty, value);
}

/// <summary>
///
/// </summary>
/// <param name="disposable"></param>
protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
{
var dispose = AssociatedObject?
.AddDisposableHandler(
InputElement.KeyUpEvent,
OnKeyUp,
RoutingStrategies.Bubble);

if (dispose is not null)
{
disposable.Add(dispose);
}
}

private void OnKeyUp(object? sender, KeyEventArgs e)
{
var haveKey = Key is not null && e.Key == Key;
var haveGesture = Gesture is not null && Gesture.Matches(e);

if (!haveKey && !haveGesture)
{
return;
}

if (e.Handled)
{
return;
}

if (ExecuteCommand())
{
e.Handled = true;
e.Handled = MarkAsHandled;
}
}
}
Loading

0 comments on commit 59a7f2d

Please sign in to comment.