Skip to content

Commit 2bd9784

Browse files
committed
Merge pull request AvaloniaUI#8253 from AvaloniaUI/stop-iscancel-on-detached
Stop listening for IsCancel on button detached
1 parent d18f979 commit 2bd9784

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/Avalonia.Controls/Button.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e
222222
StopListeningForDefault(inputElement);
223223
}
224224
}
225+
if (IsCancel)
226+
{
227+
if (e.Root is IInputElement inputElement)
228+
{
229+
StopListeningForCancel(inputElement);
230+
}
231+
}
225232
}
226233

227234
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)

tests/Avalonia.Controls.UnitTests/ButtonTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,80 @@ public void Button_Invokes_Doesnt_Execute_When_Button_Disabled()
309309

310310
Assert.Equal(0, raised);
311311
}
312+
313+
[Fact]
314+
public void Button_IsDefault_Works()
315+
{
316+
using (UnitTestApplication.Start(TestServices.StyledWindow))
317+
{
318+
var raised = 0;
319+
var target = new Button();
320+
var window = new Window { Content = target };
321+
window.Show();
322+
323+
target.Click += (s, e) => ++raised;
324+
325+
target.IsDefault = false;
326+
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
327+
Assert.Equal(0, raised);
328+
329+
target.IsDefault = true;
330+
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
331+
Assert.Equal(1, raised);
332+
333+
target.IsDefault = false;
334+
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
335+
Assert.Equal(1, raised);
336+
337+
target.IsDefault = true;
338+
window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
339+
Assert.Equal(2, raised);
340+
341+
window.Content = null;
342+
// To check if handler was raised on the button, when it's detached, we need to pass it as a source manually.
343+
window.RaiseEvent(CreateKeyDownEvent(Key.Enter, target));
344+
Assert.Equal(2, raised);
345+
}
346+
}
347+
348+
[Fact]
349+
public void Button_IsCancel_Works()
350+
{
351+
using (UnitTestApplication.Start(TestServices.StyledWindow))
352+
{
353+
var raised = 0;
354+
var target = new Button();
355+
var window = new Window { Content = target };
356+
window.Show();
357+
358+
target.Click += (s, e) => ++raised;
359+
360+
target.IsCancel = false;
361+
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
362+
Assert.Equal(0, raised);
363+
364+
target.IsCancel = true;
365+
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
366+
Assert.Equal(1, raised);
367+
368+
target.IsCancel = false;
369+
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
370+
Assert.Equal(1, raised);
371+
372+
target.IsCancel = true;
373+
window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
374+
Assert.Equal(2, raised);
375+
376+
window.Content = null;
377+
window.RaiseEvent(CreateKeyDownEvent(Key.Escape, target));
378+
Assert.Equal(2, raised);
379+
}
380+
}
381+
382+
private KeyEventArgs CreateKeyDownEvent(Key key, IInteractive source = null)
383+
{
384+
return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source };
385+
}
312386

313387
private class TestButton : Button, IRenderRoot
314388
{

0 commit comments

Comments
 (0)