-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d0baff
commit 9713f31
Showing
7 changed files
with
124 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
|
||
namespace Microsoft.Maui | ||
{ | ||
public static class CommandMapperExtensions | ||
{ | ||
/// <summary> | ||
/// Modify a command mapping in place. | ||
/// </summary> | ||
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam> | ||
/// <typeparam name="TViewHandler">The handler type.</typeparam> | ||
/// <param name="commandMapper">The command mapper in which to change the mapping.</param> | ||
/// <param name="key">The name of the command.</param> | ||
/// <param name="method">The modified method to call when the command is updated.</param> | ||
public static void ModifyMapping<TVirtualView, TViewHandler>(this CommandMapper<TVirtualView, TViewHandler> commandMapper, | ||
string key, Action<TViewHandler, TVirtualView, object?, Action<IElementHandler, IElement, object?>?> method) | ||
where TVirtualView : IElement where TViewHandler : IElementHandler | ||
{ | ||
var previousMethod = commandMapper.GetCommand(key); | ||
|
||
commandMapper.Add(key, newMethod); | ||
|
||
void newMethod(TViewHandler handler, TVirtualView view, object? args) | ||
{ | ||
method(handler, view, args, previousMethod); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Specify a method to be run after an existing command mapping. | ||
/// </summary> | ||
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam> | ||
/// <typeparam name="TViewHandler">The handler type.</typeparam> | ||
/// <param name="commandMapper">The command mapper in which to change the mapping.</param> | ||
/// <param name="key">The name of the command.</param> | ||
/// <param name="method">The method to call after the existing mapping is finished.</param> | ||
public static void AppendToMapping<TVirtualView, TViewHandler>(this CommandMapper<TVirtualView, TViewHandler> commandMapper, | ||
string key, Action<TViewHandler, TVirtualView, object?> method) | ||
where TVirtualView : IElement where TViewHandler : IElementHandler | ||
{ | ||
commandMapper.ModifyMapping(key, (handler, view, args, action) => | ||
{ | ||
action?.Invoke(handler, view, args); | ||
method(handler, view, args); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Specify a method to be run before an existing command mapping. | ||
/// </summary> | ||
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam> | ||
/// <typeparam name="TViewHandler">The handler type.</typeparam> | ||
/// <param name="commandMapper">The command mapper in which to change the mapping.</param> | ||
/// <param name="key">The name of the command.</param> | ||
/// <param name="method">The method to call before the existing mapping begins.</param> | ||
public static void PrependToMapping<TVirtualView, TViewHandler>(this CommandMapper<TVirtualView, TViewHandler> commandMapper, | ||
string key, Action<TViewHandler, TVirtualView, object?> method) | ||
where TVirtualView : IElement where TViewHandler : IElementHandler | ||
{ | ||
commandMapper.ModifyMapping(key, (handler, view, args, action) => | ||
{ | ||
method(handler, view, args); | ||
action?.Invoke(handler, view, args); | ||
}); | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
src/Core/tests/DeviceTests/Handlers/View/ViewHandlerTests.Android.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
using Microsoft.Maui.Handlers; | ||
using Android.Views; | ||
using Android.Widget; | ||
|
||
namespace Microsoft.Maui.DeviceTests | ||
{ | ||
public partial class ViewHandlerTests | ||
{ | ||
protected static Thing CreateNativeView() => | ||
|
||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Core/tests/DeviceTests/Handlers/View/ViewHandlerTests.Windows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 13 additions & 15 deletions
28
src/Core/tests/DeviceTests/Handlers/View/ViewHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,35 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Maui.DeviceTests.Stubs; | ||
using Microsoft.Maui.Handlers; | ||
using Microsoft.Maui.Hosting; | ||
using Microsoft.Maui; | ||
using Xunit; | ||
|
||
namespace Microsoft.Maui.DeviceTests | ||
{ | ||
[Category(TestCategory.View)] | ||
public partial class ViewHandlerTests : HandlerTestBase<StubBase, StubBaseHandler> | ||
public partial class ViewHandlerTests : HandlerTestBase<StubBaseHandler, StubBase> | ||
{ | ||
[Fact(DisplayName = "NativeArrange triggers MapFrame")] | ||
public Task NativeArrangeTriggersMapFrame() | ||
public async Task NativeArrangeTriggersMapFrame() | ||
{ | ||
var commandMapperField = typeof(ElementHandler).GetField("_commandMapper"); | ||
var didUpdateFrame = 0; | ||
|
||
var view = new StubBase(); | ||
|
||
return InvokeOnMainThreadAsync(() => | ||
await InvokeOnMainThreadAsync(() => | ||
{ | ||
var handler = new StubBaseHandler(CreateNativeView()); | ||
var handler = new StubBaseHandler(); | ||
var viewHandler = handler as ViewHandler; | ||
var commandMapper = commandMapperField.GetValue(handler) as CommandMapper; | ||
handler.Com | ||
handler.CommandMapper.AppendToMapping(nameof(IView.Frame), (h, v, a) => | ||
{ | ||
didUpdateFrame++; | ||
}); | ||
InitializeViewHandler(view, handler); | ||
var handler = CreateHandler(view); | ||
}); | ||
|
||
Assert.Equal(1, didUpdateFrame); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/Core/tests/DeviceTests/Handlers/View/ViewHandlerTests.iOS.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using Microsoft.Maui.Handlers; | ||
using UIKit; | ||
|
||
namespace Microsoft.Maui.DeviceTests | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters