Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avoid subscribing to touch scroll events twice #19007

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
MartinZikmund marked this conversation as resolved.
Show resolved Hide resolved

// 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
Loading