Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SplitButton.Click is not fired when SplitButton is not in focused #16940

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/Avalonia.Controls/SplitButton/SplitButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,52 +345,55 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e)
protected override void OnKeyDown(KeyEventArgs e)
{
var key = e.Key;

if (key == Key.Space || key == Key.Enter)
if (IsFocused)
{
_isKeyboardPressed = true;
UpdatePseudoClasses();
if (key == Key.Space || key == Key.Enter)
{
_isKeyboardPressed = true;
UpdatePseudoClasses();
}
}

base.OnKeyDown(e);
}

/// <inheritdoc/>
protected override void OnKeyUp(KeyEventArgs e)
{
var key = e.Key;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both of these changes it's a lot easier to read the changes if you just check and exit on entry:

if (!IsFocused)
{
    base.OnKeyUp(e);
    return;
}
  1. That would remove a lot of noise in this PR
  2. It helps with maintainability and readability to reduce nesting in the logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I preferred this solution because IL is more compact, see example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think IL really matters here, as JIT might produce very similarly efficient code in the end.
But I don't like doubling base.OnKeyUp(e); call. Early returns are great, but duplicating base calls is not so much.


if (key == Key.Space || key == Key.Enter)
if (IsFocused)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about IsEffectivelyEnabled? We should probably check that too, right?

{
_isKeyboardPressed = false;
UpdatePseudoClasses();
var key = e.Key;

// Consider this a click on the primary button
if (IsEffectivelyEnabled)
if (key == Key.Space || key == Key.Enter)
{
OnClickPrimary(null);
_isKeyboardPressed = false;
UpdatePseudoClasses();

// Consider this a click on the primary button
if (IsEffectivelyEnabled)
{
OnClickPrimary(null);
e.Handled = true;
}
}
else if (key == Key.Down && e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) && IsEffectivelyEnabled
&& !XYFocusHelpers.IsAllowedXYNavigationMode(this, e.KeyDeviceType))
{
OpenFlyout();
e.Handled = true;
}
else if (key == Key.F4 && IsEffectivelyEnabled)
{
OpenFlyout();
e.Handled = true;
}
else if (e.Key == Key.Escape && _isFlyoutOpen)
{
// If Flyout doesn't have focusable content, close the flyout here
// This is the same behavior as Button
CloseFlyout();
e.Handled = true;
}
}
else if (key == Key.Down && e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) && IsEffectivelyEnabled
&& !XYFocusHelpers.IsAllowedXYNavigationMode(this, e.KeyDeviceType))
{
OpenFlyout();
e.Handled = true;
}
else if (key == Key.F4 && IsEffectivelyEnabled)
{
OpenFlyout();
e.Handled = true;
}
else if (e.Key == Key.Escape && _isFlyoutOpen)
{
// If Flyout doesn't have focusable content, close the flyout here
// This is the same behavior as Button
CloseFlyout();
e.Handled = true;
}

base.OnKeyUp(e);
}

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 };
}
}
Loading