Browse Source

Fix #13474 by partially reverting #12883 (#13663)

* Failing unit test for #13474

* Fix for #13474 by reverting changes to ListBoxItem from #12883

* Version 2 of fix, only marks event handled for Mouse input.

* Version 3.  Re-raise the event, but backup the source before doing so and restore it after.  Closest in functionality to original, but preserves "correct" event source in order to allow Tapped events.

---------

Co-authored-by: Jumar Macato <16554748+jmacato@users.noreply.github.com>
pull/13775/head
IanRawley 2 years ago
committed by GitHub
parent
commit
b84470bf3d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/Avalonia.Controls/ListBoxItem.cs
  2. 49
      tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

4
src/Avalonia.Controls/ListBoxItem.cs

@ -104,7 +104,11 @@ namespace Avalonia.Controls
// As we only update selection from touch/pen on pointer release, we need to raise // As we only update selection from touch/pen on pointer release, we need to raise
// the pointer event on the owner to trigger a commit. // the pointer event on the owner to trigger a commit.
if (e.Pointer.Type != PointerType.Mouse) if (e.Pointer.Type != PointerType.Mouse)
{
var sourceBackup = e.Source;
owner.RaiseEvent(e); owner.RaiseEvent(e);
e.Source = sourceBackup;
}
e.Handled = true; e.Handled = true;
} }

49
tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

@ -1289,6 +1289,55 @@ namespace Avalonia.Controls.UnitTests
} }
} }
[Fact]
public void ListBoxItem_Should_Not_Block_Tapped_Events()
{
// #13474
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
Pointer _pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Touch, true);
ulong nextStamp = 1;
var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray();
var target = new ListBox
{
Template = ListBoxTemplate(),
ItemsSource = items,
SelectionMode = SelectionMode.Toggle,
ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Height = 10 })
};
Prepare(target);
var lbItems = target.GetLogicalChildren().OfType<ListBoxItem>().ToArray();
var item = lbItems[0];
int tappedCount = 0;
target.Tapped += (s, e) =>
{
tappedCount++;
};
_mouse.Click(item);
Assert.Equal(1, tappedCount);
// Raise PointerPressed and PointerReleased events with the Left Button pressed. TouchTestHelper
// assumes no button pressed, which prevents it from generating Tapped events, or I would use that.
item.RaiseEvent(new PointerPressedEventArgs(item, _pointer, (Visual)item, default, nextStamp++,
new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.LeftButtonPressed), KeyModifiers.None));
item.RaiseEvent(new PointerReleasedEventArgs(item, _pointer, (Visual)item, default, nextStamp++,
PointerPointProperties.None, KeyModifiers.None, MouseButton.Left));
Assert.Equal(2, tappedCount);
}
}
private static void RaiseKeyEvent(Control target, Key key, KeyModifiers inputModifiers = 0) private static void RaiseKeyEvent(Control target, Key key, KeyModifiers inputModifiers = 0)
{ {
target.RaiseEvent(new KeyEventArgs target.RaiseEvent(new KeyEventArgs

Loading…
Cancel
Save