Skip to content

Commit 35cfe01

Browse files
CopilotPureWeen
andcommitted
Create IGesturePlatformManager interface with service registration support
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
1 parent ab328b4 commit 35cfe01

8 files changed

Lines changed: 67 additions & 7 deletions

File tree

src/Controls/src/Core/Platform/GestureManager/GestureManager.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.ObjectModel;
66
using System.Collections.Specialized;
77
using System.ComponentModel;
8+
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Maui.Controls.Internals;
910
using Microsoft.Maui.Graphics;
1011

@@ -19,7 +20,7 @@ internal class GestureManager
1920
bool _didHaveWindow;
2021

2122
public bool IsConnected => GesturePlatformManager != null;
22-
public GesturePlatformManager? GesturePlatformManager { get; private set; }
23+
public IGesturePlatformManager? GesturePlatformManager { get; private set; }
2324

2425
public GestureManager(IControlsView view)
2526
{
@@ -76,7 +77,12 @@ void SetupGestureManager()
7677
if (GesturePlatformManager != null)
7778
return;
7879

79-
GesturePlatformManager = new GesturePlatformManager(handler);
80+
// Try to get IGesturePlatformManager from services first, fallback to default implementation
81+
var context = handler.MauiContext;
82+
GesturePlatformManager =
83+
context?.Services.GetService<IGesturePlatformManager>() ??
84+
new GesturePlatformManager(handler);
85+
8086
_handler = handler;
8187
_containerView = handler.ContainerView;
8288
_platformView = handler.PlatformView;

src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Microsoft.Maui.Controls.Platform
1616
{
17-
class GesturePlatformManager : IDisposable
17+
class GesturePlatformManager : IGesturePlatformManager
1818
{
1919
IViewHandler? _handler;
2020
Lazy<ScaleGestureDetector> _scaleDetector;

src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Standard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Microsoft.Maui.Controls.Platform
44
{
5-
class GesturePlatformManager : IDisposable
5+
class GesturePlatformManager : IGesturePlatformManager
66
{
77
public GesturePlatformManager(IViewHandler handler)
88
{

src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Tizen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.Maui.Controls.Platform
77
{
8-
class GesturePlatformManager : IDisposable
8+
class GesturePlatformManager : IGesturePlatformManager
99
{
1010
IViewHandler? _handler;
1111
Lazy<GestureDetector> _gestureDetector;

src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Microsoft.Maui.Controls.Platform
1717
{
18-
class GesturePlatformManager : IDisposable
18+
class GesturePlatformManager : IGesturePlatformManager
1919
{
2020
readonly IPlatformViewHandler _handler;
2121
readonly NotifyCollectionChangedEventHandler _collectionChangedHandler;

src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.iOS.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace Microsoft.Maui.Controls.Platform
1919
{
20-
class GesturePlatformManager : IDisposable
20+
class GesturePlatformManager : IGesturePlatformManager
2121
{
2222
readonly NotifyCollectionChangedEventHandler _collectionChangedHandler;
2323

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Microsoft.Maui.Controls.Platform
4+
{
5+
// TODO NET11: Make this interface public in .NET 11
6+
/// <summary>
7+
/// Interface for platform-specific gesture management.
8+
/// Allows users to provide custom implementations to replace the default gesture handling behavior.
9+
/// </summary>
10+
internal interface IGesturePlatformManager : IDisposable
11+
{
12+
}
13+
}

src/Controls/tests/Core.UnitTests/Gestures/GestureManagerTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Microsoft.Extensions.DependencyInjection;
23
using Microsoft.Maui.Controls.Platform;
34
using NSubstitute;
45
using Xunit;
@@ -108,5 +109,45 @@ public void PlatformManagerChangesWhenContainerViewChanged()
108109

109110
Assert.NotEqual(gestureManager.GesturePlatformManager, platformManagerForHandler1);
110111
}
112+
113+
[Fact]
114+
public void UsesDefaultGesturePlatformManagerWhenNoServiceRegistered()
115+
{
116+
var handler = Substitute.For<IViewHandler>();
117+
var view = Substitute.For<IControlsView>();
118+
var mauiContext = Substitute.For<IMauiContext>();
119+
var services = new ServiceCollection().BuildServiceProvider();
120+
121+
mauiContext.Services.Returns(services);
122+
handler.MauiContext.Returns(mauiContext);
123+
view.Handler.Returns(handler);
124+
125+
GestureManager gestureManager = new GestureManager(view);
126+
127+
Assert.NotNull(gestureManager.GesturePlatformManager);
128+
Assert.IsType<GesturePlatformManager>(gestureManager.GesturePlatformManager);
129+
}
130+
131+
[Fact]
132+
public void UsesCustomGesturePlatformManagerWhenServiceRegistered()
133+
{
134+
var handler = Substitute.For<IViewHandler>();
135+
var view = Substitute.For<IControlsView>();
136+
var mauiContext = Substitute.For<IMauiContext>();
137+
var customManager = Substitute.For<IGesturePlatformManager>();
138+
139+
var services = new ServiceCollection()
140+
.AddSingleton(customManager)
141+
.BuildServiceProvider();
142+
143+
mauiContext.Services.Returns(services);
144+
handler.MauiContext.Returns(mauiContext);
145+
view.Handler.Returns(handler);
146+
147+
GestureManager gestureManager = new GestureManager(view);
148+
149+
Assert.NotNull(gestureManager.GesturePlatformManager);
150+
Assert.Equal(customManager, gestureManager.GesturePlatformManager);
151+
}
111152
}
112153
}

0 commit comments

Comments
 (0)