Skip to content

Commit c0f2c33

Browse files
pavelovcharovMrJul
authored andcommitted
Button.IsDefault should not work when button is not effectively visible (#18484)
1 parent 0501499 commit c0f2c33

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Avalonia.Controls/Button.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ private void StopListeningForCancel(IInputElement root)
659659
/// <param name="e">The event args.</param>
660660
private void RootDefaultKeyDown(object? sender, KeyEventArgs e)
661661
{
662-
if (e.Key == Key.Enter && IsVisible && IsEnabled)
662+
if (e.Key == Key.Enter && IsEffectivelyVisible && IsEffectivelyEnabled)
663663
{
664664
OnClick();
665665
e.Handled = true;
@@ -673,7 +673,7 @@ private void RootDefaultKeyDown(object? sender, KeyEventArgs e)
673673
/// <param name="e">The event args.</param>
674674
private void RootCancelKeyDown(object? sender, KeyEventArgs e)
675675
{
676-
if (e.Key == Key.Escape && IsVisible && IsEnabled)
676+
if (e.Key == Key.Escape && IsEffectivelyVisible && IsEffectivelyEnabled)
677677
{
678678
OnClick();
679679
e.Handled = true;

tests/Avalonia.Controls.UnitTests/ButtonTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,27 @@ public void Button_IsDefault_Works()
448448
Assert.Equal(2, raised);
449449
}
450450
}
451+
452+
[Fact]
453+
public void Button_IsDefault_Should_Not_Work_When_Button_Is_Not_Effectively_Visible()
454+
{
455+
using (UnitTestApplication.Start(TestServices.StyledWindow))
456+
{
457+
var raised = 0;
458+
var panel = new Panel();
459+
var target = new Button();
460+
panel.Children.Add(target);
461+
var window = new Window { Content = panel };
462+
window.Show();
463+
464+
target.Click += (s, e) => ++raised;
465+
466+
target.IsDefault = true;
467+
panel.IsVisible = false;
468+
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
469+
Assert.Equal(0, raised);
470+
}
471+
}
451472

452473
[Fact]
453474
public void Button_IsCancel_Works()
@@ -482,6 +503,27 @@ public void Button_IsCancel_Works()
482503
Assert.Equal(2, raised);
483504
}
484505
}
506+
507+
[Fact]
508+
public void Button_IsCancel_Should_Not_Work_When_Button_Is_Not_Effectively_Visible()
509+
{
510+
using (UnitTestApplication.Start(TestServices.StyledWindow))
511+
{
512+
var raised = 0;
513+
var panel = new Panel();
514+
var target = new Button();
515+
panel.Children.Add(target);
516+
var window = new Window { Content = panel };
517+
window.Show();
518+
519+
target.Click += (s, e) => ++raised;
520+
521+
target.IsCancel = true;
522+
panel.IsVisible = false;
523+
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
524+
Assert.Equal(0, raised);
525+
}
526+
}
485527

486528
[Fact]
487529
public void Button_CommandParameter_Does_Not_Change_While_Execution()

0 commit comments

Comments
 (0)