Skip to content

Commit

Permalink
Merge pull request #19007 from unoplatform/dev/mazi/fix-scroll-touch
Browse files Browse the repository at this point in the history
fix: Avoid subscribing to touch scroll events twice
  • Loading branch information
jeromelaban authored Dec 12, 2024
2 parents d1aeac1 + d810a65 commit ffdceac
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,43 @@ public async Task When_SCP_Content_EmptySized_WithMargin_LayoutCycle()
await UITestHelper.Load(SUT, x => x.IsLoaded);
}

[TestMethod]
#if !HAS_INPUT_INJECTOR || !UNO_HAS_MANAGED_SCROLL_PRESENTER
[Ignore("This test only applies to managed scroll presenter and requires input injector.")]
#endif
public async Task When_ScrollViewer_Touch_Scrolled()
{
var stackPanel = new StackPanel();
var random = Random.Shared;
for (int i = 0; i < 10; i++)
{
stackPanel.Children.Add(
new Rectangle()
{
Width = 50,
Height = 50,
Fill = new SolidColorBrush(Color.FromArgb(255, (byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)))
});
}

var SUT = new ScrollViewer
{
Height = 300,
Content = stackPanel
};

await UITestHelper.Load(SUT);

var input = InputInjector.TryCreate() ?? throw new InvalidOperationException("Pointer injection not available on this platform.");
using var finger = input.GetFinger();
var bounds = SUT.GetAbsoluteBounds();
finger.Press(bounds.GetCenter());
finger.MoveTo(bounds.GetCenter().Offset(0, -50));
finger.Release();
await WindowHelper.WaitForIdle();
Assert.AreEqual(50, SUT.VerticalOffset);
}

[TestMethod]
public async Task When_Zero_Size_With_Margin()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Windows.Devices.Input;
using Windows.Foundation;
using Uno.UI.Xaml;
using Uno.Disposables;


#if HAS_UNO_WINUI
using _PointerDeviceType = global::Microsoft.UI.Input.PointerDeviceType;
Expand Down Expand Up @@ -56,6 +58,8 @@ public bool CanVerticallyScroll

private object RealContent => Content;

private readonly SerialDisposable _eventSubscriptions = new();

partial void InitializePartial()
{
#if __SKIA__
Expand All @@ -71,6 +75,8 @@ partial void InitializePartial()

private void HookScrollEvents(ScrollViewer sv)
{
UnhookScrollEvents(sv);

// Note: the way WinUI does scrolling is very different, and doesn't use
// PointerWheelChanged changes, etc.
// We can either subscribe on the ScrollViewer or the SCP directly, but due to
Expand All @@ -87,16 +93,21 @@ private void HookScrollEvents(ScrollViewer sv)
ManipulationStarted += TouchScrollStarted;
ManipulationDelta += UpdateTouchScroll;
ManipulationCompleted += CompleteTouchScroll;

_eventSubscriptions.Disposable = Disposable.Create(() =>
{
sv.PointerWheelChanged -= PointerWheelScroll;

ManipulationStarting -= PrepareTouchScroll;
ManipulationStarted -= TouchScrollStarted;
ManipulationDelta -= UpdateTouchScroll;
ManipulationCompleted -= CompleteTouchScroll;
});
}

private void UnhookScrollEvents(ScrollViewer sv)
{
sv.PointerWheelChanged -= PointerWheelScroll;

ManipulationStarting -= PrepareTouchScroll;
ManipulationStarted -= TouchScrollStarted;
ManipulationDelta -= UpdateTouchScroll;
ManipulationCompleted -= CompleteTouchScroll;
_eventSubscriptions.Disposable = null;
}

private protected override void OnLoaded()
Expand Down

0 comments on commit ffdceac

Please sign in to comment.