-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add tests and fix mapper scenarios
- Loading branch information
Showing
8 changed files
with
233 additions
and
35 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
23 changes: 23 additions & 0 deletions
23
src/Controls/tests/Core.UnitTests/TestClasses/BasicVisualElement.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.Maui.Animations; | ||
|
||
using Microsoft.Maui.Handlers; | ||
|
||
namespace Microsoft.Maui.Controls.Core.UnitTests | ||
{ | ||
public class BasicVisualElement : VisualElement | ||
{ | ||
} | ||
|
||
public class BasicVisualElementHandler : ViewHandler<BasicVisualElement, object> | ||
{ | ||
public BasicVisualElementHandler(IPropertyMapper mapper, CommandMapper commandMapper = null) : base(mapper, commandMapper) | ||
{ | ||
} | ||
|
||
protected override object CreatePlatformView() | ||
{ | ||
return new object(); | ||
} | ||
} | ||
} |
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
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
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,84 @@ | ||
using System; | ||
|
||
namespace Microsoft.Maui | ||
{ | ||
internal static class PropertyMapperFluentExtensions | ||
{ | ||
/// <summary> | ||
/// Creates a new PropertyMapper based on an existing PropertyMapper, targeted at a new virtual view and handler type. | ||
/// </summary> | ||
internal static IPropertyMapper<TVirtualView, THandler> RemapFor<TVirtualView, THandler>(this IPropertyMapper<IElement, IElementHandler> innerMap) | ||
where TVirtualView : IElement | ||
where THandler : IElementHandler | ||
{ | ||
return new PropertyMapper<TVirtualView, THandler>(innerMap); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a property mapping. If a previous property mapping exists, it is replaced. | ||
/// </summary> | ||
internal static IPropertyMapper<TVirtualView, THandler> Map<TVirtualView, THandler>(this IPropertyMapper<TVirtualView, THandler> propertyMapper, | ||
string key, Action<THandler, TVirtualView> propertyUpdater) | ||
where TVirtualView : IElement | ||
where THandler : IElementHandler | ||
{ | ||
propertyMapper.Add(key, propertyUpdater); | ||
return propertyMapper; | ||
} | ||
|
||
/// <summary> | ||
/// Modifies an existing mapping. | ||
/// </summary> | ||
internal static IPropertyMapper<TVirtualView, TViewHandler> Modify<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper, | ||
string key, Action<TViewHandler, TVirtualView, Action<IElementHandler, IElement>?> method) | ||
where TVirtualView : IElement where TViewHandler : IElementHandler | ||
{ | ||
propertyMapper.ModifyMapping(key, method); | ||
return propertyMapper; | ||
} | ||
|
||
/// <summary> | ||
/// Inserts a new mapping before an existing mapping. | ||
/// </summary> | ||
internal static IPropertyMapper<TVirtualView, TViewHandler> Prepend<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper, | ||
string key, Action<TViewHandler, TVirtualView> method) | ||
where TVirtualView : IElement where TViewHandler : IElementHandler | ||
{ | ||
propertyMapper.PrependToMapping(key, method); | ||
return propertyMapper; | ||
} | ||
|
||
/// <summary> | ||
/// Inserts a new mapping after an existing mapping. | ||
/// </summary> | ||
internal static IPropertyMapper<TVirtualView, TViewHandler> Append<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper, | ||
string key, Action<TViewHandler, TVirtualView> method) | ||
where TVirtualView : IElement where TViewHandler : IElementHandler | ||
{ | ||
propertyMapper.AppendToMapping(key, method); | ||
return propertyMapper; | ||
} | ||
|
||
/// <summary> | ||
/// Remaps a single property for the specified virtual view and handler types. If the virtual view or handler do not match the | ||
/// specified type, the original mapping is used. | ||
/// </summary> | ||
internal static IPropertyMapper<IElement, IElementHandler> RemapFor<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper, | ||
string key, Action<TViewHandler, TVirtualView> method) | ||
where TVirtualView : IElement where TViewHandler : IElementHandler | ||
{ | ||
var previousMethod = propertyMapper.GetProperty(key); | ||
|
||
void newMethod(IElementHandler handler, IElement view) | ||
{ | ||
if ((handler is null || handler is TViewHandler) && view is TVirtualView v) | ||
method((TViewHandler)handler!, v); | ||
else | ||
previousMethod?.Invoke(handler!, view); | ||
} | ||
|
||
propertyMapper.Add(key, newMethod); | ||
return propertyMapper; | ||
} | ||
} | ||
} |