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

Add remapping extensions for core #17918

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Core/src/Handlers/SwipeView/SwipeViewHandler.Android.cs
Expand Up @@ -52,14 +52,17 @@ public static void MapIsEnabled(ISwipeViewHandler handler, ISwipeView swipeView)
ViewHandler.MapIsEnabled(handler, swipeView);
}

public static void MapBackground(ISwipeViewHandler handler, ISwipeView swipeView)
static void MapBackground(ISwipeViewHandler handler, ISwipeView view, System.Action<IElementHandler, IElement>? action)
{
if (swipeView.Background == null)
if (view.Background == null)
handler.PlatformView.Control?.SetWindowBackground();
else
ViewHandler.MapBackground(handler, swipeView);
action?.Invoke(handler, view);
}

public static void MapBackground(ISwipeViewHandler handler, ISwipeView swipeView) =>
MapBackground(handler, swipeView, (_, _) => ViewHandler.MapBackground(handler, swipeView));

public static void MapSwipeTransitionMode(ISwipeViewHandler handler, ISwipeView swipeView)
{
handler.PlatformView.UpdateSwipeTransitionMode(swipeView.SwipeTransitionMode);
Expand Down
23 changes: 12 additions & 11 deletions src/Core/src/Handlers/SwipeView/SwipeViewHandler.cs
Expand Up @@ -14,21 +14,22 @@ namespace Microsoft.Maui.Handlers
{
public partial class SwipeViewHandler : ISwipeViewHandler
{
public static IPropertyMapper<ISwipeView, ISwipeViewHandler> Mapper = new PropertyMapper<ISwipeView, ISwipeViewHandler>(ViewHandler.ViewMapper)
{
[nameof(IContentView.Content)] = MapContent,
[nameof(ISwipeView.SwipeTransitionMode)] = MapSwipeTransitionMode,
[nameof(ISwipeView.LeftItems)] = MapLeftItems,
[nameof(ISwipeView.TopItems)] = MapTopItems,
[nameof(ISwipeView.RightItems)] = MapRightItems,
[nameof(ISwipeView.BottomItems)] = MapBottomItems,
public static IPropertyMapper<ISwipeView, ISwipeViewHandler> Mapper =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an alternative that we could do and not have more extension methods:

public static IPropertyMapper Mapper = new()
{
    [nameof(Xxx)] = MapXxx,
}
.Remap(mapper =>
{
    mapper.ModifyMapping(xxx, yyy);
    mapper.ReplaceMapping(xxx, yyy);
});

new PropertyMapper<ISwipeView, ISwipeViewHandler>(ViewHandler.ViewMapper)
.ReplaceMapping(nameof(ISwipeView.SwipeTransitionMode), MapSwipeTransitionMode)
.ReplaceMapping(nameof(ISwipeView.LeftItems), MapLeftItems)
.ReplaceMapping(nameof(ISwipeView.TopItems), MapTopItems)
.ReplaceMapping(nameof(ISwipeView.RightItems), MapRightItems)
.ReplaceMapping(nameof(ISwipeView.BottomItems), MapBottomItems)
.ReplaceMapping(nameof(IContentView.Content), MapContent)
#if ANDROID || IOS || TIZEN
[nameof(IView.IsEnabled)] = MapIsEnabled,
.ReplaceMapping(nameof(IView.IsEnabled), MapIsEnabled)
#endif
#if ANDROID
[nameof(IView.Background)] = MapBackground,
.ModifyMapping(nameof(IView.Background), MapBackground)
#endif
};
;


public static CommandMapper<ISwipeView, ISwipeViewHandler> CommandMapper = new(ViewCommandMapper)
{
Expand Down
43 changes: 31 additions & 12 deletions src/Core/src/PropertyMapperExtensions.cs
Expand Up @@ -12,7 +12,8 @@ public static class PropertyMapperExtensions
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The modified method to call when the property is updated.</param>
public static void ModifyMapping<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
#pragma warning disable RS0016 // Add public types and members to the declared API
public static IPropertyMapper<TVirtualView, TViewHandler> ModifyMapping<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is breaking... I think methods can't be overloaded just by return type, so this may actually work. I'll test and report back.

string key, Action<TViewHandler, TVirtualView, Action<IElementHandler, IElement>?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
Expand All @@ -24,6 +25,7 @@ void newMethod(TViewHandler handler, TVirtualView view)
}

propertyMapper.Add(key, newMethod);
return propertyMapper;
}

/// <summary>
Expand All @@ -34,7 +36,7 @@ void newMethod(TViewHandler handler, TVirtualView view)
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The modified method to call when the property is updated.</param>
public static void ModifyMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
public static IPropertyMapper<IElement, IElementHandler> ModifyMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView, Action<IElementHandler, IElement>?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
Expand All @@ -49,6 +51,7 @@ void newMethod(IElementHandler handler, IElement view)
}

propertyMapper.Add(key, newMethod);
return propertyMapper;
}

