Skip to content
Draft
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
10 changes: 8 additions & 2 deletions src/Controls/src/Core/Platform/GestureManager/GestureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;

Expand All @@ -19,7 +20,7 @@ internal class GestureManager
bool _didHaveWindow;

public bool IsConnected => GesturePlatformManager != null;
public GesturePlatformManager? GesturePlatformManager { get; private set; }
public IGesturePlatformManager? GesturePlatformManager { get; private set; }

public GestureManager(IControlsView view)
{
Expand Down Expand Up @@ -76,7 +77,12 @@ void SetupGestureManager()
if (GesturePlatformManager != null)
return;

GesturePlatformManager = new GesturePlatformManager(handler);
// Try to get IGesturePlatformManager from services first, fallback to default implementation
var context = handler.MauiContext;
GesturePlatformManager =
context?.Services.GetService<IGesturePlatformManager>() ??
new GesturePlatformManager(handler);

_handler = handler;
_containerView = handler.ContainerView;
_platformView = handler.PlatformView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Microsoft.Maui.Controls.Platform
{
class GesturePlatformManager : IDisposable
class GesturePlatformManager : IGesturePlatformManager
{
IViewHandler? _handler;
Lazy<ScaleGestureDetector> _scaleDetector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Microsoft.Maui.Controls.Platform
{
class GesturePlatformManager : IDisposable
class GesturePlatformManager : IGesturePlatformManager
{
public GesturePlatformManager(IViewHandler handler)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.Maui.Controls.Platform
{
class GesturePlatformManager : IDisposable
class GesturePlatformManager : IGesturePlatformManager
{
IViewHandler? _handler;
Lazy<GestureDetector> _gestureDetector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Microsoft.Maui.Controls.Platform
{
class GesturePlatformManager : IDisposable
class GesturePlatformManager : IGesturePlatformManager
{
readonly IPlatformViewHandler _handler;
readonly NotifyCollectionChangedEventHandler _collectionChangedHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Microsoft.Maui.Controls.Platform
{
class GesturePlatformManager : IDisposable
class GesturePlatformManager : IGesturePlatformManager
{
readonly NotifyCollectionChangedEventHandler _collectionChangedHandler;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Microsoft.Maui.Controls.Platform
{
// TODO NET11: Make this interface public in .NET 11
/// <summary>
/// Interface for platform-specific gesture management.
/// Allows users to provide custom implementations to replace the default gesture handling behavior.
/// </summary>
internal interface IGesturePlatformManager : IDisposable
{
}
}
41 changes: 41 additions & 0 deletions src/Controls/tests/Core.UnitTests/Gestures/GestureManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls.Platform;
using NSubstitute;
using Xunit;
Expand Down Expand Up @@ -108,5 +109,45 @@ public void PlatformManagerChangesWhenContainerViewChanged()

Assert.NotEqual(gestureManager.GesturePlatformManager, platformManagerForHandler1);
}

[Fact]
public void UsesDefaultGesturePlatformManagerWhenNoServiceRegistered()
{
var handler = Substitute.For<IViewHandler>();
var view = Substitute.For<IControlsView>();
var mauiContext = Substitute.For<IMauiContext>();
var services = new ServiceCollection().BuildServiceProvider();

mauiContext.Services.Returns(services);
handler.MauiContext.Returns(mauiContext);
view.Handler.Returns(handler);

GestureManager gestureManager = new GestureManager(view);

Assert.NotNull(gestureManager.GesturePlatformManager);
Assert.IsType<GesturePlatformManager>(gestureManager.GesturePlatformManager);
}

[Fact]
public void UsesCustomGesturePlatformManagerWhenServiceRegistered()
{
var handler = Substitute.For<IViewHandler>();
var view = Substitute.For<IControlsView>();
var mauiContext = Substitute.For<IMauiContext>();
var customManager = Substitute.For<IGesturePlatformManager>();

var services = new ServiceCollection()
.AddSingleton(customManager)
.BuildServiceProvider();

mauiContext.Services.Returns(services);
handler.MauiContext.Returns(mauiContext);
view.Handler.Returns(handler);

GestureManager gestureManager = new GestureManager(view);

Assert.NotNull(gestureManager.GesturePlatformManager);
Assert.Equal(customManager, gestureManager.GesturePlatformManager);
}
}
}
Loading