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

Commit

Permalink
Merge branch 'master' into release/11.1.0-beta1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Apr 24, 2024
2 parents bbae063 + d62d2aa commit 4484a7c
Show file tree
Hide file tree
Showing 41 changed files with 383 additions and 325 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionPrefix>11.1.0</VersionPrefix>
<VersionSuffix>beta1.3</VersionSuffix>
<VersionSuffix>beta1.4</VersionSuffix>
<Authors>Wiesław Šoltés</Authors>
<Company>Wiesław Šoltés</Company>
<Copyright>Copyright © Wiesław Šoltés 2024</Copyright>
Expand Down
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);
}

}
Loading

0 comments on commit 4484a7c

Please sign in to comment.