/// <summary>
Expand All @@ -59,11 +62,26 @@ void newMethod(IElementHandler handler, IElement view)
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The modified method to call when the property is updated.</param>
public static void ReplaceMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
public static IPropertyMapper<IElement, IElementHandler> ReplaceMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.ModifyMapping<TVirtualView, TViewHandler>(key, (h, v, p) => method.Invoke(h, v));
return propertyMapper.ModifyMapping<TVirtualView, TViewHandler>(key, (h, v, p) => method.Invoke(h, v));
}

internal static IPropertyMapper<TVirtualView, TViewHandler> ReplaceMapping<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
var previousMethod = propertyMapper.GetProperty(key);

if (previousMethod is null)
{
propertyMapper.Add(key, method);
return propertyMapper;
}

return propertyMapper.ModifyMapping(key, (h, v, p) => method.Invoke(h, v));
}

/// <summary>
Expand All @@ -74,11 +92,11 @@ void newMethod(IElementHandler handler, IElement view)
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The method to call after the existing mapping is finished.</param>
public static void AppendToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
public static IPropertyMapper<TVirtualView, TViewHandler> AppendToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.ModifyMapping(key, (handler, view, action) =>
return propertyMapper.ModifyMapping(key, (handler, view, action) =>
{
action?.Invoke(handler, view);
method(handler, view);
Expand All @@ -93,11 +111,11 @@ void newMethod(IElementHandler handler, IElement view)
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The method to call after the existing mapping is finished.</param>
public static void AppendToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
public static IPropertyMapper<IElement, IElementHandler> AppendToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.ModifyMapping(key, (handler, view, action) =>
return propertyMapper.ModifyMapping(key, (handler, view, action) =>
{
action?.Invoke(handler, view);

Expand All @@ -114,11 +132,11 @@ void newMethod(IElementHandler handler, IElement view)
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The method to call before the existing mapping begins.</param>
public static void PrependToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
public static IPropertyMapper<TVirtualView, TViewHandler> PrependToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.ModifyMapping(key, (handler, view, action) =>
return propertyMapper.ModifyMapping(key, (handler, view, action) =>
{
method(handler, view);
action?.Invoke(handler, view);
Expand All @@ -133,17 +151,18 @@ void newMethod(IElementHandler handler, IElement view)
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The method to call before the existing mapping begins.</param>
public static void PrependToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
public static IPropertyMapper<IElement, IElementHandler> PrependToMapping<TVirtualView, TViewHandler>(this IPropertyMapper<IElement, IElementHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.ModifyMapping(key, (handler, view, action) =>
return propertyMapper.ModifyMapping(key, (handler, view, action) =>
{
if ((handler is null || handler is TViewHandler) && view is TVirtualView v)
method((TViewHandler)handler!, v);

action?.Invoke(handler!, view);
});
}
#pragma warning restore RS0016 // Add public types and members to the declared API
}
}