Skip to content

Commit

Permalink
fix: SplitButton.Click is not fired when SplitButton is not in fo…
Browse files Browse the repository at this point in the history
…cused (#16940)

* test: Check that `SplitButton.Click` is not fired when `SplitButton` is not in focused

* fix: `SplitButton.Click` is not fired when `SplitButton` is not in focused

* fix: Address review

* fix: Address review

---------

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
  • Loading branch information
workgroupengineering and MrJul authored Oct 7, 2024
1 parent 2d28884 commit b11574a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Avalonia.Controls/SplitButton/SplitButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected override void OnKeyDown(KeyEventArgs e)
{
var key = e.Key;

if (key == Key.Space || key == Key.Enter)
if ((IsFocused && key == Key.Space) || key == Key.Enter)
{
_isKeyboardPressed = true;
UpdatePseudoClasses();
Expand All @@ -360,7 +360,7 @@ protected override void OnKeyUp(KeyEventArgs e)
{
var key = e.Key;

if (key == Key.Space || key == Key.Enter)
if ((IsFocused && key == Key.Space) || key == Key.Enter)
{
_isKeyboardPressed = false;
UpdatePseudoClasses();
Expand Down
35 changes: 35 additions & 0 deletions tests/Avalonia.Controls.UnitTests/SplitButtonTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Avalonia.Controls.UnitTests.Utils;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.UnitTests;
using Xunit;

Expand Down Expand Up @@ -30,4 +31,38 @@ public void SplitButton_CommandParameter_Does_Not_Change_While_Execution()

(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 SplitButton()
{
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 static KeyEventArgs CreateKeyDownEvent(Key key, Interactive source = null)
{
return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source };
}

private static KeyEventArgs CreateKeyUpEvent(Key key, Interactive source = null)
{
return new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = key, Source = source };
}
}

0 comments on commit b11574a

Please sign in to comment.