Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#8253 from AvaloniaUI/stop-iscancel-on-d…
Browse files Browse the repository at this point in the history
…etached

Stop listening for IsCancel on button detached
  • Loading branch information
danwalmsley committed Jun 3, 2022
1 parent d18f979 commit 2bd9784
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Avalonia.Controls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e
StopListeningForDefault(inputElement);
}
}
if (IsCancel)
{
if (e.Root is IInputElement inputElement)
{
StopListeningForCancel(inputElement);
}
}
}

protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
Expand Down
74 changes: 74 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,80 @@ public void Button_Invokes_Doesnt_Execute_When_Button_Disabled()

Assert.Equal(0, raised);
}

[Fact]
public void Button_IsDefault_Works()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var raised = 0;
var target = new Button();
var window = new Window { Content = target };
window.Show();

target.Click += (s, e) => ++raised;

target.IsDefault = false;
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
Assert.Equal(0, raised);

target.IsDefault = true;
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
Assert.Equal(1, raised);

target.IsDefault = false;
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
Assert.Equal(1, raised);

target.IsDefault = true;
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
Assert.Equal(2, raised);

window.Content = null;
// To check if handler was raised on the button, when it's detached, we need to pass it as a source manually.
window.RaiseEvent(CreateKeyDownEvent(Key.Enter, target));
Assert.Equal(2, raised);
}
}

[Fact]
public void Button_IsCancel_Works()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var raised = 0;
var target = new Button();
var window = new Window { Content = target };
window.Show();

target.Click += (s, e) => ++raised;

target.IsCancel = false;
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
Assert.Equal(0, raised);

target.IsCancel = true;
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
Assert.Equal(1, raised);

target.IsCancel = false;
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
Assert.Equal(1, raised);

target.IsCancel = true;
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
Assert.Equal(2, raised);

window.Content = null;
window.RaiseEvent(CreateKeyDownEvent(Key.Escape, target));
Assert.Equal(2, raised);
}
}

private KeyEventArgs CreateKeyDownEvent(Key key, IInteractive source = null)
{
return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source };
}

private class TestButton : Button, IRenderRoot
{
Expand Down

0 comments on commit 2bd9784

Please sign in to comment.