fix: Button should not fire the click event on the space key when it is not active (#16619)

* test: Check that the button should not fire the click event on the space key when it is not active

* fix: Button should not fire the click event on the space key when it is not active

* fix: Address review
This commit is contained in:
workgroupengineering 2024-08-16 11:27:27 +02:00 коммит произвёл GitHub
Родитель 9fe34158e4
Коммит 44528a8198
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 33 добавлений и 5 удалений

Просмотреть файл

@ -289,8 +289,9 @@ namespace Avalonia.Controls
OnClick(); OnClick();
e.Handled = true; e.Handled = true;
break; break;
case Key.Space: case Key.Space:
// Avoid handling Space if the button isn't focused: a child TextBox might need it for text input
if (IsFocused)
{ {
if (ClickMode == ClickMode.Press) if (ClickMode == ClickMode.Press)
{ {
@ -299,22 +300,21 @@ namespace Avalonia.Controls
IsPressed = true; IsPressed = true;
e.Handled = true; e.Handled = true;
break;
} }
break;
case Key.Escape when Flyout != null: case Key.Escape when Flyout != null:
// If Flyout doesn't have focusable content, close the flyout here // If Flyout doesn't have focusable content, close the flyout here
CloseFlyout(); CloseFlyout();
break; break;
} }
base.OnKeyDown(e); base.OnKeyDown(e);
} }
/// <inheritdoc/> /// <inheritdoc/>
protected override void OnKeyUp(KeyEventArgs e) protected override void OnKeyUp(KeyEventArgs e)
{ {
if (e.Key == Key.Space) // Avoid handling Space if the button isn't focused: a child TextBox might need it for text input
if (e.Key == Key.Space && IsFocused)
{ {
if (ClickMode == ClickMode.Release) if (ClickMode == ClickMode.Release)
{ {

Просмотреть файл

@ -511,11 +511,39 @@ namespace Avalonia.Controls.UnitTests
(target as IClickableControl).RaiseClick(); (target as IClickableControl).RaiseClick();
} }
[Fact]
void Should_Not_Fire_Click_Event_On_Space_Key_When_It_Is_Not_Focus()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var raised = 0;
var target = new TextBox();
var button = new Button()
{
Content = target,
};
var window = new Window { Content = button };
window.Show();
button.Click += (s, e) => ++raised;
target.Focus();
target.RaiseEvent(CreateKeyDownEvent(Key.Space));
target.RaiseEvent(CreateKeyUpEvent(Key.Space));
Assert.Equal(0, raised);
}
}
private KeyEventArgs CreateKeyDownEvent(Key key, Interactive source = null) private KeyEventArgs CreateKeyDownEvent(Key key, Interactive source = null)
{ {
return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source }; return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source };
} }
private KeyEventArgs CreateKeyUpEvent(Key key, Interactive source = null)
{
return new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = key, Source = source };
}
private void RaisePointerPressed(Button button, int clickCount, MouseButton mouseButton, Point position) private void RaisePointerPressed(Button button, int clickCount, MouseButton mouseButton, Point position)
{ {
_helper.Down(button, mouseButton, position, clickCount: clickCount); _helper.Down(button, mouseButton, position, clickCount: clickCount);